我想在Kubernetes中创建6个节点的Redis集群。我正在使用 Minikube 运行 kubernetes 。
以下是我创建6节点群集的实现。
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命令。
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。有了这个,我就能启动集群了。但是一旦我手动删除使用
kubernetes delete pod <podname>
例如,删除IP地址为172.17.0.6:6379的Redis节点(假设该节点为主节点),删除Redis集群后的状态为:
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
一段时间后更改为:
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无法自动加入集群吗?
还是我应该手动将该吊舱加入集群?
我已经解决了这个问题,并使用此状态集yaml创建了redis集群。问题是我没有在持久卷中装入 集群配置 文件。群集配置文件包含其他节点的位置。现在,群集配置文件将在Pod重新启动后持续存在。
由于redis集群适用于八卦协议。它只需要一个活动节点即可获得整个集群的配置。
现在,有状态集的最终配置为:
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-server时添加了 --cluster-config-file /var/cluster- config.conf 参数。