apiVersion: v1 kind: ConfigMap metadata: name: redis-config namespace: mid labels: app: redis data: #这里定义了多个数据信息 master.conf: | # Master配置 requirepass redisPassword bind 0.0.0.0 daemonize no protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo no set-proc-title yes proc-title-template "{title} {listen-addr} {server-mode}" stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /data replica-serve-stale-data yes replica-read-only no repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no lazyfree-lazy-user-flush no oom-score-adj no oom-score-adj-values 0 200 800 disable-thp yes appendonly no appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes slave.conf: | # Slave配置 replicaof redis-0.redis-headless 6379 masterauth redisPassword requirepass redisPassword replica-read-only yes bind 0.0.0.0 daemonize no protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo no set-proc-title yes proc-title-template "{title} {listen-addr} {server-mode}" stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /data replica-serve-stale-data yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-disable-tcp-nodelay no replica-priority 100 acllog-max-len 128 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no lazyfree-lazy-user-del no lazyfree-lazy-user-flush no oom-score-adj no oom-score-adj-values 0 200 800 disable-thp yes appendonly no appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes jemalloc-bg-thread yes sentinel.conf: | # 哨兵配置 port 26379 daemonize no pidfile "/var/run/redis-sentinel.pid" logfile "" dir "/data" sentinel monitor master0 redis-0.redis-headless 6379 2 sentinel auth-pass master0 K4l2kC8Y0Nsep7eS sentinel down-after-milliseconds master0 30000 sentinel parallel-syncs master0 1 sentinel failover-timeout master0 180000 acllog-max-len 128 sentinel deny-scripts-reconfig yes sentinel resolve-hostnames yes sentinel announce-hostnames yes protected-mode no user mid on nopass sanitize-payload ~* &* +@all --- apiVersion: v1 kind: Secret metadata: name: redis-secret namespace: mid labels: app: redis #Opaque 类型的数据是一个 map 类型,要求value是base64编码。 type: Opaque data: redisPassword: SzRsMmtDOFkwTnNlcDdlUw== # 实际密码为K4l2kC8Y0Nsep7eS # 这里使用了base64转换 # 1echo -n 'K4l2kC8Y0Nsep7eS' | base64 # SzRsMmtDOFkwTnNlcDdlUw== --- apiVersion: v1 kind: Service metadata: name: redis-0-nodeport namespace: mid spec: type: NodePort selector: app: redis statefulset.kubernetes.io/pod-name: redis-0 ports: - name: redis port: 6379 targetPort: 6379 --- apiVersion: v1 kind: Service metadata: name: redis-headless namespace: mid labels: app: redis spec: selector: app: redis clusterIP: None ports: - name: redis port: 6379 --- apiVersion: v1 kind: Service metadata: name: redis-sentinel-headless namespace: mid labels: app: redis-sentinel spec: selector: app: redis-sentinel clusterIP: None ports: - name: redis-sentinel port: 26379 --- apiVersion: v1 kind: Service metadata: name: redis-sentinel namespace: mid labels: app: redis-sentinel spec: selector: app: redis-sentinel ports: - name: redis-sentinel port: 26379 --- apiVersion: apps/v1 kind: StatefulSet metadata: name: redis namespace: mid labels: app: redis spec: selector: matchLabels: app: redis serviceName: redis-headless replicas: 3 template: metadata: labels: app: redis spec: initContainers: - name: init-redis #image: redis:6.2.5 #image: harbor.uenpay.com/base/redis:6.2.5 image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/redis:6.2.5 imagePullPolicy: IfNotPresent command: - bash - "-c" - | set -ex #从pod的hostname中通过正则获取序号,如果没有截取到就退出程序 ordinal=`hostname | awk -F"-" '{print $NF}'` if [ ${ordinal} -eq 0 ]; then # 如果Pod的序号为0,说明它是Master节点 cp /initconfig/master.conf /conf/redis.conf else # 否则,拷贝ConfigMap里的Slave的配置文件 cp /initconfig/slave.conf /conf/redis.conf fi sed -i "s/redisPassword/${REDIS_PASSWORD}/g" /conf/redis.conf env: - name: REDIS_PASSWORD valueFrom: secretKeyRef: name: redis-secret key: redisPassword volumeMounts: - name: config mountPath: /conf - name: initconfig mountPath: /initconfig containers: - name: redis #image: base/redis:6.2.5 #image: harbor.uenpay.com/base/redis:6.2.5 image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/redis:6.2.5 imagePullPolicy: IfNotPresent command: ["redis-server"] args: - "/conf/redis.conf" ports: - name: redis containerPort: 6379 volumeMounts: - name: data mountPath: /data - name: config mountPath: /conf resources: requests: cpu: 500m memory: 2Gi limits: cpu: 2000m memory: 4Gi volumes: - name: initconfig configMap: name: redis-config - name: config emptyDir: {} volumeClaimTemplates: - metadata: name: data spec: accessModes: - ReadWriteOnce storageClassName: cephrbd-sc resources: requests: storage: 20Gi --- apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-sentinel namespace: mid labels: app: redis-sentinel spec: selector: matchLabels: app: redis-sentinel #与redis-service.yaml中的保持一致 serviceName: redis-sentinel-headless replicas: 3 template: metadata: labels: app: redis-sentinel spec: initContainers: - name: init-redis-sentinel #image: redis:6.2.5 #image: harbor.uenpay.com/base/redis:6.2.5 image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/redis:6.2.5 imagePullPolicy: IfNotPresent command: - bash - "-c" - | set -ex cp /initconfig/sentinel.conf /conf/redis-sentinel.conf volumeMounts: - mountPath: /conf name: config - mountPath: /initconfig name: initconfig containers: - name: redis-sentinel #image: redis:6.2.5 #image: harbor.uenpay.com/base/redis:6.2.5 image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/redis:6.2.5 imagePullPolicy: IfNotPresent command: ["redis-sentinel"] args: - "/conf/redis-sentinel.conf" ports: - name: redis-sentinel containerPort: 26379 volumeMounts: - name: config mountPath: /conf resources: requests: cpu: 100m memory: 100Mi limits: cpu: 100m memory: 100Mi volumes: - name: initconfig configMap: name: redis-config - name: config emptyDir: {}