目标

安装3个master,3个slave的mysql集群,并且,在master故障时,slave可以自动升级为master的高可用机器

环境

  • kubernetes集群
master  10.115.223.121
node1   10.115.223.122
node2   10.115.223.123

手动启动一个master pod

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: redis
    redis-sentinel: "true"
    role: master
  name: redis-master
  namespace: test
spec:
  containers:
    - name: master
      image: k8s.gcr.io/redis:v1
      env:
        - name: MASTER
          value: "true"
      ports:
        - containerPort: 6379
      resources:
        limits:
          cpu: "0.1"
      volumeMounts:
        - mountPath: /redis-master-data
          name: data
    - name: sentinel
      image: kubernetes/redis:v1
      env:
        - name: SENTINEL
          value: "true"
      ports:
        - containerPort: 26379
  volumes:
    - name: data
      emptyDir: {}

启动sentinel service

apiVersion: v1
kind: Service
metadata:
  labels:
    name: sentinel
    role: service
  name: redis-sentinel
  namespace: test
spec:
  ports:
    - port: 26379
      targetPort: 26379
  selector:
    redis-sentinel: "true"

创建redis副本集

# master 副本集
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis
  namespace: test
spec:
  replicas: 1
  selector:
    name: redis
  template:
    metadata:
      labels:
        name: redis
        role: master
    spec:
      containers:
      - name: redis
        image: k8s.gcr.io/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data

#sentinel 副本集        
apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-sentinel
spec:
  replicas: 1
  selector:
    redis-sentinel: "true"
  template:
    metadata:
      labels:
        name: redis-sentinel
        redis-sentinel: "true"
        role: sentinel
    spec:
      containers:
      - name: sentinel
        image: k8s.gcr.io/redis:v1
        env:
          - name: SENTINEL
            value: "true"
        ports:
          - containerPort: 26379

扩大副本数量

kubectl scale rc redis --replicas=3 -n test
kubectl scale rc redis-sentinel --replicas=3 -n test

删除手工创建的redis pod

kubectl delete pods redis-master -n test

master副本集发现pod数量不足,会创建新的副本

验证

[root@node121 ~]# kubectl get pods -n test
NAME                               READY     STATUS    RESTARTS   AGE
redis-gp69z                        1/1       Running   0          4m
redis-nkjbm                        1/1       Running   0          15m
redis-q2tkp                        1/1       Running   0          15m
redis-sentinel-bvmfb               1/1       Running   0          32m
redis-sentinel-crvgm               1/1       Running   0          15m
redis-sentinel-jplm7               1/1       Running   0          15m

kubectl exec -it redis-gp69z sh  -n test 

# redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>