示例redis1: 将每个redis.yaml 的路径以及名称进行更改,如果不暴露端口可以将 type: NodePort 改为 ClusterIP,去掉nodePort: 32255,内部端口想修改可以将redis-2改为7001以此类推

vi redis-1.yaml
kind: Service
apiVersion: v1
metadata:
  name: redis-1
  namespace: default
  labels:
    app: redis-1
spec:
  type: NodePort
  selector:
    app: redis-1
  ports:
    - port: 6379
      name: rest
      targetPort: 6379
      protocol: TCP
      nodePort: 32255

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-1
  namespace: default
spec:
  replicas: 1
  minReadySeconds: 120
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1 
      maxUnavailable: 1 
  selector:
    matchLabels:
      app: redis-1
  template:
    metadata:
      labels:
        app: redis-1
    spec:
      containers:
      - name: redis-1
        image: redis:latest
        imagePullPolicy: Always
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
        resources:
          limits:
            cpu: 1024m
            memory: 1024Mi
          requests:
            cpu: 50m
            memory: 500Mi
        ports:
        - containerPort: 6379
          name: rest
          protocol: TCP
        volumeMounts:
        - mountPath: /var/lib/redis
          name: data
        - mountPath: /etc/redis/redis.conf
          name: conf
        securityContext:
          privileged: true
      dnsPolicy: ClusterFirst
      nodeName: 主机名或ip
      volumes:
      - hostPath:
          path: /srv/redis/test/data
        name: data
      - hostPath:
          path: /srv/redis/test/redis.conf
        name: conf

kubectl apply -f  redis-1.yaml

示例redis2:创建4个或6个自行决定 将每个redis.yaml 的路径以及名称进行更改

vi redis-2.yaml
kind: Service
apiVersion: v1
metadata:
  name: redis-2
  namespace: default
  labels:
    app: redis-2
spec:
  type: NodePort
  selector:
    app: redis-2
  ports:
    - port: 6379
      name: rest
      targetPort: 6379
      protocol: TCP
      nodePort: 32266

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-2
  namespace: default
spec:
  replicas: 1
  minReadySeconds: 120
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1 
      maxUnavailable: 1 
  selector:
    matchLabels:
      app: redis-2
  template:
    metadata:
      labels:
        app: redis-2
    spec:
      containers:
      - name: redis-2
        image: redis:latest
        imagePullPolicy: Never
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
        resources:
          limits:
            cpu: 1024m
            memory: 1024Mi
          requests:
            cpu: 50m
            memory: 500Mi
        ports:
        - containerPort: 6379
          name: rest
          protocol: TCP
        volumeMounts:
        - mountPath: /var/lib/redis
          name: data
        - mountPath: /etc/redis/redis.conf
          name: conf
        securityContext:
          privileged: true
      dnsPolicy: ClusterFirst
      nodeName: 主机名或ip
      volumes:
      - hostPath:
          path: /srv/redis/test/data2
        name: data
      - hostPath:
          path: /srv/redis/test/redis.conf
        name: conf
kubectl apply -f  redis-2.yaml				

node 节点操作

vi /srv/redis/test/redis.conf
bind 0.0.0.0
masterauth  123456
requirepass 123456
slowlog-log-slower-than 100000
appendonly yes
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379

node节点进行操作(本人是在单台node操作,如果有多个节点可以在多个节点进行创建,reids.conf也需要在多个节点进行创建) 先创建data1-data6

mkdir -p /srv/redis/test/data{1..6}

主节点操作,查看redis 容器ip

kubectl get pods -o wide | grep redis

启动好reids后进入容器后执行组建集群

redis-cli --cluster create  --cluster-replicas 1 -a "密码"  192.168.68.117:7000 192.168.94.253:7001 192.168.70.33:7002 192.168.68.117:7003 192.168.94.253:7004 192.168.70.33:7005

查看集群信息 在任意一个 redis 节点的主机上执行以下命令:

# redis-cli -c -a "12345" -h 192.168.190.133 -p 7002 cluster info
# redis-cli -c -a "12345" -h 192.168.190.133 -p 7002 cluster nodes