
NodePort는 Kubernetes에서 외부에서 클러스터 내부의 서비스로 접근할 수 있도록 해주는 서비스 유형이다. 이 실습을 통해 NodePort의 동작 방식을 확인하고, 외부에서 서비스에 접근하는 방법을 테스트해볼 것이다.

30000-327671️⃣ Nginx Deployment 생성
먼저 nginx를 실행하는 Deployment를 생성한다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
적용 후 확인:
kubectl apply -f deployment.yaml
kubectl get pods -l app=nginx -o wide
2️⃣ NodePort 서비스 생성
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080 # 30000~32767 범위에서 지정 가능
적용 후 서비스 확인:
kubectl apply -f service.yaml
kubectl get svc nginx-service -o wide
✅ 예상 출력
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service NodePort 10.97.67.52 <none> 80:30080/TCP 1m
kubectl exec -it test-pod -- wget -qO- http://10.97.67.52
✅ 정상 동작 시, Nginx 기본 페이지 출력
curl http://<노드IP>:30080
🔹 노드의 IP 확인
kubectl get nodes -o wide
✅ 정상 동작 시, Nginx 기본 페이지 출력
kubectl exec -it test-pod -- sh -c "for i in {1..5}; do wget -qO- http://10.97.67.52; sleep 1; done"
✅ 예상 출력
<h1>Pod: nginx-deployment-xxxxx</h1>
<h1>Pod: nginx-deployment-yyyyy</h1>
<h1>Pod: nginx-deployment-zzzzz</h1>
kubectl get pods -o wide
kubectl delete pod <POD_NAME>
kubectl get pods -o wide
kubectl get endpoints nginx-service
kubectl exec -it test-pod -- wget -qO- http://10.97.67.52
✅ Pod이 삭제되고 다시 생성되더라도, NodePort를 통해 계속 접근 가능해야 함.
✔ NodePort 서비스가 노드의 외부 포트를 통해 접근 가능함을 확인
✔ 노드의 IP와 NodePort를 사용해 외부에서 접근 가능
✔ 여러 Pod으로 로드밸런싱이 수행되는지 테스트
✔ Pod이 삭제되어도 서비스가 유지되는지 확인
➡ **NodePort는 간단한 외부 접근을 제공하지만, 클라우드 환경에서는 LoadBalancer 서비스와 함께 사용하는 것이 일반적이다.