当前位置: 首页 > news >正文

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集群的部署和管理。

相关文章:

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

在今天的讨论中&#xff0c;我们将深入研究如何将Redis数据库迁移到云端&#xff0c;以便更好地利用云计算的优势提高数据管理的灵活性。 Redis(Remote Dictionary Server)是一个开源的、基于内存的数据结构存储系统&#xff0c;它可以用作数据库、缓存和消息代理。Redis支持多…...

Flask狼书笔记 | 03_模板

文章目录 3 模板3.1 模板基本使用3.2 模板结构组织3.3 模板进阶 3 模板 模板&#xff08;template&#xff09;&#xff1a;包含固定内容和动态部分的可重用文件。Jinja2模板引擎可用于任何纯文本文件。 3.1 模板基本使用 HTML实体&#xff1a;https://dev.w3.org/html5/htm…...

MySQL 数据备份和数据恢复

目录 一、数据备份 1、概述 2、MySQLdump命令备份 1&#xff09;备份单个数据库中的所有表 2) 备份数据中某个或多个表 3) 备份所有数据库 4&#xff09;备份多个库 5) 只备份一个表或多个表结构 二、数据恢复 三、数据备份与恢复应用 一、数据备份 1、概述 数据备…...

软考高级系统架构设计师系列论文八十二:论软件的可维护性设计

软考高级系统架构设计师系列论文八十二:论软件的可维护性设计 一、摘要二、正文三、总结一、摘要 随着软件大型化,复杂化的发展,软件维护所耗费的资源越来越多,软件可维护性设计日益得到重视。我单位近几年开发综合业务 ATM交换机,用户对交换机的可维护性要求很高。我参加…...

Ompl初探

在/ompl-1.x.0/build/Release/bin下有很多生成的demo可执行文件 在终端执行 ./demo_Point2DPlanning 测试程序 #include <ompl/base/SpaceInformation.h> #include <ompl/base/spaces/SE3StateSpace.h> #include <ompl/base/StateSpace.h> #include <o…...

android sdk打包aar方案步骤

1.使用fat-aar库https://github.com/kezong/fat-aar-android/blob/master/README_CN.md 第一步&#xff1a;添加以下代码到你工程根目录下的build.gradle文件中: For Maven Central (The lastest release is available on Maven Central): buildscript {repositories {maven…...

Redis之bitmap类型解读

目录 基本介绍 基本命令 Setbit Getbit BITCOUNT 应用场景 统计当日活跃用户 用户签到 bitmap - Redis布隆过滤器 &#xff08;应对缓存穿透问题&#xff09; 基本介绍 Redis 的位图&#xff08;bitmap&#xff09;是由多个二进制位组成的数组&#xff0c;只有两…...

stm32之10.系统定时器

