简体   繁体   English

Redis Kubernetes 上的前哨 HA

[英]Redis sentinel HA on Kubernetes

I am trying to have 1 redis master with 2 redis replicas tied to a 3 Quorum Sentinel on Kubernetes.我正在尝试将 1 个 redis 主控与 2 个 redis 副本绑定到 Kubernetes 上的 3 个仲裁哨兵。 I am very new to Kubernetes.我对 Kubernetes 很陌生。

My initial plan was to have the master running on a pod tied to 1 Kubernetes SVC and the 2 replicas running on their own pods tied to another Kubernetes SVC.我最初的计划是让主节点在绑定到 1 Kubernetes SVC 的 Pod 上运行,而在自己的 Pod 上运行的 2 个副本绑定到另一个 Kubernetes SVC。 Finally, the 3 Sentinel pods will be tied to their own SVC.最后,3 个 Sentinel pod 将绑定到它们自己的 SVC。 The replicas will be tied to the master SVC (because without svc, ip will change).副本将绑定到主 SVC(因为没有 svc,ip 会发生变化)。 The sentinel will also be configured and tied to master and replica SVCs.哨兵也将被配置并绑定到主 SVC 和副本 SVC。 But I'm not sure if this is feasible because when master pod crashes, how will one of the replica pods move to the master SVC and become the master?但我不确定这是否可行,因为当主 pod 崩溃时,其中一个副本 pod 将如何移动到主 SVC 并成为主? Is that possible?那可能吗?

The second approach I had was to wrap redis pods in a replication controller and the same for sentinel as well.我采用的第二种方法是将 redis pod 包装在复制 controller 中,对于哨兵也是如此。 However, I'm not sure how to make one of the pods master and the others replicas with a replication controller.但是,我不确定如何使用复制 controller 使其中一个 pod 成为 master 和其他副本。

Would any of the two approaches work?这两种方法中的任何一种都行得通吗? If not, is there a better design that I can adopt?如果没有,我可以采用更好的设计吗? Any leads would be appreciated.任何线索将不胜感激。

You can deploy Redis Sentinel using the Helm package manager and the Redis Helm Chart .您可以使用Helm package 管理器和Redis Helm Chart部署 Redis Sentinel。
If you don't have Helm3 installed yet, you can use this documentation to install it.如果你还没有安装Helm3 ,你可以使用这个文档来安装它。

I will provide a few explanations to illustrate how it works.我将提供一些解释来说明它是如何工作的。


First we need to get the values.yaml file from the Redis Helm Chart to customize our installation:首先我们需要从 Redis Helm Chart 中获取values.yaml文件来自定义我们的安装:

$ wget https://raw.githubusercontent.com/bitnami/charts/master/bitnami/redis/values.yaml

We can configure a lot of parameters in the values.yaml file, but for demonstration purposes I only enabled Sentinel and set the redis password:我们可以在values.yaml文件中配置很多参数,但是为了演示,我只启用了 Sentinel 并设置了 redis 密码:
NOTE: For a list of parameters that can be configured during installation, see the Redis Helm Chart Parameters documentation.注意:有关可在安装期间配置的参数列表,请参阅Redis Helm Chart 参数文档。

# values.yaml

global:
  redis:
    password: redispassword
...
replica:
  replicaCount: 3
...
sentinel:
  enabled: true
...

Then we can deploy Redis using the configuration from the values.yaml file:然后我们可以使用values.yaml文件中的配置部署 Redis:
NOTE: It will deploy a three Pod cluster (one master and two slaves) managed by the StatefulSets with a sentinel container running inside each Pod.注意:它将部署一个由StatefulSets管理的三个 Pod 集群(一个主和两个从),每个 Pod 内运行一个sentinel容器。

$ helm install redis-sentinel bitnami/redis --values values.yaml

Be sure to carefully read the NOTES section of the chart installation output.请务必仔细阅读图表安装 output 的NOTES部分。 It contains many useful information (eg how to connect to your database from outside the cluster)它包含许多有用的信息(例如如何从集群外部连接到您的数据库)

After installation, check redis StatefulSet , Pods and Services ( headless service can be used for internal access):安装完成后,检查 redis StatefulSetPodsServices (内部访问可以使用headless 服务):

$ kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP
redis-sentinel-node-0   2/2     Running   0          2m13s   10.4.2.21
redis-sentinel-node-1   2/2     Running   0          86s     10.4.0.10
redis-sentinel-node-2   2/2     Running   0          47s     10.4.1.10


$ kubectl get sts
NAME                  READY   AGE
redis-sentinel-node   3/3     2m41s

$ kubectl get svc
NAME                      TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)              AGE
redis-sentinel            ClusterIP   10.8.15.252   <none>        6379/TCP,26379/TCP   2m
redis-sentinel-headless   ClusterIP   None          <none>        6379/TCP,26379/TCP   2m

As you can see, each redis-sentinel-node Pod contains the redis and sentinel containers:如您所见,每个redis-sentinel-node Pod 都包含redissentinel容器:

$ kubectl get pods redis-sentinel-node-0 -o jsonpath={.spec.containers[*].name}
redis sentinel

We can check the sentinel container logs to find out which redis-sentinel-node is the master:我们可以查看sentinel容器日志,找出哪个redis-sentinel-node是主节点:

$ kubectl logs -f redis-sentinel-node-0 sentinel
...
1:X 09 Jun 2021 09:52:01.017 # Configuration loaded
1:X 09 Jun 2021 09:52:01.019 * monotonic clock: POSIX clock_gettime
1:X 09 Jun 2021 09:52:01.019 * Running mode=sentinel, port=26379.
1:X 09 Jun 2021 09:52:01.026 # Sentinel ID is 1bad9439401e44e749e2bf5868ad9ec7787e914e
1:X 09 Jun 2021 09:52:01.026 # +monitor master mymaster 10.4.2.21 6379 quorum 2
...
1:X 09 Jun 2021 09:53:21.429 * +slave slave 10.4.0.10:6379 10.4.0.10 6379 @ mymaster 10.4.2.21 6379
1:X 09 Jun 2021 09:53:21.435 * +slave slave 10.4.1.10:6379 10.4.1.10 6379 @ mymaster 10.4.2.21 6379
...

As you can see from the logs above, the redis-sentinel-node-0 Pod is the master and the redis-sentinel-node-1 & redis-sentinel-node-2 Pods are slaves.从上面的日志可以看出, redis-sentinel-node-0 Pod 是 master, redis-sentinel-node-1 & redis-sentinel-node-2 Pod 是 slave。

For testing, let's delete the master and check if sentinel will switch the master role to one of the slaves:为了测试,让我们删除 master 并检查 sentinel 是否会将 master 角色切换为 slave 之一:

    $ kubectl delete pod redis-sentinel-node-0
    pod "redis-sentinel-node-0" deleted
    
    $ kubectl logs -f redis-sentinel-node-1 sentinel
    ...                                                                                           
    1:X 09 Jun 2021 09:55:20.902 # Executing user requested FAILOVER of 'mymaster'
    ...
    1:X 09 Jun 2021 09:55:22.666 # +switch-master mymaster 10.4.2.21 6379 10.4.1.10 6379
    ...
    1:X 09 Jun 2021 09:55:50.626 * +slave slave 10.4.0.10:6379 10.4.0.10 6379 @ mymaster 10.4.1.10 6379
    1:X 09 Jun 2021 09:55:50.632 * +slave slave 10.4.2.22:6379 10.4.2.22 6379 @ mymaster 10.4.1.10 6379

A new master ( redis-sentinel-node-2 10.4.1.10 ) has been selected, so everything works as expected.已经选择了一个新的主节点( redis-sentinel-node-2 10.4.1.10 ),所以一切都按预期工作。

Additionally, we can display more information by connecting to one of the Redis nodes:此外,我们可以通过连接到 Redis 节点之一来显示更多信息:

$ kubectl run --namespace default redis-client --restart='Never' --env REDIS_PASSWORD=redispassword --image docker.io/bitnami/redis:6.2.1-debian-10-r47 --command -- sleep infinity
pod/redis-client created
$ kubectl exec --tty -i redis-client --namespace default -- bash
I have no name!@redis-client:/$ redis-cli -h redis-sentinel-node-1.redis-sentinel-headless -p 6379 -a $REDIS_PASSWORD
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-sentinel-node-1.redis-sentinel-headless:6379> info replication
# Replication
role:slave
master_host:10.4.1.10
master_port:6379
master_link_status:up
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM