K8S如何部署Redis(单机、集群)

news2025/1/9 1:48:55

3.png

在今天的讨论中,我们将深入研究如何将Redis数据库迁移到云端,以便更好地利用云计算的优势提高数据管理的灵活性。

Redis(Remote Dictionary Server)是一个开源的、基于内存的数据结构存储系统,它可以用作数据库、缓存和消息代理。Redis支持多种数据结构,如字符串、列表、集合、散列等,具有高性能、低延迟、持久化等特点。

在Kubernetes(K8S)中部署Redis是一项常见的任务,因为Redis是一个高性能的键值存储数据库,非常适合用于缓存、消息队列等场景。本文将分别介绍如何在K8S集群中部署单机Redis和Redis集群。

一、部署单机Redis

步骤一:创建ConfigMap

首先,我们需要创建一个ConfigMap,用来存储和管理Redis的相关配置。

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-single-config
data:
  redis.conf: |
    daemonize no
    bind 0.0.0.0
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    pidfile /data/redis-server.pid
    logfile /data/redis.log
    loglevel notice
    databases 16
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    appendonly yes
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
    requirepass redis#single#test

步骤二:创建Deployment

接下来,我们需要创建一个Deployment,用来定义Redis的副本数量、镜像版本等相关信息。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-single
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis-single
  template:
    metadata:
      labels:
        app: redis-single
    spec:
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis-single
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/redis.conf
              subPath: redis.conf
          command: [ "redis-server" ,"/usr/local/etc/redis/redis.conf" ]
          env:
            - name: TZ
              value: "Asia/Shanghai"
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/single
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-single-config
            items:
              - key: redis.conf
                path: redis.conf

在这个文件中,我们定义了一个名为redis-single的Deployment,它使用了之前创建的ConfigMap中的配置文件,并将其挂载到容器的/usr/local/etc/redis/redis.conf路径下。此外,我们还将容器的/data目录挂载到宿主机的/var/lib/docker/redis/single目录。配置initContainers的目的是为了解决启动时出现的两个警告。

1.png

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

步骤三:创建Service

然后,我们还需要创建一个Service,用来将K8S集群中运行的Redis实例暴露为可访问的服务。

apiVersion: v1
kind: Service
metadata:
  name: service-redis-single
  labels:
    app: redis-single
spec:
  selector:
    app: redis-single
  ports:
    - name: redis-single
      port: 6379
      targetPort: 6379
      nodePort: 30000
  type: NodePort

步骤四:验证单机Redis

  • 首先,使用Redis可视化工具连接到刚部署的单机Redis上,验证Redis是否正常。

2.png

  • 接下来,将副本数量调整为0,模拟Redis宕机情况。此时与Redis已断开连接。

3.png

4.png

  • 然后,将副本数量恢复,模拟Redis宕机后重启。此时与Redis重新建立连接,功能使用正常。

6.png

小结

以上就是在K8S中部署单机Redis的相关步骤。通过这些步骤,我们成功地使用无状态的Deployment部署了一个可用的单机Redis。当然,我们也可以使用StatefulSet来部署单机Redis,两者之间的区别不大,这里就不再赘述。

二、部署6节点Redis集群

步骤一:创建ConfigMap

