K8S部署Redis(單機(jī)、集群)的超詳細(xì)步驟
前言
在今天的討論中,我們將深入研究如何將Redis數(shù)據(jù)庫(kù)遷移到云端,以便更好地利用云計(jì)算的優(yōu)勢(shì)提高數(shù)據(jù)管理的靈活性。
Redis(Remote Dictionary Server)是一個(gè)開(kāi)源的、基于內(nèi)存的數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)系統(tǒng),它可以用作數(shù)據(jù)庫(kù)、緩存和消息代理。Redis支持多種數(shù)據(jù)結(jié)構(gòu),如字符串、列表、集合、散列等,具有高性能、低延遲、持久化等特點(diǎn)。
在Kubernetes(K8S)中部署Redis是一項(xiàng)常見(jiàn)的任務(wù),因?yàn)镽edis是一個(gè)高性能的鍵值存儲(chǔ)數(shù)據(jù)庫(kù),非常適合用于緩存、消息隊(duì)列等場(chǎng)景。本文將分別介紹如何在K8S集群中部署單機(jī)Redis和Redis集群。
一、部署單機(jī)Redis
步驟一:創(chuàng)建ConfigMap
首先,我們需要?jiǎng)?chuàng)建一個(gè)ConfigMap,用來(lái)存儲(chǔ)和管理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
接下來(lái),我們需要?jiǎng)?chuàng)建一個(gè)Deployment,用來(lái)定義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
在這個(gè)文件中,我們定義了一個(gè)名為redis-single的Deployment,它使用了之前創(chuàng)建的ConfigMap中的配置文件,并將其掛載到容器的/usr/local/etc/redis/redis.conf路徑下。此外,我們還將容器的/data目錄掛載到宿主機(jī)的/var/lib/docker/redis/single目錄。配置initContainers的目的是為了解決啟動(dòng)時(shí)出現(xiàn)的兩個(gè)警告。

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
然后,我們還需要?jiǎng)?chuàng)建一個(gè)Service,用來(lái)將K8S集群中運(yùn)行的Redis實(shí)例暴露為可訪問(wèn)的服務(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
步驟四:驗(yàn)證單機(jī)Redis
- 首先,使用Redis可視化工具連接到剛部署的單機(jī)Redis上,驗(yàn)證Redis是否正常。

- 接下來(lái),將副本數(shù)量調(diào)整為0,模擬Redis宕機(jī)情況。此時(shí)與Redis已斷開(kāi)連接。


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

小結(jié)
以上就是在K8S中部署單機(jī)Redis的相關(guān)步驟。通過(guò)這些步驟,我們成功地使用無(wú)狀態(tài)的Deployment部署了一個(gè)可用的單機(jī)Redis。當(dāng)然,我們也可以使用StatefulSet來(lái)部署單機(jī)Redis,兩者之間的區(qū)別不大,這里就不再贅述。
二、部署6節(jié)點(diǎn)Redis集群
步驟一:創(chuàng)建ConfigMap
與單機(jī)版類(lèi)似,我們需要?jiǎng)?chuàng)建一個(gè)ConfigMap來(lái)存儲(chǔ)和管理Redis的相關(guān)配置。在這里,我們將創(chuàng)建6個(gè)配置文件,分別對(duì)應(yīng)Redis集群中的6個(gè)節(jié)點(diǎn),主要區(qū)別在于端口號(hào)的不同。
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
接下來(lái),我們需要?jiǎng)?chuàng)建6個(gè)Deployment,分別對(duì)應(yīng)Redis集群中的6個(gè)節(jié)點(diǎn)。主要區(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
然后,我們還需要?jiǎng)?chuàng)建一個(gè)Service,用來(lái)將K8S集群中運(yùn)行的Redis實(shí)例暴露為可訪問(wèn)的服務(wù)。這里同樣需要?jiǎng)?chuàng)建6個(gè)Service,分別對(duì)應(yīng)步驟二中的6個(gè)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的名稱(chēng)和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

