k8s-redis-cluster 集群配置 && 集群访问代理 predixy 配置

三主三从方式配置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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
要在 Kubernetes 上部署 Redis Cluster 集群,可以按照以下步骤进行: 1. 创建 Redis ConfigMap 首先,需要创建一个 Redis 配置文件 ConfigMap。可以使用以下命令创建: ``` kubectl create configmap redis-conf --from-file=redis.conf ``` 其中,`redis.conf` 是 Redis 配置文件的名称。可以根据实际情况进行修改。 2. 创建 Redis StatefulSet 接下来,可以创建 Redis StatefulSet。可以使用以下 YAML 文件作为模板: ``` apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-cluster spec: serviceName: redis-cluster replicas: 6 selector: matchLabels: app: redis-cluster template: metadata: labels: app: redis-cluster spec: containers: - name: redis image: redis:6.0.9 ports: - containerPort: 6379 name: client - containerPort: 16379 name: gossip volumeMounts: - name: config mountPath: /usr/local/etc/redis/redis.conf subPath: redis.conf command: - sh - -c - | if [ -n "${POD_NAME}" ]; then sed -i "s/%cluster-name%/redis-cluster/g" /usr/local/etc/redis/redis.conf sed -i "s/%node-name%/${POD_NAME}/g" /usr/local/etc/redis/redis.conf fi volumes: - name: config configMap: name: redis-conf volumeClaimTemplates: - metadata: name: data spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi ``` 在上述 YAML 文件中,`replicas` 字段指定了 Redis 集群的节点数,`volumeClaimTemplates` 字段指定了每个节点的数据卷大小。另外,还需要注意以下几点: - `serviceName` 字段需要与 `metadata.name` 字段相同。 - `selector.matchLabels` 字段需要与 `metadata.labels` 字段相同。 - `command` 字段用于修改 Redis 配置文件中的节点名称和集群名称。 3. 创建 Redis Service 最后,需要创建 Redis Service。可以使用以下 YAML 文件作为模板: ``` apiVersion: v1 kind: Service metadata: name: redis-cluster labels: app: redis-cluster spec: ports: - name: client port: 6379 targetPort: 6379 - name: gossip port: 16379 targetPort: 16379 clusterIP: None selector: app: redis-cluster ``` 在上述 YAML 文件中,`clusterIP` 字段需要设置为 `None`,以便创建一个 Headless Service,让每个 Redis 节点可以通过 DNS 解析到自己的 IP 地址。 4. 部署 Redis Cluster 完成以上步骤后,可以使用以下命令部署 Redis Cluster: ``` kubectl apply -f redis-cluster.yaml ``` 其中,`redis-cluster.yaml` 是包含 Redis StatefulSet 和 Redis Service 的 YAML 文件。可以根据实际情况进行修改。 部署完成后,可以使用以下命令查看 Redis Cluster 的状态: ``` kubectl exec -it redis-cluster-0 -- redis-cli cluster nodes ``` 其中,`redis-cluster-0` 是 Redis Cluster 中的一个节点名称。可以根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值