本文介绍了Redis Pods无法加入Redis集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在kubernetes中创建6个节点的redis集群.我正在使用 Minikube 运行kubernetes.

I want to create redis cluster of 6 nodes in kubernetes. I am running kubernetes using Minikube.

下面是我创建6节点群集的实现.

Below is my implementation for creating the 6 node cluster.

kind: StatefulSet
metadata:
  generation: 1
  labels:
    app: demo-app
  name: demo-app
  namespace: default
spec:
  podManagementPolicy: OrderedReady
  replicas: 6
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: demo-app
  serviceName: ""
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: demo-app
    spec:
      containers:
      - command:
        - redis-server
        - --port 6379
        - --cluster-enabled yes
        - --cluster-node-timeout 5000
        - --appendonly yes
        - --appendfilename appendonly-6379.aof
        image: redis:latest
        imagePullPolicy: Always
        name: demo-app
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
         - name: redis-pvc
           mountPath: /var
      - image: nginx:1.12
        imagePullPolicy: IfNotPresent
        name: redis-exporter
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
  updateStrategy:
    rollingUpdate:
      partition: 0
    type: RollingUpdate

  volumeClaimTemplates:
    - metadata:
        name: redis-pvc
      spec:
          accessModes:
           - ReadWriteOnce
          resources:
             requests:
                 storage: 1Gi

创建状态集后,我从一个Pod中执行redis create cluster命令.

After creation of stateful sets I am executing redis create cluster command from inside one of pods.

 redis-cli --cluster create 172.17.0.4:6379 172.17.0.5:6379  172.17.0.6:6379  172.17.0.7:6379  172.17.0.8:6379  172.17.0.9:6379 --cluster-replicas 1

所有这些都是pod的ips.有了这个,我就可以启动我的集群了.但是一旦我使用

These all are the ips of the pods.With this I am able to Start my cluster. But once I manually delete a single pod using

kubernetes delete pod <podname>

例如删除IP地址为172.17.0.6:6379的redis节点,该节点应该是主节点.删除redis集群后的状态为:

For example deleting the redis node with IP address: 172.17.0.6:6379 which was supposed to be master.After deleting the redis cluster state is:

127.0.0.1:6379> cluster nodes
1c8c238c58d99181018b37af44c2ebfe049e4564 172.17.0.9:6379@16379 slave 4b75e95772887e76eb3d0c9518d13def097ce5fd 0 1579496695000 6 connected
96e6be88d29d847aed9111410cb0f790db068d0e 172.17.0.8:6379@16379 slave 0db23edf54bb57f7db1e2c9eb182ce956229d16e 0 1579496696596 5 connected
c8be98b16a8fa7c1c9c2d43109abafefc803d345 172.17.0.7:6379@16379 master - 0 1579496695991 7 connected 10923-16383
0db23edf54bb57f7db1e2c9eb182ce956229d16e 172.17.0.4:6379@16379 myself,master - 0 1579496694000 1 connected 0-5460
4daae1051e6a72f2ffc0675649e9e2dad9430fc4 172.17.0.6:6379@16379 master,fail - 1579496680825 1579496679000 3 disconnected
4b75e95772887e76eb3d0c9518d13def097ce5fd 172.17.0.5:6379@16379 master - 0 1579496695000 2 connected 5461-10922

,并在一段时间后更改为:

and after sometime it changes to:

127.0.0.1:6379> cluster nodes
1c8c238c58d99181018b37af44c2ebfe049e4564 172.17.0.9:6379@16379 slave 4b75e95772887e76eb3d0c9518d13def097ce5fd 0 1579496697529 6 connected
96e6be88d29d847aed9111410cb0f790db068d0e 172.17.0.8:6379@16379 slave 0db23edf54bb57f7db1e2c9eb182ce956229d16e 0 1579496696596 5 connected
c8be98b16a8fa7c1c9c2d43109abafefc803d345 172.17.0.7:6379@16379 master - 0 1579496698031 7 connected 10923-16383
0db23edf54bb57f7db1e2c9eb182ce956229d16e 172.17.0.4:6379@16379 myself,master - 0 1579496697000 1 connected 0-5460
4daae1051e6a72f2ffc0675649e9e2dad9430fc4 :0@0 master,fail,noaddr - 1579496680825 1579496679000 3 disconnected
4b75e95772887e76eb3d0c9518d13def097ce5fd 172.17.0.5:6379@16379 master - 0 1579496697028 2 connected 5461-10922

由于redis集群提供了自动故障转移,但是pod的redis无法自动加入集群吗?

As redis cluster provide automatic failover but the pod's redis is unable to join automatically to the cluster?

还是我应该手动将该吊舱加入集群?

Or Should I join that pod manually into the cluster?

推荐答案

我已经解决了这个问题,并使用此有状态集合yaml创建了redis集群.问题是我没有在持久卷中装入集群配置文件.群集配置文件包含其他节点的位置.现在,群集配置文件将在Pod重新启动后持续存在.

I have solved this issue and created a redis cluster using this stateful set yaml.The problem was that I was not mounting the cluster config file in persistent volume. The cluster config file contains the location of other nodes. Now the cluster config files will persist across the pods restarts.

由于redis集群适用于八卦协议.只需一个活动节点即可获得整个集群的配置.

As redis cluster works on gossip protocol. It only needs one active node to get the configuration of the whole cluster.

现在,有状态集的最终配置为:

Now the final configuration of the stateful set are:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  generation: 1
  labels:
    app: demo-app
  name: demo-app
  namespace: default
spec:
  podManagementPolicy: OrderedReady
  replicas: 6
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: demo-app
  serviceName: ""
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: demo-app
    spec:
      containers:
      - command:
        - redis-server
        - --port 6379
        - --cluster-enabled yes
        - --cluster-node-timeout 5000
        - --appendonly yes
        - --cluster-config-file /var/cluster-config.conf
        - --appendfilename appendonly-6379.aof
        image: redis
        imagePullPolicy: Always
        name: demo-app
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
         - name: redis-pvc
           mountPath: /var
      - image: nginx:1.12
        imagePullPolicy: IfNotPresent
        name: redis-exporter
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
  updateStrategy:
    rollingUpdate:
      partition: 0
    type: RollingUpdate

  volumeClaimTemplates:
    - metadata:
        name: redis-pvc
      spec:
          accessModes:
           - ReadWriteOnce
          resources:
             requests:
                 storage: 1Gi

我所做的唯一更改是添加 在启动Redis服务器时-cluster-config-file/var/cluster-config.conf 参数.

only changes which i have done is adding --cluster-config-file /var/cluster-config.conf argument while starting the redis-server.

这篇关于Redis Pods无法加入Redis集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 11:05