示例如何將配置信息傳入Pod

在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

相關文章
相關標籤/搜索