与单机版类似,我们需要创建一个ConfigMap来存储和管理Redis的相关配置。在这里,我们将创建6个配置文件,分别对应Redis集群中的6个节点,主要区别在于端口号的不同。

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis-cluster-0.conf: |
    port 7111
    cluster-announce-bus-port 17111
    pidfile /data/redis-7111.pid    
    logfile /data/redis-7111.log
    dbfilename dump-7111.rdb
    appendfilename "appendonly-7111.aof"
    cluster-config-file nodes-7111.conf
    protected-mode no
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    loglevel notice
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes    
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes    
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes    
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-1.conf: |
    port 7112
    cluster-announce-bus-port 17112
    pidfile /data/redis-7112.pid    
    logfile /data/redis-7112.log
    dbfilename dump-7112.rdb
    appendfilename "appendonly-7112.aof"
    cluster-config-file nodes-7112.conf
	...
  redis-cluster-2.conf: |
    port 7113
    cluster-announce-bus-port 17113
    pidfile /data/redis-7113.pid    
    logfile /data/redis-7113.log
    dbfilename dump-7113.rdb
    appendfilename "appendonly-7113.aof"
    cluster-config-file nodes-7113.conf
	...
  redis-cluster-3.conf: |
    port 7114
    cluster-announce-bus-port 17114
    pidfile /data/redis-7114.pid    
    logfile /data/redis-7114.log
    dbfilename dump-7114.rdb
    appendfilename "appendonly-7114.aof"
    cluster-config-file nodes-7114.conf
	...
  redis-cluster-4.conf: |
    port 7115
    cluster-announce-bus-port 17115
    pidfile /data/redis-7115.pid    
    logfile /data/redis-7115.log
    dbfilename dump-7115.rdb
    appendfilename "appendonly-7115.aof"
    cluster-config-file nodes-7115.conf
	...
  redis-cluster-5.conf: |
    port 7116
    cluster-announce-bus-port 17116
    pidfile /data/redis-7116.pid    
    logfile /data/redis-7116.log
    dbfilename dump-7116.rdb
    appendfilename "appendonly-7116.aof"
    cluster-config-file nodes-7116.conf
	...

步骤二:创建Deployment

接下来,我们需要创建6个Deployment,分别对应Redis集群中的6个节点。主要区别在于使用ConfigMap中的配置文件的不同和containers中暴露的端口不同。redis-cluster-0参考如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-0
  name: redis-cluster-0
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-0
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-0
    spec:
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7111
              protocol: TCP
            - name: election
              containerPort: 17111
              protocol: TCP
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-0.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(POD_IP)"

步骤三:创建Service

然后,我们还需要创建一个Service,用来将K8S集群中运行的Redis实例暴露为可访问的服务。这里同样需要创建6个Service,分别对应步骤二中的6个Deployment。

apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-0
  name: redis-cluster-0
spec:
  selector:
    app: redis-cluster-0
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7111
      port: 7111
      targetPort: 7111
      nodePort: 30201
    - name: redis-17111
      port: 17111
      targetPort: 17111
      nodePort: 30211
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-1
  name: redis-cluster-1
spec:
  selector:
    app: redis-cluster-1
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7112
      port: 7112
      targetPort: 7112
      nodePort: 30202
    - name: redis-17112
      port: 17112
      targetPort: 17112
      nodePort: 30212
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-2
  name: redis-cluster-2
spec:
  selector:
    app: redis-cluster-2
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7113
      port: 7113
      targetPort: 7113
      nodePort: 30203
    - name: redis-17113
      port: 17113
      targetPort: 17113
      nodePort: 30213
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-3
  name: redis-cluster-3
spec:
  selector:
    app: redis-cluster-3
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7114
      port: 7114
      targetPort: 7114
      nodePort: 30204
    - name: redis-17114
      port: 17114
      targetPort: 17114
      nodePort: 30214
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-4
  name: redis-cluster-4
spec:
  selector:
    app: redis-cluster-4
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7115
      port: 7115
      targetPort: 7115
      nodePort: 30205
    - name: redis-17115
      port: 17115
      targetPort: 17115
      nodePort: 30215
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-cluster-5
  name: redis-cluster-5
spec:
  selector:
    app: redis-cluster-5
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-7116
      port: 7116
      targetPort: 7116
      nodePort: 30206
    - name: redis-17116
      port: 17116
      targetPort: 17116
      nodePort: 30216

步骤四:Redis集群初始化

执行以下命令,查看pod的名称和ip:

kubectl get pods -o wide

1.png

执行以下命令创建Redis集群:

kubectl exec -it redis-cluster-0-65cb5487d-kn86p -- redis-cli  -a redis#cluster#test --cluster create --cluster-replicas 1 109.233.87.199:7111 109.233.87.203:7112 109.233.87.198:7113 109.233.87.197:7114 109.233.87.205:7115 109.233.87.207:7116

2.png

返回类似以下信息表示初始化成功。

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

步骤五:验证Redis集群

最后,我们可以使用redis-cli工具来验证redis集群是否正常工作。首先,进入任意一个pod内,这里以redis-cluster-0为例:

