Closed48

Game of Pods - Bravo

kenryokenryo

Kubernetesは全くの初心者、英語も微妙だがやってみる

kenryokenryo

緑色アイコンが正常、緑色の線が通信が到達できていることを表しているみたい。
赤色アイコンは異常、赤色の線は通信が到達できていないことを表す。
このゲームの目的は全部緑色にすること。
Users以外の各アイコンは何を表しているかはなんとなくしかわからない。ディレクトリ、ロードバランサ、テーブル?、ファイル。

このゲームを始める前に説明動画があり、その動画では末端から解決していたのでそれに倣ってやっていく。

kenryokenryo

左の末端にあるdrupal-pv-hostpathをクリックすると

Configure drupal-pv with hostPath = /drupal-data (create the directory on Worker Nodes)

と書いてあった。
drupal-dataというディレクトリを作成しろというみたい。
また、drupal-mysql-pv-hostpathも同じようなことが書いてあった。
各ディレクトリ上にあるアイコンは説明を見るとボリュームっぽい、データの保存場所となるディレクトリを作るのかな

kenryokenryo

アプリケーションのpodを管理しているnode01にsshでログインできる。
ルートディレクトリにdrupal-dataとdrupal-mysql-pv-hostpathを作成してみた。

controlplane $ kubectl get nodes
NAME           STATUS   ROLES    AGE   VERSION
controlplane   Ready    master   17m   v1.18.0
node01         Ready    <none>   17m   v1.18.0
controlplane $ ssh node01
Warning: Permanently added 'node01,172.17.0.122' (ECDSA) to the list of known hosts.
node01 $ mkdir /drupal-mysql-pv-hostpath
node01 $ mkdir /drupal-data
node01 $ ll /
total 112
drwxr-xr-x  27 root root  4096 Mar 16 13:43 ./
drwxr-xr-x  27 root root  4096 Mar 16 13:43 ../
drwxr-xr-x   2 root root  4096 Nov 10  2020 bin/
drwxr-xr-x   3 root root  4096 Nov 10  2020 boot/
drwxr-xr-x  18 root root  4000 Mar 16 13:24 dev/
drwxr-xr-x   2 root root  4096 Mar 16 13:43 drupal-data/
drwxr-xr-x   2 root root  4096 Mar 16 13:43 drupal-mysql-pv-hostpath/
drwxr-xr-x 102 root root  4096 Mar 16 13:24 etc/
drwxr-xr-x   3 root root  4096 Nov 10  2020 home/
lrwxrwxrwx   1 root root    34 Nov 10  2020 initrd.img -> boot/initrd.img-4.15.0-122-generic
lrwxrwxrwx   1 root root    33 Nov 10  2020 initrd.img.old -> boot/initrd.img-4.15.0-29-generic
drwxr-xr-x  19 root root  4096 Nov 10  2020 lib/
drwxr-xr-x   2 root root  4096 Nov 10  2020 lib32/
drwxr-xr-x   2 root root  4096 Nov 10  2020 lib64/
drwxr-xr-x   2 root root  4096 Nov 10  2020 libx32/
drwx------   2 root root 16384 Nov 10  2020 lost+found/
drwxr-xr-x   4 root root  4096 Nov 10  2020 media/
drwxr-xr-x   2 root root  4096 Jul 25  2018 mnt/
drwxr-xr-x   8 root root  4096 Mar 16 13:23 opt/
dr-xr-xr-x 125 root root     0 Mar 16 13:22 proc/
drwx------   7 root root  4096 Mar 16 13:42 root/
drwxr-xr-x  25 root root   920 Mar 16 13:43 run/
drwxr-xr-x   2 root root 12288 Nov 10  2020 sbin/
drwxr-xr-x   2 root root  4096 Nov 10  2020 snap/
drwxr-xr-x   2 root root  4096 Jul 25  2018 srv/
dr-xr-xr-x  13 root root     0 Mar 16 13:22 sys/
drwxrwxrwt  10 root root  4096 Mar 16 13:23 tmp/
drwxr-xr-x  13 root root  4096 Nov 10  2020 usr/
drwxr-xr-x  12 root root  4096 Nov 10  2020 var/
lrwxrwxrwx   1 root root    31 Nov 10  2020 vmlinuz -> boot/vmlinuz-4.15.0-122-generic
lrwxrwxrwx   1 root root    30 Nov 10  2020 vmlinuz.old -> boot/vmlinuz-4.15.0-29-generic
node01 $ 
kenryokenryo

