본문 바로가기
IT 잡지식/DevOps

[CKA] KodeKloud - Env Variables

by 쯀리♥️ 2024. 7. 2.

안녕하세요, 쯀리입니다.

오늘은 Pods에서 해당 container관련 환경 변수를 설정하는 방법에 관해 알아보도록 하겠습니다. 

https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

 

Define Environment Variables for a Container

This page shows how to define environment variables for a container in a Kubernetes Pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run

kubernetes.io

 


 

Quiz.

1. How many PODs exist on the system? in the current(default) namespace

controlplane ~ ➜  k get pods
NAME           READY   STATUS    RESTARTS   AGE
webapp-color   1/1     Running   0          2m21s

 

2. What is the environment variable name set on the container in the pod?

controlplane ~ ➜  k describe pods webapp-color
Containers:
  webapp-color:
    Container ID:   containerd://5c37c0fd93fb6a6bac980cacd3906f79441bb5ce09b362ca933bae2de304aefb
    Image:          kodekloud/webapp-color
... 
	Environment:
      APP_COLOR:  pink
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l8k4

Environment:      APP_COLOR:  pink
변수 이름은 APP_COLOR
이며 변수값은 pink입니다.

3. What is the value set on the environment variable APP_COLOR on the container in the pod? pink

4. View the web application UI by clicking on the Webapp Color Tab above your terminal.

 

5. Update the environment variable on the POD to display a green background.
Note: Delete and recreate the POD. Only make the necessary changes. Do not modify the name of the Pod.

controlplane ~ ✖ k edit pods webapp-color
containers:
  - env:
    - name: APP_COLOR
      value: green
      ...

controlplane ~ ✖ k delete pods webapp-color
pod "webapp-color" deleted

controlplane ~ ➜  k apply -f /tmp/kubectl-edit-3437292592.yaml
pod/webapp-color created

controlplane ~ ➜  k get pods
NAME           READY   STATUS    RESTARTS   AGE
webapp-color   1/1     Running   0          3s

 

6. View the changes to the web application UI by clicking on the Webapp Color Tab above your terminal.
If you already have it open, simply refresh the browser.

 

7. How many ConfigMaps exists in the default namespace?  2개

controlplane ~ ➜  k get configmap
NAME               DATA   AGE
kube-root-ca.crt   1      19m
db-config          3      21s

 

8. Identify the database host from the config map db-config

controlplane ~ ➜  k describe configmap db-config
Name:         db-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
DB_HOST:
----
SQL01.example.com

DB_NAME:
----
SQL01

DB_PORT:
----
3306

BinaryData
====

Events:  <none>

SQL01.example.com

9. Create a new ConfigMap for the webapp-color POD. Use the spec given below.

ConfigMap Name: webapp-config-map
Data: APP_COLOR=darkblue
Data: APP_OTHER=disregard
controlplane ~ ➜  vi webapp-configmap.yaml
###
apiVersion: v1
data:
  APP_COLOR: darkblue
  APP_OTHER: disregard
kind: ConfigMap
metadata:
  name: webapp-config-map
  namespace: default

controlplane ~ ➜  k apply -f webapp-configmap.yaml 
configmap/webapp-config-map created

controlplane ~ ➜  k get configmap
NAME                DATA   AGE
kube-root-ca.crt    1      25m
db-config           3      6m31s
webapp-config-map   2      3s

 

★ 10. Update the environment variable on the POD to use only the APP_COLOR key from the newly created ConfigMap. Note: Delete and recreate the POD. Only make the necessary changes. Do not modify the name of the Pod.

---
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: webapp-color
  name: webapp-color
  namespace: default
spec:
  containers:
  - env:
    - name: APP_COLOR
      valueFrom:
       configMapKeyRef:
         name: webapp-config-map
         key: APP_COLOR
    image: kodekloud/webapp-color
    name: webapp-color

 

 

 


 

 


참조

 Udemy Labs - Certified Kubernetes Administrator with Practice Tests