delay_s()延时秒 delay_ms()毫秒*1000 delay_us()微秒*1000000 微秒定时器代码 void delay_us(uint32_t n) { SysTick->CTRL 0; // Disable SysTick&#xff0c;关闭系统定时器 SysTick->LOAD SystemCoreClock/1000000*n-1; // 就是nus SysTick->LOAD Sys…...

PyTorch安装教程:从头开始配置PyTorch环境

PyTorch是一个开源的机器学习框架&#xff0c;广泛用于深度学习任务。要开始使用PyTorch&#xff0c;您需要在计算机上正确配置PyTorch环境。本文将为您提供一步步的指南&#xff0c;帮助您成功安装和配置PyTorch。 第一部分&#xff1a;安装Python和相关工具 第一步&#xf…...

Docker拉取并配置Grafana

Linux下安装Docker请参考&#xff1a;Linux安装Docker 安装准备 新建挂载目录 /opt/grafana/data目录&#xff0c;准备用来挂载放置grafana的数据 /opt/grafana/plugins目录&#xff0c;准备用来放置grafana的插件 /opt/grafana/config目录&#xff0c;准备用来挂载放置graf…...

Vue+Axios搭建二次元动态登录页面(mp4视频格式)

最近想做一个前端登录页面&#xff0c;背景好看的&#xff0c;格式中规中矩的&#xff0c;这么难&#xff1f;我自己创一个吧&#xff01; 效果图如下&#xff1a; 源码可以参考我的github&#xff0c;复制源码即可用&#xff1a;gym02/loginPage_Vue: 使用VueAxios搭建的动态…...

【Kubernetes】K8S到底是什么,最近怎么这么火

前言 kubernetes&#xff0c;简称K8s&#xff0c;是用8代替名字中间的8个字符“ubernete”而成的缩写。是一个开源的&#xff0c;用于管理云平台中多个主机上的容器化的应用&#xff0c;Kubernetes的目标是让部署容器化的应用简单并且高效&#xff08;powerful&#xff09;,Kub…...

Java爬虫下载网页图片

在Java中&#xff0c;可以使用HttpURLConnection&#xff0c;Jsoup等库来实现网页爬取和图片下载。下面是一个基本的例子&#xff1a; 首先&#xff0c;需要添加Jsoup库到你的项目中。如果你使用Maven&#xff0c;可以在你的pom.xml文件中添加以下依赖&#xff1a; xml <…...

C语言之扫雷游戏实现篇

目录 主函数test.c 菜单函数 选择循环 扫雷游戏实现分析 整体思路 问题1 问题2 问题3 问题4 游戏函数&#xff08;函数调用&#xff09; 创建游戏盘数组mine 创建游戏盘数组show 初始化游戏盘数组InitBoard 展示游戏盘DisplayBoard 游戏盘置雷SetMine 游戏…...

Python面向对象中super用法与MRO机制

Python面向对象中super用法与MRO机制 最近再看trackformer&#xff0c;里面用到了super的用法&#xff0c;记录一下super的用法 class A(object):def __init__(self):print(init A)def fun(self):print(A.fun)print(self)super(A, self).fun()class B(object):def __init__(s…...

高性能网络模式-Reactor

事实上&#xff0c;Reactor 模式也叫Dispatcher模式&#xff0c;即I/O 多路复⽤监听事件&#xff0c;收到事件后&#xff0c;根据事件类型分配&#xff08;Dispatch&#xff09;给某个进程/线程。Reactor 模式也是一种非阻塞同步网络模式。 Reactor 模式主要由 Reactor部分和处…...

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

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

JWT令牌的介绍

目录 一、什么是JWT 二、JWT令牌和Cookie客户端、Session服务端对比 三、特点与注意事项 四、使用场景 优点&#xff1a; 五、结构组成 一、什么是JWT JWT&#xff08;JSON Web Token&#xff09;是一种用于在网络应用间传递信息的开放标准&#xff08;RFC 7519&#x…...

C语言入门 Day_9 条件判断

目录 前言&#xff1a; 1.if判断 2.else判断 3.易错点 4.思维导图 前言&#xff1a; 我们知道比较运算和逻辑运算都会得到一个布尔型的数据&#xff0c;要么为真&#xff08;true&#xff09;&#xff0c;要么为假&#xff08;false&#xff09;。 今天我们来学习真和假在…...

Nodejs-nrm:快速切换npm源 / npm官方源和其他自定义源之间切换

一、理解 Nodejs nrm Nodejs nrm 是一个管理 npm 源的工具。由于 npm 在国内的速度较慢&#xff0c;很多开发者会使用淘宝的 npm 镜像源&#xff0c;但是也会遇到一些问题&#xff0c;例如某些包在淘宝镜像源中不存在&#xff0c;或者淘宝镜像源本身也会有问题。 Nodejs nrm …...

数据驱动洞察:各种词频分析技术挖掘热点数据

一、引言 随着信息时代的发展&#xff0c;人们的关注点日益复杂多样。社交媒体、新闻网站和论坛等平台上涌现了大量的信息&#xff0c;这使得热点分析成为了解社会热点话题和舆情动向的重要手段。词频统计是热点分析的基础&#xff0c;本文将分别介绍基于ElasticSearch、基于S…...

ES6-简介、语法

ES6 ES6简介 ​ ECMAScript 6&#xff08;简称ES6&#xff09;是于2015年6月正式发布的JavaScript语言的标准&#xff0c;正式名为ECMAScript 2015&#xff08;ES2015&#xff09;。它的目标是使得JavaScript语言可以用来编写复杂的大型应用程序&#xff0c;成为企业级开发语…...

诚迈科技子公司智达诚远与Unity中国达成合作,打造智能座舱新时代

2023 年 8 月 23 日&#xff0c;全球领先的实时 3D 引擎 Unity 在华合资公司 Unity 中国举办发布会&#xff0c;正式对外发布 Unity 引擎中国版——团结引擎&#xff0c;并带来专为次世代汽车智能座舱打造的团结引擎车机版。发布会上&#xff0c;诚迈科技副总裁、诚迈科技子公司…...

算法与数据结构(十)--图的入门

一.图的定义和分类 定义&#xff1a;图是由一组顶点和一组能够将两个顶点连接的边组成的。 特殊的图&#xff1a; 1.自环&#xff1a;即一条连接一个顶点和其自身的边; 2.平行边&#xff1a;连接同一对顶点的两条边&#xff1b; 图的分类&#xff1a; 按照连接两个顶点的边的…...

【Go 基础篇】Go语言 init函数详解:包的初始化与应用

介绍 在Go语言中&#xff0c;init() 函数是一种特殊的函数&#xff0c;用于在包被导入时执行一次性的初始化操作。init() 函数不需要手动调用&#xff0c;而是在包被导入时自动执行。这使得我们可以在包导入时完成一些必要的初始化工作&#xff0c;确保包的使用具有正确的环境…...

wazuh环境配置及漏洞复现

目录 一、wazuh配置 1进入官网下载OVA启动软件 2.虚拟机OVA安装 二、wazuh案例复现 1.wazuh初体验 2.这里我们以SQL注入为例&#xff0c;在我们的代理服务器上进行SQL注入&#xff0c;看wazuh如何检测和响应 一、wazuh配置 1进入官网下载OVA启动软件 Virtual Machine (O…...

Java接收前端请求体方式

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; 文章目录 RequestBodyPathVariableRequestParamValidated方法参数校验方法返回值校验 RequestHeaderHttpServletRequest ## Java接收前端请求体的方式 请求体&#xf…...

私有化部署即时通讯平台,30分钟替换钉钉和企业微信

随着企业对即时通讯和协作工具的需求不断增长&#xff0c;私有化部署的即时通讯平台成为企业的首选。WorkPlus作为有10余年行业深耕经验与技术沉淀品牌&#xff0c;以其安全高效的私有化部署即时通讯解决方案&#xff0c;帮助企业在30分钟内替换钉钉和企业微信。本文将深入探讨…...

如何深入理解 Node.js 中的流(Streams)

Node.js是一个强大的允许开发人员构建可扩展和高效的应用程序。Node.js的一个关键特性是其内置对流的支持。流是Node.js中的一个基本概念&#xff0c;它能够实现高效的数据处理&#xff0c;特别是在处理大量信息或实时处理数据时。 在本文中&#xff0c;我们将探讨Node.js中的流…...

MSP430FR2xxx开发(一)添加driverlib

一、新建工程 根据自己手上的硬件型号新建工程&#xff0c;文中已MSP430FR2355为例。 二、添加driverlib 首先去官方下载driverlib. https://www.ti.com.cn/tool/cn/MSPDRIVERLIB?keyMatchMSP430%20DRIVERLIB#downloads 下载后的内容如下&#xff1a; 我这里就选择MSP430…...

【C++】做一个飞机空战小游戏(九)——发射子弹的编程技巧

[导读]本系列博文内容链接如下&#xff1a; 【C】做一个飞机空战小游戏(一)——使用getch()函数获得键盘码值 【C】做一个飞机空战小游戏(二)——利用getch()函数实现键盘控制单个字符移动【C】做一个飞机空战小游戏(三)——getch()函数控制任意造型飞机图标移动 【C】做一个飞…...

34.SpringMVC获取请求参数

SpringMVC获取请求参数 通过ServletAPI获取 将HttpServletRequest作为控制器方法的形参&#xff0c;此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象 index.html <form th:action"{/test/param}" method"post">用户名&#…...

TC1016-同星4路CAN(FD),2路LIN转USB接口卡

TC1016是同星智能推出的一款多通道CAN&#xff08;FD&#xff09;和LIN总线接口设备&#xff0c;CANFD总线速率最高支持8M bps&#xff0c;LIN支持速率0~20K bps&#xff0c;产品采用高速USB2.0接口与PC连接&#xff0c;Windows系统免驱设计使得设备具备极佳的系统兼容性。 支…...

Android源码——从Looper看ThreadLocal

1 概述 ThreadLocal用于在当前线程中存储数据&#xff0c;由于存储的数据只能在当前线程内使用&#xff0c;所以自然是线程安全的。 Handler体系中&#xff0c;Looper只会存在一个实例&#xff0c;且只在当前线程使用&#xff0c;所以使用ThreadLocal进行存储。 2 存储原理 …...

16、Flink 的table api与sql之连接外部系统: 读写外部系统的连接器和格式以及JDBC示例(4)

Flink 系列文章 1、Flink 部署、概念介绍、source、transformation、sink使用示例、四大基石介绍和示例等系列综合文章链接 13、Flink 的table api与sql的基本概念、通用api介绍及入门示例 14、Flink 的table api与sql之数据类型: 内置数据类型以及它们的属性 15、Flink 的ta…...

MySQL 自定义 split 存储过程

MySQL 没有提供 split 函数&#xff0c;但可以自己建立一个存储过程&#xff0c;将具有固定分隔符的字符串转成多行。之所以不能使用自定义函数实现此功能&#xff0c;是因为 MySQL 的自定义函数自能返回标量值&#xff0c;不能返回多行结果集。 MySQL 8&#xff1a; drop pr…...

专题-【十字链表】

有向图的十字链表表示法&#xff1a;...

微信小程序教学系列(2)

第二章&#xff1a;小程序开发基础 1. 小程序页面布局与样式 在小程序开发中&#xff0c;我们可以使用 WXML&#xff08;WeiXin Markup Language&#xff09;和 WXSS&#xff08;WeiXin Style Sheet&#xff09;来定义页面的布局和样式。 1.1 WXML基础 WXML 是一种类似于 H…...

社科院与美国杜兰大学金融管理硕士项目——畅游于金融世界

随着社会经济的不断发展&#xff0c;职场竞争愈发激烈&#xff0c;很多同学都打算通过报考研究生来实现深造&#xff0c;提升自己的综合能力和竞争优势&#xff0c;获得优质的证书。而对于金融专业的学生和在职人员来说&#xff0c;社科院与美国杜兰大学金融管理硕士项目是一个…...

功能强大、超低功耗的STM32WL55JCI7、STM32WL55CCU7、STM32WL55CCU6 32位无线远距离MCU

STM32WL55xx 32位无线远距离MCU嵌入了功能强大、超低功耗、符合LPWAN标准的无线电解决方案&#xff0c;可提供LoRa、(G)FSK、(G)MSK和BPSK等各种调制。STM32WL55xx无线MCU的功耗超低&#xff0c;基于高性能Arm Cortex-M4 32位RISC内核&#xff08;工作频率高达48MHz&#xff09…...

【自适应稀疏度量方法和RQAM】疏度测量、RQAM特征、AWSPT和基于AWSPT的稀疏度测量研究(Matlab代码实现)

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

sql递归查询

一、postgresql 递归sql with recursive p as(select t1.* from t_org_test t1 where t1.id2union allselect t2.*from t_org_test t2 join p on t2.parent_idp.id) select id,name,parent_id from p; sql中with xxxx as () 是对一个查询子句做别名&#xff0c;同时数据库会对…...

常见前端面试之VUE面试题汇总三

7. Vue 中封装的数组方法有哪些&#xff0c;其如何实现页面更新 在 Vue 中&#xff0c;对响应式处理利用的是 Object.defineProperty 对数据进 行拦截&#xff0c;而这个方法并不能监听到数组内部变化&#xff0c;数组长度变化&#xff0c;数 组的截取变化等&#xff0c;所以需…...

Three.js 实现模型材质分解,拆分,拆解效果

原理&#xff1a;通过修改模型材质的 x,y,z 轴坐标 positon.set( x,y,z) 来实现拆解&#xff0c;分解的效果。 注意&#xff1a;支持模型材质position 修改的材质类型为 type“Mesh” ,其他类型的材质修改了position 可能没有实际效果 在上一篇 Three.js加载外部glb,fbx,gltf…...

《JVM修仙之路》初入JVM世界

《JVM修仙之路》初入JVM世界 博主目前正在学习JVM的相关知识&#xff0c;想以一种不同的方式记录下&#xff0c;娱乐一下 清晨&#xff0c;你睁开双眼&#xff0c;看到刺眼的阳光&#xff0c;你第一反应就是完了完了&#xff0c;又要迟到了。刚准备起床穿衣的你突然意识到不对&…...

苍穹外卖 day1 搭建成功环境

引入 idea找不到打包生成的文件目录怎么办&#xff0c;首先点击这个小齿轮 show ecluded files然后就能找到隐藏的文件 这个jar包内含tomcat&#xff0c;可以直接丢在linux上用 开发环境&#xff1a;开发人员在开发阶段使用的环境&#xff0c;一般外部用户无法访问 测试环…...

智能主体按照功能划分

(1) 构件接口主体 构件接口主体提供构件与用户之间的接口。当一个用户通过代理主体向 元组空间提出申请&#xff0c;并找到相匹配的构件主体时&#xff0c;此构件主体会将其所在构件主体 组中的构件接口主体通过申请用户的代理主体传送到用户的界面。 (2) 构件主体 通过构…...

python中的matplotlib画折线图(数据分析与可视化)

先导包&#xff08;必须安装了numpy 、pandas 和matplotlib才能导包&#xff09;&#xff1a; import numpy as np import pandas as pd import matplotlib.pyplot as plt核心代码&#xff1a; import numpy as np import pandas as pd import matplotlib.pyplot as pltpd.se…...

大数据数据仓库

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

Java“牵手“速卖通商品详情页面数据获取方法,速卖通API实现批量商品数据抓取示例

速卖通商城是一个网上购物平台&#xff0c;售卖各类商品&#xff0c;包括服装、鞋类、家居用品、美妆产品、电子产品等。要获取速卖通商品详情数据&#xff0c;您可以通过开放平台的接口或者直接访问速卖通商城的网页来获取商品详情信息。以下是两种常用方法的介绍&#xff1a;…...