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

3.png

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

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

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

一、部署单机Redis

步骤一:创建ConfigMap

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-single-config
data:redis.conf: |daemonize nobind 0.0.0.0port 6379tcp-backlog 511timeout 0tcp-keepalive 300pidfile /data/redis-server.pidlogfile /data/redis.logloglevel noticedatabases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /dataslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100appendonly yesappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yesrequirepass redis#single#test

步骤二:创建Deployment

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

apiVersion: apps/v1
kind: Deployment
metadata:name: redis-single
spec:replicas: 1selector:matchLabels:app: redis-singletemplate:metadata:labels:app: redis-singlespec:initContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redis-singleimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/redis.confsubPath: redis.confcommand: [ "redis-server" ,"/usr/local/etc/redis/redis.conf" ]env:- name: TZvalue: "Asia/Shanghai"volumes:- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai- name: redis-datahostPath:path: /var/lib/docker/redis/singletype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-single-configitems:- key: redis.confpath: redis.conf

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

1.png

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

步骤三:创建Service

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

apiVersion: v1
kind: Service
metadata:name: service-redis-singlelabels:app: redis-single
spec:selector:app: redis-singleports:- name: redis-singleport: 6379targetPort: 6379nodePort: 30000type: NodePort

步骤四:验证单机Redis

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

2.png

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

3.png

4.png

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

6.png

小结

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

二、部署6节点Redis集群

步骤一:创建ConfigMap

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-cluster-config
data:redis-cluster-0.conf: |port 7111cluster-announce-bus-port 17111pidfile /data/redis-7111.pid    logfile /data/redis-7111.logdbfilename dump-7111.rdbappendfilename "appendonly-7111.aof"cluster-config-file nodes-7111.confprotected-mode notcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nologlevel noticedatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yes    dir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yes    appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yes    cluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-1.conf: |port 7112cluster-announce-bus-port 17112pidfile /data/redis-7112.pid    logfile /data/redis-7112.logdbfilename dump-7112.rdbappendfilename "appendonly-7112.aof"cluster-config-file nodes-7112.conf...redis-cluster-2.conf: |port 7113cluster-announce-bus-port 17113pidfile /data/redis-7113.pid    logfile /data/redis-7113.logdbfilename dump-7113.rdbappendfilename "appendonly-7113.aof"cluster-config-file nodes-7113.conf...redis-cluster-3.conf: |port 7114cluster-announce-bus-port 17114pidfile /data/redis-7114.pid    logfile /data/redis-7114.logdbfilename dump-7114.rdbappendfilename "appendonly-7114.aof"cluster-config-file nodes-7114.conf...redis-cluster-4.conf: |port 7115cluster-announce-bus-port 17115pidfile /data/redis-7115.pid    logfile /data/redis-7115.logdbfilename dump-7115.rdbappendfilename "appendonly-7115.aof"cluster-config-file nodes-7115.conf...redis-cluster-5.conf: |port 7116cluster-announce-bus-port 17116pidfile /data/redis-7116.pid    logfile /data/redis-7116.logdbfilename dump-7116.rdbappendfilename "appendonly-7116.aof"cluster-config-file nodes-7116.conf...

步骤二:创建Deployment

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

apiVersion: apps/v1
kind: Deployment
metadata:labels:app: redis-cluster-0name: redis-cluster-0
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-0strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-0spec:volumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7111protocol: TCP- name: electioncontainerPort: 17111protocol: TCPenv:- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: TZvalue: "Asia/Shanghai"command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-0.conf" ]args:- "--cluster-announce-ip"- "$(POD_IP)"

步骤三:创建Service

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

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

步骤四:Redis集群初始化

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

kubectl get pods -o wide

1.png

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

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

2.png

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

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

步骤五:验证Redis集群

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

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

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

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

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

1.png

2.png

