Kubernetes に Redis をインストールする

Jul 18, 2024, 9:21 AM
今まで、Difyの helm に内用されているRedisを使用していましたが、best to use external redis とのことですので、別途インストールして使っていきます。

参考情報

https://github.com/douban/charts/blob/master/charts/dify/values.yaml に
redis:
# using embedded redis
# connection info would be set automatically
# best to use external redis if you have one
embedded: true
と書かれています。

ネームスペースを作る

# kubectl create namespace redis

helm でさくっとインストール

https://bitnami.com/stack/redis/helm

my-release の部分は、好きな名前に変えてください。
my-release-redis-node-0 のような Pod や Service が起動します。

# helm install my-release oci://registry-1.docker.io/bitnamicharts/redis \
--set replica.replicaCount=2 \
--set master.persistence.enabled=true \
--set master.persistence.size=5Gi \
--set sentinel.enabled=true \
--namespace redis

Pulled: registry-1.docker.io/bitnamicharts/redis:19.6.2
Digest: sha256:1f996c79bf42e3dd69abdac6dccbf6d2a6a96aeef89e0e17ee6b28aa20922b6d
NAME: my-release
NAMESPACE: redis
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 19.6.2
APP VERSION: 7.2.5

** 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:

    my-release-redis.redis.svc.cluster.local for read only operations

For read/write operations, first access the Redis® Sentinel cluster, which is available in port 26379 using the same domain name above.



To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace redis my-release-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.2.5-debian-12-r2 --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 my-release-redis -p 6379 # Read only operations
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h my-release-redis -p 26379 # Sentinel access

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

    kubectl port-forward --namespace redis svc/my-release-redis 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379

WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
  - replica.resources
  - sentinel.resources
+info https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

起動確認

# kubectl get pod,svc -n redis
NAME READY STATUS RESTARTS AGE
pod/my-release-redis-node-0 2/2 Running 0 14m
pod/my-release-redis-node-1 2/2 Running 0 13m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/my-release-redis ClusterIP 10.103.121.24 <none> 6379/TCP,26379/TCP 14m
service/my-release-redis-headless ClusterIP None <none> 6379/TCP,26379/TCP 14m

パスワードの取得

# export REDIS_PASSWORD=$(kubectl get secret --namespace redis my-release-redis -o jsonpath="{.data.redis-password}" | base64 -d)
# echo $REDIS_PASSWORD
6NRNDTAngB

マスターの判別

# kubectl exec -it my-release-redis-node-0 -n redis -- redis-cli -a 6NRNDTAngB -p 26379 sentinel masters

Defaulted container "redis" out of: redis, sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "my-release-redis-node-0.my-release-redis-headless.redis.svc.cluster.local"
    5) "port"
    6) "6379"
    7) "runid"
    8) "0fe46dfa6a07eeba4d2fa571bb3499fabd745129"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "855"
   19) "last-ping-reply"
   20) "855"
   21) "down-after-milliseconds"
   22) "60000"
   23) "info-refresh"
   24) "8519"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "419865"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "1"
   33) "num-other-sentinels"
   34) "1"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "180000"
   39) "parallel-syncs"
   40) "1"


レプリカの判別

# kubectl exec -it my-release-redis-node-0 -n redis -- redis-cli -a 6NRNDTAngB -p 26379 sentinel slaves mymaster

Defaulted container "redis" out of: redis, sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1)  1) "name"
    2) "my-release-redis-node-1.my-release-redis-headless.redis.svc.cluster.local:6379"
    3) "ip"
    4) "my-release-redis-node-1.my-release-redis-headless.redis.svc.cluster.local"
    5) "port"
    6) "6379"
    7) "runid"
    8) "867ee7037736bd499b4549f30b5c8b796b3d9fa1"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "196"
   19) "last-ping-reply"
   20) "196"
   21) "down-after-milliseconds"
   22) "60000"
   23) "info-refresh"
   24) "9384"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "452690"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "my-release-redis-node-0.my-release-redis-headless.redis.svc.cluster.local"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "114908"
   41) "replica-announced"
   42) "1"

テスト

コンテナ起動して、クラスター内から接続テストを行います
# kubectl run temporary-pod --image=ubuntu --rm -it --restart=Never -- /bin/bash
# apt update
# apt install -y python3 python3-pip python3-venv
# python3 -m venv myenv
# source myenv/bin/activate
# pip3 install redis
# echo "
import redis

r = redis.StrictRedis(
host='my-release-redis-headless.redis.svc.cluster.local',
port=6379,
password='6NRNDTAngB',
decode_responses=True
)

r.set('mykey', 'Hello, Redis!')
value = r.get('mykey')
print(f'The value of \'mykey\' is: {value}')
" > redis_test.py

# python3 redis_test.py
The value of 'mykey' is: Hello, Redis!
正しく動いてくれました🍻

これで、Dify が使用する
が出揃いましたので、次は、これら外部のサービスを使用した Dify のインストールにチャレンジしてみたいと思います。
MinIO を Python から操作してみる
ベクトルデータベース Weaviate (GPU使用) を Jetson Orin Nano NX 16G で動かす