镜像版本:wordpress
mysql版本:mysql:8.0.27
部署wordpress:v1
此版本包含wordpress基础服务,可访问,但是一旦pod重新创建会丢失数据,文章中的图片等也会丢失,且只又一个pod,性能不够
文件目录
wordpress-mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:name: mysql
spec:replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- name: mysqlimage: mysqlenv:- name: MYSQL_ROOT_PASSWORDvalue: "123"- name: MYSQL_DATABASEvalue: wordpress- name: MYSQL_USERvalue: wordpress- name: MYSQL_PASSWORDvalue: wordpress
wordpress-svc.yml
apiVersion: v1
kind: Service
metadata:name: wordpress
spec:type: NodePortports:- port: 9999targetPort: 80nodePort: 30080selector:app: wordpress
wordpress.yml
apiVersion: apps/v1
kind: Deployment
metadata:name: wordpress-mysql
spec:replicas: 1selector:matchLabels:app: wordpresstemplate:metadata:labels:app: wordpressspec:containers:- name: wordpressimage: wordpressenv:- name: WORDPRESS_DB_HOSTvalue: 10.244.1.16 # 连接数据库,这里的IP地址是你的MySQL数据库的IP地址- name: WORDPRESS_DB_USERvalue: wordpress- name: WORDPRESS_DB_PASSWORDvalue: 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的ipports:- port: 3306targetPort: 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: wordpresstemplate:metadata:labels:app: wordpressspec:volumes:- name: wpnfs: #使用nfs持久化存储server: 192.168.199.149 #存储到149的指定目录下path: /data/nfs/dddd/readOnly: false #关闭只读,可能权限不够,建议开启containers:- name: wordpressimage: wordpressenv:- name: WORDPRESS_DB_HOSTvalue: 10.110.100.100 # 连接数据库,这里的IP地址是你的MySQL数据库的IP地址- name: WORDPRESS_DB_USERvalue: wordpress- name: WORDPRESS_DB_PASSWORDvalue: wordpressvolumeMounts:- mountPath: /var/www/html # 正确的键名是 `mountPath` 而非 `ountPath`name: wp
wordprss-svc
apiVersion: v1
kind: Service
metadata:name: wordpress
spec:type: NodePortports:- port: 9999targetPort: 80nodePort: 30088selector:app: wordpress
wordpress-mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:name: mysql
spec:replicas: 1selector:matchLabels:app: mysql8template:metadata:labels:app: mysql8spec:volumes:- name: test-volumenfs:server: 192.168.199.149path: /data/nfs/hahah/readOnly: falsecontainers:- name: mysqlimage: mysqlvolumeMounts:- mountPath: /var/lib/mysqlname: test-volumeenv:- name: MYSQL_ROOT_PASSWORDvalue: "123"- name: MYSQL_DATABASEvalue: wordpress- name: MYSQL_USERvalue: wordpress- name: MYSQL_PASSWORDvalue: 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.100ports:- port: 3306targetPort: 3306selector:app: mysql8
原因:
最后两个cordens会解析svc对应的ip