WordPress環境の構築
MySQLのデプロイ
1.データベースのパスワードを格納する Kubernetes シークレットを作成
passwordには、任意のパスワードを指定します。
コマンド
# kubectl create secret generic mysql --from-literal=password=JkdJkd123@@@
secret/mysql created
2.「mysql-pv.yml」の作成
コマンド
# vim mysql-pv.yml
-----------------------------------------------------
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
type: local
spec:
capacity:
storage: 5Gi
storageClassName: mysql
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
hostPath:
path: /tmp/data/mysql
-----------------------------------------------------
Esc + :wq
3.pvの作成
コマンド
# kubectl create -f mysql-pv.yml
persistentvolume/mysql-pv created
4.「mysql-pvc.yml」の作成
コマンド
# vim mysql-pvc.yml
-----------------------------------------------------
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
labels:
app: wordpress
tier: mysql
spec:
accessModes:
- ReadWriteOnce
storageClassName: mysql
resources:
requests:
storage: 5Gi
-----------------------------------------------------
Esc + :wq
5.pvcの作成
コマンド
# kubectl create -f mysql-pvc.yml
persistentvolumeclaim/mysql-pvc created
6.pvとpvcの確認
コマンド
# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pvc Bound mysql-pv 5Gi RWO mysql 8s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 5Gi RWO Recycle Bound default/mysql-pvc mysql 40s
7.「mysql.yml」の作成
コマンド
# vim mysql.yml
-----------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 1
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: mysql
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-local-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-local-storage
persistentVolumeClaim:
claimName: mysql-pvc
-----------------------------------------------------
Esc + :wq
8.MySQLのdeploymentの作成
コマンド
# kubectl create -f mysql.yml
deployment.extensions/mysql created
9.podの確認
コマンド
# kubectl get pod -l app=mysql
NAME READY STATUS RESTARTS AGE
mysql-7fb595fb84-dnn9w 1/1 Running 0 14s
10.「mysql-service.yml」の作成
コマンド
# vim mysql-service.yml
-----------------------------------------------------
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
type: ClusterIP
ports:
- port: 3306
selector:
app: mysql
-----------------------------------------------------
Esc + :wq
11.serviceの作成
コマンド
# kubectl create -f mysql-service.yml
service/mysql created
12.serviceの確認
コマンド
# kubectl get service mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.43.82.149 <none> 3306/TCP 47s
WordPressのデプロイ
1.「wordpress-pv.yml」の作成
コマンド
# vim wordpress-pv.yml
-----------------------------------------------------
apiVersion: v1
kind: PersistentVolume
metadata:
name: wordpress-pv
labels:
type: local
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
storageClassName: wordpress
persistentVolumeReclaimPolicy: Recycle
hostPath:
path: /tmp/data/wordpress
-----------------------------------------------------
Esc + :wq
2.pvの作成
コマンド
# kubectl create -f wordpress-pv.yml
persistentvolume/wordpress-pv created
3.「wordpress-pvc.yml」の作成
コマンド
# vim wordpress-pvc.yml
-----------------------------------------------------
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
labels:
app: wordpress
tier: wordpress
spec:
accessModes:
- ReadWriteOnce
storageClassName: wordpress
resources:
requests:
storage: 5Gi
-----------------------------------------------------
Esc + :wq
4.pvcの作成
コマンド
# kubectl create -f wordpress-pvc.yml
persistentvolumeclaim/wordpress-pvc created
5.pvとpvcの確認
コマンド
# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pvc Bound mysql-pv 5Gi RWO mysql 3m8s
persistentvolumeclaim/wordpress-pvc Bound wordpress-pv 5Gi RWO wordpress 8s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 5Gi RWO Recycle Bound default/mysql-pvc mysql 3m40s
persistentvolume/wordpress-pv 5Gi RWO Recycle Bound default/wordpress-pvc wordpress 35s
6.「wordpress.yml」の作成
コマンド
# vim wordpress.yml
-----------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql:3306
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-local-storage
mountPath: /var/www/html
volumes:
- name: wordpress-local-storage
persistentVolumeClaim:
claimName: wordpress-pvc
-----------------------------------------------------
Esc + :wq
7.WordPressのdeploymentの作成
コマンド
# kubectl create -f wordpress.yml
deployment.extensions/wordpress created
8.podの確認
コマンド
# kubectl get pod -l app=wordpress
NAME READY STATUS RESTARTS AGE
wordpress-f5db5bccc-42z6p 1/1 Running 0 41s
9.「wordpress-service.yml」の作成
コマンド
# vim wordpress-service.yml
-----------------------------------------------------
apiVersion: v1
kind: Service
metadata:
labels:
app: wordpress
name: wordpress
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: wordpress
-----------------------------------------------------
Esc + :wq
10.serviceの作成
コマンド
# kubectl create -f wordpress-service.yml
service/wordpress created
11.serviceの確認
コマンド
# kubectl get svc -l app=wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NordPort 10.43.102.184 <pending> 80:30983/TCP 2m
この後、ブラウザでアクセスする際に利用するNodePortは、80:の後のポート番号になります。
WordPressにアクセス
1.Choromeブラウザを起動してアクセス
Podの自動修復(セルフヒーリング)
1.WordPressのPod状況を確認
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-7fb595fb84-q4csx 1/1 Running 0 20m
wordpress-f5db5bccc-5hgc5 1/1 Running 0 19m
2.WordPressのPodを削除
コマンド
# kubectl delete pod -l app=wordpress
pod "wordpress-f5db5bccc-5hgc5" deleted
3.WordPressのPod名の確認
Pod削除前と名前が変わり、新たなPodが作られています。
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-7fb595fb84-q4csx 1/1 Running 0 23m
wordpress-f5db5bccc-ds8wk 1/1 Running 0 1m
Podのスケールアウト
1.WordPressのPodを10個増やす
コマンド
# kubectl scale deployment wordpress --replicas 10
deployment.extensions/wordpress scaled
2.WordPressのPod状況を確認
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-7fb595fb84-q4csx 1/1 Running 0 30m
wordpress-f5db5bccc-95pwv 1/1 Running 0 37s
wordpress-f5db5bccc-dr44d 1/1 Running 0 36s
wordpress-f5db5bccc-ds8wk 1/1 Running 0 7m
wordpress-f5db5bccc-dx8xs 1/1 Running 0 37s
wordpress-f5db5bccc-hfqpc 1/1 Running 0 37s
wordpress-f5db5bccc-jcz7t 1/1 Running 0 36s
wordpress-f5db5bccc-l5vq9 1/1 Running 0 36s
wordpress-f5db5bccc-lf24s 1/1 Running 0 36s
wordpress-f5db5bccc-pznkd 1/1 Running 0 36s
wordpress-f5db5bccc-qg6kk 1/1 Running 0 36s
「wordpress.yml」内のreplicasの数値を変更して、kubectl applyコマンドを実行しても同じ結果となります。
クリーンアップ
1.deploymentの削除
コマンド
# kubectl delete -f wordpress.yml
deployment.extensions "wordpress" deleted
# kubectl delete -f mysql.yml
deployment.extensions "mysql" deleted
2.serviceの削除
コマンド
# kubectl delete -f wordpress-service.yml
service "wordpress" deleted
# kubectl delete -f mysql-service.yml
service "mysql" deleted
3.pvcの削除
コマンド
# kubectl delete -f wordpress-pvc.yml
persistentvolumeclaim "wordpress-pvc" deleted
# kubectl delete -f mysql-pvc.yml
persistentvolumeclaim "mysql-pvc" deleted
4.pvの削除
コマンド
# kubectl delete -f wordpress-pv.yml
persistentvolume "wordpress-pv" deleted
# kubectl delete -f mysql-pv.yml
persistentvolume "mysql-pv" deleted
5.podの確認
コマンド
# kubectl get pod -l app=wordpress
No resources found in default namespace.
# kubectl get pod -l app=mysql
No resources found in default namespace.
6.serviceの確認
コマンド
# kubectl get svc -l app=wordpress
No resources found in default namespace.
# kubectl get svc -l app=mysql
No resources found in default namespace.
7.pv,pvcの確認
コマンド
# kubectl get pvc,pv
No resources found in default namespace.