kubectl exec -it redis-cluster-0-65cb5487d-kn86p -- /bin/bash

然后,使用以下命令连接到redis集群:

redis-cli -a redis#cluster#test -c -h <HOST_IP> -p 30201

在redis-cli中,可以执行各种redis命令来测试集群的功能。

1.png

2.png

小结

在K8S中部署Redis集群的相关步骤已经介绍完毕。通过这些步骤,我们成功地使用无状态的Deployment部署了一个可用的Redis集群。当然,我们还可以使用StatefulSet来部署Redis集群,两者之间的区别不大,相关配置文件参考详见附录。

附录1:StatefulSet方式部署Redis集群(暴露1个端口)

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
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 redis#cluster#test
    masterauth redis#cluster#test
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service
spec:
  selector:
    app: redis-cluster
  clusterIP: None
  ports:
    - name: redis-6379
      port: 6379
    - name: redis-16379
      port: 16379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service-access
spec:
  selector:
    app: redis-cluster
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-6379
      port: 6379
      targetPort: 6379
      nodePort: 30201
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: redis-cluster
  name: redis-cluster
spec:
  serviceName: redis-cluster-service
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      labels:
        app: redis-cluster
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "redis-server", "/etc/redis/redis-cluster.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(POD_IP)"
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
            - name: cluster
              containerPort: 16379
              protocol: TCP
          volumeMounts:
            - name: redis-conf
              mountPath: /etc/redis
            - name: pvc-data
              mountPath: /data
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: redis-conf
          configMap:
            name: redis-cluster-config
            items:
              - key: redis-cluster.conf
                path: redis-cluster.conf
  volumeClaimTemplates:
    - metadata:
        name: pvc-data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi

附录2:StatefulSet方式部署Redis集群(暴露6个端口)

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis-cluster-0.conf: |
    protected-mode no
    port 7111
    cluster-announce-bus-port 17111
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7111.pid
    loglevel notice
    logfile /data/redis-7111.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7111.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7111.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7111.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-1.conf: |
    protected-mode no
    port 7112
    cluster-announce-bus-port 17112
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7112.pid
    loglevel notice
    logfile /data/redis-7112.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7112.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7112.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7112.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-2.conf: |
    protected-mode no
    port 7113
    cluster-announce-bus-port 17113
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7113.pid
    loglevel notice
    logfile /data/redis-7113.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7113.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7113.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7113.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-3.conf: |
    protected-mode no
    port 7114
    cluster-announce-bus-port 17114
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7114.pid
    loglevel notice
    logfile /data/redis-7114.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7114.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7114.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7114.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-4.conf: |
    protected-mode no
    port 7115
    cluster-announce-bus-port 17115
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7115.pid
    loglevel notice
    logfile /data/redis-7115.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7115.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7115.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7115.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-5.conf: |
    protected-mode no
    port 7116
    cluster-announce-bus-port 17116
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7116.pid
    loglevel notice
    logfile /data/redis-7116.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7116.rdb
    dir /data
    masterauth qxb#redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass qxb#redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7116.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7116.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-0
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-0
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30201
      port: 7111
      targetPort: 7111
      nodePort: 30201
    - name: redis-30211
      port: 17111
      targetPort: 17111
      nodePort: 30211
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-1
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-1
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30202
      port: 7112
      targetPort: 7112
      nodePort: 30202
    - name: redis-30212
      port: 17112
      targetPort: 17112
      nodePort: 30212
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-2
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-2
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30203
      port: 7113
      targetPort: 7113
      nodePort: 30203
    - name: redis-30213
      port: 17113
      targetPort: 17113
      nodePort: 30213
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-3
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-3
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30204
      port: 7114
      targetPort: 7114
      nodePort: 30204
    - name: redis-30214
      port: 17114
      targetPort: 17114
      nodePort: 30214
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-4
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-4
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30205
      port: 7115
      targetPort: 7115
      nodePort: 30205
    - name: redis-30215
      port: 17115
      targetPort: 17115
      nodePort: 30215
