본문 바로가기
Compute/kubernetis

[따배씨] 16. ConfigMap 운영 / CKA 시험 문제 학습

by 조청유곽 2025. 1. 31.
반응형

이 포스팅은 아래의 유튜브 채널 "따배"를 따라서 학습한 내용입니다.  

 

 

[관련 이론]


 

 

[Precondition]

(1) 테스트 환경

(1.1) Rocky Linux Cluster 

: 직접 구성

[root@k8s-master ~]# k get nodes -o wide
NAME         STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                            KERNEL-VERSION                  CONTAINER-RUNTIME
k8s-master   Ready    control-plane   30d   v1.27.2   192.168.56.30   <none>        Rocky Linux 8.10 (Green Obsidian)   4.18.0-553.33.1.el8_10.x86_64   containerd://1.6.32
k8s-node1    Ready    <none>          30d   v1.27.2   192.168.56.31   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
k8s-node2    Ready    <none>          30d   v1.27.2   192.168.56.32   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
[root@k8s-master ~]#

 

(1.2) Ubuntu Cluster 

: kodekloud 테스트 환경 활용

controlplane ~ ➜  kubectl get nodes -o wide
NAME           STATUS   ROLES           AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
controlplane   Ready    control-plane   9m6s    v1.31.0   192.6.94.6    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26
node01         Ready    <none>          8m31s   v1.31.0   192.6.94.9    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26

https://learn.kodekloud.com/user/courses/udemy-labs-certified-kubernetes-administrator-with-practice-tests

 

 

(2) 사전 필요 설정 

- CKAD namespace 생성 

controlplane ~ ➜  k create namespace ckad
namespace/ckad created

 

 

[Question]

Expose Configuration settings

Task:

1. All operations in this question should be performed in the ckad namespace

2. Create a ConfigMap called web-config that contains the following two entries:
- connection_string=localhost:80
- external_url=cncf.io

3. Run a pod called web-pod with a single container running the nginx:1.19.8-alpine image, 
and expose these configuration settings as environment variables inside the container.

 

 

[Solve]

(1) configmap 생성

controlplane ~ ➜  k create configmap web-config -n ckad --from-
--from-env-file  (Specify the path to a file to read lines of key=val pairs to create a configmap.)
--from-file      (Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with…)
--from-literal   (Specify a key and literal value to insert in configmap (i.e. mykey=somevalue))

controlplane ~ ➜  k create configmap web-config -n ckad --from-literal=connection_string=localhost:80 --from-literal=external_url=cncf.io 
configmap/web-config created

 

 

(2) pod 생성

: (1)에서 생성한 configmap을 container에 environment variables로 설정

controlplane ~ ➜  k run web-pod -n ckad --image=nginx:1.19.8-alpine --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: web-pod
  name: web-pod
  namespace: ckad
spec:
  containers:
  - image: nginx:1.19.8-alpine
    name: web-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

controlplane ~ ➜  k run web-pod -n ckad --image=nginx:1.19.8-alpine --dry-run=client -o yaml > 16-pod.yaml

controlplane ~ ➜  vi 16-pod.yaml

 

 

(3) pod 확인 

: pod의 env 확인 시 설정한 configmap이 적용된 것을 확인


 

 

[사용 커맨드 정리]

k create configmap web-config -n ckad --from-literal=connection_string=localhost:80 --from-literal=external_url=cncf.io

k get configmaps -n ckad web-config 

k describe configmaps -n ckad web-config 

k run web-pod -n ckad --image=nginx:1.19.8-alpine --dry-run=client -o yaml

k run web-pod -n ckad --image=nginx:1.19.8-alpine --dry-run=client -o yaml > 16-pod.yaml

vi 16-pod.yaml

k apply -f 16-pod.yaml 

k get pods -n ckad

k exec -n ckad web-pod -- env

 

반응형