1. 程式人生 > >k8s部署wordpress記錄

k8s部署wordpress記錄

containe == cat container lib image 5.7 readwrite chl

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密碼


[root@kubernetes1 wp]# echo -n abc123! |base64

YWJjMTIzIQ==

[root@kubernetes1 wp]#

[root@kubernetes1 wp]# cat secret1.yml

apiVersion: v1

kind: Secret

metadata:

name: mysecret

data:

password: YWJjMTIzIQ==

[root@kubernetes1 wp]# kubectl apply -f secret1.yml

secret "mysecret" configured

[root@kubernetes1 wp]#


wordpress相關,HOST使用mysql svc的名字,連接數據庫用戶就用root,密碼就是上面配置的那個密碼,所以無需額外再配置secret。



3.配置PV和PVC


mysql-pv


[root@kubernetes1 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

[root@kubernetes1 wp]#

[root@kubernetes1 wp]# kubectl apply -f mysql-pv.yml

persistentvolume "mysql-pv" created



mysql-pvc


[root@kubernetes1 wp]# cat mysql-pvc.yml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: mysql-pvc

spec:

accessModes:

- ReadWriteMany

resources:

requests:

storage: 5Gi

storageClassName: mysql-pv-nfs

[root@kubernetes1 wp]#

[root@kubernetes1 wp]# kubectl apply -f mysql-pvc.yml

persistentvolumeclaim "mysql-pvc" created



wordpess-pv


[root@kubernetes1 wp]# cat wordpress-pvc.yml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: wordpress-pvc

spec:

accessModes:

- ReadWriteMany

resources:

requests:

storage: 5Gi

storageClassName: wordpress-pv-nfs


[root@kubernetes1 wp]# kubectl apply -f wordpress-pv.yml

persistentvolume "wordpress-pv" created

[root@kubernetes1 wp]#


wordpress-pvc


[root@kubernetes1 wp]# kubectl apply -f wordpress-pv.yml

persistentvolume "wordpress-pv" created

[root@kubernetes1 wp]# cat wordpress-pvc.yml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: wordpress-pvc

spec:

accessModes:

- ReadWriteMany

resources:

requests:

storage: 5Gi

storageClassName: wordpress-pv-nfs


[root@kubernetes1 wp]# kubectl apply -f wordpress-pvc.yml

persistentvolumeclaim "wordpress-pvc" created

[root@kubernetes1 wp]#



[root@kubernetes1 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

[root@kubernetes1 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

[root@kubernetes1 wp]#



4.配置mysql


[root@kubernetes1 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



[root@kubernetes1 wp]# kubectl apply -f mysql-svc.yml

service "mysql" created

deployment.apps "mysql" created


5.配置wordpress


[root@kubernetes1 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

[root@kubernetes1 wp]#


[root@kubernetes1 wp]# kubectl apply -f wordpress.yml

service "wordpress" created

deployment.apps "wordpress-pod" created



6.檢查服務和端口


[root@kubernetes1 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

[root@kubernetes1 wp]#


[root@kubernetes1 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加端口,看看能正常連接進去嗎?


部署完成。



k8s部署wordpress記錄