---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-5
spec:
  selector:
    statefulset.kubernetes.io/pod-name: redis-cluster-5
  type: NodePort
  sessionAffinity: None
  ports:
    - name: redis-30206
      port: 7116
      targetPort: 7116
      nodePort: 30206
    - name: redis-30216
      port: 17116
      targetPort: 17116
      nodePort: 30216
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
spec:
  serviceName: redis-cluster
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      annotations:
        statefulset.kubernetes.io/pod-name: $(POD_NAME)
      labels:
        app: redis-cluster
    spec:
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/$(POD_NAME).conf" ]
          args:
            - --cluster-announce-ip
            - $(POD_IP)

三、Redis集群存在的问题以及解决方案

尽管我们按照步骤二已经成功部署了Redis集群,但这种方式仅适用于在K8S集群内部使用Redis。如果我们使用可视化工具连接刚部署的Redis集群,一旦发生节点切换,集群将无法正常工作。

3.png

想要解决这个问题,我们可以按照如下步骤进行修改我们的部署文件。

步骤一:设置hostNetwork

首先,在Deployment或者StatefulSet中设置hostNetworktrue,使pod与宿主机共享网络命名空间。

spec:
  template:
    spec:
      hostNetwork: true

设置hostNetwork字段为true可能会带来以下风险:

  • 安全风险:Pod将共享宿主机的网络命名空间,这意味着Pod中的容器可以直接访问宿主机上的其他进程和服务。这可能导致潜在的安全漏洞和攻击。

  • 性能风险:使用宿主机的IP地址可能会导致网络延迟和性能下降,因为Pod需要在宿主机上进行网络通信。

  • 配置复杂性:使用宿主机的IP地址可能会增加K8S集群的配置复杂性,因为需要确保Pod可以正确地访问宿主机上的网络资源。

为了规避这些风险,可以采取以下措施:

  • 仅在必要时使用hostNetwork:只有在需要完全控制容器网络时才应使用hostNetwork。在大多数情况下,建议使用默认的Pod网络模式。
  • 限制Pod中的访问权限:通过设置适当的SELinux上下文、AppArmor策略等,可以限制Pod中容器的访问权限,从而降低安全风险。
  • 使用CNI插件:CNI(Container Network Interface)插件可以帮助你更好地管理容器网络,提供更多的网络隔离和安全性。常见的CNI插件有Calico、Flannel、Weave等。
  • 监控和日志记录:定期检查Kubernetes集群中的网络流量和日志,以便及时发现和解决潜在的安全问题。

步骤二:配置环境变量HOST_IP

接下来,我们需要在containersenv中配置环境变量HOST_IP,以便让pod获取到宿主机的IP地址。

- name: HOST_IP
  valueFrom:
    fieldRef:
      fieldPath: status.hostIP

同时,还需要修改containersargs的参数为HOST_IP

args:
  - --cluster-announce-ip
  - $(HOST_IP)

步骤三:使用宿主机IP初始化Redis集群

使用宿主机ip和集群中任意一个pod的名称执行以下命令:

kubectl exec -it redis-cluster-0-6bb87c5c79-cnrtg -- redis-cli -a redis#cluster#test --cluster create --cluster-replicas 1 10.x.xxx.xx:7111 10.x.xxx.xx:7112 10.x.xxx.xx:7113 10.x.xxx.xx:7114 10.x.xxx.xx:7115 10.x.xxx.xx:7116

步骤四:验证Redis集群

使用可视化工具连接重新部署的Redis集群,验证Redis集群是否正常。

1.png

小结

以上就是在K8S中部署Redis集群的相关步骤。通过这些步骤,我们成功地部署了一个可以在K8S集群外可访问的Redis集群,解决了非K8S项目如何使用K8S中Redis集群的问题。由于我们使用了hostNetwork,使pod与宿主机共享网络命名空间,这会带来一定的安全风险,需要结合实际情况进行充分考虑。

