Namespace

apiVersion: v1
kind: Namespace
metadata:
  name: base

pvc 搞6个

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-0
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs
--- 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-1
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs
--- 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-2
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs
--- 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-3
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs
--- 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-4
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-redis-app-5
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: "30Gi"
  volumeName: 
  storageClassName: nfs

configmap-redis配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-conf
data:
  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

headless service

apiVersion: v1
kind: Service
metadata:
  name: redis-service
  labels:
    app: redis
spec:
  ports:
  - name: redis-port
    port: 6379
  clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster

StatefulSet-redis

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: redis-app
spec:
  serviceName: "redis-service"
  replicas: 6
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      terminationGracePeriodSeconds: 20
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                #已经存在Redis的Node上尽量不要再分配Redis Pod
                - key: app
                  operator: In
                  values:
                  - redis
              topologyKey: kubernetes.io/hostname
      containers:
      - name: redis
        image: "redis"
        command:
          - "redis-server"                  #redis启动命令
        args:
          - "/etc/redis/redis.conf"         #redis-server后面跟的参数,换行代表空格
          - "--protected-mode"              #允许外网访问
          - "no"
        # command: redis-server /etc/redis/redis.conf --protected-mode no
        resources:                          #资源
          requests:                         #请求的资源
            cpu: "100m"                     #m代表千分之,相当于0.1 个cpu资源
            memory: "100Mi"                 #内存100m大小
        ports:
            - name: redis
              containerPort: 6379
              protocol: "TCP"
            - name: cluster
              containerPort: 16379
              protocol: "TCP"
        volumeMounts:
          - name: "redis-conf"              #挂载configmap生成的文件
            mountPath: "/etc/redis"         #挂载到哪个路径下
          - name: "redis-data"              #挂载持久卷的路径
            mountPath: "/var/lib/redis"
      volumes:
      - name: "redis-conf"                  #引用configMap卷
        configMap:
          name: "redis-conf"
          items:
            - key: "redis.conf"             #创建configMap指定的名称
              path: "redis.conf"            #里面的那个文件--from-file参数后面的文件
  volumeClaimTemplates:
  - metadata:
      name: redis-data
    spec:
      accessModes: ["ReadWriteMany"]
      resources:
        requests:
          storage: 30Gi

初始化redis集群

#master执行
kubectl -n base run -i --tty centos --image=centos --restart=Never /bin/bash
yum install -y epel-release && \
yum install -y redis-trib.noarch bind-utils  

#base改为你自己namespace

[root@centos /]# redis-trib create --replicas 1 \
> `dig +short redis-app-0.redis-service.base.svc.cluster.local`:6379 \
> `dig +short redis-app-1.redis-service.base.svc.cluster.local`:6379 \
> `dig +short redis-app-2.redis-service.base.svc.cluster.local`:6379 \
> `dig +short redis-app-3.redis-service.base.svc.cluster.local`:6379 \
> `dig +short redis-app-4.redis-service.base.svc.cluster.local`:6379 \
> `dig +short redis-app-5.redis-service.base.svc.cluster.local`:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.233.64.14:6379
10.233.64.17:6379
10.233.64.15:6379
Adding replica 10.233.64.18:6379 to 10.233.64.14:6379
Adding replica 10.233.64.16:6379 to 10.233.64.17:6379
Adding replica 10.233.64.19:6379 to 10.233.64.15:6379
M: 0448477c3cd7c2784eb1d435e6a4e21394b76029 10.233.64.14:6379
   slots:0-5460 (5461 slots) master
M: 6d2fd74685c58d7fbc49feb9bcb5e07070780e1a 10.233.64.17:6379
   slots:5461-10922 (5462 slots) master
M: 4a9fc51adc5f0e70b0bf278f6ddf253928f50981 10.233.64.15:6379
   slots:10923-16383 (5461 slots) master
S: 49510ad993931141e16663999e555f5d40fa0a63 10.233.64.18:6379
   replicates 0448477c3cd7c2784eb1d435e6a4e21394b76029
S: 2de2930e1f17435dfa9d9df5dff60cf979cb43c2 10.233.64.16:6379
   replicates 6d2fd74685c58d7fbc49feb9bcb5e07070780e1a
S: 03f18eb3e03973117f8ebde43511ca55648cda99 10.233.64.19:6379
   replicates 4a9fc51adc5f0e70b0bf278f6ddf253928f50981
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 10.233.64.14:6379)
M: 0448477c3cd7c2784eb1d435e6a4e21394b76029 10.233.64.14:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 2de2930e1f17435dfa9d9df5dff60cf979cb43c2 10.233.64.16:6379@16379
   slots: (0 slots) slave
   replicates 6d2fd74685c58d7fbc49feb9bcb5e07070780e1a
S: 49510ad993931141e16663999e555f5d40fa0a63 10.233.64.18:6379@16379
   slots: (0 slots) slave
   replicates 0448477c3cd7c2784eb1d435e6a4e21394b76029
M: 4a9fc51adc5f0e70b0bf278f6ddf253928f50981 10.233.64.15:6379@16379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 6d2fd74685c58d7fbc49feb9bcb5e07070780e1a 10.233.64.17:6379@16379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 03f18eb3e03973117f8ebde43511ca55648cda99 10.233.64.19:6379@16379
   slots: (0 slots) slave
   replicates 4a9fc51adc5f0e70b0bf278f6ddf253928f50981
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

外部访问

apiVersion: v1
kind: Service
metadata:
  name: redis-access-service
  labels:
    app: redis
spec:
  type: NodePort
  ports:
    - name: tcp
      port: 6379
      nodePort: 30000
      targetPort: 6379
  selector:
    app: redis
    appCluster: redis-cluster

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code