Kubernetes Redis Cluster搭建

一、服务定义

先贴yaml,共四个文件:

  • 000.ns.yml
    定义一个namespace
  • 001.pv.yml
    定义6个PV,本例子中使用了三台服务器,每台服务器运行两个redis,使用了localStorage,可以根据具体情况进行调整。
  • 002.sts.yml
    定义StatefulSet,运行6个redis实例,组成一个集群。
  • 003.svc.yml
    定义Service,暴露Redis服务端口,此处需要注意使用了Headless Services,可以使用pod名称访问各个pod
    URI构成...svc.cluster.local
    例如:redis-app-0.redis-svc.redis-cluster.svc.cluster.local:6379 访问第一个POD

二、构建集群

启动完成之后,需要使用redis-cli将节点加入到集群中。

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

三、Yaml详细内容

以下是详细内容:

  • 000.ns.yml
apiVersion: v1
kind: Namespace
metadata:
  name: redis-cluste
  • 001.pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node001-01
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis01
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node001
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node001-02
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis02
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node001
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node002-01
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis01
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node002
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node002-02
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis02
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node002
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node003-01
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis01
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node003
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-redis-node003-02
  labels:
    pv: pv-redis
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /k8s-pv/redis02
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node003
  • 002.sts.yml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster
  namespace: redis-cluster
data:
  update-node.sh: |
    #!/bin/sh
    REDIS_NODES="/data/nodes.conf"
    sed -i -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${REDIS_NODES}
    exec "$@"
  redis.conf: |+
    cluster-enabled yes
    cluster-require-full-coverage no
    cluster-node-timeout 15000
    cluster-config-file /data/nodes.conf
    cluster-migration-barrier 1
    appendonly yes
    protected-mode no
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-app
  namespace: redis-cluster
spec:
  serviceName: redis-svc
  replicas: 6
  selector:
    matchLabels:
      app: redis-cluster
  template:
    metadata:
      labels:
        app: redis-cluster
    spec:
      containers:
      - name: redis
        image: redis:7
        ports:
        - containerPort: 6379
          name: client
        - containerPort: 16379
          name: gossip
        command: ["/conf/update-node.sh", "redis-server", "/conf/redis.conf"]
        env:
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        volumeMounts:
        - name: conf
          mountPath: /conf
          readOnly: false
        - name: data
          mountPath: /data
          readOnly: false
      volumes:
      - name: conf
        configMap:
          name: redis-cluster
          defaultMode: 0755
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
      storageClassName: local-storage
  • 003.svc.yml
---
apiVersion: v1
kind: Service
metadata:
  name: redis-svc
  namespace: redis-cluster
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - port: 6379
    targetPort: 6379
    name: client
  - port: 16379
    targetPort: 16379
    name: gossip
  selector:
    app: redis-cluster

你可能感兴趣的:(Kubernetes Redis Cluster搭建)