示例如何将配置信息传入Pod

在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下

你可能感兴趣的:(kubernetes)