$npx -y skills add chaterm/terminal-skills --skill configmap-secretKubernetes ConfigMap 与 Secret
| 1 | # ConfigMap 与 Secret |
| 2 | |
| 3 | ## 概述 |
| 4 | 配置管理、敏感信息处理等技能。 |
| 5 | |
| 6 | ## ConfigMap |
| 7 | |
| 8 | ### 创建 ConfigMap |
| 9 | |
| 10 | ```bash |
| 11 | # 从字面值创建 |
| 12 | kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2 |
| 13 | |
| 14 | # 从文件创建 |
| 15 | kubectl create configmap my-config --from-file=config.properties |
| 16 | kubectl create configmap my-config --from-file=my-key=config.properties |
| 17 | |
| 18 | # 从目录创建 |
| 19 | kubectl create configmap my-config --from-file=config-dir/ |
| 20 | |
| 21 | # 从环境文件创建 |
| 22 | kubectl create configmap my-config --from-env-file=env.properties |
| 23 | ``` |
| 24 | |
| 25 | ### ConfigMap YAML |
| 26 | ```yaml |
| 27 | apiVersion: v1 |
| 28 | kind: ConfigMap |
| 29 | metadata: |
| 30 | name: my-config |
| 31 | data: |
| 32 | # 简单键值对 |
| 33 | database_url: "mysql://localhost:3306/mydb" |
| 34 | log_level: "info" |
| 35 | |
| 36 | # 多行配置文件 |
| 37 | nginx.conf: | |
| 38 | server { |
| 39 | listen 80; |
| 40 | server_name localhost; |
| 41 | location / { |
| 42 | root /usr/share/nginx/html; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | # JSON 配置 |
| 47 | config.json: | |
| 48 | { |
| 49 | "debug": true, |
| 50 | "port": 8080 |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | ### 使用 ConfigMap |
| 55 | |
| 56 | #### 环境变量方式 |
| 57 | ```yaml |
| 58 | spec: |
| 59 | containers: |
| 60 | - name: app |
| 61 | env: |
| 62 | # 单个键 |
| 63 | - name: DATABASE_URL |
| 64 | valueFrom: |
| 65 | configMapKeyRef: |
| 66 | name: my-config |
| 67 | key: database_url |
| 68 | # 所有键 |
| 69 | envFrom: |
| 70 | - configMapRef: |
| 71 | name: my-config |
| 72 | ``` |
| 73 | |
| 74 | #### 挂载为文件 |
| 75 | ```yaml |
| 76 | spec: |
| 77 | containers: |
| 78 | - name: app |
| 79 | volumeMounts: |
| 80 | - name: config-volume |
| 81 | mountPath: /etc/config |
| 82 | volumes: |
| 83 | - name: config-volume |
| 84 | configMap: |
| 85 | name: my-config |
| 86 | # 可选:指定特定键 |
| 87 | items: |
| 88 | - key: nginx.conf |
| 89 | path: nginx.conf |
| 90 | ``` |
| 91 | |
| 92 | #### 挂载为单个文件(不覆盖目录) |
| 93 | ```yaml |
| 94 | spec: |
| 95 | containers: |
| 96 | - name: app |
| 97 | volumeMounts: |
| 98 | - name: config-volume |
| 99 | mountPath: /etc/app/config.json |
| 100 | subPath: config.json |
| 101 | volumes: |
| 102 | - name: config-volume |
| 103 | configMap: |
| 104 | name: my-config |
| 105 | ``` |
| 106 | |
| 107 | ## Secret |
| 108 | |
| 109 | ### 创建 Secret |
| 110 | |
| 111 | ```bash |
| 112 | # 从字面值创建 |
| 113 | kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret123 |
| 114 | |
| 115 | # 从文件创建 |
| 116 | kubectl create secret generic my-secret --from-file=ssh-privatekey=~/.ssh/id_rsa |
| 117 | |
| 118 | # TLS Secret |
| 119 | kubectl create secret tls tls-secret --cert=cert.pem --key=key.pem |
| 120 | |
| 121 | # Docker Registry Secret |
| 122 | kubectl create secret docker-registry regcred \ |
| 123 | --docker-server=registry.example.com \ |
| 124 | --docker-username=user \ |
| 125 | --docker-password=password \ |
| 126 | --docker-email=user@example.com |
| 127 | ``` |
| 128 | |
| 129 | ### Secret YAML |
| 130 | ```yaml |
| 131 | apiVersion: v1 |
| 132 | kind: Secret |
| 133 | metadata: |
| 134 | name: my-secret |
| 135 | type: Opaque |
| 136 | data: |
| 137 | # Base64 编码 |
| 138 | username: YWRtaW4= |
| 139 | password: c2VjcmV0MTIz |
| 140 | --- |
| 141 | # 使用 stringData(自动编码) |
| 142 | apiVersion: v1 |
| 143 | kind: Secret |
| 144 | metadata: |
| 145 | name: my-secret |
| 146 | type: Opaque |
| 147 | stringData: |
| 148 | username: admin |
| 149 | password: secret123 |
| 150 | ``` |
| 151 | |
| 152 | ### Secret 类型 |
| 153 | ```yaml |
| 154 | # Opaque(默认) |
| 155 | type: Opaque |
| 156 | |
| 157 | # TLS |
| 158 | type: kubernetes.io/tls |
| 159 | data: |
| 160 | tls.crt: <base64> |
| 161 | tls.key: <base64> |
| 162 | |
| 163 | # Docker Registry |
| 164 | type: kubernetes.io/dockerconfigjson |
| 165 | data: |
| 166 | .dockerconfigjson: <base64> |
| 167 | |
| 168 | # Basic Auth |
| 169 | type: kubernetes.io/basic-auth |
| 170 | data: |
| 171 | username: <base64> |
| 172 | password: <base64> |
| 173 | |
| 174 | # SSH Auth |
| 175 | type: kubernetes.io/ssh-auth |
| 176 | data: |
| 177 | ssh-privatekey: <base64> |
| 178 | ``` |
| 179 | |
| 180 | ### 使用 Secret |
| 181 | |
| 182 | #### 环境变量方式 |
| 183 | ```yaml |
| 184 | spec: |
| 185 | containers: |
| 186 | - name: app |
| 187 | env: |
| 188 | - name: DB_PASSWORD |
| 189 | valueFrom: |
| 190 | secretKeyRef: |
| 191 | name: my-secret |
| 192 | key: password |
| 193 | envFrom: |
| 194 | - secretRef: |
| 195 | name: my-secret |
| 196 | ``` |
| 197 | |
| 198 | #### 挂载为文件 |
| 199 | ```yaml |
| 200 | spec: |
| 201 | containers: |
| 202 | - name: app |
| 203 | volumeMounts: |
| 204 | - name: secret-volume |
| 205 | mountPath: /etc/secrets |
| 206 | readOnly: true |
| 207 | volumes: |
| 208 | - name: secret-volume |
| 209 | secret: |
| 210 | secretName: my-secret |
| 211 | defaultMode: 0400 |
| 212 | ``` |
| 213 | |
| 214 | #### 镜像拉取凭证 |
| 215 | ```yaml |
| 216 | spec: |
| 217 | imagePullSecrets: |
| 218 | - name: regcred |
| 219 | containers: |
| 220 | - name: app |
| 221 | image: registry.example.com/myapp:latest |
| 222 | ``` |
| 223 | |
| 224 | ## 操作命令 |
| 225 | |
| 226 | ```bash |
| 227 | # 查看 ConfigMap |
| 228 | kubectl get configmap |
| 229 | kubectl describe configmap my-config |
| 230 | kubectl get configmap my-config -o yaml |
| 231 | |
| 232 | # 查看 Secret |
| 233 | kubectl get secret |
| 234 | kubectl describe secret my-secret |
| 235 | kubectl get secret my-secret -o yaml |
| 236 | |
| 237 | # 解码 Secret |
| 238 | kubectl get secret my-secret -o jsonpath='{.data.password}' | base64 -d |
| 239 | |
| 240 | # 编辑 |
| 241 | kubectl edit configmap my-config |
| 242 | kubectl edit secret my-secret |
| 243 | |
| 244 | # 删除 |
| 245 | kubectl delete configmap my-config |
| 246 | kubectl delete secret my-secret |
| 247 | ``` |
| 248 | |
| 249 | ## 常见场景 |
| 250 | |
| 251 | ### 场景 1:应用配置热更新 |
| 252 | ```yaml |
| 253 | # 使用 ConfigMap 挂载(自动更新) |
| 254 | spec: |
| 255 | containers: |
| 256 | - name: app |
| 257 | volumeMounts: |
| 258 | - name: config |
| 259 | mountPath: /etc/config |
| 260 | volumes: |
| 261 | - name: config |
| 262 | configMap: |
| 263 | name: my-config |
| 264 | # 注意:subPath 挂载不会自动更新 |
| 265 | ``` |
| 266 | |
| 267 | ### 场景 2:多环境配置 |
| 268 | ```bash |
| 269 | # 创建不同环境的 ConfigMap |
| 270 | kubectl create configmap app-config-dev --from-file=config-dev/ |
| 271 | kubectl create configmap app-config-prod --from-file=config-prod/ |
| 272 | |
| 273 | # 在 Deployment 中引用 |
| 274 | # 通过 Kustomize 或 Helm 管理不同环境 |
| 275 | ``` |
| 276 | |
| 277 | ### 场景 3:外部 Secret 管理 |
| 278 | ```yaml |
| 279 | # 使用 External Secrets Operator |
| 280 | apiVersion: external-secrets.io/v1beta1 |
| 281 | kind: ExternalSecret |
| 282 | metadata: |
| 283 | name: my-external-secret |
| 284 | spec: |
| 285 | refreshInterval: 1h |
| 286 | secretStoreRef: |
| 287 | name: aws-secrets-manager |
| 288 | kind: SecretStore |
| 289 | target: |
| 290 | name: my-secret |
| 291 | da |