You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在现有NestJS部署的ConfigMap中添加Redis配置信息?

问题描述

我已经获取了Redis的URL和端口,现在有一个Deployment的ConfigMap,但不确定应该在何处以及如何添加Redis配置信息。我拿到了一个通用示例,但不清楚该插入到哪里,我认为可能是在容器部分,但不确定。

示例代码

- name: redis
  image: redis:latest  
  ports:
  - containerPort: xxxx
  resources: {}

现有Deployment配置

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: __APP__-__ENV__
  name: __APP__-__ENV__
  namespace: __PROJECT__
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: __APP__-__ENV__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: __APP__-__ENV__
    spec:
      containers:
      - image: __REGISTRY__/__APP__:__TAG__
        imagePullPolicy: Always
        name: __APP__
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        env:
        - name: APP
          value: __APP__
        - name: ENV
          value: __ENV__
        envFrom: 
        - secretRef:
            name: __APP__-__ENV__
        livenessProbe:
          tcpSocket:
            port: 3000
          initialDelaySeconds: 15
          periodSeconds: 20
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
解决方案

根据你的需求,分两种常见场景处理:

场景1:在Deployment中添加Redis Sidecar容器

如果你的目标是让每个应用Pod都附带一个Redis实例(sidecar模式),需要把示例中的Redis容器配置添加到Deployment的spec.template.spec.containers数组里,和现有应用容器同级。修改后的配置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: __APP__-__ENV__
  name: __APP__-__ENV__
  namespace: __PROJECT__
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: __APP__-__ENV__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: __APP__-__ENV__
    spec:
      containers:
      - image: __REGISTRY__/__APP__:__TAG__
        imagePullPolicy: Always
        name: __APP__
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        env:
        - name: APP
          value: __APP__
        - name: ENV
          value: __ENV__
        - name: REDIS_URL
          value: "redis://localhost:6379" # 同Pod内可直接用localhost访问sidecar
        envFrom: 
        - secretRef:
            name: __APP__-__ENV__
        livenessProbe:
          tcpSocket:
            port: 3000
          initialDelaySeconds: 15
          periodSeconds: 20
      # 新增Redis Sidecar容器
      - name: redis
        image: redis:latest  
        ports:
        - containerPort: 6379 # 替换为你的Redis端口
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

场景2:通过ConfigMap给应用传递独立Redis的配置

如果你的Redis是独立部署的(非sidecar),需要先把Redis的URL和端口存入ConfigMap,再让Deployment的应用容器读取这些配置:

1. 创建Redis配置的ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: __PROJECT__
data:
  REDIS_URL: "你的Redis地址"
  REDIS_PORT: "你的Redis端口"

2. 修改Deployment加载ConfigMap配置

在现有应用容器的envFrom中添加ConfigMap引用:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: __APP__-__ENV__
  name: __APP__-__ENV__
  namespace: __PROJECT__
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: __APP__-__ENV__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: __APP__-__ENV__
    spec:
      containers:
      - image: __REGISTRY__/__APP__:__TAG__
        imagePullPolicy: Always
        name: __APP__
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        env:
        - name: APP
          value: __APP__
        - name: ENV
          value: __ENV__
        envFrom: 
        - secretRef:
            name: __APP__-__ENV__
        # 新增:从ConfigMap加载Redis配置
        - configMapRef:
            name: redis-config
        livenessProbe:
          tcpSocket:
            port: 3000
          initialDelaySeconds: 15
          periodSeconds: 20
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

你也可以单独指定环境变量(更灵活):

env:
# ... 原有环境变量
- name: REDIS_URL
  valueFrom:
    configMapKeyRef:
      name: redis-config
      key: REDIS_URL
- name: REDIS_PORT
  valueFrom:
    configMapKeyRef:
      name: redis-config
      key: REDIS_PORT

内容的提问来源于stack exchange,提问作者itsjustcarlos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 10:15:15