返回類(lèi)似以下信息表示初始化成功。
[OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
步驟五:驗(yàn)證Redis集群
最后,我們可以使用redis-cli工具來(lái)驗(yàn)證redis集群是否正常工作。首先,進(jìn)入任意一個(gè)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命令來(lái)測(cè)試集群的功能。


小結(jié)
在K8S中部署Redis集群的相關(guān)步驟已經(jīng)介紹完畢。通過(guò)這些步驟,我們成功地使用無(wú)狀態(tài)的Deployment部署了一個(gè)可用的Redis集群。當(dāng)然,我們還可以使用StatefulSet來(lái)部署Redis集群,兩者之間的區(qū)別不大,相關(guān)配置文件參考詳見(jiàn)附錄。
附錄1:StatefulSet方式部署Redis集群(暴露1個(gè)端口)
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個(gè)端口)
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集群存在的問(wèn)題以及解決方案
盡管我們按照步驟二已經(jīng)成功部署了Redis集群,但這種方式僅適用于在K8S集群內(nèi)部使用Redis。如果我們使用可視化工具連接剛部署的Redis集群,一旦發(fā)生節(jié)點(diǎn)切換,集群將無(wú)法正常工作。

想要解決這個(gè)問(wèn)題,我們可以按照如下步驟進(jìn)行修改我們的部署文件。
步驟一:設(shè)置hostNetwork
首先,在Deployment或者StatefulSet中設(shè)置hostNetwork為true,使pod與宿主機(jī)共享網(wǎng)絡(luò)命名空間。
spec:
template:
spec:
hostNetwork: true
設(shè)置hostNetwork字段為true可能會(huì)帶來(lái)以下風(fēng)險(xiǎn):
安全風(fēng)險(xiǎn):Pod將共享宿主機(jī)的網(wǎng)絡(luò)命名空間,這意味著Pod中的容器可以直接訪問(wèn)宿主機(jī)上的其他進(jìn)程和服務(wù)。這可能導(dǎo)致潛在的安全漏洞和攻擊。
性能風(fēng)險(xiǎn):使用宿主機(jī)的IP地址可能會(huì)導(dǎo)致網(wǎng)絡(luò)延遲和性能下降,因?yàn)镻od需要在宿主機(jī)上進(jìn)行網(wǎng)絡(luò)通信。
配置復(fù)雜性:使用宿主機(jī)的IP地址可能會(huì)增加K8S集群的配置復(fù)雜性,因?yàn)樾枰_保Pod可以正確地訪問(wèn)宿主機(jī)上的網(wǎng)絡(luò)資源。
為了規(guī)避這些風(fēng)險(xiǎn),可以采取以下措施:
- 僅在必要時(shí)使用
hostNetwork:只有在需要完全控制容器網(wǎng)絡(luò)時(shí)才應(yīng)使用hostNetwork。在大多數(shù)情況下,建議使用默認(rèn)的Pod網(wǎng)絡(luò)模式。 - 限制Pod中的訪問(wèn)權(quán)限:通過(guò)設(shè)置適當(dāng)?shù)腟ELinux上下文、AppArmor策略等,可以限制Pod中容器的訪問(wèn)權(quán)限,從而降低安全風(fēng)險(xiǎn)。
- 使用CNI插件:CNI(Container Network Interface)插件可以幫助你更好地管理容器網(wǎng)絡(luò),提供更多的網(wǎng)絡(luò)隔離和安全性。常見(jiàn)的CNI插件有Calico、Flannel、Weave等。
- 監(jiān)控和日志記錄:定期檢查Kubernetes集群中的網(wǎng)絡(luò)流量和日志,以便及時(shí)發(fā)現(xiàn)和解決潛在的安全問(wèn)題。
步驟二:配置環(huán)境變量HOST_IP
接下來(lái),我們需要在containers的env中配置環(huán)境變量HOST_IP,以便讓pod獲取到宿主機(jī)的IP地址。
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
同時(shí),還需要修改containers中args的參數(shù)為HOST_IP。
args: - --cluster-announce-ip - $(HOST_IP)
步驟三:使用宿主機(jī)IP初始化Redis集群
使用宿主機(jī)ip和集群中任意一個(gè)pod的名稱(chēng)執(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
步驟四:驗(yàn)證Redis集群
使用可視化工具連接重新部署的Redis集群,驗(yàn)證Redis集群是否正常。

小結(jié)
以上就是在K8S中部署Redis集群的相關(guān)步驟。通過(guò)這些步驟,我們成功地部署了一個(gè)可以在K8S集群外可訪問(wèn)的Redis集群,解決了非K8S項(xiàng)目如何使用K8S中Redis集群的問(wèn)題。由于我們使用了hostNetwork,使pod與宿主機(jī)共享網(wǎng)絡(luò)命名空間,這會(huì)帶來(lái)一定的安全風(fēng)險(xiǎn),需要結(jié)合實(shí)際情況進(jìn)行充分考慮。
附錄1:Deployment方式部署Redis集群(暴露6個(gè)端口)
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個(gè)端口)
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é)論
這篇文章詳細(xì)介紹了在K8S環(huán)境中部署Redis單機(jī)和Redis集群的具體步驟。通過(guò)閱讀全文,我們可以發(fā)現(xiàn),我們并沒(méi)有使用PVC來(lái)存儲(chǔ)Redis的相關(guān)數(shù)據(jù),而是直接將其掛載到了宿主機(jī)上。這樣做的目的是為了方便Redis的遷移。相較于傳統(tǒng)的手動(dòng)部署方式,使用K8S可以更便捷、快速地完成Redis集群的部署和管理。
到此這篇關(guān)于K8S部署Redis(單機(jī)、集群)的文章就介紹到這了,更多相關(guān)K8S部署Redis單機(jī)、集群內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis數(shù)據(jù)結(jié)構(gòu)之intset的實(shí)例詳解
這篇文章主要介紹了redis數(shù)據(jù)結(jié)構(gòu)之intset的實(shí)例詳解的相關(guān)資料, intset也即整數(shù)集合,當(dāng)集合保存的值數(shù)量不多時(shí),redis使用intset作為其底層數(shù)據(jù)保存結(jié)構(gòu),希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
redis實(shí)現(xiàn)好友關(guān)注&消息推送的方法示例
Redis作為一款開(kāi)源的內(nèi)存數(shù)據(jù)庫(kù),具有可靠性、速度快、易用性等優(yōu)點(diǎn),已經(jīng)被廣泛應(yīng)用于開(kāi)發(fā)實(shí)際項(xiàng)目中,本文主要介紹了redis實(shí)現(xiàn)好友關(guān)注&消息推送的方法示例,感興趣的可以了解一下2023-10-10
Redis5之后版本的高可用集群搭建的實(shí)現(xiàn)
這篇文章主要介紹了Redis5之后版本的高可用集群搭建的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
詳解如何利用Redis實(shí)現(xiàn)生成唯一ID
隨著下單流量逐漸上升,為了降低數(shù)據(jù)庫(kù)的訪問(wèn)壓力,需要通過(guò)請(qǐng)求唯一ID+redis分布式鎖來(lái)防止接口重復(fù)提交。今天我們就一起來(lái)看探討一下,如何通過(guò)服務(wù)端來(lái)完成請(qǐng)求唯一?ID?的生成2022-11-11
Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過(guò)程
這篇文章主要介紹了Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Redis服務(wù)端主動(dòng)回收配置的使用小結(jié)
本文主要介紹了Redis服務(wù)端主動(dòng)回收配置的使用小結(jié),包括客戶(hù)端主動(dòng)回收、連接池配置、連接泄漏檢測(cè)及服務(wù)端策略設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08

