写在最前

1. docker 部署

2. kubernetes 部署

2.1 service

kind: Service
apiVersion: v1
metadata:
  name: redis-cluster
  namespace: bx
  labels:
    app: redis-cluster
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: redis-6379
      protocol: TCP
      port: 6379
      targetPort: 6379
    - name: redis-16379
      protocol: TCP
      port: 16379
      targetPort: 16379
  selector:
    app: redis-cluster
  clusterIP: None
  clusterIPs:
    - None
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

2.2 configmap

kind: ConfigMap
apiVersion: v1
metadata:
  name: redis-config
  namespace: bx
  annotations:
    kubesphere.io/creator: admin
data:
  redis-cluster.conf: |
    daemonize no
    supervised no
    protected-mode no
    bind 0.0.0.0
    port 6379
    cluster-announce-bus-port 16379
    cluster-enabled yes
    appendonly yes
    cluster-node-timeout 5000
    dir /data
    cluster-config-file /data/nodes.conf
    requirepass 78236648-F942-4237-B9AB-0798F5E93143
    masterauth 78236648-F942-4237-B9AB-0798F5E93143

2.3 statefulset

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: redis-cluster
  namespace: bx
  labels:
    app: redis-cluster
  annotations:
    kubesphere.io/creator: admin
spec:
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: redis-cluster
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{}'
    spec:
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
            type: ''
        - name: redis-conf
          configMap:
            name: redis-config
            items:
              - key: redis-cluster.conf
                path: redis-cluster.conf
            defaultMode: 420
      containers:
        - name: redis
          image: 'redis:6.0.8'
          command:
            - redis-server
            - /etc/redis/redis-cluster.conf
          args:
            - '--cluster-announce-ip'
            - $(POD_IP)
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
            - name: cluster
              containerPort: 16379
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: TZ
              value: Asia/Shanghai
          resources: {}
          volumeMounts:
            - name: redis-conf
              mountPath: /etc/redis
            - name: data
              mountPath: /data
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  volumeClaimTemplates:
    - kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: data
        creationTimestamp: null
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
        volumeMode: Filesystem
      status:
        phase: Pending
  serviceName: redis-cluster
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: Retain
    whenScaled: Retain

2.4 初始化集群

# 获取所有的redis-cluster实例容器ip
bash-5.1# kubectl get po -A -owide |grep redis-cluster
bx  redis-cluster-0  1/1     Running     0               19m    172.244.248.201   hybxvdka02   <none>           <none>
bx  redis-cluster-1  1/1     Running     0               19m    172.244.126.254   hybxvdka03   <none>           <none>
bx  redis-cluster-2  1/1     Running     0               19m    172.244.55.138    hybxvdka01   <none>           <none>
bx  redis-cluster-3  1/1     Running     0               19m    172.244.126.209   hybxvdka03   <none>           <none>
bx  redis-cluster-4  1/1     Running     0               19m    172.244.55.169    hybxvdka01   <none>           <none>
bx  redis-cluster-5  1/1     Running     0               19m    172.244.248.248   hybxvdka02   <none>           <none>


# 初始化需要使用ip来进行,不能使用集群内域名
redis-cli -h 172.244.248.201 -p 6379 -a 78236648-F942-4237-B9AB-0798F5E93143 --cluster create \
  172.244.248.201:6379 \
  172.244.126.254:6379 \
  172.244.55.138:6379 \
  172.244.126.209:6379 \
  172.244.55.169:6379 \
  172.244.248.248:6379 \
  --cluster-replicas 1
> > > > > > > Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.244.55.169:6379 to 172.244.248.201:6379
Adding replica 172.244.248.248:6379 to 172.244.126.254:6379
Adding replica 172.244.126.209:6379 to 172.244.55.138:6379
M: 16da044e3b80b29fb9640146031e2b54424f46c8 172.244.248.201:6379
   slots:[0-5460] (5461 slots) master
M: ad3cf8b167bbebdd770cab8118a18b6aec510e49 172.244.126.254:6379
   slots:[5461-10922] (5462 slots) master
M: ae70116ee9d394864e2ef88458d01e318ab1542e 172.244.55.138:6379
   slots:[10923-16383] (5461 slots) master
S: 3ec54dc2f474d8c17af503e3a74400e8a8698406 172.244.126.209:6379
   replicates ae70116ee9d394864e2ef88458d01e318ab1542e
S: 9cbe3e4f9f668b24a0422f489bf0c2262c976a79 172.244.55.169:6379
   replicates 16da044e3b80b29fb9640146031e2b54424f46c8
S: ca1f401701dce26e5bcd674f9341b5c265ecf446 172.244.248.248:6379
   replicates ad3cf8b167bbebdd770cab8118a18b6aec510e49
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> 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.244.248.201:6379)
M: 16da044e3b80b29fb9640146031e2b54424f46c8 172.244.248.201:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 3ec54dc2f474d8c17af503e3a74400e8a8698406 172.244.126.209:6379
   slots: (0 slots) slave
   replicates ae70116ee9d394864e2ef88458d01e318ab1542e
M: ae70116ee9d394864e2ef88458d01e318ab1542e 172.244.55.138:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ad3cf8b167bbebdd770cab8118a18b6aec510e49 172.244.126.254:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9cbe3e4f9f668b24a0422f489bf0c2262c976a79 172.244.55.169:6379
   slots: (0 slots) slave
   replicates 16da044e3b80b29fb9640146031e2b54424f46c8
S: ca1f401701dce26e5bcd674f9341b5c265ecf446 172.244.248.248:6379
   slots: (0 slots) slave
   replicates ad3cf8b167bbebdd770cab8118a18b6aec510e49
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


# 验证集群状态
# redis-cli -h 172.244.248.201 -p 6379 -a 78236648-F942-4237-B9AB-0798F5E93143 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:74
cluster_stats_messages_pong_sent:74
cluster_stats_messages_sent:148
cluster_stats_messages_ping_received:69
cluster_stats_messages_pong_received:74
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:148

写在最后