简体   繁体   中英

How can I use environment variable in kubernetes replication controller yaml file

How to read environment variables in kubernetes yaml file? for example, I want to change the docker image tag but do not want to rewrite the file, like this

apiVersion: v1
kind: ReplicationController
...
spec:
  containers:
    - name: myapp
      image: myapp:${VERSION}
 ...

With this I can do kubectl rolling-update without updating the yaml file.

thanks

If you want a simple, lightweight approch you might try using envsubst . So assuming your example is in a file called example.yaml in a bash shell you'd execute:

export VERSION=69
envsubst < example.yaml | kubectl apply -f -

Also recent versions of Kustomize can do it too.

Helm 应该可以解决您的配置问题 - https://github.com/kubernetes/helm

你应该像这样使用一个带有kubectl set imageDeployment

kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

I would highly recommend using HELM. https://github.com/kubernetes/helm

You can install HELM using the information contained in the above link. That will make the helm command available to you.

By running helm create YOUR_APP_NAME it will create a directory structure like the following.

YOUR_APP_NAME/
  Chart.yaml          # A YAML file containing information about the chart
  LICENSE             # OPTIONAL: A plain text file containing the license for the chart
  README.md           # OPTIONAL: A human-readable README file
  values.yaml         # The default configuration values for this chart
  charts/             # OPTIONAL: A directory containing any charts upon which this chart depends.
  templates/          # OPTIONAL: A directory of templates that, when combined with values,
                      # will generate valid Kubernetes manifest files.
  templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

In the values.yaml file you can set some ENV variables like:

container:
    name: "nginx"
    version: "latest"

In your ReplicationController file you can reference the variables using:

apiVersion: v1
kind: ReplicationController
...
spec:
  containers:
    - name: myapp
      image: {{.Values.container.name}}:{{.Values.container.version}}
...

The YAML file for your replication controller should be placed in the templates directory.

You can then run the command helm package YOUR_PACKAGE_NAME . To install the package on your K8S cluster you can then run helm install PACKAGE_NAME

NOTE: I would suggest you switch to using Deployments instead of ReplicationController . See: https://kubernetes.io/docs/user-guide/deployments/

Maybe you mean this?

- name: PUBLIC_URL
  value: "http://gitserver.$(POD_NAMESPACE):$(SERVICE_PORT)"

This is something what their docs specified.. but it doesn't work for me anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM