Skip to content

Redis in Kubernetes

namespace.yaml

yaml
apiVersion: v1
kind: Namespace
metadata:
  name: common

redis-deployment.yaml

yaml
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: common
spec:
  type: ClusterIP
  ports:
    - port: 6379
  selector:
    app: redis
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pv-claim
  namespace: common
spec:
  storageClassName: local
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv-volume
  labels:
    type: local
spec:
  storageClassName: local
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/root/kubernetes/common/redis/redis_pv_volume" # 此目录要正确填写
---
kind: Secret
apiVersion: v1
metadata:
  name: common-redis-password
  namespace: common
  annotations:
    kubesphere.io/creator: admin
data:
  username: cm9vdA==
  password:  # 此处配置密码
type: kubernetes.io/basic-auth
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: common
spec:
  selector:
    matchLabels:
      app: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - image: redis:7.4.1
          name: redis
          command:
            - "redis-server"
            - "--requirepass"
            - "$(REDIS_PASSWORD)"
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: common-redis-password
                  key: password
          ports:
            - containerPort: 6379
              name: redis
              hostPort: 6379
          volumeMounts:
            - name: redis-persistent-storage
              mountPath: /data
      volumes:
        - name: redis-persistent-storage
          persistentVolumeClaim:
            claimName: redis-pv-claim