Kubernetes安装Redis集群

半兽人 发表于: 2023-03-29   最后更新时间: 2023-09-19 18:12:06  
{{totalSubscript}} 订阅, 877 游览

安装

创建命名空间:

kubectl create ns my-space

创建:

kubectl create -f https://www.kubebiz.com/raw/KubeBiz/redis/Cluster/all

如果你需要修改namespace,可以到 https://www.kubebiz.com/KubeBiz/redis 直接修改,下载新的yaml安装。

查看pod状态:

$ kubectl get pods -n my-space
...
redis-cluster-0                       1/1     Running     0             69m
redis-cluster-1                       1/1     Running     0             69m
redis-cluster-2                       1/1     Running     0             69m
redis-cluster-3                       1/1     Running     0             69m
redis-cluster-4                       1/1     Running     0             69m
redis-cluster-5                       1/1     Running     0             69m

等待所有pod的状态为Running

创建集群

现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群。

kubectl exec -it redis-cluster-0 -n my-space redis-cli --cluster create --cluster-replicas 1 \
$(kubectl get pods -n my-space -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ' | sed "s/ :6379//g")

注意:现在的命名空间是my-space,如果不同,请自行修改(有2个地方需要修改)。

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.23.62.194:6379 to 172.23.115.119:6379
Adding replica 172.23.151.243:6379 to 172.23.209.33:6379
Adding replica 172.23.14.224:6379 to 172.23.64.167:6379
M: ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0 172.23.115.119:6379
   slots:[0-5460] (5461 slots) master
M: e5d7826180374fb6ea77e0c73e782584203a66b8 172.23.209.33:6379
   slots:[5461-10922] (5462 slots) master
M: e9bef3975724463b8b9f1bdbfa10f2dca579eab8 172.23.64.167:6379
   slots:[10923-16383] (5461 slots) master
S: f9e4bbd6e637ff6e6231a66c2800273224d6ff17 172.23.62.194:6379
   replicates ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0
S: 5281452241fff0f9289d59a7a875d87290950367 172.23.151.243:6379
   replicates e5d7826180374fb6ea77e0c73e782584203a66b8
S: 717cfbc6f6715ab3e1eaa268821da985604b542e 172.23.14.224:6379
   replicates e9bef3975724463b8b9f1bdbfa10f2dca579eab8
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated

输入:yes

>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.......
>>> Performing Cluster Check (using node 172.23.115.119:6379)
M: ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0 172.23.115.119:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: e5d7826180374fb6ea77e0c73e782584203a66b8 172.23.209.33:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f9e4bbd6e637ff6e6231a66c2800273224d6ff17 172.23.62.194:6379
   slots: (0 slots) slave
   replicates ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0
S: 5281452241fff0f9289d59a7a875d87290950367 172.23.151.243:6379
   slots: (0 slots) slave
   replicates e5d7826180374fb6ea77e0c73e782584203a66b8
M: e9bef3975724463b8b9f1bdbfa10f2dca579eab8 172.23.64.167:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 717cfbc6f6715ab3e1eaa268821da985604b542e 172.23.14.224:6379
   slots: (0 slots) slave
   replicates e9bef3975724463b8b9f1bdbfa10f2dca579eab8
[OK] All nodes agree about slots configuration.

输出以下,就是成功啦:

>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

验证

kubectl exec -it redis-cluster-0 -n my-space -- redis-cli cluster nodes

输出集群信息:

e5d7826180374fb6ea77e0c73e782584203a66b8 172.23.209.33:6379@16379 master - 0 1695117923385 2 connected 5461-10922
f9e4bbd6e637ff6e6231a66c2800273224d6ff17 172.23.62.194:6379@16379 slave ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0 0 1695117924388 4 connected
5281452241fff0f9289d59a7a875d87290950367 172.23.151.243:6379@16379 slave e5d7826180374fb6ea77e0c73e782584203a66b8 0 1695117923000 5 connected
e9bef3975724463b8b9f1bdbfa10f2dca579eab8 172.23.64.167:6379@16379 master - 0 1695117925392 3 connected 10923-16383
717cfbc6f6715ab3e1eaa268821da985604b542e 172.23.14.224:6379@16379 slave e9bef3975724463b8b9f1bdbfa10f2dca579eab8 0 1695117923000 6 connected
ed05d9200f5740bd2c7f27e61d3eae9c40a7c1c0 172.23.115.119:6379@16379 myself,master - 0 1695117922000 1 connected 0-5460

写入,注意集群模式要加-c

kubectl exec -it redis-cluster-0 -n my-space -- redis-cli -c set k v

返回:”OK“

获取刚set的信息:

kubectl exec -it redis-cluster-0 -n my-space -- redis-cli -c get k

返回:"v"
安装完成,可以使用啦

Ref

https://www.kubebiz.com/KubeBiz/redis?version=Cluster
https://github.com/rustudorcalin/deploying-redis-cluster

更新于 2023-09-19
在线,6小时前登录

查看kubernetes更多相关的文章或提一个关于kubernetes的问题,也可以与我们一起分享文章