Game of Pods攻略(Bravo編)

# Game of Pods攻略~Bravo~
Drupalの構築です。


Game of Pods関連のソースコードを挙げてます
https://github.com/hayama17/Game-of-Pods

## 作りたい環境
* drupal
    * nodeport
    * PVC
        * PV

* mysql
    * clusterIP
    * PVC
        * PV
    * secret

## PVからPVCまで
Podやdeploymentを作る過程でPVCにマウントするので、先に作成しておく


PODに対してマウントする際にディレクトリが無い場合は作成される為、マウントするディレクトリをあらかじめワーカー側で作成する必要は無い

PV、PVCを作成する段階では出来ないので要注意

### drupal用
```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /drupal-data
    type: DirectoryOrCreate

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
```

### mysql用
```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: drupal-mysql-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /drupal-mysql-data
    type: DirectoryOrCreate
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
```

## Mysqlの作成

要件に合わせて作成

```yaml
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: drupal-mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root_password
            - name: MYSQL_DATABASE
              value: drupal-database
          ports:
            - containerPort: 3306
              name: drupal-mysql
          volumeMounts:
            - name: pvc
              mountPath: /var/lib/mysql
              subPath: dbdata
      volumes:
        - name: pvc
          persistentVolumeClaim: 
            claimName: drupal-mysql-pvc
```
### POD間通信を行うためのClusterIPの作成
```yaml
apiVersion: v1
kind: Service
metadata:
  name: drupal-mysql-service
spec:
  type: ClusterIP
  ports:
    - name: drupal-mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app: drupal-mysql
```

### Secretの作成
mysqlのrootパスワードとデータベースのbase64でエンコード

```bash
$ echo -n 'pass' | base64
cGFzcw0K
$ echo -n 'database'| base 64
ZGF0YWJhc2UNCg==
```
キーとエンコードした値をdataに書く

```yaml
apiVersion: v1
kind: Secret
metadata:
  name: drupal-mysql-secret
type: Opaque
data: 
  MYSQL_ROOT_PASSWORD: cGFzcw0K
  MYSQL_DATABASE: ZGF0YWJhc2UNCg==
```

MySQLのyamlを書き換え

```yaml
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
```
## drupalの作成
initConteinerの記載があるのでkubectlで監視する際に間違えないように

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drupal
spec:
  replicas: 1
  selector:
    matchLabels:
      app: drupal
  template:
    metadata:
      labels:
        app: drupal
    spec:
      initContainers:
        - image: drupal:8.6
          name: init-sites-volume
          command: ["/bin/bash", "-c" ]
          Args: ['cp -r /var/www/html/sites/ /data/; chown www-data:www-data /data/ -R']
          volumeMounts:
            - name: init-pvc
              mountPath: /data
      containers:
        - image: drupal:8.6
          name: drupal
          ports:
            - containerPort: 80
          volumeMounts:
            - name: regular-pvc
              mountPath: /var/www/html/modules
              subPath: modules
            - name: regular-pvc
              mountPath: /var/www/html/profiles
              subPath: profiles
            - name: regular-pvc
              mountPath: /var/www/html/sites
              subPath: sites
            - name: regular-pvc
              mountPath: /var/www/html/themes
              subPath: themes

      volumes:
        - name: regular-pvc
          persistentVolumeClaim:
            claimName: drupal-pvc

```
## 外部へサービスを公開するためのNodeIPの作成

```yaml
apiVersion: v1
kind: Service
metadata: 
  name: drupal-service
spec:
  type: NodePort
  selector:
    app: drupal
  ports:
    - name: drupal-ports
      protocol: TCP
      nodePort: 30095
      targetPort: 80
      port: 30095
```


nodeIPの指定できるんですね...

# 参考
https://medium.com/containerum/how-to-easily-deploy-a-drupal-8-instance-on-kubernetes-b90acc7786b7

# github
今回使ったyamlファイル上げておきます
https://github.com/hayama17/Game-of-Pods/tree/main