Net Eng

[CKA] - ConfigMap 운영 본문

Cloud/Kubernetes

[CKA] - ConfigMap 운영

欲心 2024. 1. 22. 11:14

[선수 작업]

kubectl config set-context k8s --user=kubernetes-admin --cluster=kubernetes

 


 

[문제]

Expose Configuration Settings

 

Tasks:

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

 

Create a ConfigMap called web-config that contains the following two entries.

  - connection_string=localhost:80

  - external_url=cncf.io

 

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.

 

작업 클러스터: k8s

 


 

[풀이]

kubectl config use-context k8s

* k8s 클러스터 사용

 

kubectl create ns ckad

* ckad 네임스페이스 생성

 

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

* ConfigMap 생성

 

kubectl run web-pod --image=nginx:1.19.8-alpine --port=80 -n ckad --dry-run=client -o yaml > web-pod.yaml

* dry-run 명령어로 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
    ports:
    - containerPort: 80
    envFrom:
    - configMapRef:
        name: web-config
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

* yaml 파일 수정하여 ConfigMap 적용

 

kubectl apply -f web-pod.yaml

* yaml 파일 실행

 

kubectl get pods -n ckad

* 생성된 Pod 확인

 

kubectl exec web-pod -n ckad -- env

* 환경 변수 확인

 

환경변수 확인

 

[다른 풀이]

cat << EOF > web-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
  namespace: ckad
data:
  connection_string: "localhost:80"
  external_url: "cncf.io"
EOF
kubectl apply -f web-config.yaml

* ConfigMap 파일 생성

 

cat << EOF > web-pod.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: web-pod
  name: web-pod
  namespace: ckad
spec:
  containers:
  - image: nginx:1.19.8-alpine
    name: web-pod
    ports:
    - containerPort: 80
    env:
    - name: connection_string
      valueFrom:
        configMapKeyRef:
          name: web-config
          key: connection_string
    - name: external_url
      valueFrom:
        configMapKeyRef:
          name: web-config
          key: external_url
EOF

* value 값으로 지정


[참고]

 

Configure a Pod to Use a ConfigMap

Many applications rely on configuration which is used during either application initialization or runtime. Most times, there is a requirement to adjust values assigned to configuration parameters. ConfigMaps are a Kubernetes mechanism that let you inject c

kubernetes.io

 

'Cloud > Kubernetes' 카테고리의 다른 글

[CKA] - Persistent Volume 생성  (0) 2024.01.22
[CKA] - Secret 운영  (0) 2024.01.22
[CKA] - NodePort 서비스 생성  (1) 2024.01.22
[CKA] - Init Container를 포함한 Pod 운영  (1) 2024.01.22
[CKA] - CPU 사용량이 높은 파드 검색  (1) 2024.01.21