一、案例架构
步骤简单分析:
1,准备NFS环境
2,【wordpress的pod】创建deployment资源的wordpress(pod)容器;
3,【用户访问的svc】创建用户访问的svc资源;
4,【数据库的pod】创建deployment资源的数据库服务的pod容器;
5,【数据库的svc】创建业务服务wordpress的pod资源访问数据库的svc资源;
二、开始部署
1,准备NFS环境
我们采用k8s外部虚拟机的路径,实现数据卷的nfs挂载,本次学习,我们采用harbor的虚拟机进行存储静态数据;
由于,k8s集群,在创建业务pod时候,需要挂载外部的nfs,那么也需要有nfs的命令的使用,所以,k8s集群中 的机器,同样也需要安装nfs;
存储节点和k8s集群全部安装nfs工具
注意,是k8s集群所有节点,加上挂载节点(数据存储节点)都需要安装哦;
yum -y install nfs-utils
nfs存储节点配置nfs配置文件
[root@harbor ~]# vim /etc/exports
/xinjizhiwa/data *(rw,no_root_squash)
nfs存储节点创建存储路径
[root@harbor ~]# mkdir –服务器托管网p /xinjizhiwa/data/{mysql,wordpress}
启动nfs
[root@harbor ~]# systemctl enable –now nfs
验证是否成功
[root@harbor ~]# exportfs
/xinjizhiwa/data
2,编辑wordpress的depoloy资源清单
[root@k8s231 deployment]# cat dm-wordpress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dm-wp
spec:
replicas: 3
selector:
matchLabels:
k8s: wp
template:
metadata:
name: pod01
labels:
k8s: wp
spec:
volumes:
– name: vol-wp
nfs:
server: 10.0.0.230
path: /xinjizhiwa/data/wordpress
containers:
– name: c-wp
image: wordpress:latest
ports:
– name: wp-c-port
containerPort: 80
volumeMounts:
– name: vol-wp
mountPath: /var/www/html/wp-content/uploads
env:
– name: WORDPRESS_DB_HOST
#数据库的svc资源的clusterIP地址;
value: 10.200.200.200:3306
– name: WORDPRESS_DB_USER
value: xinjizhiwa
– name: WORDPRESS_DB_PASSWORD
value: xinjizhiwa
– name: WORDPRESS_DB_NAME
value: wordpress
3,编辑wordpress的svc资源
[root@k8s231 deployment]# vim svc-wordpress.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-wp
spec:
type: NodePort
selector:
k8s: wp
clusterIP: 10.200.200.100
ports:
– port: 99
targetPort: 80
nodePort: 31000
4,编辑数据库的deploy资源清单
[root@k8s231 deployment]# cat dm-mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dm-sql
spec:
replicas: 1
selector:
matchLabels:
k8s: sql
template:
metadata:
name: pod02
labels:
k8s: sql
spec:
volumes:
– name: vol-sql
nfs:
server: 10.0.0.230
path: /xinjizhiwa/data/mysql
containers:
– name: c-db
image: mysql:8.0
ports:
– name: db-port
containerPort: 3306
volumeMounts:
– name: vol-sql
mountPath: /var/lib/mysql
env:
– name: MYSQL_DATABASE
value: wordpress
– name: MYSQL_USER
value: xinjizhiwa
– name: MYSQL_PASSWORD
value: xinjizhiwa
– name: MYSQL_ROOT_PASSWORD
value: xinjizhiwa
5,编辑数据库的svc资源
[root@k8s231 deployment]# vim svc-mysql.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-sql
spec:
type: NodePort
selector:
k8s: sql
clusterIP: 10.200.200.200
ports:
– port: 3306
targetPort: 3306
nodePort: 32000
6,创建所有资源
[root@k8s231 deployment]# kubectl apply -f .
三、验证是否完成部署及数据共享
1,查看svc资源
[root@k8s231 deployment]# kubectl get svc
2,查看nfs存储路径,是否有数据
3,浏览器访问并安装
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net