小结

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

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-cluster-config
data:redis-cluster.conf: |daemonize nosupervised noprotected-mode nobind 0.0.0.0port 6379cluster-announce-bus-port 16379cluster-enabled yesappendonly yescluster-node-timeout 5000dir /datacluster-config-file /data/nodes.confrequirepass redis#cluster#testmasterauth redis#cluster#test
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-service
spec:selector:app: redis-clusterclusterIP: Noneports:- name: redis-6379port: 6379- name: redis-16379port: 16379
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-service-access
spec:selector:app: redis-clustertype: NodePortsessionAffinity: Noneports:- name: redis-6379port: 6379targetPort: 6379nodePort: 30201
---
apiVersion: apps/v1
kind: StatefulSet
metadata:labels:app: redis-clustername: redis-cluster
spec:serviceName: redis-cluster-servicereplicas: 6selector:matchLabels:app: redis-clustertemplate:metadata:labels:app: redis-clusterspec:terminationGracePeriodSeconds: 30containers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "redis-server", "/etc/redis/redis-cluster.conf" ]args:- "--cluster-announce-ip"- "$(POD_IP)"env:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "Asia/Shanghai"ports:- name: rediscontainerPort: 6379protocol: TCP- name: clustercontainerPort: 16379protocol: TCPvolumeMounts:- name: redis-confmountPath: /etc/redis- name: pvc-datamountPath: /datavolumes:- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai- name: redis-confconfigMap:name: redis-cluster-configitems:- key: redis-cluster.confpath: redis-cluster.confvolumeClaimTemplates:- metadata:name: pvc-dataspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 1Gi

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-cluster-config
data:redis-cluster-0.conf: |protected-mode noport 7111cluster-announce-bus-port 17111tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7111.pidloglevel noticelogfile /data/redis-7111.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7111.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7111.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7111.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-1.conf: |protected-mode noport 7112cluster-announce-bus-port 17112tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7112.pidloglevel noticelogfile /data/redis-7112.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7112.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7112.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7112.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-2.conf: |protected-mode noport 7113cluster-announce-bus-port 17113tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7113.pidloglevel noticelogfile /data/redis-7113.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7113.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7113.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7113.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-3.conf: |protected-mode noport 7114cluster-announce-bus-port 17114tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7114.pidloglevel noticelogfile /data/redis-7114.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7114.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7114.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7114.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-4.conf: |protected-mode noport 7115cluster-announce-bus-port 17115tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7115.pidloglevel noticelogfile /data/redis-7115.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7115.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7115.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7115.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-5.conf: |protected-mode noport 7116cluster-announce-bus-port 17116tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7116.pidloglevel noticelogfile /data/redis-7116.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7116.rdbdir /datamasterauth qxb#redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass qxb#redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7116.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7116.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-0
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-0type: NodePortsessionAffinity: Noneports:- name: redis-30201port: 7111targetPort: 7111nodePort: 30201- name: redis-30211port: 17111targetPort: 17111nodePort: 30211
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-1
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-1type: NodePortsessionAffinity: Noneports:- name: redis-30202port: 7112targetPort: 7112nodePort: 30202- name: redis-30212port: 17112targetPort: 17112nodePort: 30212
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-2
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-2type: NodePortsessionAffinity: Noneports:- name: redis-30203port: 7113targetPort: 7113nodePort: 30203- name: redis-30213port: 17113targetPort: 17113nodePort: 30213
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-3
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-3type: NodePortsessionAffinity: Noneports:- name: redis-30204port: 7114targetPort: 7114nodePort: 30204- name: redis-30214port: 17114targetPort: 17114nodePort: 30214
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-4
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-4type: NodePortsessionAffinity: Noneports:- name: redis-30205port: 7115targetPort: 7115nodePort: 30205- name: redis-30215port: 17115targetPort: 17115nodePort: 30215
---
apiVersion: v1
kind: Service
metadata:name: redis-cluster-5
spec:selector:statefulset.kubernetes.io/pod-name: redis-cluster-5type: NodePortsessionAffinity: Noneports:- name: redis-30206port: 7116targetPort: 7116nodePort: 30206- name: redis-30216port: 17116targetPort: 17116nodePort: 30216
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: redis-cluster
spec:serviceName: redis-clusterreplicas: 6selector:matchLabels:app: redis-clustertemplate:metadata:annotations:statefulset.kubernetes.io/pod-name: $(POD_NAME)labels:app: redis-clusterspec:volumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/env:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "Asia/Shanghai"command: [ "redis-server" ,"/usr/local/etc/redis/$(POD_NAME).conf" ]args:- --cluster-announce-ip- $(POD_IP)

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

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

