SUPER-G.EEK.JP

twitter: @hum_op_dev
github: ophum

Redis TLSを試す

目次

Redis 6 で対応した TLS を試してみます。 Redis は Kubernetes で動かします。

証明書

前回のcert-manager でプライベート認証局を作ってみるで作成した証明書を利用します。

$ kubectl -n sandbox get secret example-com-tls
NAME              TYPE                DATA   AGE
example-com-tls   kubernetes.io/tls   3      17h

redis の deployment を作成

以下のような manifest を作成します。

 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: redis
 5  namespace: sandbox
 6spec:
 7  replicas: 1
 8  selector:
 9    matchLabels:
10      app: redis
11  template:
12    metadata:
13      labels:
14        app: redis
15    spec:
16      containers:
17        - name: redis
18          image: redis:7-alpine
19          command:
20            - redis-server
21          args:
22            - --tls-port 6379
23            - --port 0
24            - --tls-cert-file /etc/redis/tls/tls.crt
25            - --tls-key-file /etc/redis/tls/tls.key
26            - --tls-ca-cert-file /etc/redis/tls/ca.crt
27            - --auth-clients no
28
29          ports:
30            - containerPort: 6379
31          volumeMounts:
32            - name: example-com-tls
33              mountPath: /etc/redis/tls
34              readOnly: true
35      volumes:
36        - name: example-com-tls
37          secret:
38            secretName: example-com-tls

テスト

$ kubectl get pods -n sandbox
NAME                     READY   STATUS    RESTARTS   AGE
redis-5fc9c6bfc7-tlhrk   1/1     Running   0          7m1s

shell に入ります。

$ kubectl -n sandbox exec -it redis-5fc9c6bfc7-tlhrk -- ash
/data #

redis-cli を起動します。起動できましたが、keys *などのコマンドを実行すると Connection reset by peerというエラーになります。

/data # redis-cli
127.0.0.1:6379> keys *
Error: Connection reset by peer

redis-cli に--tls オプションを付けて起動してみます。 今度は、SSL_connect failed: certificate verify failedとなります。証明書の検証ができなかったというエラーです。

/data # redis-cli --tls
Could not connect to Redis at 127.0.0.1:6379: SSL_connect failed: certificate verify failed

--cacert オプションで/etc/redis/tls/ca.crt を指定して起動してみます。 うまく接続できました。

/data # redis-cli --tls --cacert /etc/redis/tls/ca.crt
127.0.0.1:6379> keys *
(empty array)

--auth-clients no--auth-clients yesとしてクライアント証明書を使ったクライアント認証を有効にしてみます。

 1$ diff -u deploy.yaml.old deploy.yaml
 2--- deploy.yaml.old     2025-04-15 19:45:34.981957295 +0900
 3+++ deploy.yaml 2025-04-15 19:45:41.187313887 +0900
 4@@ -24,7 +24,7 @@
 5           - --tls-cert-file /etc/redis/tls/tls.crt
 6           - --tls-key-file /etc/redis/tls/tls.key
 7           - --tls-ca-cert-file /etc/redis/tls/ca.crt
 8-          - --tls-auth-clients no
 9+          - --tls-auth-clients yes
10
11         ports:
12         - containerPort: 6379

再度 shell に入り先ほどのオプションで redis-cli を起動します。 keys *を実行するとエラーになります。

/data # redis-cli --tls --cacert /etc/redis/tls/ca.crt
127.0.0.1:6379> keys *
Error: Server closed the connection

--cert --keyでクライアント証明書と秘密鍵を指定してみます。 エラーにならず実行できました。

/data # redis-cli --tls --cacert /etc/redis/tls/ca.crt --cert /etc/redis/tls/tls.crt --key /etc/redis/tls/tls.key
127.0.0.1:6379> keys *
(empty array)