附录1:Deployment方式部署Redis集群(暴露6个端口)

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis-cluster-0.conf: |
    protected-mode no
    port 7111
    cluster-announce-bus-port 17111
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7111.pid
    loglevel notice
    logfile /data/redis-7111.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7111.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7111.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7111.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-1.conf: |
    protected-mode no
    port 7112
    cluster-announce-bus-port 17112
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7112.pid
    loglevel notice
    logfile /data/redis-7112.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7112.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7112.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7112.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-2.conf: |
    protected-mode no
    port 7113
    cluster-announce-bus-port 17113
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7113.pid
    loglevel notice
    logfile /data/redis-7113.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7113.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7113.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7113.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-3.conf: |
    protected-mode no
    port 7114
    cluster-announce-bus-port 17114
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7114.pid
    loglevel notice
    logfile /data/redis-7114.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7114.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7114.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7114.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-4.conf: |
    protected-mode no
    port 7115
    cluster-announce-bus-port 17115
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7115.pid
    loglevel notice
    logfile /data/redis-7115.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7115.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7115.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7115.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-5.conf: |
    protected-mode no
    port 7116
    cluster-announce-bus-port 17116
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7116.pid
    loglevel notice
    logfile /data/redis-7116.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7116.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7116.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7116.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-0
  name: redis-cluster-0
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-0
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-0
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7111
              protocol: TCP
            - name: election
              containerPort: 17111
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-0.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-1
  name: redis-cluster-1
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-1
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-1
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7112
              protocol: TCP
            - name: election
              containerPort: 17112
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-1.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-2
  name: redis-cluster-2
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-2
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-2
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7113
              protocol: TCP
            - name: election
              containerPort: 17113
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-2.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-3
  name: redis-cluster-3
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-3
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-3
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7114
              protocol: TCP
            - name: election
              containerPort: 17114
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-3.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-4
  name: redis-cluster-4
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-4
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-4
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7115
              protocol: TCP
            - name: election
              containerPort: 17115
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-4.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: redis-cluster-5
  name: redis-cluster-5
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: redis-cluster-5
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: redis-cluster-5
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          ports:
            - name: redis
              containerPort: 7116
              protocol: TCP
            - name: election
              containerPort: 17116
              protocol: TCP
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-5.conf" ]
          args:
            - "--cluster-announce-ip"
            - "$(HOST_IP)"

附录2:StatefulSet方式部署Redis集群(暴露6个端口)

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis-cluster-0.conf: |
    protected-mode no
    port 7111
    cluster-announce-bus-port 17111
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7111.pid
    loglevel notice
    logfile /data/redis-7111.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7111.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7111.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7111.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-1.conf: |
    protected-mode no
    port 7112
    cluster-announce-bus-port 17112
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7112.pid
    loglevel notice
    logfile /data/redis-7112.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7112.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7112.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7112.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-2.conf: |
    protected-mode no
    port 7113
    cluster-announce-bus-port 17113
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7113.pid
    loglevel notice
    logfile /data/redis-7113.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7113.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7113.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7113.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-3.conf: |
    protected-mode no
    port 7114
    cluster-announce-bus-port 17114
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7114.pid
    loglevel notice
    logfile /data/redis-7114.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7114.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7114.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7114.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-4.conf: |
    protected-mode no
    port 7115
    cluster-announce-bus-port 17115
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7115.pid
    loglevel notice
    logfile /data/redis-7115.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7115.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7115.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7115.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  redis-cluster-5.conf: |
    protected-mode no
    port 7116
    cluster-announce-bus-port 17116
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /data/redis-7116.pid
    loglevel notice
    logfile /data/redis-7116.log
    databases 1
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump-7116.rdb
    dir /data
    masterauth redis#cluster#test
    slave-serve-stale-data yes
    slave-read-only yes
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    requirepass redis#cluster#test
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly yes
    appendfilename "appendonly-7116.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes-7116.conf
    cluster-node-timeout 15000
    cluster-migration-barrier 1
    cluster-require-full-coverage yes
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
spec:
  serviceName: redis-cluster
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      annotations:
        statefulset.kubernetes.io/pod-name: $(POD_NAME)
      labels:
        app: redis-cluster
    spec:
      hostNetwork: true
      volumes:
        - name: redis-data
          hostPath:
            path: /var/lib/docker/redis/cluster
            type: DirectoryOrCreate
        - name: redis-config
          configMap:
            name: redis-cluster-config
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
      initContainers:
        - name: init-0
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
          securityContext:
            privileged: true
        - name: init-1
          image: busybox
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
          securityContext:
            privileged: true
      containers:
        - name: redis
          image: redis:6.0.8
          imagePullPolicy: IfNotPresent
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /usr/local/etc/redis/
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: TZ
              value: "Asia/Shanghai"
          command: [ "redis-server" ,"/usr/local/etc/redis/$(POD_NAME).conf" ]
          args:
            - --cluster-announce-ip
            - $(HOST_IP)

