본문 바로가기
Compute/kubernetis

[따배씨] 15. NodePort 서비스 생성 / 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) 사전 필요 설정 

: label이 app:webui인 pod 준비 

[root@k8s-master ~]# k create deployment nginx --image=nginx --dry-run=client -o yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
[root@k8s-master ~]# k create deployment nginx --image=nginx --dry-run=client -o yaml > 15-dep.yaml
[root@k8s-master ~]# vi 15-dep.yaml 
[root@k8s-master ~]# k apply -f 15-dep.yaml 
deployment.apps/nginx created
[root@k8s-master ~]# k get pods 
NAME                       READY   STATUS    RESTARTS        AGE
cpuload-669c759d97-2f269   1/1     Running   0               52m
cpuload-669c759d97-5qdkw   1/1     Running   0               52m
cpuload-669c759d97-7dq4w   1/1     Running   0               52m
cpuload-669c759d97-kxrhj   1/1     Running   0               52m
cpuload-669c759d97-qhgl2   1/1     Running   0               52m
nginx-596cfb4f67-gt9sz     1/1     Running   0               8s
nginx-596cfb4f67-qrtdn     1/1     Running   0               45s
nginx-596cfb4f67-rrrhw     1/1     Running   0               8s
web-pod                    1/1     Running   2 (4m46s ago)   14m
[root@k8s-master ~]# k get pods --show-labels 
NAME                       READY   STATUS    RESTARTS     AGE   LABELS
cpuload-669c759d97-2f269   1/1     Running   0            52m   name=overloaded-cpu,pod-template-hash=669c759d97
cpuload-669c759d97-5qdkw   1/1     Running   0            52m   name=overloaded-cpu,pod-template-hash=669c759d97
cpuload-669c759d97-7dq4w   1/1     Running   0            52m   name=overloaded-cpu,pod-template-hash=669c759d97
cpuload-669c759d97-kxrhj   1/1     Running   0            52m   name=overloaded-cpu,pod-template-hash=669c759d97
cpuload-669c759d97-qhgl2   1/1     Running   0            52m   name=overloaded-cpu,pod-template-hash=669c759d97
nginx-596cfb4f67-gt9sz     1/1     Running   0            22s   app=webui,pod-template-hash=596cfb4f67
nginx-596cfb4f67-qrtdn     1/1     Running   0            59s   app=webui,pod-template-hash=596cfb4f67
nginx-596cfb4f67-rrrhw     1/1     Running   0            22s   app=webui,pod-template-hash=596cfb4f67
web-pod                    1/1     Running   2 (5m ago)   15m   <none>

 

 

[Question]

Create the service as type NodePort with the port 32767 for the nginx pod with the pod selector app:webui

 

 

[Solve]

(1) service 생성 

: yaml로 service를 생성하고 문제에서 요구하는 설정을 추가한다.

[root@k8s-master ~]# k expose deployment nginx --port=80 --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: webui
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: webui
status:
  loadBalancer: {}
[root@k8s-master ~]# k expose deployment nginx --port=80 --dry-run=client -o yaml > 15-service.yaml
[root@k8s-master ~]# vi 15-service.yaml

[root@k8s-master ~]# k apply -f 15-service.yaml 
service/nginx created
[root@k8s-master ~]# 
[root@k8s-master ~]# k get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        3h6m
nginx        NodePort    10.97.51.126   <none>        80:32767/TCP   10s
[root@k8s-master ~]#

 

 

(2) 생성한 service의 nodeport로 접속이 되는지 확인 

 

[root@k8s-master ~]# k get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        3h6m
nginx        NodePort    10.97.51.126   <none>        80:32767/TCP   10s
[root@k8s-master ~]# 
[root@k8s-master ~]# k get pods -o wide
NAME                       READY   STATUS    RESTARTS        AGE   IP              NODE        NOMINATED NODE   READINESS GATES
cpuload-669c759d97-2f269   1/1     Running   0               66m   20.96.169.130   k8s-node2   <none>           <none>
cpuload-669c759d97-5qdkw   1/1     Running   0               66m   20.96.169.131   k8s-node2   <none>           <none>
cpuload-669c759d97-7dq4w   1/1     Running   0               66m   20.96.36.70     k8s-node1   <none>           <none>
cpuload-669c759d97-kxrhj   1/1     Running   0               66m   20.96.169.132   k8s-node2   <none>           <none>
cpuload-669c759d97-qhgl2   1/1     Running   0               66m   20.96.36.69     k8s-node1   <none>           <none>
nginx-596cfb4f67-gt9sz     1/1     Running   0               14m   20.96.36.71     k8s-node1   <none>           <none>
nginx-596cfb4f67-qrtdn     1/1     Running   0               15m   20.96.169.135   k8s-node2   <none>           <none>
nginx-596cfb4f67-rrrhw     1/1     Running   0               14m   20.96.169.136   k8s-node2   <none>           <none>
web-pod                    1/1     Running   5 (2m40s ago)   29m   20.96.169.134   k8s-node2   <none>           <none>


 

 

[사용 커맨드 정리]

k expose deployment nginx --port=80 --dry-run=client -o yaml > 15-service.yaml
vi 15-service.yaml 

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: webui
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 32767 
  selector:
    app: webui
status:
  loadBalancer: {}


k apply -f 15-service.yaml 

k get service

k get pods -o wide

curl k8s-node1:32767

curl k8s-node2:32767

 


[번외]

- 문제 풀이를 하면서 들었던 의문 사항

: node2에 배포된 pod가 2개인데 node port로 접속을 하면 (curl k8s-node2:32767) 어떤 pod의 응답이 오는 것일까? 

: 그래서 node2에 배포된 pod 2개 중 1개의 nginx page를 수정 후 curl로 반복해서 접속해 보았다.

 

 

위 2개의 nginx pod 중 nginx-596cfb4f67-rrrhw 의 main page를 수정하였다. 

body 내용을 일부 수정 "<p>nonononononon</p>"

 

 

curl k8s-node2:32767을 반복했을 때, 2개 pod의 응답이 번갈아서 오는 것을 확인함 

아마도 round robin으로 응답이 오도록 어딘가에 설정이 있는듯,,, 

이 부분은 추후 시간이 있을 때, 좀 더 확인해 보자.,. 

반응형