在 K8s 中经常需要将一些公用配置信息传入各个 Pod。
配置信息可以写在.toml 文件中,通过其内容作为 secret 挂载在 Pod 的 volume 下进行访问
直接上示例
common.toml 文件内容如下
[Mysql]
UserName = "developer"
Password = "123456"
IpHost = "192.168.112.11:8902"
DbName = "contract"
[etcd]
addrs = ["121.11.199.160:2379","121.11.199.161:2379","121.11.199.162:2379"]
执行命令将 common.toml 内容加载到 k8s 集群
kubectl create secret -n mytest generic common-config --from-file=./common.toml -o yaml --dry-run | kubectl apply -n mytest -f -
testconfig.yaml 内容如下
apiVersion: v1
kind: Pod
metadata:
name: configtest-pod
spec:
containers:
- name: configtest-container
image: busybox
command: ["sh", "-c"]
args:
- while true; do
if [[-e /etc/config/common.toml]]; then
echo -en '\n\n'; cat /etc/config/common.toml;
fi;
sleep 5;
done;
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
secret:
secretName: common-config
items:
- key: common.toml
path: common.toml
生成 Pod
kubectl apply -f testconfig.yaml -n mytest
查看 Pod
kubectl get pods -n mytest
NAME READY STATUS RESTARTS AGE
configtest-pod 1/1 Running 0 66s
进入 Pod
kubectl -it exec configtest-pod -n mytest /bin/sh
# ls /etc/config/
common.toml
# cat /etc/config/common.toml
[Mysql]
UserName = "developer"
Password = "123456"
IpHost = "192.168.112.11:8902"
DbName = "contract"
[etcd]
addrs = ["121.11.199.160:2379","121.11.199.161:2379","121.11.199.162:2379"]
common.toml 文件已经挂载在 /etc/config 下