k8s用StatefulSet部署redis

redis-config.yaml (配置文件)

bash 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
data:
  redis.conf: |

    # Redis general configuration

​    bind 0.0.0.0
​    protected-mode no
​    port 6379
​    dir /data
​    appendonly yes

  sentinel.conf: |
    sentinel monitor mymaster redis-master 6379 2
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 10000
    sentinel parallel-syncs mymaster 1

redis-master-statefulset.yaml (主)

bash 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-master
spec:
  serviceName: "redis-master"
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: master
  template:
    metadata:
      labels:
        app: redis
        role: master
    spec:
      containers:
      - name: redis
        image: redis:5.0.7
        command: ["redis-server", "/data/redis.conf"]
        volumeMounts:
        - name: redis-data
          mountPath: /data
        - name: config
          mountPath: /data/redis.conf
          subPath: redis.conf
      volumes:
      - name: config
        configMap:
          name: redis-config
  volumeClaimTemplates:

  - metadata:
    name: redis-data
    spec:
      storageClassName: "nfs-client-storageclass"
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

redis-slave-statefulset.yaml (从)

bash 复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-slave
spec:
  serviceName: "redis-slave"
  replicas: 2
  selector:
    matchLabels:
      app: redis
      role: slave
  template:
    metadata:
      labels:
        app: redis
        role: slave
    spec:
      containers:
      - name: redis
        image: redis:5.0.7
        command: ["redis-server", "/data/redis.conf", "--slaveof", "redis-master", "6379"]
        volumeMounts:
        - name: redis-data
          mountPath: /data
        - name: config
          mountPath: /data/redis.conf
          subPath: redis.conf
      volumes:
      - name: config
        configMap:
          name: redis-config
  volumeClaimTemplates:

  - metadata:
    name: redis-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "nfs-client-storageclass"
      resources:
        requests:
          storage: 1Gi

redis-service.yaml (端口)

bash 复制代码
apiVersion: v1
kind: Service
metadata:
  name: redis-master
spec:
  ports:

  - port: 6379
    targetPort: 6379
      selector:
    app: redis
    role: master

---

apiVersion: v1
kind: Service
metadata:
  name: redis-slave
spec:
  ports:

  - port: 6379
    targetPort: 6379
      selector:
    app: redis
    role: slave

---

apiVersion: v1
kind: Service
metadata:
  name: redis-sentinel
spec:
  ports:

  - port: 26379
    targetPort: 26379
      selector:
    app: redis
    role: sentinel

redis-sentinel-statefulset.yaml (哨兵)

bash 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-sentinel
spec:
  replicas: 3
  selector:
    matchLabels:
      app: redis
      role: sentinel
  template:
    metadata:
      labels:
        app: redis
        role: sentinel
    spec:
      # 使用 initContainer 复制并修改权限
      initContainers:
      - name: copy-config
        image: busybox:1.27
        command: ['sh', '-c', 'cp /data/sentinel.conf /tmp/sentinel.conf && chmod 644 /tmp/sentinel.conf']
        volumeMounts:
        - name: config
          mountPath: /data
        - name: sentinel-config
          mountPath: /tmp
     # Redis Sentinel 容器
       containers:
       - name: sentinel
         image: redis:5.0.7
         command: ["redis-sentinel", "/tmp/sentinel.conf"]
         volumeMounts:
         - name: sentinel-config
           mountPath: /tmp
      # 卷定义
       volumes:
       - name: config
         configMap:
           name: redis-config
       - name: sentinel-config
         emptyDir: {}
相关推荐
yohoo菜鸟11 分钟前
kubernetes简单入门实战
云原生·容器·kubernetes
哭哭啼3 小时前
Redis环境部署(主从模式、哨兵模式、集群模式)
数据库·redis·缓存
狂奔solar3 小时前
分享个好玩的,在k8s上部署web版macos
前端·macos·kubernetes
明志致远淡泊宁静4 小时前
记录一次服务器redis被入侵
运维·服务器·redis
WuMingf_4 小时前
redis
数据库·redis
精进攻城狮@7 小时前
Redis(value的数据类型)
数据库·redis
jwybobo20078 小时前
redis7.x源码分析:(3) dict字典
linux·redis
simpleGq8 小时前
Redis知识点整理 - 脑图
数据库·redis·缓存
@东辰8 小时前
【golang-技巧】-自定义k8s-operator-by kubebuilder
开发语言·golang·kubernetes
小安运维日记8 小时前
CKA认证 | Day3 K8s管理应用生命周期(上)
运维·云原生·容器·kubernetes·云计算·k8s