KubernetesでWordPress用のMySQLのパスワード設定をする方法


# 環境設定
・Kubernetes v1.21.5+rke2r2
・Ubuntu 22.04


#シークレットの作り方
```powershell
kubectl create secret generic ＜secretKeyRefのnameの値＞ --from-literal=＜keyの値ひとつ目（password）＞=＜パスワード＞ --from-literal=＜keyの値ふたつ目（user）＞=＜ユーザー名＞
```
↑のコードからpasswordには自分が決めたパスワードをuserには自分が決めたユーザーの名前をそれぞれ設定する


例

```powershell
kubectl create secret generic mysql-pass-test --from-literal=password=wordpress123@@@ --from-literal=user=username
```
#yamlファイル

```yaml:test.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc-test
  labels:
    app: wordpress
    tier: mysql-test
spec:
  storageClassName: sc-db-test
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-test
  labels:
    type: local
spec:
  storageClassName: sc-db-test
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: /tmp/data/mysql-test
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-test
  labels:
    app: mysql
spec:
  type: ClusterIP
  ports:
    - port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-test
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql-test
        env:
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-pass-test
                key: password
          - name: MYSQL_DATABASE
            value: wordpress_db
          - name: MYSQL_USER
            value: user
          - name: MYSQL_PASSWORD
            value: pass
        ports:
          - containerPort: 3306
            name: mysql-test
        volumeMounts:
          - name: mysql-local-storage-test
            mountPath: /var/lib/mysql
      volumes:
        - name: mysql-local-storage-test
          persistentVolumeClaim:
            claimName: mysql-pvc-test
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress-test
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-test
  labels:
    app: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
        - image: wordpress
          name: wordpress-test
          env:
            - name: WORDPRESS_DB_HOST
              value: mysql-test
            - name: WORDPRESS_DB_NAME
              value: wordpress_db
            - name: WORDPRESS_DB_PASSWORD
              value: pass
            - name: WORDPRESS_DB_USER
              value: user
          ports:
            - containerPort: 80
              name: wordpress-test
          volumeMounts:
            - name: wordpress-local-storage-test
              mountPath: /var/www/html
      volumes:
        - name: wordpress-local-storage-test
          persistentVolumeClaim:
            claimName: wordpress-pvc-test
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc-test
  labels:
    app: wordpress
    tier: wordpress-test
spec:
  storageClassName: sc-test
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wordpress-pv-test
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  storageClassName: sc-test
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  hostPath:
    path: /tmp/data/wordpress-test
---

```
#起動 
```powershell
kubectl apply -f test.yaml
```
#WordPressのアクセス方法
##MasterIPを確認

```powershell
kubectl cluster-info
```
```powershell
Kubernetes master is running at https://192.168.100.136:6443
CoreDNS is running at https://192.168.100.136:6443/api/v1/namespaces/kube-system/services/rke2-coredns-rke2-coredns:udp-53/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
```
##port番号の確認
```powershell
kubectl get service
```

```powershell
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
mysql-test       ClusterIP   10.43.171.123   <none>        3306/TCP       43h
wordpress-test   NodePort    10.43.202.67    <none>        80:32393/TCP   43h
```
## WordPressにアクセス
ブラウザにURLを打ち込む

```
http://＜MasterIP＞:＜Port番号＞
```

例

```
http://192.168.100.136:32393
```
#参考文献
[WordPress環境を作る Kubernetes編](https://qiita.com/cyberblack28/items/0d7abff8efb33ecc1ca2)