结论

这篇文章详细介绍了在K8S环境中部署Redis单机和Redis集群的具体步骤。通过阅读全文,我们可以发现,我们并没有使用PVC来存储Redis的相关数据,而是直接将其挂载到了宿主机上。这样做的目的是为了方便Redis的迁移。相较于传统的手动部署方式,使用K8S可以更便捷、快速地完成Redis集群的部署和管理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/918293.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MES生产报工管理

一、MES生产报工管理的定义与功能&#xff1a; MES生产报工管理是指利用制造执行系统&#xff08;MES&#xff09;对生产过程进行实时监控、数据采集和分析&#xff0c;并及时记录和报告生产工单的实际完成情况。其主要功能包括&#xff1a; 1. 实时数据采集&#xff1a;通过…

如何在MT4安装和设置ADX指标,3步完成

ADX是MT4和MT5中的基本指标之一&#xff0c;但是很多投资者都不知道如何在MT4安装和设置ADX指标&#xff0c;其实很简单&#xff0c;跟着anzo capital昂首资本3步完成设置。 第一步&#xff1a;在终端中打开货币对报价图表&#xff0c;并创建一个新的图形。可以通过单击“文件”…

自定义滑动到底部触发指令,elementUI实现分页下拉框

在 main.js 中添加 // 自定义滑动到底部指令 Vue.directive(selectLoadMore, {bind(el, binding) {// 获取element-ui定义好的scroll盒子const SELECTWRAP_DOM el.querySelector(.el-select-dropdown .el-select-dropdown__wrap)SELECTWRAP_DOM.addEventListener(scroll, fun…

MySQL-Centos下MySQL5.7安装教程

MySQL安装教程 一&#xff0c;卸载MySQL二&#xff0c;安装MySQL三&#xff0c;mysql登录四&#xff0c;修改配置文件 一&#xff0c;卸载MySQL 1.如果你的机器上mysqld服务器还在运行&#xff0c;那么第一步就是要停掉服务。 systemctl stop mysqld;2.查看系统中安装的关于m…

6个剪辑必备的音效素材网站。

视频剪辑必备的6个音效、配乐素材网站&#xff0c;免费下载&#xff0c;赶紧收藏好~ 菜鸟图库 https://www.sucai999.com/audio.html?vNTYwNDUx 菜鸟图库是一个综合性素材网站&#xff0c;站内涵盖设计、图片、办公、视频、音效等素材。其中音效素材就有上千首&#xff0c;全…

利用torchvision库实现目标检测与语义分割

一、介绍 利用torchvision库实现目标检测与语义分割。 二、代码 1、目标检测 from PIL import Image import matplotlib.pyplot as plt import torchvision.transforms as T import torchvision import numpy as np import cv2 import randomCOCO_INSTANCE_CATEGORY_NAMES …

[保研/考研机试] KY188 哈夫曼树 北京邮电大学复试上机题 C++实现

题目链接&#xff1a; 哈夫曼树_牛客题霸_牛客网哈夫曼树&#xff0c;第一行输入一个数n&#xff0c;表示叶结点的个数。需要用这些叶结点生成哈夫曼树&#xff0c;根据哈夫曼树。题目来自【牛客题霸】https://www.nowcoder.com/share/jump/437195121692781391110 描述 哈夫…

Halcon错误 #2021: System clock has been set back.

修复"Halcon#2021 System clock has been set back."一键即可解决。

照片怎么换背景,换背景的简单方法

你是否曾经为了照片背景不合适而苦恼&#xff1f;是否曾经因为照片背景影响美观而错过了重要的纪念时刻&#xff1f;今天&#xff0c;来为你介绍以后照片抠图换背景的简单方法&#xff0c;让你不再需要担心照片背景的问题&#xff01;一起来看看吧&#xff01; 有时候照片背景…

Pycharm 控制台 输出 中文 乱码 黑方块

