Read about Environment Variables
Imperative commands for env variables
Tips and Tricks
For simulated Practice problems visit KillerCoda.
-
create a pod
mypod
with imagenginx
containing two environment variablesenv1=value1
andenv2=value2
.Solution
#generate yaml file k run mypod --image=nginx --dry-run=client -o yaml > pod.yaml #update pod.yaml apiVersion: v1 kind: Pod metadata: labels: run: mypod name: mypod spec: containers: - image: nginx name: mypod resources: {} env: - name: env1 value: "value1" - name: env2 value: "value2" dnsPolicy: ClusterFirst restartPolicy: Always # create the pod k create -f pod.yaml
-
write env variables of
mypod
tomypod-env.txt
and checkenv1=value1
andenv2=value2
environment variables are present.Solution
# write env to a file k exec mypod -ti -- env > mypod-env.txt # print env variables cat mypod-env.txt # env1 and env2 must be present
-
Solution
# create pod yaml file k run nginx --image=nginx --dry-run=client -o yaml > nginx.yaml # update nginx port to 8080 using env variable apiVersion: v1 kind: Pod metadata: labels: run: nginx name: nginx spec: containers: - image: nginx name: nginx resources: {} env: - name: NGINX_PORT value: "8080" dnsPolicy: ClusterFirst restartPolicy: Always
-
- name: MYSQL_ROOT_PASSWORD value: "root_password"
- name: MYSQL_USER value: "username"
- name: MYSQL_PASSWORD value: "password"
- name: MYSQL_DATABASE value: "mydatabase"
Solution
# create pod yaml file k run db --image=mysql --dry-run=client -o yaml > db.yaml # set required env variables apiVersion: v1 kind: Pod metadata: name: db spec: containers: - name: mysql image: mysql env: - name: MYSQL_ROOT_PASSWORD value: "root_password" - name: MYSQL_USER value: "username" - name: MYSQL_PASSWORD value: "password" - name: MYSQL_DATABASE value: "mydatabase"