動画に倣ってdrupal-mysql-pv.yamlを作成。なお、master(controlplane)で行う。
drupal-dataも同じ感じで作成する。

controlplane $ cat > drupal-mysql-pv.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-mysql-pv
spec:
 accessModes: [ “ReadWriteOnce” ]
 capacity:
 storage: “5Gi”
 hostPath:
 path: /drupal-mysql-data
controlplane $ ll
total 52
drwx------ 7 root root 4096 Mar 16 13:55 ./
drwxr-xr-x 1 root root 4096 Mar 16 13:24 ../
-rw------- 1 root root 1213 Jun 18  2020 .bash_history
-rw-r--r-- 1 root root 3288 Mar 16 13:24 .bashrc
-rw-r--r-- 1 root root 3231 Mar 16 13:22 .bashrc.old
drwx------ 2 root root 4096 Mar 16 13:22 .cache/
drwx------ 3 root root 4096 Mar 16 13:22 .gnupg/
-rw-r--r-- 1 root root    0 Nov 16  2020 .hushlogin
drwxr-xr-x 4 root root 4096 Mar 16 13:23 .kube/
-rw-r--r-- 1 root root  148 Aug 17  2015 .profile
drwxr-xr-x 2 root root 4096 Mar 16 13:41 .ssh/
-rw-r--r-- 1 root root  123 Nov 10  2020 .vimrc
-rw-r--r-- 1 root root  183 Mar 16 13:55 drupal-mysql-pv.yaml
drwxr-xr-x 4 root root 4096 Nov 16  2020 go/
controlplane $ 
kenryokenryo

drupal-mysql-pv-hostpathじゃなくdrupal-mysql-pvだったので修正

node01 $ rmdir /drupal-
drupal-data/              drupal-mysql-pv-hostpath/ 
node01 $ rmdir /drupal-mysql-pv-hostpath/
node01 $ mkdir /drupal-mysql-pv
node01 $ 
kenryokenryo

drupal-pv用も作成

controlplane $ cat > drupal-data.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-pv
spec:
 accessModes: [ “ReadWriteOnce” ]
 capacity:
 storage: “5Gi”
 hostPath:
 path: /drupal-data
controlplane $ ll
total 60
drwx------ 7 root root 4096 Mar 16 14:04 ./
drwxr-xr-x 1 root root 4096 Mar 16 13:24 ../
-rw------- 1 root root 1213 Jun 18  2020 .bash_history
-rw-r--r-- 1 root root 3288 Mar 16 13:24 .bashrc
-rw-r--r-- 1 root root 3231 Mar 16 13:22 .bashrc.old
drwx------ 2 root root 4096 Mar 16 13:22 .cache/
drwx------ 3 root root 4096 Mar 16 13:22 .gnupg/
-rw-r--r-- 1 root root    0 Nov 16  2020 .hushlogin
drwxr-xr-x 4 root root 4096 Mar 16 13:23 .kube/
-rw-r--r-- 1 root root  148 Aug 17  2015 .profile
drwxr-xr-x 2 root root 4096 Mar 16 13:41 .ssh/
-rw------- 1 root root  876 Mar 16 14:03 .viminfo
-rw-r--r-- 1 root root  123 Nov 10  2020 .vimrc
-rw-r--r-- 1 root root  171 Mar 16 14:04 drupal-data.yaml
-rw-r--r-- 1 root root  183 Mar 16 13:55 drupal-mysql-pv.yaml
drwxr-xr-x 4 root root 4096 Nov 16  2020 go/
controlplane $ 
kenryokenryo

書いたyamlを適用するにはkubectl createを使用する。
drupal-data.yamlに対して実行したらエラーになった。

controlplane $ kubectl create -f drupal-data.yaml   
error: error validating "drupal-data.yaml": error validating data: [ValidationError(PersistentVolume.spec): unknown field "path" in io.k8s.api.core.v1.PersistentVolumeSpec, ValidationError(PersistentVolume.spec): unknown field "storage" in io.k8s.api.core.v1.PersistentVolumeSpec]; if you choose to ignore these errors, turn validation off with --validate=false
kenryokenryo

drupal-mysql-pv.yaml も同様。