3.png

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

步骤一:设置hostNetwork

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

spec:template:spec:hostNetwork: true

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

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

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

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

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

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

步骤二:配置环境变量HOST_IP

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

- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP

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

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

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

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

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

步骤四:验证Redis集群

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

1.png

小结

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

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-cluster-config
data:redis-cluster-0.conf: |protected-mode noport 7111cluster-announce-bus-port 17111tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7111.pidloglevel noticelogfile /data/redis-7111.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7111.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7111.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7111.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-1.conf: |protected-mode noport 7112cluster-announce-bus-port 17112tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7112.pidloglevel noticelogfile /data/redis-7112.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7112.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7112.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7112.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-2.conf: |protected-mode noport 7113cluster-announce-bus-port 17113tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7113.pidloglevel noticelogfile /data/redis-7113.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7113.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7113.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7113.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-3.conf: |protected-mode noport 7114cluster-announce-bus-port 17114tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7114.pidloglevel noticelogfile /data/redis-7114.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7114.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7114.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7114.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-4.conf: |protected-mode noport 7115cluster-announce-bus-port 17115tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7115.pidloglevel noticelogfile /data/redis-7115.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7115.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7115.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7115.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-5.conf: |protected-mode noport 7116cluster-announce-bus-port 17116tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7116.pidloglevel noticelogfile /data/redis-7116.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7116.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7116.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7116.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: redis-cluster-0name: redis-cluster-0
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-0strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-0spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7111protocol: TCP- name: electioncontainerPort: 17111protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "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-1name: redis-cluster-1
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-1strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-1spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7112protocol: TCP- name: electioncontainerPort: 17112protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "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-2name: redis-cluster-2
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-2strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-2spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7113protocol: TCP- name: electioncontainerPort: 17113protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "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-3name: redis-cluster-3
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-3strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-3spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7114protocol: TCP- name: electioncontainerPort: 17114protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "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-4name: redis-cluster-4
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-4strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-4spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7115protocol: TCP- name: electioncontainerPort: 17115protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "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-5name: redis-cluster-5
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: redis-cluster-5strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: redis-cluster-5spec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/ports:- name: rediscontainerPort: 7116protocol: TCP- name: electioncontainerPort: 17116protocol: TCPenv:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "Asia/Shanghai"command: [ "redis-server" ,"/usr/local/etc/redis/redis-cluster-5.conf" ]args:- "--cluster-announce-ip"- "$(HOST_IP)"

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

apiVersion: v1
kind: ConfigMap
metadata:name: redis-cluster-config
data:redis-cluster-0.conf: |protected-mode noport 7111cluster-announce-bus-port 17111tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7111.pidloglevel noticelogfile /data/redis-7111.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7111.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7111.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7111.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-1.conf: |protected-mode noport 7112cluster-announce-bus-port 17112tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7112.pidloglevel noticelogfile /data/redis-7112.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7112.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7112.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7112.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-2.conf: |protected-mode noport 7113cluster-announce-bus-port 17113tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7113.pidloglevel noticelogfile /data/redis-7113.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7113.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7113.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7113.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-3.conf: |protected-mode noport 7114cluster-announce-bus-port 17114tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7114.pidloglevel noticelogfile /data/redis-7114.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7114.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7114.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7114.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-4.conf: |protected-mode noport 7115cluster-announce-bus-port 17115tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7115.pidloglevel noticelogfile /data/redis-7115.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7115.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7115.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7115.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesredis-cluster-5.conf: |protected-mode noport 7116cluster-announce-bus-port 17116tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /data/redis-7116.pidloglevel noticelogfile /data/redis-7116.logdatabases 1always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump-7116.rdbdir /datamasterauth redis#cluster#testslave-serve-stale-data yesslave-read-only yesreplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100requirepass redis#cluster#testlazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename "appendonly-7116.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000cluster-enabled yescluster-config-file nodes-7116.confcluster-node-timeout 15000cluster-migration-barrier 1cluster-require-full-coverage yesslowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: redis-cluster
spec:serviceName: redis-clusterreplicas: 6selector:matchLabels:app: redis-clustertemplate:metadata:annotations:statefulset.kubernetes.io/pod-name: $(POD_NAME)labels:app: redis-clusterspec:hostNetwork: truevolumes:- name: redis-datahostPath:path: /var/lib/docker/redis/clustertype: DirectoryOrCreate- name: redis-configconfigMap:name: redis-cluster-config- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/ShanghaiinitContainers:- name: init-0image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sysctl", "-w", "net.core.somaxconn=511" ]securityContext:privileged: true- name: init-1image: busyboximagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: Filecommand: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ]securityContext:privileged: truecontainers:- name: redisimage: redis:6.0.8imagePullPolicy: IfNotPresentterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: redis-datamountPath: /data- name: redis-configmountPath: /usr/local/etc/redis/env:- name: HOST_IPvalueFrom:fieldRef:fieldPath: status.hostIP- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: TZvalue: "Asia/Shanghai"command: [ "redis-server" ,"/usr/local/etc/redis/$(POD_NAME).conf" ]args:- --cluster-announce-ip- $(HOST_IP)

