1.直接部署一个pod

需要挂载存储款, 可参考 之前文章设置 https://blog.csdn.net/weimeibuqieryu/article/details/140183843

2.部署yaml

先创建命名空间ruoyi, 有就不用创建了

kubectl create namespace ruoyi

创建部署文件 redis-deploy.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: redis-pv
  namespace: ruoyi #使用ns ruoyi
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi  #存储容量为 5 GiB
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /nfs/redis        #使用 NFS 服务器的路径和地址, 使用自己定义的
    server: 139.159.140.xxx
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: redis-pvc
  namespace: ruoyi
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: ruoyi
data:
  redis.conf: |-
    bind 0.0.0.0
    protected-mode no
    appendonly yes
---
apiVersion: v1
kind: Secret
metadata:
  name: redis-secret
  namespace: ruoyi
type: Opaque
stringData:
  password: password   #明文密码, 记得改为自己的
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: ruoyi
spec:
  replicas: 1  
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      nodeSelector:
        node-role.kubernetes.io/master: ""  # 指定 Pod 运行在 master 节点
      containers:
        - name: redis
          image: redis:6.2.7
          ports:
            - containerPort: 6379
          args:
            - "--requirepass"
            - "$(REDIS_PASSWORD)"
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-secret
                  key: password
          volumeMounts:    # 挂载 PVC 和 ConfigMap
            - name: redis-data
              mountPath: /data
            - name: redis-config
              mountPath: /redis.conf
              subPath: redis.conf
      volumes:
        - name: redis-data
          persistentVolumeClaim:
            claimName: redis-pvc
        - name: redis-config
          configMap:
            name: redis-config
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: ruoyi
spec:
  selector:
    app: redis
  ports:
    - port: 6379
      targetPort: 6379
      nodePort: 30379 # 节点端口 30379, 可外部访问
  type: NodePort   # 暴露服务为 NodePort 类型

部署该服务

kubectl apply -f redis-deploy.yaml

就可以使用节点的ip+30379 来访问了. 公网记得防火墙的端口要打开

Logo

快速构建 Web 应用程序

更多推荐