1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Game of Pods攻略(Bravo編)

Last updated at Posted at 2022-01-02

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用

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用

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の作成

要件に合わせて作成

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の作成

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でエンコード

$ echo -n 'pass' | base64
cGFzcw0K
$ echo -n 'database'| base 64
ZGF0YWJhc2UNCg==

キーとエンコードした値をdataに書く

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

MySQLの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で監視する際に間違えないように

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の作成

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の指定できるんですね...

参考

github

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

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?