* 쿠버네티스 공식 문서에서 튜토리얼 진행하며 한국어로 번역하여 정리한 내용입니다.
* https://kubernetes.io/ko/docs/tutorials/ 에서 Katacoda를 사용하여 브라우저 상 터미널로 동일한 내용을 실습해 볼 수 있습니다.
목표 : kubectl expose 커맨드를 사용하여 클러스터 외부로 쿠버네티스 애플리케이션을 노출하는 방법을 배우고, 또한 kubectl label 커맨드로 오브젝트에 레이블을 적용하고 보는 방법을 배운다.
- Step 1 : Create a new service
애플리케이션이 실행하는지 확인하세요.
kubectl get 명령어를 사용하여 기존 포드들을 찾습니다.
$ kubectl get pods
다음으로, 클러스터에서 현재 서비스 목록을 봅시다.
$ kubectl get services
우리는 minikube가 클러스터를 시작할 때, 기본적으로 생성된 kubernetes라는 서비스를 가지게 됩니다.
새로운 서비스를 만들고 그것을 외부 트래픽으로 노출하기 위해, NodePort를 파라미터로 expose 명령어를 사용할 것입니다(미니큐브는 아직 LoadBalancer 옵션을 지원하지 않습니다).
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
get services 명령어를 다시 실행해봅시다.
이제 kubernetes-bootcamp라는 서비스가 실행 중입니다.
서비스가 고유한 cluster-IP, 내부 포트, 외부-IP(노드의 IP)를 수신한 것이 보입니다.
NodePort 옵션에 의해, 외부적으로 열려 있는 포트가 무엇인지 알기 위해, describe service 명령어를 실행합니다.
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.105.193.149
IPs: 10.105.193.149
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30513/TCP
Endpoints: 172.18.0.4:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
노드 포트에 할당된 값을 가지는 NODE_PORT 환경 변수를 만들어줍니다.
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp \
-o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=30513
이제 curl과 노드의 IP 주소, 외부로 노출된 포트를 이용하여 클러스터의 외부로 앱이 노출되었는지 테스트할 수 있습니다.
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-fms7g | v=1
서버로부터 응답을 얻었습니다. 서비스가 노출되었습니다.
- Step 2 : Using labels
deployment는 포드의 레이블을 자동으로 생성했습니다.
describe deployment 명령어로 레이블의 이름을 확인할 수 있습니다.
$ kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Fri, 15 Oct 2021 10:51:50 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 16m deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
포드 목록을 쿼리하기 위해서 이 레이블을 사용해봅시다.
kubectl get pods 명령어를 사용하고, -l 파라미터로 레이블 값을 줍니다.
$ kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-fms7g 1/1 Running 0 19m
동일한 작업을 수행하여, 기존 서비스 목록을 확인할 수 있습니다.
$ kubectl get services -l app=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.105.193.149 <none> 8080:30513/TCP 14m
POD_NAME 환경 변수로 포드의 이름을 저장해봅시다.
$ export POD_NAME=$(kubectl get pods -o go-template \
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-fb5c67579-fms7g
새로운 레이블을 적용하기 위해 오브젝트 타입, 오브젝트 이름, 새로운 레이블을 지정하는 label 명령어를 사용합니다.
$ kubectl label pods $POD_NAME version=v1
pod/kubernetes-bootcamp-fb5c67579-fms7g labeled
포드에 새로운 레이블이 적용되고(애플리케이션 버전을 포드에 고정), describe pod 명령어로 확인할 수 있습니다.
$ kubectl describe pods $POD_NAME
Name: kubernetes-bootcamp-fb5c67579-fms7g
Namespace: default
Priority: 0
Node: minikube/172.17.0.10
Start Time: Fri, 15 Oct 2021 10:52:06 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
version=v1
Annotations: <none>
Status: Running
IP: 172.18.0.4
IPs:
IP: 172.18.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://c99469b5936c082e835c78cb4c8faff73666283ef2fd02408bba14660e16ecef
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 15 Oct 2021 10:52:08 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-8qrtd (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-8qrtd:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8qrtd
Optional: false
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 24m default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-fms7g to minikube
Normal Pulled 24m kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 24m kubelet Created container kubernetes-bootcamp
Normal Started 24m kubelet Started container kubernetes-bootcamp
포드에 부착된 레이블을 보게 됩니다. 이제 새로운 레이블을 사용하여 포드의 목록을 쿼리할 수 있습니다.
$ kubectl get pods -l version=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-fms7g 1/1 Running 0 25m
- Step 3 : Deleting a service
서비스를 삭제하기 위해 delete service 명령어를 사용할 수 있습니다.
레이블은 여기에서도 사용될 수 있습니다.
$ kubectl delete service -l app=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27m
서비스가 사라진 것을 확인할 수 있습니다.
이것은 서비스가 제거된 것을 확인합니다.
경로가 더이상 노출되지 않는 것을 확인하기 위해, 이전에 노출된 IP와 포트로 curl을 사용해봅시다.
$ curl $(minikube ip):$NODE_PORT
curl: (7) Failed to connect to 172.17.0.10 port 30513: Connection refused
이것은 클러스터 외부에서 더이상 앱에 도달할 수 없는 것을 보여줍니다.
포드 내에서는 여전히 앱이 실행 중인 것을 curl로 확인할 수 있습니다.
$ kubectl exec -ti $POD_NAME -- curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-fms7g | v=1
여기서는 애플리케이션이 구동 중입니다. 이것은 deployment이 애플리케이션을 관리하고 있기 때문입니다.
애플리케이션을 종료하기 위해서는, deployment 또한 제거해야합니다.
출처 : https://kubernetes.io/ko/docs/tutorials/kubernetes-basics/expose/expose-interactive/
'Kubernetes' 카테고리의 다른 글
쿠버네티스 튜토리얼 6 : 앱 업데이트하기 (Kubernetes tutorial) (0) | 2021.11.11 |
---|---|
쿠버네티스 튜토리얼 5 : 앱 스케일링하기 (Kubernetes tutorial) (0) | 2021.11.11 |
쿠버네티스 튜토리얼 3 : 앱 조사하기 (Kubernetes tutorial) (0) | 2021.11.11 |
쿠버네티스 튜토리얼 2 : 앱 배포하기 (Kubernetes tutorial) (0) | 2021.11.11 |
쿠버네티스 튜토리얼 1 : 클러스터 생성하기 (Kubernetes tutorial) (0) | 2021.11.11 |