三主三从方式配置k8s-redis集群,采用最新的 redis6.0.6 版本官方镜像,在启动容器时增加 IP 同步脚本 fix-ip.sh

镜像的相关的脚本和配置在文章最后。各位复制粘贴的兄弟,务必根据自己的服务器配置情况修改

---
# 配置 有状态集模式
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
  namespace: default
spec:
  podManagementPolicy: OrderedReady
  replicas: 6
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.cn/name: redis-cluster
  serviceName: ''
  template:
    metadata:
      labels:
        k8s.kuboard.cn/layer: cloud
        k8s.kuboard.cn/name: redis-cluster
    spec:
      containers:
        - command:
            - /bin/sh
            - /etc/redis/fix-ip.sh  #此处fix-ip为新增加的指令,用来同步各个pod的IP地址,后边补上相关脚本
            - redis-server
            - /etc/redis/redis.conf
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
          image: 'redis:6.0.6'
          imagePullPolicy: IfNotPresent
          name: redis-cluster
          ports:
            - containerPort: 6389
              hostPort: 6389
              name: redis-port
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /etc/redis/
              name: config-dir
            - mountPath: /data
              name: redis-cluster-data
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 30
      volumes:
        - configMap:
            defaultMode: 420
            name: redis-cluster
          name: config-dir
  updateStrategy:
    rollingUpdate:
      partition: 0
    type: RollingUpdate
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: redis-cluster-data
      spec:
        accessModes:
          - ReadWriteMany
        resources:
          requests:
            storage: 30Gi
        storageClassName: redis-cluster-db
        volumeMode: Filesystem

---
apiVersion: v1 # Service 采用 Headless 无头方式
kind: Service
metadata:
  creationTimestamp: '2021-04-29T19:10:02Z'
  name: redis-cluster
  namespace: default
spec:
  clusterIP: None
  clusterIPs:
    - None
  ports:
    - name: redis-port
      port: 6379
      protocol: TCP
      targetPort: 6379
    - name: redis-gossip-port
      port: 16379
      protocol: TCP
      targetPort: 16379
  selector:
    k8s.cn/name: redis-cluster
  sessionAffinity: None
  type: ClusterIP

k8s的redis集群方式,对于客户端开发者来说是很不友好的,这里为了让我这样的老程序狗们轻松一些,经过长时间爬网做测试,选择predixy1.0.5做为集群的访问代理,如此一来,客户端编码时,集群访问跟单台redis访问是一样的,很多老的项目不用做任何代码修改就可以直接访问redis集群了,适配性很强,常规的redis锁、pub、sub等操作都支持,基本满足几乎所有的应用场景了。

predixy的部署如下

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '1'
  name: predixy
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      k8s.cn/name: predixy
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    spec:
      containers:
        - command:
            - predixy
            - /etc/predixy/predixy.conf
          image: 'predixy:1.0.5'
          imagePullPolicy: IfNotPresent
          name: predixy
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /etc/predixy/
              name: predixy-config-dir
              readOnly: true
            - mountPath: /data/
              name: predixy-data-dir
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      terminationGracePeriodSeconds: 30
      volumes:
        - configMap:
            defaultMode: 420
            name: predixy-config  # 文档后边补上predixy-config的配置信息
          name: predixy-config-dir
        - name: predixy-data-dir
          nfs: #容器相关的物理磁盘挂载,请自行设置
            path: /home/predixy/
            server: 172.24.0.88
---
apiVersion: v1
kind: Service
metadata:
  name: predixy
  namespace: default
spec:
  externalTrafficPolicy: Cluster
  ports:
    - name: predixy-port
      nodePort: 30617
      port: 7617
      protocol: TCP
      targetPort: 7617
  selector:
    k8s.cn/name: predixy
  sessionAffinity: None
  type: NodePort

predixy.conf

################################### GENERAL ####################################
## Predixy configuration file example

## Specify a name for this predixy service
## redis command INFO can get this
Name Predixy-DefaultNS

## Specify listen address, support IPV4, IPV6, Unix socket
## Examples:
# Bind 127.0.0.1:7617
# Bind 0.0.0.0:7617
# Bind /tmp/predixy

## Default is 0.0.0.0:7617
Bind 0.0.0.0:7617

## Worker threads
WorkerThreads 4

## Memory limit, 0 means unlimited

## Examples:
# MaxMemory 100M
# MaxMemory 1G
# MaxMemory 0

## MaxMemory can change online by CONFIG SET MaxMemory xxx
## Default is 0
# MaxMemory 0

## Close the connection after a client is idle for N seconds (0 to disable)
## ClientTimeout can change online by CONFIG SET ClientTimeout N
## Default is 0 为0时表示禁止该功能,不主动断开客户端连接
ClientTimeout 0


## IO buffer size
## Default is 4096
# BufSize 4096

################################### LOG ########################################
## Log file path
## Unspecify will log to stdout
## Default is Unspecified
Log /data/predixy.log

## LogRotate support

## 1d rotate log every day
## nh rotate log every n hours   1 <= n <= 24
## nm rotate log every n minutes 1 <= n <= 1440
## nG rotate log evenry nG bytes
## nM rotate log evenry nM bytes
## time rotate and size rotate can combine eg 1h 2G, means 1h or 2G roate a time
## Examples:
# LogRotate 1d 2G
# LogRotate 1d
LogRotate 1d

## Default is disable LogRotate


## In multi-threads, worker thread log need lock,
## AllowMissLog can reduce lock time for improve performance
## AllowMissLog can change online by CONFIG SET AllowMissLog true|false
## Default is true
# AllowMissLog false

## LogLevelSample, output a log every N
## all level sample can change online by CONFIG SET LogXXXSample N
LogVerbSample 0
LogDebugSample 0
LogInfoSample 100
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1


################################### AUTHORITY ##################################
# Include auth.conf

################################### SERVERS ####################################
Include cluster.conf
# Include sentinel.conf
# Include try.conf


################################### DATACENTER #################################
## LocalDC specify current machine dc
# LocalDC bj

## see dc.conf
# Include dc.conf


################################### COMMAND ####################################
## Custom command define, see command.conf
#Include command.conf

################################### LATENCY ####################################
## Latency monitor define, see latency.conf
Include latency.conf

redis 镜像的 fix-ip.sh 脚本

#!/bin/sh
CLUSTER_CONFIG="/data/nodes.conf"
if [ -f ${CLUSTER_CONFIG} ]; then
  if [ -z "${POD_IP}" ]; then
    echo "Unable to determine Pod IP address!"
    exit 1
  fi
  echo "更新本机 POD IP to ${POD_IP} in ${CLUSTER_CONFIG}"
  sed -i.bak -e '/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/'${POD_IP}'/' ${CLUSTER_CONFIG}
fi
exec "$@"

redis.conf

cluster-enabled yes
cluster-config-file /data/nodes.conf
cluster-node-timeout 10000
protected-mode no
daemonize no
pidfile /var/run/redis.pid
port 6379
tcp-backlog 511
bind 0.0.0.0
timeout 3600
tcp-keepalive 1
loglevel verbose
logfile /data/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
#requirepass yl123456
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 20000
slowlog-log-slower-than 10000
slowlog-max-len 128
#rename-command FLUSHALL  ""
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