欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

K8S部署Redis(單機、集群)的超詳細步驟

 更新時間:2024年05月10日 11:14:11   作者:muguazhi  
redis是一款基于BSD協(xié)議,開源的非關(guān)系型數(shù)據(jù)庫(nosql數(shù)據(jù)庫)這篇文章主要給大家介紹了關(guān)于K8S部署Redis(單機、集群)的超詳細步驟,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

前言

在今天的討論中,我們將深入研究如何將Redis數(shù)據(jù)庫遷移到云端,以便更好地利用云計算的優(yōu)勢提高數(shù)據(jù)管理的靈活性。

Redis(Remote Dictionary Server)是一個開源的、基于內(nèi)存的數(shù)據(jù)結(jié)構(gòu)存儲系統(tǒng),它可以用作數(shù)據(jù)庫、緩存和消息代理。Redis支持多種數(shù)據(jù)結(jié)構(gòu),如字符串、列表、集合、散列等,具有高性能、低延遲、持久化等特點。

在Kubernetes(K8S)中部署Redis是一項常見的任務(wù),因為Redis是一個高性能的鍵值存儲數(shù)據(jù)庫,非常適合用于緩存、消息隊列等場景。本文將分別介紹如何在K8S集群中部署單機Redis和Redis集群。

一、部署單機Redis

步驟一:創(chuàng)建ConfigMap

首先,我們需要創(chuàng)建一個ConfigMap,用來存儲和管理Redis的相關(guān)配置。

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

步驟二:創(chuàng)建Deployment

接下來,我們需要創(chuàng)建一個Deployment,用來定義Redis的副本數(shù)量、鏡像版本等相關(guān)信息。

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,它使用了之前創(chuàng)建的ConfigMap中的配置文件,并將其掛載到容器的/usr/local/etc/redis/redis.conf路徑下。此外,我們還將容器的/data目錄掛載到宿主機的/var/lib/docker/redis/single目錄。配置initContainers的目的是為了解決啟動時出現(xiàn)的兩個警告。

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.

步驟三:創(chuàng)建Service

然后,我們還需要創(chuàng)建一個Service,用來將K8S集群中運行的Redis實例暴露為可訪問的服務(wù)。

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是否正常。

  • 接下來,將副本數(shù)量調(diào)整為0,模擬Redis宕機情況。此時與Redis已斷開連接。

  • 然后,將副本數(shù)量恢復(fù),模擬Redis宕機后重啟。此時與Redis重新建立連接,功能使用正常。

小結(jié)

以上就是在K8S中部署單機Redis的相關(guān)步驟。通過這些步驟,我們成功地使用無狀態(tài)的Deployment部署了一個可用的單機Redis。當(dāng)然,我們也可以使用StatefulSet來部署單機Redis,兩者之間的區(qū)別不大,這里就不再贅述。

二、部署6節(jié)點Redis集群

步驟一:創(chuàng)建ConfigMap

與單機版類似,我們需要創(chuàng)建一個ConfigMap來存儲和管理Redis的相關(guān)配置。在這里,我們將創(chuàng)建6個配置文件,分別對應(yīng)Redis集群中的6個節(jié)點,主要區(qū)別在于端口號的不同。

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
	...

步驟二:創(chuàng)建Deployment

接下來,我們需要創(chuàng)建6個Deployment,分別對應(yīng)Redis集群中的6個節(jié)點。主要區(qū)別在于使用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)"

步驟三:創(chuàng)建Service

然后,我們還需要創(chuàng)建一個Service,用來將K8S集群中運行的Redis實例暴露為可訪問的服務(wù)。這里同樣需要創(chuàng)建6個Service,分別對應(yīng)步驟二中的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集群初始化

執(zhí)行以下命令,查看pod的名稱和ip:

kubectl get pods -o wide

執(zhí)行以下命令創(chuàng)建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

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

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

步驟五:驗證Redis集群

最后,我們可以使用redis-cli工具來驗證redis集群是否正常工作。首先,進入任意一個pod內(nèi),這里以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中,可以執(zhí)行各種redis命令來測試集群的功能。

小結(jié)

在K8S中部署Redis集群的相關(guān)步驟已經(jīng)介紹完畢。通過這些步驟,我們成功地使用無狀態(tài)的Deployment部署了一個可用的Redis集群。當(dāng)然,我們還可以使用StatefulSet來部署Redis集群,兩者之間的區(qū)別不大,相關(guān)配置文件參考詳見附錄。

附錄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集群存在的問題以及解決方案

盡管我們按照步驟二已經(jīng)成功部署了Redis集群,但這種方式僅適用于在K8S集群內(nèi)部使用Redis。如果我們使用可視化工具連接剛部署的Redis集群,一旦發(fā)生節(jié)點切換,集群將無法正常工作。

想要解決這個問題,我們可以按照如下步驟進行修改我們的部署文件。

步驟一:設(shè)置hostNetwork

首先,在Deployment或者StatefulSet中設(shè)置hostNetworktrue,使pod與宿主機共享網(wǎng)絡(luò)命名空間。

spec:
  template:
    spec:
      hostNetwork: true

