NFSとredisセットアップ-DigitalOceanのkubernetes環境にwordpressを構築する01

2024年1月14日

OpenEBS Dynamic NFS Provisioner のインストールと設定

install helm

k8sのパッケージ管理ツールです。

kubernetesに接続した作業端末で行います。

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 > get_helm.sh
chmod 700 get_helm.sh
./get_helm.sh

container-blueprints repositoryのダウンロード

OpenEBS Helm Chartを使ってKubernetesクラスタにOpenEBS Dynamic NFS Provisionerをインストールします。Wordpressは動的NFSプロビジョナを必要とするので、動的NFSプロビジョナのみをインストールし設定します。

AWSでいうEFSみたいなストレージをマウントさせる場合に利用できるk8sのNFSサービスだと思われます。

mkdir work/doks
cd work/doks
git clone https://github.com/digitalocean/container-blueprints.git
cd container-blueprints/DOKS-Wordpress/

Helm repository の登録

helm repo add openebs-nfs https://openebs.github.io/dynamic-nfs-provisioner

helm repo update

nfsStorageClassを確認する

cat assets/manifests/openEBS-nfs-provisioner-values.yaml
nfsStorageClass:
  backendStorageClass: "do-block-storage"

install the chart using Helm

NFSモジュールのインストール

helm install openebs-nfs openebs-nfs/nfs-provisioner --version 0.9.0 \
  --namespace openebs \
  --create-namespace \
  -f "assets/manifests/openEBS-nfs-provisioner-values.yaml"
NAME: openebs-nfs
LAST DEPLOYED: Sun Dec 31 13:28:15 2023
NAMESPACE: openebs
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Thank you for installing nfs-provisioner 😀

Your release is named openebs-nfs and it's installed to namespace: openebs.

The OpenEBS NFSPV Provisioner has been installed check its status by running:
$ kubectl get pods -n openebs

For more information, visit our Slack at https://openebs.io/community or view
the documentation online at https://github.com/openebs/dynamic-nfs-provisioner/.
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ helm list -A
NAME        NAMESPACE   REVISION    UPDATED                                 STATUS      CHART                   APP VERSION
openebs-nfs openebs     1           2023-12-31 13:28:15.802054728 +0900 JST deployed    nfs-provisioner-0.9.0   0.9.0     
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ kubectl get sc
NAME                          PROVISIONER                 RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
do-block-storage (default)    dobs.csi.digitalocean.com   Delete          Immediate           true                   16m
do-block-storage-retain       dobs.csi.digitalocean.com   Retain          Immediate           true                   16m
do-block-storage-xfs          dobs.csi.digitalocean.com   Delete          Immediate           true                   16m
do-block-storage-xfs-retain   dobs.csi.digitalocean.com   Retain          Immediate           true                   16m
openebs-kernel-nfs            openebs.io/nfsrwx           Delete          Immediate           false                  60s
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ 

openEBS をapply

redisとwordpressの永続ストレージに利用するPVのストレージクラスです

cat assets/manifests/sc-rwx-values.yaml 
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: rwx-storage
  annotations: 
    openebs.io/cas-type: nsfrwx
    cas.openebs.io/config: |
      - name: NSFServerType
        value: "kernel"
      - name: BackendStorageClass
        value: "do-block-storage"
provisioner: openebs.io/nfsrwx
reclaimPolicy: Delete

ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ kubectl apply -f assets/manifests/sc-rwx-values.yaml
storageclass.storage.k8s.io/rwx-storage created
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ kubectl get sc
NAME                          PROVISIONER                 RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
do-block-storage (default)    dobs.csi.digitalocean.com   Delete          Immediate           true                   16m
do-block-storage-retain       dobs.csi.digitalocean.com   Retain          Immediate           true                   16m
do-block-storage-xfs          dobs.csi.digitalocean.com   Delete          Immediate           true                   16m
do-block-storage-xfs-retain   dobs.csi.digitalocean.com   Retain          Immediate           true                   16m
openebs-kernel-nfs            openebs.io/nfsrwx           Delete          Immediate           false                  101s
rwx-storage                   openebs.io/nfsrwx           Delete          Immediate           false                  9s
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ 

これで、DigitalOceanブロックストレージ上に共有ボリュームを動的にプロビジョニングするrwx-storageという新しいStorageClassができました。

Configuring the WordPress

