Linux

envsubst——将环境变量传递给文件

2018-12-19  本文已影响0人  桩i

将当前脚本中的变量传入到文件中

简单示例

$ cat deploy.sh
export NAMENODE_HOST=${NAMENODE_HOST:-`hostname -I|awk '{print $1}'`}
export KUBE_CLUSTER_DNS_IP=${KUBE_CLUSTER_DNS_IP:-'10.96.0.10'}
export K8S_CLUSTER_DOMAIN=${K8S_CLUSTER_DOMAIN:-'cluster.k8s.local'}
export DOCKER_REGISTRY=${DOCKER_REGISTRY:-'10.190.144.32:5000'}
envsubst < /data/kube_dns.yaml 
cat /data/kube_dns.yaml
$ cat /data/kube_dns.yaml
#!/bin/bash
echo ${NAMENODE_HOST}
echo ${KUBE_CLUSTER_DNS_IP}
echo ${K8S_CLUSTER_DOMAIN}
echo ${DOCKER_REGISTRY}
]# bash  deploy.sh 
#!/bin/bash
echo 192.168.19.139
echo 10.96.0.10
echo cluster.k8s.local
echo 10.190.144.32:5000
#!/bin/bash
echo ${NAMENODE_HOST}
echo ${KUBE_CLUSTER_DNS_IP}
echo ${K8S_CLUSTER_DOMAIN}
echo ${DOCKER_REGISTRY}

实战演练:

$ cat deploy.sh
export NAMENODE_HOST=${NAMENODE_HOST:-`hostname -I|awk '{print $1}'`}
export KUBE_CLUSTER_DNS_IP=${KUBE_CLUSTER_DNS_IP:-'10.96.0.10'}
export K8S_CLUSTER_DOMAIN=${K8S_CLUSTER_DOMAIN:-'cluster.k8s.local'}
export DOCKER_REGISTRY=${DOCKER_REGISTRY:-'10.190.144.32:5000'}
#envsubst < /data/kube_dns.yaml | kubectl create -f -
envsubst < /data/kube_dns.yaml 
cat /data/kube_dns.yaml

部分脚本内容

$ cat /data/kube_dns.yaml
kind: Service
metadata:
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: $KUBE_CLUSTER_DNS_IP
   spec:
      containers:
      - name: kubedns
        image: ${DOCKER_REGISTRY}/k8s-dns-kube-dns-amd64:1.14.7
       readinessProbe:
          httpGet:
            path: /readiness
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 3
          timeoutSeconds: 5
        args:
        - --domain=${K8S_CLUSTER_DOMAIN}.
       env:
       - name: AIDU_KUBERNETES_LOG_ENDPOINT
          value: ${NAMENODE_HOST}:8070

以上俩个脚本,deploy.sh脚本中定义了一些变量,需要替换kube_dns.yaml文件中写好的变量,然后执行。

]# bash -xv deploy.sh 
export NAMENODE_HOST=${NAMENODE_HOST:-`hostname -I|awk '{print $1}'`}
hostname -I|awk '{print $1}'
++ awk '{print $1}'
++ hostname -I
+ export NAMENODE_HOST=192.168.19.139
+ NAMENODE_HOST=192.168.19.139
export KUBE_CLUSTER_DNS_IP=${KUBE_CLUSTER_DNS_IP:-'10.96.0.10'}
+ export KUBE_CLUSTER_DNS_IP=10.96.0.10
+ KUBE_CLUSTER_DNS_IP=10.96.0.10
export K8S_CLUSTER_DOMAIN=${K8S_CLUSTER_DOMAIN:-'cluster.k8s.local'}
+ export K8S_CLUSTER_DOMAIN=cluster.k8s.local
+ K8S_CLUSTER_DOMAIN=cluster.k8s.local
export DOCKER_REGISTRY=${DOCKER_REGISTRY:-'10.190.144.32:5000'}
+ export DOCKER_REGISTRY=10.190.144.32:5000
+ DOCKER_REGISTRY=10.190.144.32:5000
#envsubst < /data/kube_dns.yaml | kubectl create -f -
envsubst < /data/kube_dns.yaml 
+ envsubst
apiVersion: v1
kind: Service
metadata:
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.96.0.10
   spec:
      containers:
      - name: kubedns
        image: 10.190.144.32:5000/k8s-dns-kube-dns-amd64:1.14.7
       readinessProbe:
          httpGet:
            path: /readiness
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 3
          timeoutSeconds: 5
        args:
        - --domain=cluster.k8s.local.
       env:
       - name: AIDU_KUBERNETES_LOG_ENDPOINT
          value: 192.168.19.139:8070
cat /data/kube_dns.yaml
+ cat /data/kube_dns.yaml
apiVersion: v1
kind: Service
metadata:
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: $KUBE_CLUSTER_DNS_IP
   spec:
      containers:
      - name: kubedns
        image: ${DOCKER_REGISTRY}/k8s-dns-kube-dns-amd64:1.14.7
       readinessProbe:
          httpGet:
            path: /readiness
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 3
          timeoutSeconds: 5
        args:
        - --domain=${K8S_CLUSTER_DOMAIN}.
       env:
       - name: AIDU_KUBERNETES_LOG_ENDPOINT
          value: ${NAMENODE_HOST}:8070
上一篇下一篇

猜你喜欢

热点阅读