controlplane $ kubectl create -f drupal-mysql-pv.yaml 
error: error validating "drupal-mysql-pv.yaml": error validating data: [ValidationError(PersistentVolume.spec): unknown field "path" in io.k8s.api.core.v1.PersistentVolumeSpec, ValidationError(PersistentVolume.spec): unknown field "storage" in io.k8s.api.core.v1.PersistentVolumeSpec]; if you choose to ignore these errors, turn validation off with --validate=false
kenryokenryo

ファイルをlessで見たら文字化けしていた、また、yamlのインデントがずれていた。
直して適用したらうまくいったっぽい

controlplane $ kubectl create -f drupal-data.yaml  
persistentvolume/drupal-pv created
kenryokenryo

先ほどの画面でcheckして緑になるか確かめようとしたが制限時間(1時間)になってしまった。明日最初から実施する。
サーバがクソ重い(無料だから仕方がない)

kenryokenryo
ssh node01
mkdir /drupal-data
mkdir /drupal-mysql-data
exit
vi drupal-data.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-data
vi drupal-mysql-pv.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-mysql-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-mysql-data
kubectl create -f drupal-data.yaml  
kubectl create -f drupal-mysql-pv.yaml 
kenryokenryo

上のをやってdrupal-pv, drupal-mysql-pvが緑になった。
これらは永続ストレージのようで、kuberctlコマンドにより作成されたようだ。
なお、上のyamlの内容をサイトのエディタにコピペするとダブルクォートが文字化けするので注意。

kenryokenryo

これからどうするか確認する。
まず、drupal-pvcをクリックする。pvcの意味はわからない。
容量が記載されているのでこれもストレージ?
drupal-mysql-pvcも同じような感じだった。

kenryokenryo

drupal-mysql-secret
その名前と表示された情報からmysqlの認証情報やデータベース名に関するものみたい。

kenryokenryo

drupal-mysql
Imageとはdocker imageのことだろう。
Deployment Volume uses PVC : drupal-mysql-pvcとは、mysqlをデプロイする際に使用するボリューム?
Volume Mount Pathはmysqlのインストール先となるパスだろう。このディレクトリをnode01に作成する必要がある。

kenryokenryo

drupal-mysql-service
よくわからない。
ポートは3306。サーバ?

kenryokenryo

drupal
出力情報が一番多い。
Imageはdocker imageだろう。

Deployment has an initContainer, name: 'init-sites-volume'
initContainer 'init-sites-volume', image: drupal:8.6
initContainer 'init-sites-volume', persistentVolumeClaim: drupal-pvc
initContainer 'init-sites-volume', mountPath: /data
initContainer 'init-sites-volume', Command: [ "/bin/bash", "-c" ], initContainer: Args: [ 'cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R' ]

initContainerってなんだろうか?init-sites-volumeという名前で/dataにマウントされる。
永続ボリュームとしてdrupal-pvcを使用する?
実行時にcpコマンドで/dataになんかコピーしている。

Deployment 'drupal' uses correct pvc: drupal-pvc
Deployment has a regular container, name: 'drupal', image: 'drupal:8.6'
container: 'drupal', Volume mountPath: /var/www/html/modules, subPath: modules
container: 'drupal', Volume mountPath: /var/www/html/profiles, subPath: profiles
container: 'drupal', Volume mountPath: /var/www/html/sites, subPath: sites
container: 'drupal', Volume mountPath: /var/www/html/themes, subPath: themes
Deployment: "drupal" running
Deployment: 'drupal' has label 'app=drupal'

あとはうまくいっているようなテキストっぽい。

kenryokenryo

drupal-service

frontend service の名前をdrupal-serviceとしている。

kenryokenryo

pvってPersistentVolumeの略か。
pvcはPersistentVolumeClaimの略で、ユーザーの要求によってpvからリソースを拝借する。
このゲームだとdrupal-pvc, drupal-mysql-pvcともに5Giをpvに要求している。

https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/

pvと同じようにyaml作って適用すればいい感じ?
https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/#永続ボリュームの予約

https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims

kenryokenryo
vi drupal-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-pv"
vi drupal-mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-mysql-pv"
kubectl apply -f drupal-data.yaml  
kubectl apply -f drupal-mysql-pv.yaml 

なお、kubectl createを使用するとapplyでの更新でおかしなことになる可能性があるため、今後createは使わず、applyを使用する
https://tenzen.hatenablog.com/entry/2019/11/24/033035

kenryokenryo

PVCの作成うまくいったようだ