DBの選定

  • kubernetesのpodでのDBは今回は稼働させたくないので却下
  • digitaloceanのマネージド型DBは最低価格で$15かかる。
    • 同じくdigitaloceanのDroplets(インスタンス)だと$4から構築可能
  • GCEのalways freeの未使用のインスタンス所持
    ということでGCEとなりました。

DB通信でWAN経由、しかもシンガポールからアメリカはネットワーク周りで遅延ありそうなので将来的にdigitalocean内で完結するようにするかもしれません。

  • GCPのネットワークのfirewallで、DOKSのIPアドレスを許可しておきます。
  • GCEでmariadb-serverをインストール
  • GCEでmariadb SSL/TLSモードでの起動
  • 空のdatabase作成、DBユーザーの作成

Configuring the Redis Database

Redisのキャッシュメカニズムは、MySQLやMariaDBと組み合わせることで、WordPressのデータベースクエリを高速化します。Redisを使用すると、データをキャッシュしてメモリに保存し、高性能なデータ検索と保存を行うことができます。

digitaloceanのマネージド型redisもありますが、同じく$15かかりますので、
kubernetesのpodで稼働させます。

devとprod環境で同じredisサーバーを利用したらwordpressがちょいちょいバグるのでredisはdev,prodそれぞれで作ります

add bitnami repo

helm repo add bitnami https://charts.bitnami.com/bitnami

helm repo update bitnami

configure redis password

cp -ip assets/manifests/redis-values.yaml{,.org}
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ sed -e "s/<YOUR_REDIS_PASSWORD_HERE>/$(cat /dev/urandom | tr -dc '[a-zA-Z0-9!-/:-@\\[-{-~]$'| fold -w 16 | head -n 1)/" assets/manifests/redis-values.yaml.org > assets/manifests/redis-values.yaml

ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ cat assets/manifests/redis-values.yaml
master:
  persistence:
    enabled: true
    storageClass: rwx-storage
    accessModes: ["ReadWriteMany"]
    size: 5Gi

volumePermissions:
  enabled: true

auth:
  enabled: true
  password: #Z.OFjbMA!7r?a]_

architecture: standalone

同様にdevの分を作ります。(sizeのみ1Giへ変更)

install redis

helm upgrade redis bitnami/redis \
    --atomic \
    --create-namespace \
    --install \
    --namespace redis \
    --version 17.0.5 \
    --values assets/manifests/redis-values.yaml

devは--namespace redis-devとして同様にインストールします。--valuesでdev用のマニフェストを指定します。

Release "redis" does not exist. Installing it now.
NAME: redis
LAST DEPLOYED: Sun Dec 31 13:31:51 2023
NAMESPACE: redis
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 17.0.5
APP VERSION: 7.0.4

** Please be patient while the chart is being deployed **

Redis® can be accessed via port 6379 on the following DNS name from within your cluster:

    redis-master.redis.svc.cluster.local

To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d)

To connect to your Redis® server:

1. Run a Redis® pod that you can use as a client:

   kubectl run --namespace redis redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:7.0.4-debian-11-r4 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace redis -- bash

2. Connect using the Redis® CLI:
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-master

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace redis svc/redis-master 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379
ocarina@ab350-pro4:~/work/doks-wp/dev/k8s/helm$ kubectl get svc -n redis-dev
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
redis-headless   ClusterIP   None             <none>        6379/TCP   3m25s
redis-master     ClusterIP   10.245.174.246   <none>        6379/TCP   3m25s
ocarina@ab350-pro4:~/work/doks-wp/dev/k8s/helm$ kubectl get svc -n redis
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
redis-headless   ClusterIP   None            <none>        6379/TCP   8d
redis-master     ClusterIP   10.245.96.144   <none>        6379/TCP   8d
ocarina@ab350-pro4:~/work/doks-wp/dev/k8s/helm$ 

redisのpodの起動確認

ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ kubectl get all -n redis
NAME                 READY   STATUS    RESTARTS       AGE
pod/redis-master-0   1/1     Running   1 (109s ago)   3m19s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/redis-headless   ClusterIP   None            <none>        6379/TCP   3m19s
service/redis-master     ClusterIP   10.245.96.144   <none>        6379/TCP   3m19s

NAME                            READY   AGE
statefulset.apps/redis-master   1/1     3m19s
ocarina@ab350-pro4:~/work/doks/container-blueprints/DOKS-Wordpress$ 

パスワード

kubectl get secret --namespace redis redis -o jsonpath="{.data.redis-password}" | base64 -d
kubectl get secret --namespace redis-dev redis -o jsonpath="{.data.redis-password}" | base64 -d

Posted by ocarina