MicroK8s Redis 部署

MicroK8s Redis 部署笔记(整合 PVC 单文件版)

本文档记录了在 microk8s 上从创建 Secret 到部署 Redis 的完整流程,并将 PVC、Deployment、Service 整合到一个 YAML 文件 redis-deployment.yaml 中,方便一次性部署。


1️⃣ 启用 hostpath-storage

microk8s enable hostpath-storage
  • microk8s 自带 hostPath StorageClass (microk8s-hostpath) 用于持久化存储。

2️⃣ 创建 Secret(可选)

如果你想为 Redis 设置 密码认证,可以通过 Kubernetes Secret 管理。

Secret YAML 片段

apiVersion: v1
kind: Secret
metadata:
  name: redis-secret
  namespace: database
type: Opaque
data:
  redis-password: cmVkaXNfcGFzc3dvcmQ=  # base64 编码的密码

命令

microk8s kubectl apply -f redis-secret.yaml
microk8s kubectl get secret -n database

3️⃣ 整合 PVC + Deployment + Service (redis-deployment.yaml)

# ===================== PVC: Redis 数据持久化 =====================
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc
  namespace: database
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: microk8s-hostpath
---
# ===================== Deployment: Redis =====================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-instance
  namespace: database
  labels:
    app: redis-instance
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis-instance
  template:
    metadata:
      labels:
        app: redis-instance
    spec:
      containers:
      - name: redis
        image: redis:7.0
        command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
        args:
          - "--requirepass"
          - "$(REDIS_PASSWORD)"
        env:
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-secret
              key: redis-password
        ports:
        - containerPort: 6379
          name: redis
        volumeMounts:
        - name: redis-storage
          mountPath: /data
        - name: redis-config
          mountPath: /usr/local/etc/redis/redis.conf
          subPath: redis.conf
      volumes:
      - name: redis-storage
        persistentVolumeClaim:
          claimName: redis-pvc
      - name: redis-config
        configMap:
          name: redis-config
---
# ===================== ConfigMap: Redis 配置 =====================
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: database
data:
  redis.conf: |
    appendonly yes
    dir /data
---
# ===================== Service: 对外暴露 =====================
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: database
spec:
  selector:
    app: redis-instance
  ports:
  - port: 6379
    targetPort: 6379
    nodePort: 30379
  type: NodePort

命令

microk8s kubectl apply -f redis-deployment.yaml
microk8s kubectl get pvc -n database
microk8s kubectl get pods -n database
microk8s kubectl get svc -n database
  • PVC 状态为 Bound 表示持久化成功。
  • Pod 状态为 Running 表示 Redis 已启动。
  • Service 显示 NodePort 端口,可用宿主机 IP + 端口外部访问 Redis。

外部访问示例

redis-cli -h <宿主机IP> -p 30379 -a <redis_password>

4️⃣ 流程总结

  1. 启用 hostpath-storage 插件。
  2. 创建 Secret 管理 Redis 密码(可选)。
  3. 创建单文件 YAML redis-deployment.yaml,整合 PVC、Deployment、ConfigMap 和 Service。
  4. 应用 YAML 文件部署 Redis。
  5. 检查 Pod 和 Service 状态。
  6. 使用 NodePort 端口从外部访问 Redis。

✅ 这样就可以一次性完成 Redis 的部署、持久化存储、配置管理以及外部访问,和你的 MySQL 部署方式保持一致。

无标签