美文网首页Linux
envsubst——将环境变量传递给文件

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

作者: 桩i | 来源:发表于2018-12-19 15:58 被阅读0次

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

    简单示例

    $ 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
    

    相关文章

      网友评论

        本文标题:envsubst——将环境变量传递给文件

        本文链接:https://www.haomeiwen.com/subject/jssjkqtx.html