问题&#xff1a; 解决&#xff1a; 打开设置 》编辑器 》常规 》控制台》默认编码&#xff08;由系统默认GBK改为UTF-8&#xff09;

【回味“经典”】DFS练习题解(工作分配问题,最大平台)

这篇文章是一年前写的 走进“深度搜索基础训练“&#xff0c;踏入c算法殿堂&#xff08;四&#xff09;和 走进“深度搜索基础训练“&#xff0c;踏入c算法殿堂&#xff08;二&#xff09;的重编版。 希望以此&#xff0c;唤起对那位故人的回忆。 【搜索与回溯算法】工作分配问…

Authing 官网新升级,「客户第一」是我们的方法论

赶在立秋前&#xff0c;我们上线了全新一版官网。 官网链接&#xff1a;http://www.authing.com 如果你说&#xff0c;在几个月前我会怎么描述我们的官网&#xff0c;我会说&#xff1a;它很好&#xff0c;很标准。和其它绝大多数企业的官网一样&#xff0c;它作为展示信息的页…

MQ消息队列(主要介绍RabbitMQ)

消息队列概念&#xff1a;是在消息的传输过程中保存消息的容器。 作用&#xff1a;异步处理、应用解耦、流量控制..... RabbitMQ&#xff1a; SpringBoot继承RabbitMQ步骤&#xff1a; 1.加入依赖 <dependency><groupId>org.springframework.boot</groupId&g…

汽配企业MES管理系统如何追溯产品质量问题

随着汽车行业的快速发展&#xff0c;汽配行业也面临着越来越严格的质量要求。为了满足客户需求并提高产品质量&#xff0c;汽配企业需要实现生产过程的可追溯性。MES管理系统解决方案作为生产过程的核心管理系统&#xff0c;可以通过记录生产数据和流程&#xff0c;实现产品质量…

寡肽-54/Oligopeptide-54, CG-Nokkin---------一种新型的促进头发生长的多肽

功效与应用----寡肽-54 1. 头发色素沉积和逆转头发变白过程 2. 刺激头发生长 1. Hair pigment deposition and reversal of hair whitening process 2. Stimulate hair growth 作用机理----寡肽-54 寡肽-54&#xff0c;oligopeptide-54&#xff08;CG nokkin&#xff09;增…

基于量子粒子群算法(QPSO)优化LSTM的风电、负荷等时间序列预测算法(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

【C++杂货铺】探索string的底层实现

文章目录 一、成员变量二、成员函数2.1 默认构造函数2.2 拷贝构造函数2.3 operator2.4 c_str()2.5 size()2.6 operator[ ]2.7 iterator2.8 reserve2.9 resize2.10 push_back2.11 append2.12 operator2.13 insert2.14 erase2.15 find2.16 substr2.17 operator<<2.18 opera…

【数据结构】 LinkedList的模拟实现与使用

文章目录 &#x1f340;什么是LinkedList&#x1f334;LinkedList的模拟实现&#x1f6a9;创建双链表&#x1f6a9;头插法&#x1f6a9;尾插法&#x1f6a9;任意位置插入&#x1f6a9;查找关键字&#x1f6a9;链表长度&#x1f6a9;打印链表&#x1f6a9;删除第一次出现关键字为…

【技术】安防视频监控平台EasyNVR平台启用国标级联的操作步骤

安防视频监控汇聚EasyNVR视频集中存储平台&#xff0c;是基于RTSP/Onvif协议的安防视频平台&#xff0c;可支持将接入的视频流进行全平台、全终端分发&#xff0c;分发的视频流包括RTSP、RTMP、HTTP-FLV、WS-FLV、HLS、WebRTC等格式。 为提高用户体验&#xff0c;让用户更加便捷…

【Midjourney电商与平面设计实战】创作效率提升300%

不得不说&#xff0c;最近智能AI的话题火爆圈内外啦。这不&#xff0c;战火已经从IT行业燃烧到设计行业里了。 刚研究完ChatGPT&#xff0c;现在又出来一个AI作图Midjourney。 其视觉效果令不少网友感叹&#xff1a;“AI已经不逊于人类画师了!” 现如今&#xff0c;在AIGC 热…