在今天的討論中,我們將深入研究如何將Redis資料庫遷移到雲端,以便更好地利用雲端計算的優勢提高資料管理的靈活性。
Redis(Remote Dictionary Server)是一個開源的、基於記憶體的資料結構儲存系統,它可以用作資料庫、快取和訊息代理。Redis支援多種資料結構,如字串、列表、集合、雜湊等,具有高效能、低延遲、持久化等特點。
在Kubernetes(K8S)中部署Redis是一項常見的任務,因為Redis是一個高效能的鍵值儲存資料庫,非常適合用於快取、訊息佇列等場景。本文將分別介紹如何在K8S叢集中部署單機Redis和Redis叢集。
一、部署單機Redis
步驟一:建立ConfigMap
首先,我們需要建立一個ConfigMap,用來儲存和管理Redis的相關配置。
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-single-config
data:
redis.conf: |
daemonize no
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
pidfile /data/redis-server.pid
logfile /data/redis.log
loglevel notice
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass redis#single#test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
步驟二:建立Deployment
接下來,我們需要建立一個Deployment,用來定義Redis的副本數量、映象版本等相關資訊。
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-single
spec:
replicas: 1
selector:
matchLabels:
app: redis-single
template:
metadata:
labels:
app: redis-single
spec:
initContainers:
- name: init-0
image: busybox
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
securityContext:
privileged: true
- name: init-1
image: busybox
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
securityContext:
privileged: true
containers:
- name: redis-single
image: redis:6.0.8
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-config
mountPath: /usr/local/etc/redis/redis.conf
subPath: redis.conf
command: [ "redis-server" ,"/usr/local/etc/redis/redis.conf" ]
env:
- name: TZ
value: "Asia/Shanghai"
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
- name: redis-data
hostPath:
path: /var/lib/docker/redis/single
type: DirectoryOrCreate
- name: redis-config
configMap:
name: redis-single-config
items:
- key: redis.conf
path: redis.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
在這個檔案中,我們定義了一個名為redis-single的Deployment,它使用了之前建立的ConfigMap中的配置檔案,並將其掛載到容器的/usr/local/etc/redis/redis.conf路徑下。此外,我們還將容器的/data目錄掛載到宿主機的/var/lib/docker/redis/single目錄。配置initContainers的目的是為了解決啟動時出現的兩個警告。
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1
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.
1
步驟三:建立Service
然後,我們還需要建立一個Service,用來將K8S叢集中執行的Redis例項暴露為可訪問的服務。
apiVersion: v1
kind: Service
metadata:
name: service-redis-single
labels:
app: redis-single
spec:
selector:
app: redis-single
ports:
- name: redis-single
port: 6379
targetPort: 6379
nodePort: 30000
type: NodePort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
步驟四:驗證單機Redis
首先,使用Redis視覺化工具連線到剛部署的單機Redis上,驗證Redis是否正常。
接下來,將副本數量調整為0,模擬Redis當機情況。此時與Redis已斷開連線。
然後,將副本數量恢復,模擬Redis當機後重啟。此時與Redis重新建立連線,功能使用正常。
小結
以上就是在K8S中部署單機Redis的相關步驟。透過這些步驟,我們成功地使用無狀態的Deployment部署了一個可用的單機Redis。當然,我們也可以使用StatefulSet來部署單機Redis,兩者之間的區別不大,這裡就不再贅述。
二、部署6節點Redis叢集
步驟一:建立ConfigMap
與單機版類似,我們需要建立一個ConfigMap來儲存和管理Redis的相關配置。在這裡,我們將建立6個配置檔案,分別對應Redis叢集中的6個節點,主要區別在於埠號的不同。
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster-config
data:
redis-cluster-0.conf: |
port 7111
cluster-announce-bus-port 17111
pidfile /data/redis-7111.pid
logfile /data/redis-7111.log
dbfilename dump-7111.rdb
appendfilename "appendonly-7111.aof"
cluster-config-file nodes-7111.conf
protected-mode no
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
loglevel notice
databases 1
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dir /data
masterauth redis#cluster#test
slave-serve-stale-data yes
slave-read-only yes
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass redis#cluster#test
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
redis-cluster-1.conf: |
port 7112
cluster-announce-bus-port 17112
pidfile /data/redis-7112.pid
logfile /data/redis-7112.log
dbfilename dump-7112.rdb
appendfilename "appendonly-7112.aof"
cluster-config-file nodes-7112.conf
...
redis-cluster-2.conf: |
port 7113
cluster-announce-bus-port 17113
pidfile /data/redis-7113.pid
logfile /data/redis-7113.log
dbfilename dump-7113.rdb
appendfilename "appendonly-7113.aof"
cluster-config-file nodes-7113.conf
...
redis-cluster-3.conf: |
port 7114
cluster-announce-bus-port 17114
pidfile /data/redis-7114.pid
logfile /data/redis-7114.log
dbfilename dump-7114.rdb
appendfilename "appendonly-7114.aof"
cluster-config-file nodes-7114.conf
...
redis-cluster-4.conf: |
port 7115
cluster-announce-bus-port 17115
pidfile /data/redis-7115.pid
logfile /data/redis-7115.log
dbfilename dump-7115.rdb
appendfilename "appendonly-7115.aof"
cluster-config-file nodes-7115.conf
...
redis-cluster-5.conf: |
port 7116
cluster-announce-bus-port 17116
pidfile /data/redis-7116.pid
logfile /data/redis-7116.log
dbfilename dump-7116.rdb
appendfilename "appendonly-7116.aof"
cluster-config-file nodes-7116.conf
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
步驟二:建立Deployment
接下來,我們需要建立6個Deployment,分別對應Redis叢集中的6個節點。主要區別在於使用ConfigMap中的配置檔案的不同和containers中暴露的埠不同。redis-cluster-0參考如下:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis-cluster-0
name: redis-cluster-0
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: redis-cluster-0
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: redis-cluster-0
spec:
volumes:
- name: redis-data
hostPath:
path: /var/lib/docker/redis/cluster
type: DirectoryOrCreate
- name: redis-config
configMap:
name: redis-cluster-config
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
initContainers:
- name: init-0
image: busybox
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: [ "sysctl", "-w", "net.core.somaxconn=511" ]
securityContext:
privileged: true
- name: init-1
image: busybox
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]
securityContext:
privileged: true
containers:
- name: redis
image: redis:6.0.8
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-config
mountPath: /usr/local/etc/redis/
ports:
- name: redis
containerPort: 7111
protocol: TCP
- name: election
containerPort: 17111
protocol: TCP
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: TZ
value: "Asia/Shanghai"
command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-0.conf" ]
args:
- "--cluster-announce-ip"
- "$(POD_IP)"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
步驟三:建立Service
然後,我們還需要建立一個Service,用來將K8S叢集中執行的Redis例項暴露為可訪問的服務。這裡同樣需要建立6個Service,分別對應步驟二中的6個Deployment。
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-0
name: redis-cluster-0
spec:
selector:
app: redis-cluster-0
type: NodePort
sessionAffinity: None
ports:
- name: redis-7111
port: 7111
targetPort: 7111
nodePort: 30201
- name: redis-17111
port: 17111
targetPort: 17111
nodePort: 30211
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-1
name: redis-cluster-1
spec:
selector:
app: redis-cluster-1
type: NodePort
sessionAffinity: None
ports:
- name: redis-7112
port: 7112
targetPort: 7112
nodePort: 30202
- name: redis-17112
port: 17112
targetPort: 17112
nodePort: 30212
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-2
name: redis-cluster-2
spec:
selector:
app: redis-cluster-2
type: NodePort
sessionAffinity: None
ports:
- name: redis-7113
port: 7113
targetPort: 7113
nodePort: 30203
- name: redis-17113
port: 17113
targetPort: 17113
nodePort: 30213
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-3
name: redis-cluster-3
spec:
selector:
app: redis-cluster-3
type: NodePort
sessionAffinity: None
ports:
- name: redis-7114
port: 7114
targetPort: 7114
nodePort: 30204
- name: redis-17114
port: 17114
targetPort: 17114
nodePort: 30214
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-4
name: redis-cluster-4
spec:
selector:
app: redis-cluster-4
type: NodePort
sessionAffinity: None
ports:
- name: redis-7115
port: 7115
targetPort: 7115
nodePort: 30205
- name: redis-17115
port: 17115
targetPort: 17115
nodePort: 30215
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis-cluster-5
name: redis-cluster-5
spec:
selector:
app: redis-cluster-5
type: NodePort
sessionAffinity: None
ports:
- name: redis-7116
port: 7116
targetPort: 7116
nodePort: 30206
- name: redis-17116
port: 17116
targetPort: 17116
nodePort: 30216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
步驟四:Redis叢集初始化
執行以下命令,檢視pod的名稱和ip:
kubectl get pods -o wide
1
執行以下命令建立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
1
返回類似以下資訊表示初始化成功。
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
1
2
3
4
步驟五:驗證Redis叢集
最後,我們可以使用redis-cli工具來驗證redis叢集是否正常工作。首先,進入任意一個pod內,這裡以redis-cluster-0為例:
kubectl exec -it redis-cluster-0-65cb5487d-kn86p -- /bin/bash
1
然後,使用以下命令連線到redis叢集:
redis-cli -a redis#cluster#test -c -h <HOST_IP> -p 30201
1
在redis-cli中,可以執行各種redis命令來測試叢集的功能。
小結
在K8S中部署Redis叢集的相關步驟已經介紹完畢。透過這些步驟,我們成功地使用無狀態的Deployment部署了一個可用的Redis叢集。當然,我們還可以使用StatefulSet來部署Redis叢集,兩者之間的區別不大,相關配置檔案參考詳見附錄。
附錄1:StatefulSet方式部署Redis叢集(暴露1個埠)
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster-config
data:
redis-cluster.conf: |
daemonize no
supervised no
protected-mode no
bind 0.0.0.0
port 6379
cluster-announce-bus-port 16379
cluster-enabled yes
appendonly yes
cluster-node-timeout 5000
dir /data
cluster-config-file /data/nodes.conf
requirepass redis#cluster#test
masterauth redis#cluster#test
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster-service
spec:
selector:
app: redis-cluster
clusterIP: None
ports:
- name: redis-6379
port: 6379
- name: redis-16379
port: 16379
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster-service-access
spec:
selector:
app: redis-cluster
type: NodePort
sessionAffinity: None
ports:
- name: redis-6379
port: 6379
targetPort: 6379
nodePort: 30201
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app: redis-cluster
name: redis-cluster
spec:
serviceName: redis-cluster-service
replicas: 6
selector:
matchLabels:
app: redis-cluster
template:
metadata:
labels:
app: redis-cluster
spec:
terminationGracePeriodSeconds: 30
containers:
- name: redis
image: redis:6.0.8
imagePullPolicy: IfNotPresent
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
command: [ "redis-server", "/etc/redis/redis-cluster.conf" ]
args:
- "--cluster-announce-ip"
- "$(POD_IP)"
env:
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: TZ
value: "Asia/Shanghai"
ports:
- name: redis
containerPort: 6379
protocol: TCP
- name: cluster
containerPort: 16379
protocol: TCP
volumeMounts:
- name: redis-conf
mountPath: /etc/redis
- name: pvc-data
mountPath: /data
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
- name: redis-conf
configMap:
name: redis-cluster-config
items:
- key: redis-cluster.conf
path: redis-cluster.conf
volumeClaimTemplates:
- metadata:
name: pvc-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
附錄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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
三、Redis叢集存在的問題以及解決方案
儘管我們按照步驟二已經成功部署了Redis叢集,但這種方式僅適用於在K8S叢集內部使用Redis。如果我們使用視覺化工具連線剛部署的Redis叢集,一旦發生節點切換,叢集將無法正常工作。
想要解決這個問題,我們可以按照如下步驟進行修改我們的部署檔案。
步驟一:設定hostNetwork
首先,在Deployment或者StatefulSet中設定hostNetwork為true,使pod與宿主機共享網路名稱空間。
spec:
template:
spec:
hostNetwork: true
————————————————
版權宣告:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本宣告。
原文連結:https://blog.csdn.net/muguazhi/article/details/132455056