設(shè)置hostNetwork字段為true可能會帶來以下風(fēng)險:

  • 安全風(fēng)險:Pod將共享宿主機的網(wǎng)絡(luò)命名空間,這意味著Pod中的容器可以直接訪問宿主機上的其他進程和服務(wù)。這可能導(dǎo)致潛在的安全漏洞和攻擊。

  • 性能風(fēng)險:使用宿主機的IP地址可能會導(dǎo)致網(wǎng)絡(luò)延遲和性能下降,因為Pod需要在宿主機上進行網(wǎng)絡(luò)通信。

  • 配置復(fù)雜性:使用宿主機的IP地址可能會增加K8S集群的配置復(fù)雜性,因為需要確保Pod可以正確地訪問宿主機上的網(wǎng)絡(luò)資源。

為了規(guī)避這些風(fēng)險,可以采取以下措施:

  • 僅在必要時使用hostNetwork:只有在需要完全控制容器網(wǎng)絡(luò)時才應(yīng)使用hostNetwork。在大多數(shù)情況下,建議使用默認的Pod網(wǎng)絡(luò)模式。
  • 限制Pod中的訪問權(quán)限:通過設(shè)置適當(dāng)?shù)腟ELinux上下文、AppArmor策略等,可以限制Pod中容器的訪問權(quán)限,從而降低安全風(fēng)險。
  • 使用CNI插件:CNI(Container Network Interface)插件可以幫助你更好地管理容器網(wǎng)絡(luò),提供更多的網(wǎng)絡(luò)隔離和安全性。常見的CNI插件有Calico、Flannel、Weave等。
  • 監(jiān)控和日志記錄:定期檢查Kubernetes集群中的網(wǎng)絡(luò)流量和日志,以便及時發(fā)現(xiàn)和解決潛在的安全問題。

步驟二:配置環(huán)境變量HOST_IP

接下來,我們需要在containersenv中配置環(huán)境變量HOST_IP,以便讓pod獲取到宿主機的IP地址。

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

同時,還需要修改containersargs的參數(shù)為HOST_IP。

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

步驟三:使用宿主機IP初始化Redis集群

使用宿主機ip和集群中任意一個pod的名稱執(zhí)行以下命令:

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集群是否正常。

小結(jié)

以上就是在K8S中部署Redis集群的相關(guān)步驟。通過這些步驟,我們成功地部署了一個可以在K8S集群外可訪問的Redis集群,解決了非K8S項目如何使用K8S中Redis集群的問題。由于我們使用了hostNetwork,使pod與宿主機共享網(wǎng)絡(luò)命名空間,這會帶來一定的安全風(fēng)險,需要結(jié)合實際情況進行充分考慮。

附錄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)

結(jié)論

這篇文章詳細介紹了在K8S環(huán)境中部署Redis單機和Redis集群的具體步驟。通過閱讀全文,我們可以發(fā)現(xiàn),我們并沒有使用PVC來存儲Redis的相關(guān)數(shù)據(jù),而是直接將其掛載到了宿主機上。這樣做的目的是為了方便Redis的遷移。相較于傳統(tǒng)的手動部署方式,使用K8S可以更便捷、快速地完成Redis集群的部署和管理。

到此這篇關(guān)于K8S部署Redis(單機、集群)的文章就介紹到這了,更多相關(guān)K8S部署Redis單機、集群內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis安裝與使用方法小結(jié)

    Redis安裝與使用方法小結(jié)

    這篇文章主要介紹了Redis安裝與使用方法,結(jié)合實例形式分析了Redis數(shù)據(jù)庫的下載、安裝、啟動、設(shè)置及相關(guān)使用操作注意事項,需要的朋友可以參考下
    2018-04-04
  • Redis集群Lettuce主從切換問題解決方案

    Redis集群Lettuce主從切換問題解決方案

    這篇文章主要為大家介紹了Redis集群Lettuce主從切換問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • redis?lua腳本解決高并發(fā)下秒殺場景

    redis?lua腳本解決高并發(fā)下秒殺場景

    這篇文章主要為大家介紹了redis?lua腳本解決高并發(fā)下秒殺場景,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • 詳解Redis在SpringBoot工程中的綜合應(yīng)用

    詳解Redis在SpringBoot工程中的綜合應(yīng)用

    這篇文章主要介紹了Redis在SpringBoot工程中的綜合應(yīng)用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • Redis實現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時情況

    Redis實現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時情況

    本文主要介紹了Redis實現(xiàn)分布式鎖(setnx、getset、incr)以及如何處理超時情況,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Centos7 Redis主從搭建配置的實現(xiàn)

    Centos7 Redis主從搭建配置的實現(xiàn)

    這篇文章主要介紹了Centos7 Redis主從搭建配置的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Redis對象與redisObject超詳細分析源碼層

    Redis對象與redisObject超詳細分析源碼層

    這篇文章主要介紹了Redis對象與redisObject源碼層的分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • redis5集群如何主動手工切換主從節(jié)點命令

    redis5集群如何主動手工切換主從節(jié)點命令

    這篇文章主要介紹了redis5集群如何主動手工切換主從節(jié)點命令,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 利用Redis的有序集合實現(xiàn)排行榜功能實例代碼

    利用Redis的有序集合實現(xiàn)排行榜功能實例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用Redis的有序集合實現(xiàn)排行榜功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 一篇文章讓你明白Redis主從同步

    一篇文章讓你明白Redis主從同步

    今天小編就為大家分享一篇關(guān)于一篇文章讓你明白Redis主從同步,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02

最新評論