k8s 部署 redis

创建部署文件

bash 复制代码
vim redis.yaml

添加如下内容:

bash 复制代码
apiVersion: v1
kind: Namespace
metadata:
  name: redis
---
apiVersion: v1
kind: Secret
metadata:
  name: redis-password
  namespace: redis
type: Opaque
data:
  password: d2d3cmhnZWE= # 建议生产环境使用更复杂的密码
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: redis
data:
  redis.conf: |
    # 这里放置其他Redis配置,但移除了requirepass行
    # 密码将通过命令行动态传入
    maxmemory 256mb
    maxmemory-policy allkeys-lru
    appendonly yes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:6.2
          ports:
            - containerPort: 6379
          env:
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-password
                  key: password
          # 主要修改点在这里:通过命令行参数传递密码
          command: 
            - "redis-server"
            - "/usr/local/etc/redis/redis.conf"
            - "--requirepass"
            - "$(REDIS_PASSWORD)" # 这里会正确解析环境变量
          volumeMounts:
            - name: redis-config
              mountPath: /usr/local/etc/redis/redis.conf
              subPath: redis.conf
          resources:
            requests:
              memory: "64Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          readinessProbe:
            exec:
              command:
                - redis-cli
                - -a
                - $(REDIS_PASSWORD)
                - ping
            initialDelaySeconds: 5
            periodSeconds: 10
      volumes:
        - name: redis-config
          configMap:
            name: redis-config
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  namespace: redis
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: 6379
      targetPort: 6379
  # type: ClusterIP 是默认值,如果只在集群内部访问,无需修改
  # 如果要从集群外部访问,可以改为 NodePort 或 LoadBalancer
  # type: LoadBalancer

启动服务

bash 复制代码
kubectl apply -f redis.yaml

查看服务

bash 复制代码
kubectl get pods -n redis
相关推荐
查尔斯-BUG万象集12 小时前
解决 OceanBase CE 启动失败:OBD-2002: Failed to start 0.0.0.0 observer
docker·k8s·oceanbase
极客小云20 小时前
【Dockerfile 编写最佳实践:优化镜像构建与层缓存】
缓存·docker·k8s
汪碧康2 天前
【k8s-1.34.2安装部署】三.etcd-v3.6.6 TLS版集群安装
容器·kubernetes·k8s·etcd·dashboard·xkube·etcd集群部署
小石潭记丶4 天前
安装/配置Longhorn
k8s
ymf514 天前
DevOps平台部署K8s容器的Values值配置详解
k8s·devops
百锦再5 天前
UniApp与UniApp X:跨平台开发的范式革命与全面技术解析
服务器·ai·uni-app·k8s·core·net
新手小白*11 天前
Kubernetes 运维实战:etcd 备份恢复、集群升级与证书更新
k8s
百锦再11 天前
【无标题】
服务器·ai·k8s·京东云·core·net·云鼎
新手小白*13 天前
K8s-Calico 网络组件
k8s
幽弥千月17 天前
k8s离线环境下部署【calico】网络插件
k8s