步骤简单分析:
1,准备NFS环境
2,【wordpress的pod】创建deployment资源的wordpress(pod)容器;
3,【用户访问的svc】创建用户访问的svc资源;
4,【数据库的pod】创建deployment资源的数据库服务的pod容器;
5,【数据库的svc】创建业务服务wordpress的pod资源访问数据库的svc资源;
我们采用k8s外部虚拟机的路径,实现数据卷的nfs挂载,本次学习,我们采用harbor的虚拟机进行存储静态数据;
由于,k8s集群,在创建业务pod时候,需要挂载外部的nfs,那么也需要有nfs的命令的使用,所以,k8s集群中 的机器,同样也需要安装nfs;
注意,是k8s集群所有节点,加上挂载节点(数据存储节点)都需要安装哦;
yum -y install nfs-utils
[root@harbor ~]# vim /etc/exports
/xinjizhiwa/data *(rw,no_root_squash)
[root@harbor ~]# mkdir -p /xinjizhiwa/data/{mysql,wordpress}
[root@harbor ~]# systemctl enable --now nfs
验证是否成功
[root@harbor ~]# exportfs
/xinjizhiwa/data
[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
[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
[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
[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
[root@k8s231 deployment]# kubectl apply -f .
[root@k8s231 deployment]# kubectl get svc