使用kubernetes部署wordpress+MySQL, 并利用NFS去保存我们容器的源代码以及DB数据.
安装环境
System: CentOS 7.4
Kubernetes: Kubernetes1.9
Docker: 17.03.2-ce
kube-master 10.110.16.10
kube-node-1 10.110.16.11
一. NFS配置:
1. NFS依赖包安装
在Master与Node分别安装NFS组件
# yum install nfs-utils -y
Tip: 这里需保证nfs-utils安装到所有master和node中, 否则容器挂载NFS时会报错.
2. 为Master下mysql data和wordpress源码配置NFS共享目录
# systemctl enable nfs-server && systemctl start nfs-server
# mkdir -p /kube/mysql-db
# mkdir -p /kube/wordpress
# chown nfsnobody:nfsnobody /kube/mysql-db
# chown nfsnobody:nfsnobody /kube/wordpress
# chmod 755 /kube/mysql-db
# chmod 755 /kube/wordpress
# echo -e "/kube/mysql-db kube-*(rw,sync,no_subtree_check,no_root_squash)" > /etc/exports
# echo -e "/kube/wordpress kube-*(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
Tip: 这里kube-*限制只有kube相关的server才能连接Master下NFS共享目录, no_root_squash参数保证wordpress-mysql pod在初始化mysql配置的时候向在其下挂载的/var/lib/mysql目录有写入权限
3.应用配置
# exportfs -a
二. Persistent volume配置
1.为mysql data与wordpress源码存储创建Persistent volume
# kubectl create -f mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
app: mysql
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /kube/mysql-db
server: kube-master
# kubectl create -f wordpress-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv
labels:
app: wordpress
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /kube/wordpress
server: kube-master
2.创建存放mysql data的PVC
# kubectl create -f mysql-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pv-claim
labels:
app: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
3.创建存放wordpress源码的PVC
# kubectl create -f wordpress-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
查看绑定
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound mysql-pv 5Gi RWO 3m
wp-pv-claim Bound wp-pv 5Gi RWO 6s
三. Secret配置
1.创建mysql root password
# kubectl create secret generic mysql-pass --from-literal='password=countonme'
四. Deployment配置
1.部署mysql deployment with PVC
# kubectl create -f mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
2.部署wordpress deployment with PVC
# kubectl create -f wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
3.Service配置
Tip: 这里我们开启了node IP的80端口的外部访问权限, 可以方便我们直接利用主机去访问虚拟机任意Node地址从而登录我们的Wordpress网站.
# kubectl create -f wp-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
nodePort: 80
selector:
app: wordpress
tier: frontend
type: NodePort
Tip: 这里service定义的name: wordpress-mysql保证我们wordpress-deployment.yaml定义的如下环境变量可以作为有效的域名成功去访问我们的mysql容器, 保证网站服务器与数据库服务器的通讯.
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
五. 验证结果
1.访问wordpress主页
这里我们可以直接在浏览器访问任意node的IP地址从而进入wordpress主页