转:http://www.showerlee.com/archives/2336
这部分我们结合之前的k8s知识点给大家展示如何使用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主页
添加相关信息并初始化安装WordPress
完成安装
后台
主页
2.查看NFS主机对在容器里的mysql data与wordpress root dir的目录挂载.
有兴趣的小伙伴可以直接访问如下代码仓库去下载本文相关代码:
https://git.showerlee.com/showerlee/kube-deploy/tree/master/wordpress-mysql
Finished...
后记:
如果我们使用helm包管理去部署wordpress, 将大大简化我们的工作量.
这里我的代码仓库提供了wordpress chart部署脚本, 以下是详细的部署步骤:
Prerequisite:
Kubernetes cluster setup
http://www.showerlee.com/archives/2200
Helm setup
http://www.showerlee.com/archives/2455
Helm deployment:
# git clone [email protected]:showerlee/kube-deploy.git
# cd kube-deploy
# kubectl create secret generic mysql-pass --from-literal=‘password=countonme‘
# helm install --name wordpress-mysql ./wordpress-helm-chart --set service.type=NodePort
NAME: wordpress-mysql LAST DEPLOYED: Sat Apr 14 03:09:46 2018 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/PersistentVolume NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mysql-pv 5Gi RWO Recycle Bound default/mysql-pv-claim 1s wp-pv 5Gi RWO Recycle Bound default/wp-pv-claim 1s ==> v1/PersistentVolumeClaim NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE mysql-pv-claim Bound mysql-pv 5Gi RWO 1s wp-pv-claim Bound wp-pv 5Gi RWO 1s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE wordpress-mysql ClusterIP None <none> 3306/TCP 1s wordpress NodePort 10.110.14.233 <none> 80:80/TCP 1s ==> v1/Deployment NAME AGE wordpress-mysql 1s wordpress 1s NOTES: 1. Get the application URL by running these commands: export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services wordpress-mysql-wordpress-helm-chart) export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}") echo http://$NODE_IP:$NODE_PORT
原文地址:https://www.cnblogs.com/wangle1001986/p/9726902.html