controlplane $ kubectl apply -f drupal-pvc.yaml 
persistentvolumeclaim/drupal-pvc created
controlplane $ kubectl apply -f drupal-mysql-pvc.yaml 
persistentvolumeclaim/drupal-mysql-pvc created

kenryokenryo
ssh node01
mkdir /drupal-data
mkdir /drupal-mysql-data
exit
vi drupal-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-pv"
vi drupal-mysql-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-mysql-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-mysql-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-mysql-pv"
kubectl apply -f drupal-pv-pvc.yaml  
kubectl apply -f drupal-mysql-pv-pvc.yaml 
kenryokenryo

こんな感じ?Secretはコマンドから作成して、drupal-mysqlに参照させる。

kubectl create secret generic drupal-mysql-secret --from-literal=MYSQL_ROOT_PASSWORD=root_password --from-literal=MYSQL_DATABASE=drupal-database --from-literal=MYSQL_USER=root

まだ、問題あり。serviceがdeploymentを参照する必要がある。

apiVersion: v1
kind: Service
metadata:
  name: drupal-mysql-service
spec:
  type: ClusterIP
  ports:
    - name: mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app: drupal-mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal-mysql
  template:
    metadata:
      labels:
        app: drupal-mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_USER
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: drupal-mysql-vol
          mountPath: /var/lib/mysql
          subPath: dbdata
      volumes:
        - name: drupal-mysql-vol
          persistentVolumeClaim:
            claimName: drupal-mysql-pvc
kenryokenryo

Secretはうまく作られたようだ

controlplane $ kubectl create secret generic drupal-mysql-secret --from-literal=MYSQL_ROOT_PASSWORD=root_password --from-literal=MYSQL_DATABASE=drupal-database --from-literal=MYSQL_USER=root
secret/drupal-mysql-secret created
controlplane $ 

kenryokenryo

pvとpvcの関連付けができていなかったのでyaml修正

ssh node01
mkdir /drupal-data
mkdir /drupal-mysql-data
exit
vi drupal-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-data
 claimRef:
  name: drupal-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-pv"
  volumeName: drupal-pv
vi drupal-mysql-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-mysql-pv
spec:
 accessModes: [ "ReadWriteOnce" ]
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-mysql-data
 claimRef:
  name: drupal-mysql-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: ""
  selector:
    matchLabels:
      name: "drupal-mysql-pv"
  volumeName: drupal-mysql-pv
kubectl apply -f drupal-pv-pvc.yaml  
kubectl apply -f drupal-mysql-pv-pvc.yaml 
kenryokenryo

Deploymentの情報確認

controlplane $ kubectl describe deployment drupal-mysql
Name:                   drupal-mysql
Namespace:              default
CreationTimestamp:      Mon, 18 Apr 2022 14:49:40 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=drupal-mysql
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=drupal-mysql
  Containers:
   mysql:
    Image:      mysql:5.7
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'MYSQL_ROOT_PASSWORD' in secret 'drupal-mysql-secret'>  Optional: false
      MYSQL_DATABASE:       <set to the key 'MYSQL_DATABASE' in secret 'drupal-mysql-secret'>       Optional: false
      MYSQL_USER:           <set to the key 'MYSQL_USER' in secret 'drupal-mysql-secret'>           Optional: false
    Mounts:
      /var/lib/mysql from drupal-mysql-vol (rw,path="dbdata")
  Volumes:
   drupal-mysql-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-mysql-pvc
    ReadOnly:   false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    False   ProgressDeadlineExceeded
