자격증/CKA
[CKA] NodePort 서비스 생성
후드리챱챱
2023. 3. 31. 23:35
반응형
NodePort 란?
- 외부 사용자가 서비스에 접근하기 위해 생성
- Woker Node의 랜카드에 포트를 열어주는 방식
[문제]
Set configuration context $ kubectl config use-context kubernetes-admin@kubernetes
Create the service as type NodePort with the port 32767 for the nginx pod with the pod selector app: webui
[풀이]
- selector의 label 설정이 app: webui 사용하는 deployment 준비
# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: webui
template:
metadata:
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
# kubectl apply -f deployment.yaml
- pod selector app: webui 확인 (아래 방법 중 하나 선택해서 진행)
# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
web-5cfbcf5f65-cvw28 1/1 Running 0 18m app=webui,pod-template-hash=5cfbcf5f65
web-5cfbcf5f65-j5qfz 1/1 Running 0 18m app=webui,pod-template-hash=5cfbcf5f65
# kubectl get pod --selector app=webui --show-labels
NAME READY STATUS RESTARTS AGE LABELS
web-5cfbcf5f65-cvw28 1/1 Running 0 10m app=webui,pod-template-hash=5cfbcf5f65
web-5cfbcf5f65-j5qfz 1/1 Running 0 10m app=webui,pod-template-hash=5cfbcf5f65
# kubectl get pod -l app=webui --show-labels
NAME READY STATUS RESTARTS AGE LABELS
web-5cfbcf5f65-cvw28 1/1 Running 0 10m app=webui,pod-template-hash=5cfbcf5f65
web-5cfbcf5f65-j5qfz 1/1 Running 0 10m app=webui,pod-template-hash=5cfbcf5f65
- Service 생성
Nodeport 관련 템플릿은 아래 링크 참고
https://kubernetes.io/docs/concepts/services-networking/service/#nodeport
# cat > myservice.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: webui
ports:
- port: 80
targetPort: 80
nodePort: 32767
# kubectl apply -f myservice.yaml
service/my-service created
# kubectl get service my-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service NodePort 10.105.140.63 <none> 80:32767/TCP 15s
※ kubectel describe 명령을 통해서 pod 에서 80port를 사용하는지 체크 필요
- curl 명령 통해서 32767 포트 접근시 정상적으로 호출되는지 확인
# kubectl get node
NAME STATUS ROLES AGE VERSION
master.example.com Ready control-plane 128d v1.25.4
node1.example.com Ready <none> 128d v1.25.4
node2.example.com Ready <none> 128d v1.25.4
# curl node1.example.com:32767
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[참고]
- 유투브 따배씨
반응형