apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-app
spec:
  selector:
    matchLabels:
      app: redis
  serviceName: "redis-service"
  replicas: 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: redis
        command:
          - "redis-server"  #redis启动命令
        args:
          - "/etc/redis/redis.conf"  #redis-server后面跟的参数,换行代表空格
          - "--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:  #进行pvc持久卷声明
  - metadata:
      name: redis-data
    spec:
      accessModes: [ "ReadWriteMany" ]
      storageClassName: "dg-nfs-storage"  #--注意这里是使用nfs storageClass，如果没有改默认的，可以忽略不写
      resources:
        requests:
          storage: 2Gi