结论

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

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

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

相关文章

flutter TARGET_SDK_VERSION和android 13

config.gradle ext{SDK_VERSION 33MIN_SDK_VERSION 23TARGET_SDK_VERSION 33COMPILE_SDK_VERSION SDK_VERSIONBUILD_TOOL_VERSION "33.0.0"//兼容库版本SUPPORT_LIB_VERSION "33.0.0"}app/build.gradle里面的 defaultConfig {// TODO: Specify your…

C++设计模式之桥接模式

文章目录 一、桥接模式二、std::error_code与设计模式&#xff08;桥接模式&#xff09;参考 一、桥接模式 在C中&#xff0c;桥接模式通常涉及以下几个角色&#xff1a; 抽象类接口&#xff08;Abstraction&#xff09;&#xff1a;定义抽象部分的接口&#xff0c;并维护一个…

DETRs with Collaborative Hybrid Assignments Training论文笔记

Title&#xff1a;[DETRs with Collaborative Hybrid Assignments Training Code 文章目录 1. Motivation2. one to one VS one to many3. Method&#xff08;1&#xff09;Encoder feature learning&#xff08;2&#xff09;Decoder attention learning 1. Motivation 当前…

【C#学习笔记】数据类中常用委托及接口——以List<T>为例

文章目录 List\<T\>/LinkedList \<T\>为什么是神&#xff1f;&#xff08;泛型为什么是神&#xff09;一些常见&#xff0c;通用的委托和接口ComparisonEnumerator List<T>/LinkedList <T>为什么是神&#xff1f;&#xff08;泛型为什么是神&#xff0…

打印技巧——word中A4排版打印成A3双面对折翻页

在进行会议文件打印时&#xff0c;我们常会遇到需要将A4排版的文件&#xff0c;在A3纸张上进行双面对折翻页打印&#xff0c;本文对设置方式进行介绍&#xff1a; 1、在【布局】选项卡中&#xff0c;点击右下角小箭头&#xff0c;打开页面设置选项卡 1.1在【页边距】中将纸张…

发力服务业务,龙湖集团半程领跑赢在“智慧”

成立三十载&#xff0c;龙湖集团一直是房地产行业“特立独行”的存在。 一方面&#xff0c;龙湖在对外战略方面长期量入为出&#xff0c;从不背上过重的“包袱”。 不久前&#xff0c;一则消息引发市场关注&#xff1a;龙湖集团提前偿还17亿元债务&#xff0c;已基本全部还清…

Unity 3D之 利用Vector3 计算移动方向,以及实现位移多少

文章目录 先分析代码&#xff0c;从代码中了解Vector3 moveDirection new Vector3(10f, 0f, 100f);合法吗Vector3 moveDirection new Vector3 (xf,yf,zf)不是用来表示三维坐标的怎么表示在某个方向的位移 先分析代码&#xff0c;从代码中了解 这段代码是一个在游戏开发中常见…

基于Jenkins自动打包并部署docker、PHP环境,ansible部署-------从小白到大神之路之学习运维第86天

第四阶段提升 时 间&#xff1a;2023年8月23日 参加人&#xff1a;全班人员 内 容&#xff1a; 基于Jenkins部署docker、PHP环境 目录 一、环境部署 &#xff08;一&#xff09;实验环境&#xff0c;服务器设置 &#xff08;二&#xff09;所有主机关闭防火墙和selinu…

【案例】登录注册

<template><div class"loginhome"><Header :butShow"butShow"></Header><div class"formdiv"><div style"text-align:center;padding:10px;"><h3>你好登录账号{{ stauts 3? 注册:登录 }}…

光谱成像系统视觉均匀校准积分球光源

数字相机的光谱灵敏度是成像传感器、光学透镜、滤光片以及相机内部图像处理过程等诸多因素的综合结果。即使是同一台相机&#xff0c;采用不同的光学镜头和不同的滤光片&#xff0c;由于光学系统的结构和光学材料的透过率不同&#xff0c;导致整个成像系统的光谱灵敏度也有所差…

大数据数据仓库

一.在线教育 1.数据采集 1.数仓概念 数据仓库是为企业制定决策&#xff0c;提供数据支持的。数据采集和存储、对数据进行计算和分析 2.项目架构 2.数据分类 业务数据 用户行为数据 爬虫数据 2.离线数仓 3.实时数仓

LVS之keepalived

1、keepalived 概述 总结&#xff1a;Keepalived 软件就是通过VRRP协议来实现高可用功能。 应用场景&#xff1a;企业应用中&#xff0c;单台服务器承担应用存在单点故障的危险 单点故障一旦发生&#xff0c;企业服务将发生中断&#xff0c;造成极大的危害 VRRP通信原理&…

gRpc的四种通信方式详细介绍

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

微服务 Eureka

Eureka Eureka是Netflix开源的一个用于构建基于微服务架构的服务发现和注册中心技术。在微服务架构中&#xff0c;系统被拆分成多个小型、自治的服务&#xff0c;每个服务负责特定的业务功能。这些服务需要能够相互发现和通信&#xff0c;这就是Eureka所提供的功能。 Eureka主…

网络互联与互联网 - TCP 协议详解

文章目录 1 概述2 TCP 传输控制协议2.1 报文格式2.2 三次握手&#xff0c;建立连接2.3 四次挥手&#xff0c;释放连接 3 扩展3.1 实验演示3.2 网工软考 1 概述 在 TCP/IP 协议簇 中有两个传输协议 TCP&#xff1a;Transmission Control Protocol&#xff0c;传输控制协议&…

利用tidevice+mysql+grafana实现ios性能测试

利用tidevicemysqlgrafana实现ios性能测试 1.什么是tidevice&#xff1f; tidevice是一个可以和ios设备进行通信的工具&#xff0c;提供以下功能&#xff1a; 截图获取手机信息ipa包的安装和卸载根据bundleID 启动和停止应用列出安装应用信息模拟Xcode运行XCTest&#xff0c…

机器学习深度学习——针对序列级和词元级应用微调BERT

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位即将上大四&#xff0c;正专攻机器学习的保研er &#x1f30c;上期文章&#xff1a;机器学习&&深度学习——NLP实战&#xff08;自然语言推断——注意力机制实现&#xff09; &#x1f4da;订阅专栏&#xff1a;机…

【C++设计模式】用简单工厂模式实现按汽车重量输出汽车类型

2023年8月24日&#xff0c;周四凌晨 #include<iostream>class CarType{ public:virtual std::string getType()0; };class MiniCar:public CarType{ public:std::string getType() override{return "小型车";}; };class MidSizeCar:public CarType{ public:std…

游戏出海需知:Admob游戏广告变现策略

越来越多的出海游戏公司更加重视应用内的广告变现&#xff0c;而 AdMob因为其提供的丰富的广告资源&#xff0c;稳定平台支持&#xff0c;被广泛接入采用。 Admob推出的广告变现策略包括bidding、插页式激励视频、开屏广告、各种细分功能的报告等等。 一、Bidding 竞价策略 …

CSS background 背景

background属性为元素添加背景效果。 它是以下属性的简写&#xff0c;按顺序为&#xff1a; background-colorbackground-imagebackground-repeatbackground-attachmentbackground-position 以下所有示例中的花花.jpg图片的大小是4848。 1 background-color background-col…