1.部署说明
wordpress和mysql分开部署,各部署一个svc。
wordpress svc通过kube-dns组件服务访问mysql svc
配置NFS(实验环境测试用nfs方便)
配置PV,PVC存储wordpress和mysql数据
需要用到的变量:
mysql
MYSQL_ROOT_PASSWORD
root的密码
wordpress
WORDPRESS_DB_HOST
后端mysql主机
WORDPRESS_DB_PASSWORD
连接wordpress 数据的密码
WORDPRESS_DB_USER
连接wordpress的用户名,如果不指定,默认使用root
2.配置secret
配置mysql root密码
[[email protected] wp]# echo -n abc123! |base64
YWJjMTIzIQ==
[[email protected] wp]#
[[email protected] wp]# cat secret1.yml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
data:
password: YWJjMTIzIQ==
[[email protected] wp]# kubectl apply -f secret1.yml
secret "mysecret" configured
[[email protected] wp]#
wordpress相关,HOST使用mysql svc的名字,连接数据库用户就用root,密码就是上面配置的那个密码,所以无需额外再配置secret。
3.配置PV和PVC
mysql-pv
[[email protected] wp]# cat mysql-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: mysql-pv-nfs
nfs:
path: /data/k8s/mysql-pv
server: 192.168.211.135
[[email protected] wp]#
[[email protected] wp]# kubectl apply -f mysql-pv.yml
persistentvolume "mysql-pv" created
mysql-pvc
[[email protected] wp]# cat mysql-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
storageClassName: mysql-pv-nfs
[[email protected] wp]#
[[email protected] wp]# kubectl apply -f mysql-pvc.yml
persistentvolumeclaim "mysql-pvc" created
wordpess-pv
[[email protected] wp]# cat wordpress-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
storageClassName: wordpress-pv-nfs
[[email protected] wp]# kubectl apply -f wordpress-pv.yml
persistentvolume "wordpress-pv" created
[[email protected] wp]#
wordpress-pvc
[[email protected] wp]# kubectl apply -f wordpress-pv.yml
persistentvolume "wordpress-pv" created
[[email protected] wp]# cat wordpress-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
storageClassName: wordpress-pv-nfs
[[email protected] wp]# kubectl apply -f wordpress-pvc.yml
persistentvolumeclaim "wordpress-pvc" created
[[email protected] wp]#
[[email protected] wp]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 5Gi RWX Retain Bound default/mysql-pvc mysql-pv-nfs 17m
wordpress-pv 5Gi RWX Retain Bound default/wordpress-pvc wordpress-pv-nfs 1m
[[email protected] wp]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mysql-pv 5Gi RWX mysql-pv-nfs 10m
wordpress-pvc Bound wordpress-pv 5Gi RWX wordpress-pv-nfs 46s
[[email protected] wp]#
4.配置mysql
[[email protected] wp]# cat mysql-svc.yml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
[[email protected] wp]# kubectl apply -f mysql-svc.yml
service "mysql" created
deployment.apps "mysql" created
5.配置wordpress
[[email protected] wp]# cat wordpress.yml
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
type: NodePort
ports:
- protocol: TCP
nodePort: 30000
port: 8080
targetPort: 80
selector:
run: wordpress-pod
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: wordpress-pod
spec:
replicas: 1
template:
metadata:
labels:
run: wordpress-pod
spec:
containers:
- image: docker.io/wordpress
name: wordpress1
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
ports:
- containerPort: 80
name: wordpress1
volumeMounts:
- name: wordpress-data
mountPath: /var/www/html
volumes:
- name: wordpress-data
persistentVolumeClaim:
claimName: wordpress-pvc
[[email protected] wp]#
[[email protected] wp]# kubectl apply -f wordpress.yml
service "wordpress" created
deployment.apps "wordpress-pod" created
6.检查服务和端口
[[email protected] wp]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 55d
mysql ClusterIP 10.99.233.22 <none> 3306/TCP 9d
wordpress NodePort 10.109.38.87 <none> 8080:30000/TCP 9d
[[email protected] wp]#
[[email protected] wp]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-5bbbf49b4f-6ktxg 1/1 Running 1 9d 10.244.1.152 kubernetes2
wordpress-pod-7dd7659959-kx9wg 1/1 Running 1 9d 10.244.1.150 kubernetes2
wordpress 和mysql都起来了。
请注意,wordpress启用了NodePort,如果没有出问题,已经可以通过node的IP和这个30000端口访问web
如果不能访问,参考如下简单思路
检查service pod起来了吗?
在k8s部署节点,telnet service 的ip加端口,pod的ip加端口,看看能正常连接进去吗?
部署完成。
原文地址:http://blog.51cto.com/goome/2144667