镜像版本:wordpress
mysql版本:mysql:8.0.27
部署wordpress:v1
此版本包含wordpress基础服务,可访问,但是一旦pod重新创建会丢失数据,文章中的图片等也会丢失,且只又一个pod,性能不够
文件目录
wordpress-mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
wordpress-svc.yml
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
type: NodePort
ports:
- port: 9999
targetPort: 80
nodePort: 30080
selector:
app: wordpress
wordpress.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
env:
- name: WORDPRESS_DB_HOST
value: 10.244.1.16 # 连接数据库,这里的IP地址是你的MySQL数据库的IP地址
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
执行命令kubectl apply -f .
查看pod,svc
此时pod,svc正常,在查看pod的endpoint表中有没有wordpress-mysql的pod的ip
登录wordpress界面
按照提示输入账号和密码
在文章中点击写文章,写下文字并且上传图片,点击发布
此时模拟pod损坏,删除pod,完成后再次查看,新建的pod正在run
此时刷新网站,发现出现数据库连接错误
解决:此时wordpress.yml连接是数据库id发生改变,需要查看新pod的id并且修改即可
此时发现之前的数据已经丢失
部署wordpress:v2
此版本基于deployment创建多个pod,把数据库分出来,再加上nfs数据共享
mysql-svc.yml
apiVersion: v1
kind: Service
metadata:
name: mysql-v2
spec:
clusterIP: 10.110.100.100 #,虚拟ip,此时先固定svc的ip
ports:
- port: 3306
targetPort: 3306 #port 是 Service 自身暴露的端口,targetPort 是将请求转发后端pod端口
selector:
app: mysql8 #标签选择器
wordpress.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
spec:
replicas: 3 #deploy启动3个副本
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
volumes:
- name: wp
nfs: #使用nfs持久化存储
server: 192.168.199.149 #存储到149的指定目录下
path: /data/nfs/dddd/
readOnly: false #关闭只读,可能权限不够,建议开启
containers:
- name: wordpress
image: wordpress
env:
- name: WORDPRESS_DB_HOST
value: 10.110.100.100 # 连接数据库,这里的IP地址是你的MySQL数据库的IP地址
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
volumeMounts:
- mountPath: /var/www/html # 正确的键名是 `mountPath` 而非 `ountPath`
name: wp
wordprss-svc
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
type: NodePort
ports:
- port: 9999
targetPort: 80
nodePort: 30088
selector:
app: wordpress
wordpress-mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql8
template:
metadata:
labels:
app: mysql8
spec:
volumes:
- name: test-volume
nfs:
server: 192.168.199.149
path: /data/nfs/hahah/
readOnly: false
containers:
- name: mysql
image: mysql
volumeMounts:
- mountPath: /var/lib/mysql
name: test-volume
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
注意一:
wordpress.yml中 volumeMounts.mountPath路径要写对,否则会丢失数据(与mysql数据存储地方不同)
注意二:
本次实验用到两个svc,两个deploy
svc
deploy
最后查看所有svc,pod
测试
首先添加文字和图片,文字存储在mysql数据库中,图片文件存储在podd/var/www/html中
完成后删除所有pod,再次登录查看之前的数据,图片是否存在
部署wordpress:v3
优化:把wordpress中Deployment.spec.containres.template.env下面的
- name: WORDPRESS_DB_HOST
# value: 10.110.100.100
# value: mysql-v3 #这里可以写成svc的名称,此时svc的ip可以改变
mysql-svc.yml 注释掉clusterIP,此时IP会自动更换
apiVersion: v1
kind: Service
metadata:
name: mysql-v3
spec:
# clusterIP: 10.110.100.100
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql8
原因:
最后两个cordens会解析svc对应的ip