How to set dynamic values with Kubernetes yaml file

前端 未结 13 1937
旧巷少年郎
旧巷少年郎 2020-12-04 16:22

For example, a deployment yaml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: guestbook
spec:
  replicas: 2
  template:
    metadata         


        
相关标签:
13条回答
  • 2020-12-04 17:08

    I create a script called kubectl_create and use it to run the create command. It will substitute any value in the template that is referenced in an environment variable.

    #!/bin/bash
    set -e
    eval "cat <<EOF
    $(<$1)
    EOF
    " | kubectl create -f -
    
    

    For example, if the template file has:

    apiVersion: v1
    kind: Service
    
    metadata:
      name: nginx-external
      labels:
        app: nginx
    
    spec:
      loadBalancerIP: ${PUBLIC_IP}
      type: LoadBalancer
      ports:
      - name: http
        port: 80
        targetPort: 80
      - name: https
        port: 443
        targetPort: 443
    
      selector:
        app: nginx
    

    Run kubectl_create nginx-service.yaml and then the environment variable PUBLIC_IP will be substituted before running the actual kubectl create command.

    0 讨论(0)
提交回复
热议问题