在K8s中常常须要将一些公用配置信息传入各个Pod。
配置信息能够写在.toml文件中,经过其内容做为secret挂载在Pod的volume下进行访问
直接上示例
common.toml文件内容以下sql
[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集群api
kubectl create secret -n mytest generic common-config --from-file=./common.toml -o yaml --dry-run | kubectl apply -n mytest -f -
testconfig.yaml内容以下app
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
生成Podcode
kubectl apply -f testconfig.yaml -n mytest
查看Podget
kubectl get pods -n mytest NAME READY STATUS RESTARTS AGE configtest-pod 1/1 Running 0 66s
进入Podit
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下io