學習 Kubernetes 容器管理平台(八):使用 Persistent Volume 部署 WordPress 內容管理系統

前言

本文為〈Kubernetes 官方文件〉的學習筆記。

環境

  • MacOS
  • minikube

概述

目標為創建 PersistentVolumes(叢集中的一塊儲存區)和 PersistentVolumeClaims(使用者對儲存區的請求),以及創建 Secret 生成器、MySQL 資源配置和 WordPress 資源配置。

創建資源

建立 mysql-deployment.yaml 檔,這是 MySQL 的 Deployment 設定檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
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
  • MySQL 容器將 PersistentVolume 掛載在 /var/lib/mysql 資料夾。

建立 wordpress-deployment.yaml 檔,這是 WordPress 的 Deployment 設定檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
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
  • WordPress 容器將 PersistentVolume 掛載在 /var/www/html 資料夾。

新建 kustomization.yaml 檔,添加一個 Secret 生成器,以及 MySQL 和 WordPress 資源配置。

1
2
3
4
5
6
7
secretGenerator:
- name: mysql-pass
literals:
- password=MY_PASSWORD
resources:
- mysql-deployment.yaml
- wordpress-deployment.yaml
  • secretGenerator 中的 mysql-pass 會被 MySQL 容器所使用。

創建所有資源。

1
kubectl apply -k .

查看所有 Secrets。

1
kubectl get secrets

查看所有 PersistentVolumeClaims。

1
kubectl get pvc

查看所有 Pods。

1
kubectl get pods

查看所有 Services。

1
kubectl get services

訪問應用程式

訪問應用程式。

1
minikube service wordpress

清除資源

清除所有資源。

1
kubectl delete -k .