kubernetesにnfsでpvを作れるようにする

# 目的
自前で立てたkubernetesにnfsでpvを作れるようにする。masterをnfs serverにした。

# Ref
わかりやすくまとめてくださり、ありがとうございます。
https://sminamot-dev.hatenablog.com/entry/2020/03/17/223253

# 手順

# node側での作業

```bash:bash
# worker nodeにnfs clientをinstall
$ sudo apt install nfs-common
```

# master側での作業
Refをマネして、masterをnfs serverにすることにした。
自分のk8s clusterがいるsubnet環境は172.31.0.0/16なので、それで設定

```bash:bash
# NFSサーバのインストール
$ sudo apt install nfs-kernel-server

# NFSで利用するディレクトリの作成
$ sudo mkdir -p /mnt/share/nfs

# NFSサーバ用の設定
$ echo "/mnt/share/nfs  172.31.0.0/16(rw,async,crossmnt,no_root_squash,no_subtree_check)" | sudo tee -a /etc/exports

# 設定が完了したら以下を実行
sudo exportfs -ra

# NFSサーバの起動
$ sudo systemctl start nfs-server.service
$ sudo systemctl enable nfs-server.service
```

# 動作確認
試しに、pvとpvcを作ってうまく動作するか確認した。動作確認には以下Refのwordpressを用いた。

https://kubernetes.io/ja/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/

## 手順

```bash:bash
# PV用のdirectoryを作成
$ sudo mkdir /mnt/share/nfs/pv0001
$ sudo mkdir /mnt/share/nfs/pv0002

# pvのmanifestを2つ作成
$ cat pv0001.yaml 
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0001
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  nfs:
    server: 172.31.xx.xx
    path: /mnt/share/nfs/pv0001

ubuntu@ip-172-31-43-82:~/wordpress_mysql$ cat pv0002.yaml 
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0002
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  nfs:
    server: 172.31.xx.xx
    path: /mnt/share/nfs/pv0002

# mysqlのmanifestを作成 (refのものからpvcの部分を修正)
ubuntu@ip-172-31-43-82:~/wordpress_mysql$ cat mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: slow   # storageClassNameをpvと合わせないとpvを掴んでくれない
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

# wordpressのdeploymentの作成
ubuntu@ip-172-31-43-82:~/wordpress_mysql$ cat wordpress-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: slow  # ここも同じ
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

# kustomiationを作成
ubuntu@ip-172-31-43-82:~/wordpress_mysql$ cat kustomization.yaml
secretGenerator:
- name: mysql-pass
  literals:
  - password=xxxxxxxx    # passwordを設定
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
ubuntu@ip-172-31-43-82:~/wordpress_mysql$
```

## deplyしてみる
```bash:bash
$ kubectl apply -f pv0001.yaml
$ kubectl apply -f pv0002.yaml
$ kubectl apply -k .

# 状態確認
$ kubectl get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                    STORAGECLASS   REASON   AGE
pv0001   20Gi       RWO            Retain           Bound    default/mysql-pv-claim   slow                    12m
pv0002   20Gi       RWO            Retain           Bound    default/wp-pv-claim      slow                    12m

$ kubectl get pvc
NAME             STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pv-claim   Bound    pv0001   20Gi       RWO            slow           11m
wp-pv-claim      Bound    pv0002   20Gi       RWO            slow           11m

$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-5cd87d7fbb-v6m8t        1/1     Running   0          12m
wordpress-mysql-84c759b56-qb72r   1/1     Running   0          12m
$
```