kubernetes实验挑战四(Build a highly available Redis Cluster | redis-cluster-config | PersistentVolume)

Build a highly available Redis Cluster based on the given architecture diagram.
kubernetes实验挑战四(Build a highly available Redis Cluster | redis-cluster-config | PersistentVolume)_第1张图片
1、创建redis-cluster
StatefulSet - Name: redis-cluster

Replicas: 6

Pods status: Running (All 6 replicas)

Image: redis:5.0.1-alpine, Label = app: redis-cluster

container name: redis, command: [“/conf/update-node.sh”, “redis-server”, “/conf/redis.conf”]

Env: name: ‘POD_IP’, valueFrom: ‘fieldRef’, fieldPath: ‘status.podIP’ (apiVersion: v1)

Ports - name: ‘client’, containerPort: ‘6379’

Ports - name: ‘gossip’, containerPort: ‘16379’

Volume Mount - name: ‘conf’, mountPath: ‘/conf’, readOnly:‘false’ (ConfigMap Mount)

Volume Mount - name: ‘data’, mountPath: ‘/data’, readOnly:‘false’ (volumeClaim)

volumes - name: ‘conf’, Type: ‘ConfigMap’, ConfigMap Name: ‘redis-cluster-configmap’,

Volumes - name: ‘conf’, ConfigMap Name: ‘redis-cluster-configmap’, defaultMode = ‘0755’

volumeClaimTemplates - name: ‘data’

volumeClaimTemplates - accessModes: ‘ReadWriteOnce’

volumeClaimTemplates - Storage Request: ‘1Gi’

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
  labels:
    run: redis-cluster
spec:
  serviceName: redis-cluster-service
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      name: redis-cluster
      labels:
        app: redis-cluster
    spec:
      volumes:
        - name: conf
          configMap:
            name: redis-cluster-configmap
            defaultMode: 0755
      containers:
        - image: redis:5.0.1-alpine
          name: redis
          command:
            - "/conf/update-node.sh"
            - "redis-server"
            - "/conf/redis.conf"
          env:
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
                apiVersion: v1
          ports:
            - containerPort: 6379
              name: client
            - name: gossip
              containerPort: 16379
          volumeMounts:
            - name: conf
              mountPath: /conf
              readOnly: false
            - name: data
              mountPath: /data
              readOnly: false
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi


            

2、创建redis01-06 pv卷:在Kubernetes中,可以使用List来批量创建pv卷

PersistentVolume - Name: redis01…06

Access modes: ReadWriteOnce

Size: 1Gi

hostPath: /redis01…06, directory should be created on worker node

---
apiVersion: v1
kind: List
items:
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis01
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis01
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis02
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis02
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis03
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis03
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis04
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis04
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis05
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis05
  - kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: redis06
    spec:
      accessModes: ["ReadWriteOnce"]
      capacity:
        storage: 1Gi
      hostPath:
        path: /redis06     

        

3、创建redis-cluster-service

Ports - service name ‘redis-cluster-service’, port name: ‘client’, port: ‘6379’

Ports - service name ‘redis-cluster-service’, port name: ‘gossip’, port: ‘16379’

Ports - service name ‘redis-cluster-service’, port name: ‘client’, targetPort: ‘6379’

Ports - service name ‘redis-cluster-service’, port name: ‘gossip’, targetPort: ‘16379’

apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-service
spec:
  ports:
    - port: 6379
      name: client
      targetPort: 6379
    - port: 16379
      name: gossip
      targetPort: 16379
  selector:
    app: redis-cluster

4、创建集群

kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 {end}')

你可能感兴趣的:(kubernetes,redis,容器)