K8S 部署redis cluster分片集群 —— 筑梦之路

环境说明:

redis版本: 5.0

部署所需的yaml文件内容:

apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: redis-service
  labels:
    app: redis
spec:
  ports:
    - name: redis
      port: 6379
      targetPort: 6379
      protocol: TCP
      nodePort: 26379
    - name: cluster
      port: 16379
      targetPort: 16379
      protocol: TCP
  #clusterIP: None
  type: NodePort
  selector:
    app: redis
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: test
  name: redis-cluster
spec:
  serviceName: redis-service
  selector:
    matchLabels:
      app: redis 
  replicas: 6
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:5.0
        ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
            - name: cluster
              containerPort: 16379
              protocol: TCP
        command: ["/etc/redis/fix-ip.sh", "redis-server", "/etc/redis/redis.conf"]
        volumeMounts:
          - name: redis-conf
            mountPath: /etc/redis
          - name: redis-data
            mountPath: /data
        resources:
          limits:
            cpu: 0.5
            memory: 1Gi
          requests:
            cpu: 0.1
            memory: 1Gi
      volumes:
      - name: redis-conf
        configMap:
          name: redis-cluster-conf
          items:
            - key: redis.conf
              path: redis.conf
            - key: fix-ip.sh
              path: fix-ip.sh
          defaultMode: 0755
  volumeClaimTemplates:
  - metadata:
      name: redis-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: managed-nfs-storage
      resources:
        requests:
          storage: 2Gi
---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: test
  labels:
    name: redis
  name: redis-cluster-conf
# maxmemory 设置为 limit.memory 80%
data:
  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 "Updating my 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: |
    port 6379
    cluster-enabled yes
    cluster-config-file nodes.conf
    repl-ping-slave-period 10
    repl-timeout 60
    cluster-node-timeout 15000
    maxmemory 966367641
    maxmemory-policy noeviction 
    appendonly yes
    appendfsync everysec 
    tcp-keepalive 60
    protected-mode no
    requirepass admin123
    masterauth  admin123

 镜像使用官方的就行

你可能感兴趣的:(云计算,linux系统运维,虚拟化,redis,kubernetes,数据库)