본문 바로가기
Compute/kubernetis

[따배씨] 11. Deployment & Expose the Service / 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) 사전 필요 설정 

- 문제에서 사용할 deployment를 미리 생성.

- 유튜브 강좌에서는 container의 name이 http로 되어 있지만, 내가 해석한 내용으로는 container name은 nginx가 맞다고 생각하여 이름을 nginx로 다르게 설정함. 

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: front-end
  name: front-end
spec:
  replicas: 2
  selector:
    matchLabels:
      app: front-end
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: front-end
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

 

 

[Question]

k8s
Reconfigure the existing deployment front-end and add a port specification named http exposing port 80/tcp of the existing container nginx.
Create a new service named front-end-svc exposing the container port http
Configure the new service to also expose the individual Pods via a NodePort on the nodes on which they are scheduled

 

 

[Solve]

(1) deployment 설정을 파일로 저장 후 문제 요구 사항 반영 

: nginx container의 port 이름 "http", 포트 번호 "80" 추가 

controlplane ~ ➜  k get deployments.apps -o yaml > deploy.yaml
controlplane ~ ➜  vi deploy.yaml

 

 

: 다시 배포 후 수정 사항 확인 

controlplane ~ ✖ k delete deployments.apps front-end 
deployment.apps "front-end" deleted

controlplane ~ ➜  k apply -f deploy.yaml 
deployment.apps/front-end created

 

controlplane ~ ➜  k get deployments.apps -o yaml

 

 

(2) service 생성 

: 서비스 생성 시 NodePort로 pods에 service가 expose되도록 설정

controlplane ~ ➜  kubectl expose deployment front-end --port=80 --target-port=80 --name=front-end-svc --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: front-end
  name: front-end-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: front-end
status:
  loadBalancer: {}

controlplane ~ ➜  kubectl expose deployment front-end --port=80 --target-port=80 --name=front-end-svc --dry-run=client -o yaml > 11-service.yaml
controlplane ~ ➜  vi 11-service.yaml

 

controlplane ~ ➜  kubectl expose deployment front-end --port=80 --target-port=80 --name=front-end-svc --dry-run=client -o yaml > 11-service.yaml
 
controlplane ~ ➜  vi 11-service.yaml 

controlplane ~ ➜  k apply -f 11-service.yaml 
service/front-end-svc created

 

 

(3) service 확인 

: deployment의 pod가 배포된 노드를 확인 

: 배포된 노드의 nodeport로 접속 시 해당 컨테이너로 접속됨을 확인 

controlplane ~ ➜  k get service front-end-svc -o wide
controlplane ~ ➜  k get pods -o wide
controlplane ~ ➜  curl node01:30007
controlplane ~ ➜  curl controlplane:30007


 

 

[사용 커맨드 정리]

k config use-context k8s

k get deployments.apps -o yaml > deploy.yaml
vi deploy.yaml
service/front-end-svc created

kubectl expose deployment front-end --port=80 --target-port=80 --name=front-end-svc --dry-run=client -o yaml > 11-service.yaml
vi 11-service.yaml 
k apply -f 11-service.yaml 
service/front-end-svc created

k get service front-end-svc -o wide
get pods -o wide
curl node01:30007
curl controlplane:30007

 

 

 

반응형