配置资源管理
目录
Secret(密钥管理)
概念
Secret 类型
创建secrect
方法1:命令行创建
方法2:YAML 文件(Base64 编码)
使用secrect
方式1:挂载为文件
方式2:导入环境变量
ConfigMap(配置管理)
概念
创建 ConfigMap
使用 ConfigMap
方式1:作为环境变量注入
方式2:命令行参数使用
方式3:以 Volume 形式挂载
总结
Secret(密钥管理)
概念
**Secret** 用来保存敏感数据,如:
- 密码
- Token
- 密钥
Secret 类型
类型 | 说明 |
---|---|
kubernetes.io/service-account-token | Kubernetes 自动创建,用于访问 APIServer。Pod 默认挂载在 /run/secrets/kubernetes.io/serviceaccount 。 |
Opaque | 默认类型,用户自定义密码、密钥等(Base64 编码)。 |
kubernetes.io/dockerconfigjson | 存储私有 Docker Registry 的认证信息。 |
kubernetes.io/tls | 存储 SSL/TLS 证书与私钥。 |
官方文档:https://kubernetes.io/docs/concepts/configuration/secret/
创建secrect
方法1:命令行创建
echo -n 'zhangsan' > username.txt
echo -n 'abc1234' > password.txtkubectl create secret generic mysecret --from-file=username.txt --from-file=password.txt
查看
kubectl get secrets
kubectl describe secret mysecret
方法2:YAML 文件(Base64 编码)
[root@master01 opt]# echo -n "zhangsan" | base64 -w 0
emhhbmdzYW4=
[root@master01 opt]# echo -n "abc1234" | base64 -w 0
YWJjMTIzNA==# 编辑yaml文件
vim secret.yaml apiVersion: v1
kind: Secret
metadata:name: mysecret1
type: Opaque
data:username: emhhbmdzYW4=password: YWJjMTIzNA==# 执行脚本
kubectl apply -f secret.yaml
kubectl get secret mysecret1 -o yaml|grep user # 捕获user
kubectl get secret mysecret1 -o yaml|grep password # 捕获password
使用secrect
方式1:挂载为文件
vim use-secrect.yaml apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: "/etc/secrets"readOnly: truevolumes:- name: secretssecret:secretName: mysecret# 执行yaml文件
kubectl apply -f use-secrect.yaml # 查看/etc/secrets
kubectl exec -it mypod -- ls /etc/secrets
password.txt username.txt
方式2:导入环境变量
vim use-secrect.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: "/etc/secrets"readOnly: truevolumes:- name: secretssecret:secretName: mysecret
验证结果
kubectl apply -f use-secrect.yaml
kubectl exec -it mypod1 -- printenv | grep TEST
ConfigMap(配置管理)
概念
与 Secret 类似,但存储**非敏感配置数据**
创建 ConfigMap
#### 2.2.1 方法一:从目录创建
mkdir /opt/configmap/
touch /opt/configmap/game.properties
touch /opt/configmap/ui.propertieskubectl create configmap game-config --from-file=/opt/configmap/#### 2.2.2 方法二:从文件创建
kubectl create configmap game-config-2 \--from-file=/opt/configmap/game.properties \--from-file=/opt/configmap/ui.properties#### 2.2.3 方法三:使用字面值
kubectl create configmap special-config \--from-literal=special.how=very \--from-literal=special.type=good
使用 ConfigMap
方式1:作为环境变量注入
# 创建 env-config ConfigMap
kubectl create configmap env-config \--from-literal=APP_NAME=myapp \--from-literal=APP_ENV=production \--from-literal=LOG_LEVEL=info# yaml脚本
vim pod-configmap.yamlapiVersion: v1
kind: Pod
metadata:name: test-pod
spec:containers:- name: busyboximage: busybox:1.28.4command: ["/bin/sh", "-c", "env"]env:- name: SPECIAL_HOW_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.how- name: SPECIAL_TYPE_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.typeenvFrom:- configMapRef:name: env-configrestartPolicy: Never# 执行一下
kubectl apply -f pod-configmap.yaml
最后查一下log日志
kubectl logs test-pod
方式2:命令行参数使用
vim cat command-config.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod
spec:containers:- name: busyboximage: busybox:1.28.4command:- /bin/sh- -c- echo "$(SPECIAL_HOW_KEY) $(SPECIAL_TYPE_KEY)"env:- name: SPECIAL_HOW_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.how- name: SPECIAL_TYPE_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.typerestartPolicy: Never# 创建新 Pod
kubectl apply -f command-config.yaml# 查看日志:
kubectl logs test-pod
方式3:以 Volume 形式挂载
vim test-pod-volume.yamlapiVersion: v1
kind: Pod
metadata:name: test-pod-volume
spec:containers:- name: busyboximage: busybox:1.28.4command: ["/bin/sh", "-c", "sleep 3600"]volumeMounts:- name: config-volumemountPath: /etc/configvolumes:- name: config-volumeconfigMap:name: special-configrestartPolicy: Neverkubectl apply -f test-pod-volume.yaml
验证
kubectl exec -it test-pod-volume -- ls /etc/config/
总结
项目 | Secret | ConfigMap |
---|---|---|
存储内容 | 敏感信息(密码、密钥) | 普通配置信息 |
数据编码 | Base64 | 纯文本 |
使用方式 | Volume、Env、镜像拉取凭证 | Volume、Env、命令参数 |
是否自动更新 | Volume 延迟更新,Env 不会 | Volume 延迟更新,Env 不会 |
安全性 | 高(需 RBAC 控制) | 普通 |
应用场景 | 凭据管理 | 应用配置管理 |