k8s搭建redis集群

创建PV

# kubectl get pv
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                    STORAGECLASS   REASON   AGE
nfs1.10.0.0.11   5Gi        RWX            Recycle          Bound       keyida/consul-consul-0                           4h47m
nfs2.10.0.0.11   5Gi        RWX            Recycle          Bound       keyida/consul-consul-1                           4h47m
nfs3.10.0.0.11   5Gi        RWX            Recycle          Bound       keyida/consul-consul-2                           4h47m
nfs4.10.0.0.11   5Gi        RWX            Recycle          Available                                                    4h47m
nfs5.10.0.0.11   5Gi        RWX            Recycle          Available                                                    4h47m
nfs6.10.0.0.11   5Gi        RWX            Recycle          Available                                                    4h47m

创建Configmap

# vim redis.conf
appendonly yes      
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379
# kubectl create configmap redis-conf --from-file=redis.conf -n keyida
configmap/redis-conf created

创建headless_service

# vim headless.yml
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: keyida
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    port: 6379
  clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster
# kubectl create -f headless.yml
service/redis-service created

创建statefulset

# vim redis.yml
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: redis
  namespace: keyida
spec:
  serviceName: redis-service
  replicas: 3   # 一般为6,电脑虚拟机资源不够
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      terminationGracePeriodSeconds: 20
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - redis
              topologyKey: kubernetes.io/hostname
      containers:
      - name: redis
        image: 10.0.0.11:81/library/redis:latest
        imagePullPolicy: IfNotPresent
        command:
          - "redis-server"
        args:
          - "/etc/redis/redis.conf"
          - "--protected-mode"
          - "no"
        resources:
          requests:
            cpu: 200m
            memory: 200Mi
        ports:
            - name: redis
              containerPort: 6379
              protocol: TCP
            - name: cluster
              containerPort: 16379
              protocol: TCP
        volumeMounts:
          - name: redis-conf
            mountPath: /etc/redis
          - name: host-time
            mountPath: /etc/localtime                   
          - name: redis-data
            mountPath: /var/lib/redis
      volumes:
      - name: redis-conf
        configMap:
          name: redis-conf
          items:
            - key: redis.conf
              path: redis.conf
      - name: host-time
        hostPath:
          path: /etc/localtime  
  volumeClaimTemplates:
  - metadata:
      name: redis-data
      namespace: keyida
    spec:
      selector:
        matchLabels:
          pv: redis
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 2Gi
# kubectl apply -f redis.yml
查看是否创建相对的pvc
#  kubectl get pods -o wide -n keyida
NAME       READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
consul-0   1/1     Running   0          5h17m   10.244.3.5    node02   <none>           <none>
consul-1   1/1     Running   0          5h17m   10.244.1.11   node01   <none>           <none>
consul-2   1/1     Running   0          5h17m   10.244.2.6    node03   <none>           <none>
redis-0    1/1     Running   0          51s     10.244.1.20   node01   <none>           <none>
redis-1    1/1     Running   0          44s     10.244.3.10   node02   <none>           <none>
redis-2    1/1     Running   0          37s     10.244.2.8    node03   <none>           <none>

创建redis集群

# v=""
# for i in `kubectl get pod -n keyida -o wide |grep 'redis'| awk  '{print $6}' | grep -v IP`; do v="$v $i:6379";done
# echo $v
10.244.1.23:6379 10.244.3.28:6379 10.244.2.11:6379
# kubectl exec -ti redis-0 -n keyida -- redis-cli --cluster create $v --cluster-replicas 1
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.
command terminated with exit code 1
注意:redis集群需要三个master组成,需要6个节点,现在的节点创建不了集群,可以再增加三个node去创建redis节点

创建redis_svc

# vim redis_svc.yml
apiVersion: v1
kind: Service
metadata:
  name: redis-access-service
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    protocol: "TCP"
    port: 6379
    targetPort: 6379
  selector:
    app: redis
    appCluster: redis-cluster
# kubectl apply -f redis_svc.yml
service/redis-access-service created
# kubectl get svc redis-access-service -o wide -n keyida
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE   SELECTOR
redis-access-service   ClusterIP   10.254.163.102   <none>        6379/TCP   31s   app=redis,appCluster=redis-cluster
点赞

发表评论

电子邮件地址不会被公开。

2 × 1 =