본문 바로가기
Compute/kubernetis

[따배씨] 2. Pod 생성하기 / CKA 시험 문제 학습

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

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

 

 

 

 

[관련 이론]

 

 

Pod 생성 과정 

1. 사용자가 pod 생성 요청을 보낸다. 이 요청은 kube-apiserver로 전달된다.
2. kube-apiserver는 새로운 pod의 정보를 etcd에 저장한다.
3. kube-scheduler는 etcd의 정보를 참조하여 scheduler 정책에 따라 pod가 생성될 node를 결정한다.
4. 선택된 노드의 kubelet은 etcd에서 할당된 Pod 정보를 조회하고, 컨테이너 런타임(Container Runtime)을 사용하여 Pod을 실행한다.
5. kube-proxy는 서비스(Service)와 Pod 간 네트워크 라우팅을 설정한다. 

✅ 전체 Pod 생성 흐름 요약
1️⃣ 사용자 → kubectl apply -f pod.yaml 실행
2️⃣ kube-apiserver → 요청 검증 후 etcd에 저장
3️⃣ kube-scheduler → 적절한 노드 선택
4️⃣ kubelet → 컨테이너 이미지 다운로드(containerd repository) & Pod 실행
5️⃣ kube-proxy → 네트워크 설정
6️⃣ Pod가 Running 상태로 전환됨

 

 


 

 

[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) 사전 필요 설정 

: 문제 풀이에 사용할 namespace 생성 

controlplane ~ ➜  k create namespace ecommerce
namespace/ecommerce created

controlplane ~ ➜  k get namespaces 
NAME              STATUS   AGE
default           Active   18m
ecommerce         Active   10s
kube-node-lease   Active   18m
kube-public       Active   18m
kube-system       Active   18m

 

 

: 문제에서 제시하는 "k8s" context를 생성하고 적용 

controlplane ~ ➜  k config get-contexts 
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin   

controlplane ~ ➜  k config set-context k8s --cluster=kubernetes --user=kubernetes-admin
Context "k8s" created.

controlplane ~ ➜  k config get-contexts 
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
          k8s                           kubernetes   kubernetes-admin   
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin   

controlplane ~ ➜  k config use-context k8s
Switched to context "k8s".

controlplane ~ ➜  k config get-contexts 
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         k8s                           kubernetes   kubernetes-admin   
          kubernetes-admin@kubernetes   kubernetes   kubernetes-admin

 

 

[Question]

Create a new namespace and create a pod in the namespace

cluster: k8s
namespace name: ecommerce
pod Name: eshop-main
image: nginx:1.17
env: DB=mysql

 

 

[Solve]

(1) pod 생성 옵션 확인

: k run --help로 pod 생성 옵션 확인 

 

 

(2) pod 생성

: dry-run으로 pod 생성 설정에 이상이 없는지 생성 전에 확인 후 pod 생성 

controlplane ~ ➜  kubectl run eshop-main --image=nginx:1.17 --env="DB=mysql" --namespace=ecommerce --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: eshop-main
  name: eshop-main
  namespace: ecommerce
spec:
  containers:
  - env:
    - name: DB
      value: mysql
    image: nginx:1.17
    name: eshop-main
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

controlplane ~ ➜  kubectl run eshop-main --image=nginx:1.17 --env="DB=mysql" --namespace=ecommerce
pod/eshop-main created

 

 

(3) 생성한 pod 확인 

controlplane ~ ➜  kubectl get pods -n ecommerce 
NAME         READY   STATUS    RESTARTS   AGE
eshop-main   1/1     Running   0          21s

controlplane ~ ➜  kubectl describe pods -n ecommerce eshop-main 
Name:             eshop-main
Namespace:        ecommerce
Priority:         0
Service Account:  default
Node:             node01/192.168.63.141
Start Time:       Wed, 29 Jan 2025 11:28:49 +0000
Labels:           run=eshop-main
Annotations:      cni.projectcalico.org/containerID: 11ef1a1332de00fca960c7fe0b1016374f8cba0e9c3211640862f312309656ca
                  cni.projectcalico.org/podIP: 172.17.1.2/32
                  cni.projectcalico.org/podIPs: 172.17.1.2/32
Status:           Running
IP:               172.17.1.2
IPs:
  IP:  172.17.1.2
Containers:
  eshop-main:
    Container ID:   containerd://a2794196facfb452d8ad7b9740071f52c169a4003035f5d3d7029506139337ea
    Image:          nginx:1.17
    Image ID:       docker.io/library/nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Wed, 29 Jan 2025 11:28:53 +0000
    Ready:          True
    Restart Count:  0
    Environment:
      DB:  mysql
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-t4kd9 (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True 
  Initialized                 True 
  Ready                       True 
  ContainersReady             True 
  PodScheduled                True 
Volumes:
  kube-api-access-t4kd9:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  40s   default-scheduler  Successfully assigned ecommerce/eshop-main to node01
  Normal  Pulling    39s   kubelet            Pulling image "nginx:1.17"
  Normal  Pulled     36s   kubelet            Successfully pulled image "nginx:1.17" in 2.862s (2.862s including waiting). Image size: 51030575 bytes.
  Normal  Created    36s   kubelet            Created container eshop-main
  Normal  Started    36s   kubelet            Started container eshop-main

controlplane ~ ➜

 

 

[사용 커맨드 정리]

k config use-context k8s

kubectl run eshop-main --image=nginx:1.17 --env="DB=mysql" --namespace=ecommerce --dry-run=client -o yaml

kubectl run eshop-main --image=nginx:1.17 --env="DB=mysql" --namespace=ecommerce
kubectl get pods -n ecommerce 
kubectl describe pods -n ecommerce eshop-main 

 

반응형