OldReplicaSets:  <none>
NewReplicaSet:   drupal-mysql-d69c48b85 (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  22m   deployment-controller  Scaled up replica set drupal-mysql-d69c48b85 to 1

Podの一覧を確認

controlplane $ kubectl get pods -l app=drupal-mysql
NAME                           READY   STATUS    RESTARTS   AGE
drupal-mysql-d69c48b85-bz4hm   0/1     Pending   0          20m

Podが起動していないので以下のコマンドで原因確認

controlplane $ kubectl describe pod drupal-mysql-d69c48b85-bz4hm
Name:           drupal-mysql-d69c48b85-bz4hm
Namespace:      default
Priority:       0
Node:           <none>
Labels:         app=drupal-mysql
                pod-template-hash=d69c48b85
Annotations:    <none>
Status:         Pending
IP:             
IPs:            <none>
Controlled By:  ReplicaSet/drupal-mysql-d69c48b85
Containers:
  mysql:
    Image:      mysql:5.7
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'MYSQL_ROOT_PASSWORD' in secret 'drupal-mysql-secret'>  Optional: false
      MYSQL_DATABASE:       <set to the key 'MYSQL_DATABASE' in secret 'drupal-mysql-secret'>       Optional: false
      MYSQL_USER:           <set to the key 'MYSQL_USER' in secret 'drupal-mysql-secret'>           Optional: false
    Mounts:
      /var/lib/mysql from drupal-mysql-vol (rw,path="dbdata")
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-bzwg6 (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  drupal-mysql-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-mysql-pvc
    ReadOnly:   false
  default-token-bzwg6:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-bzwg6
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason            Age                 From               Message
  ----     ------            ----                ----               -------
  Warning  FailedScheduling  77s (x27 over 31m)  default-scheduler  running "VolumeBinding" filter plugin for pod "drupal-mysql-d69c48b85-bz4hm": pod has unbound immediate PersistentVolumeClaims
controlplane $  
kenryokenryo

Eventsのエラーっぽいメッセージを調べてみると

https://stackoverflow.com/questions/60774220/kubernetes-pod-has-unbound-immediate-persistentvolumeclaims

pvcのボリュームがpvより大きいとダメ
pvcの数がpvより多いとダメ
と記載があった。

コマンドで作成したpv、pvcの状態を見てみると、上記とは関係ないが、pvcのvolumeが0になっておりおかしい。

controlplane $ kubectl get pv
NAME              CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM               STORAGECLASS   REASON   AGE
drupal-mysql-pv   5Gi        RWO            Retain           Available   /drupal-mysql-pvc                           43m
drupal-pv         5Gi        RWO            Retain           Available   /drupal-pvc                                 43m
controlplane $ kubectl get pvc
NAME               STATUS    VOLUME            CAPACITY   ACCESS MODES   STORAGECLASS   AGE
drupal-mysql-pvc   Pending   drupal-mysql-pv   0                                        43m
drupal-pvc         Pending   drupal-pv         0                                        44m
controlplane $ 
kenryokenryo
ssh node01
mkdir /drupal-data
mkdir /drupal-mysql-data
exit
vi drupal-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-pv
spec:
 accessModes: 
  - ReadWriteOnce
 storageClassName: standard
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: standard
  selector:
    matchLabels:
      name: "drupal-pv"
  volumeName: drupal-pv
vi drupal-mysql-pv-pvc.yaml  
apiVersion: v1
kind: PersistentVolume
metadata:
 name: drupal-mysql-pv
spec:
 accessModes: 
  - ReadWriteOnce
 storageClassName: standard
 capacity:
  storage: "5Gi"
 hostPath:
  path: /drupal-mysql-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "5Gi"
  storageClassName: standard
  selector:
    matchLabels:
      name: "drupal-mysql-pv"
  volumeName: drupal-mysql-pv
kubectl apply -f drupal-pv-pvc.yaml  
kubectl apply -f drupal-mysql-pv-pvc.yaml 
kubectl create secret generic drupal-mysql-secret --from-literal=MYSQL_ROOT_PASSWORD=root_password --from-literal=MYSQL_DATABASE=drupal-database --from-literal=MYSQL_USER=root
vi drupal-mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: drupal-mysql-service
spec:
  type: ClusterIP
  ports:
    - name: mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app: drupal-mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal-mysql
  template:
    metadata:
      labels:
        app: drupal-mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        envFrom:
        - secretRef:
            name: drupal-mysql-secret
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: drupal-mysql-secret
              key: MYSQL_USER
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: drupal-mysql-vol
          mountPath: /var/lib/mysql
          subPath: dbdata
      volumes:
        - name: drupal-mysql-vol
          persistentVolumeClaim:
            claimName: drupal-mysql-pvc
kubectl apply -f drupal-mysql-service.yaml 
kenryokenryo

podの状態をみるとよくわからないステータスになっていた。

controlplane $ kubectl get pods
NAME                            READY   STATUS             RESTARTS   AGE
drupal-mysql-858b9df8bb-9fhkf   0/1     CrashLoopBackOff   1          40s
controlplane $ 

調べたら再起動を繰り返しているみたいなので記事の通り追加してみる。

https://qiita.com/tkusumi/items/01cd18c59b742eebdc6a

restartPolicy: OnFailure

yaml修正し、apply後エラー発生。

controlplane $ kubectl apply -f drupal-mysql-service.yaml 
service/drupal-mysql-service unchanged
The Deployment "drupal-mysql" is invalid: spec.template.spec.restartPolicy: Unsupported value: "OnFailure": supported values: "Always"

おそらくDeploymentにはOnFailureは指定できない?一旦コメントアウト。

https://qiita.com/oguogura/items/2d7df2b645063dc47804

kubectl describe podの情報からは原因わからなかった。
kubectl logs pods/{pod名}で以下の情報が出力された。
MYSQL_USERにはrootは使えないから怒られている。

controlplane $ kubectl logs pods/drupal-mysql-d69c48b85-2b5xb
2022-04-21 15:58:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.37-1debian10 started.
2022-04-21 15:58:02+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-04-21 15:58:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.37-1debian10 started.
2022-04-21 15:58:02+00:00 [ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
    Remove MYSQL_USER="root" and use one of the following to control the root user password:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD

yamlのMYSQL_USERをコメントアウトし、apply再実施。

controlplane $ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
drupal-mysql-7448b8fb7c-26flf   1/1     Running   0          17s

drupal-mysqlは緑になったが、secretとのつながりが赤になってしまった。
MYSQL_USERを指定しろと書かれていた。secretはMYSQL_USERをrootで指定しろと記載されている。
つんだ?

kenryokenryo

https://community.kodekloud.com/t/hi-all-ive-got-an-mysql-error-with-the-bravos-deployment-in-the-game-of-pods-l/21509/5

上を参考に以下を追加したところ解決した。
以下がどのような役割を持っているかは後で調べる。

        envFrom:
        - secretRef:
            name: drupal-mysql-secret
kenryokenryo

yaml修正版を使用して最初からやったところ、同じエラーとなった。

kenryokenryo

原因が調べてもわからない。
一度、Secretを削除し、MYSQL_USERがないもので再生成。Service, Deploymentを再作成。
その後、再度Secretを削除し、MYSQL_USERありのものを再生成。

これで切り抜けることができた。
他の方の記事見た感じではこんな手順しなくても良さそうだが。。。

kenryokenryo

Podの一覧取得

kubectl get pods

controlplane $ kubectl get pods
NAME                           READY   STATUS   RESTARTS   AGE
drupal-mysql-885f5dd7d-db8f2   0/1     Error    2          45s

Podの詳細取得

kubectl describe pod {Pod名}

controlplane $ kubectl describe pod drupal-mysql-885f5dd7d-db8f2
Name:         drupal-mysql-885f5dd7d-db8f2
Namespace:    default
Priority:     0
Node:         node01/10.0.0.22
Start Time:   Mon, 02 May 2022 14:23:40 +0000
Labels:       app=drupal-mysql
              pod-template-hash=885f5dd7d
Annotations:  <none>
Status:       Running
IP:           10.244.1.3
IPs:
  IP:           10.244.1.3
Controlled By:  ReplicaSet/drupal-mysql-885f5dd7d
Containers:
  mysql:
    Container ID:   docker://4e38e2b79cd6184a1563f20dbfb0c0e72cb9ea8cd0c9150388fcfb03d4103c1e
    Image:          mysql:5.7
    Image ID:       docker-pullable://mysql@sha256:e767595ba3408fbb2dda493be3594b9a148178df58325fafe8b0363662935624
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Mon, 02 May 2022 14:26:57 +0000
      Finished:     Mon, 02 May 2022 14:26:57 +0000
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Mon, 02 May 2022 14:25:33 +0000
      Finished:     Mon, 02 May 2022 14:25:34 +0000
    Ready:          False
    Restart Count:  5
    Environment Variables from:
      drupal-mysql-secret  Secret  Optional: false
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'MYSQL_ROOT_PASSWORD' in secret 'drupal-mysql-secret'>  Optional: false
      MYSQL_DATABASE:       <set to the key 'MYSQL_DATABASE' in secret 'drupal-mysql-secret'>       Optional: false
      MYSQL_USER:           <set to the key 'MYSQL_USER' in secret 'drupal-mysql-secret'>           Optional: false
    Mounts:
      /var/lib/mysql from drupal-mysql-vol (rw,path="dbdata")
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-kl265 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  drupal-mysql-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-mysql-pvc
    ReadOnly:   false
  default-token-kl265:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-kl265
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  3m23s                default-scheduler  Successfully assigned default/drupal-mysql-885f5dd7d-db8f2 to node01
  Normal   Pulling    3m22s                kubelet, node01    Pulling image "mysql:5.7"
  Normal   Pulled     3m                   kubelet, node01    Successfully pulled image "mysql:5.7"
  Normal   Created    90s (x5 over 2m59s)  kubelet, node01    Created container mysql
  Normal   Started    90s (x5 over 2m58s)  kubelet, node01    Started container mysql
  Normal   Pulled     90s (x4 over 2m57s)  kubelet, node01    Container image "mysql:5.7" already present on machine
  Warning  BackOff    89s (x9 over 2m56s)  kubelet, node01    Back-off restarting failed container
controlplane $ 

Podが出力しているログを表示

kubectl logs pods/{Pod名}

controlplane $ kubectl logs pods/drupal-mysql-885f5dd7d-db8f2
2022-05-02 14:26:57+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.38-1debian10 started.
2022-05-02 14:26:57+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-05-02 14:26:57+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.38-1debian10 started.
2022-05-02 14:26:57+00:00 [ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
    Remove MYSQL_USER="root" and use one of the following to control the root user password:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD
controlplane $ 
kenryokenryo

残っているのはdrupal-serviceとdrupal。以下のような感じ?
明日試す。

apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: NodePort
  ports:
    - name: node-port
      protocol: TCP
      port: 8080
      targetPort: 80
      nodePort: 30095
  selector:
    app: drupal
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      initContainers:
      - name: init-sites-volume
        image: drupal:8.6
        args:
        - /bin/bash
        - -c
        - cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R
        volumeMounts:
        - name: drupal-vol
          mountPath: /data
      volumes:
        - name: drupal-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
      containers:
      - image: drupal:8.6
        name: drupal
        ports:
        - containerPort: 30095
          name: drupal
        volumeMounts:
        - name: drupal-profiles-vol
          mountPath: /var/www/html/profiles
          subPath: profiles
        - name: drupal-sites-vol
          mountPath: /var/www/html/sites
          subPath: sites
        - name: drupal-themes-vol
          mountPath: /var/www/html/themes
          subPath: themes
      volumes:
        - name: drupal-pvc
          persistentVolumeClaim:
            claimName: drupal-pvc
kenryokenryo

volumesの指定が誤っていたので修正。

apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: NodePort
  ports:
    - name: node-port
      protocol: TCP
      port: 8080
      targetPort: 80
      nodePort: 30095
  selector:
    app: drupal
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      initContainers:
      - name: init-sites-volume
        image: drupal:8.6
        args:
        - /bin/bash
        - -c
        - cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R
        volumeMounts:
        - name: drupal-vol
          mountPath: /data
      containers:
      - image: drupal:8.6
        name: drupal
        ports:
        - containerPort: 30095
          name: drupal
        volumeMounts:
        - name: drupal-profiles-vol
          mountPath: /var/www/html/profiles
          subPath: profiles
        - name: drupal-sites-vol
          mountPath: /var/www/html/sites
          subPath: sites
        - name: drupal-themes-vol
          mountPath: /var/www/html/themes
          subPath: themes
      volumes:
        - name: drupal-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-profiles-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-sites-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-themes-vol
          persistentVolumeClaim:
            claimName: drupal-pvc

後少し

kenryokenryo

drupal-modules-volが足りなかったので追加
Deploymentにlabel追加
initContainersのcommand, args修正

apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: NodePort
  ports:
    - name: node-port
      protocol: TCP
      port: 8080
      targetPort: 80
      nodePort: 30095
  selector:
    app: drupal
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      initContainers:
      - name: init-sites-volume
        image: drupal:8.6
        command: ["/bin/bash", "-c"]
        args: ["cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R"]
        volumeMounts:
        - name: drupal-vol
          mountPath: /data
      containers:
      - image: drupal:8.6
        name: drupal
        ports:
        - containerPort: 30095
          name: drupal
        volumeMounts:
        - name: drupal-modules-vol
          mountPath: /var/www/html/modules
          subPath: modules
        - name: drupal-profiles-vol
          mountPath: /var/www/html/profiles
          subPath: profiles
        - name: drupal-sites-vol
          mountPath: /var/www/html/sites
          subPath: sites
        - name: drupal-themes-vol
          mountPath: /var/www/html/themes
          subPath: themes
      volumes:
        - name: drupal-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-modules-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-profiles-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-sites-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
        - name: drupal-themes-vol
          persistentVolumeClaim:
            claimName: drupal-pvc

結果、Deploymentが動いていない様子。

kenryokenryo

get pods

controlplane $ kubectl get pods
NAME                           READY   STATUS     RESTARTS   AGE
drupal-7dc7f4b488-hr2b6        0/1     Init:0/1   0          4m34s
drupal-7fdd5f8c7f-znk9j        0/1     Init:0/1   0          6m28s
drupal-mysql-885f5dd7d-wc7ss   1/1     Running    0          11m

describe

controlplane $ kubectl describe pod drupal-7dc7f4b488-hr2b6
Name:           drupal-7dc7f4b488-hr2b6
Namespace:      default
Priority:       0
Node:           node01/10.0.0.22
Start Time:     Wed, 04 May 2022 09:07:05 +0000
Labels:         app=drupal
                pod-template-hash=7dc7f4b488
Annotations:    <none>
Status:         Pending
IP:             
IPs:            <none>
Controlled By:  ReplicaSet/drupal-7dc7f4b488
Init Containers:
  init-sites-volume:
    Container ID:  
    Image:         drupal:8.6
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/bash
      -c
    Args:
      cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /data from drupal-vol (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-v666d (ro)
Containers:
  drupal:
    Container ID:   
    Image:          drupal:8.6
    Image ID:       
    Port:           30095/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-v666d (ro)
      /var/www/html/modules from drupal-modules-vol (rw,path="modules")
      /var/www/html/profiles from drupal-profiles-vol (rw,path="profiles")
      /var/www/html/sites from drupal-sites-vol (rw,path="sites")
      /var/www/html/themes from drupal-themes-vol (rw,path="themes")
Conditions:
  Type              Status
  Initialized       False 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  drupal-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-pvc
    ReadOnly:   false
  drupal-modules-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-pvc
    ReadOnly:   false
  drupal-profiles-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-pvc
    ReadOnly:   false
  drupal-sites-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-pvc
    ReadOnly:   false
  drupal-themes-vol:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  drupal-pvc
    ReadOnly:   false
  default-token-v666d:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-v666d
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason       Age   From               Message
  ----     ------       ----  ----               -------
  Normal   Scheduled    5m6s  default-scheduler  Successfully assigned default/drupal-7dc7f4b488-hr2b6 to node01
  Warning  FailedMount  3m2s  kubelet, node01    Unable to attach or mount volumes: unmounted volumes=[drupal-modules-vol drupal-profiles-vol drupal-themes-vol drupal-vol], unattached volumes=[drupal-modules-vol drupal-profiles-vol drupal-sites-vol drupal-themes-vol drupal-vol default-token-v666d]: timed out waiting for the condition
  Warning  FailedMount  46s   kubelet, node01    Unable to attach or mount volumes: unmounted volumes=[drupal-profiles-vol drupal-themes-vol drupal-vol drupal-modules-vol], unattached volumes=[drupal-profiles-vol drupal-sites-vol drupal-themes-vol drupal-vol default-token-v666d drupal-modules-vol]: timed out waiting for the condition

Unable to attach or mount volumesとあるのでマウントに失敗している模様。

kenryokenryo

https://stackoverflow.com/questions/69544012/unable-to-attach-or-mount-volumes-timed-out-waiting-for-the-condition

volumesからdrupal-vol以外を削除
volumeMountsのnameを全てdrupal-volに変更

apiVersion: v1
kind: Service
metadata:
  name: drupal-service
spec:
  type: NodePort
  ports:
    - name: node-port
      protocol: TCP
      port: 8080
      targetPort: 80
      nodePort: 30095
  selector:
    app: drupal
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      initContainers:
      - name: init-sites-volume
        image: drupal:8.6
        command: ["/bin/bash", "-c"]
        args: ["cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R"]
        volumeMounts:
        - name: drupal-vol
          mountPath: /data
      containers:
      - image: drupal:8.6
        name: drupal
        ports:
        - containerPort: 30095
          name: drupal
        volumeMounts:
        - name: drupal-vol
          mountPath: /var/www/html/modules
          subPath: modules
        - name: drupal-vol
          mountPath: /var/www/html/profiles
          subPath: profiles
        - name: drupal-vol
          mountPath: /var/www/html/sites
          subPath: sites
        - name: drupal-vol
          mountPath: /var/www/html/themes
          subPath: themes
      volumes:
        - name: drupal-vol
          persistentVolumeClaim:
            claimName: drupal-pvc
このスクラップは2022/05/07にクローズされました