Information in this document may be out of date
This document has an older update date than the original, so the information it contains may be out of date. If you're able to read English, see the English version for the most up-to-date information: Use a Service to Access an Application in a Cluster
ํด๋ฌ์คํฐ ๋ด ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๊ทผํ๊ธฐ ์ํด ์๋น์ค ์ฌ์ฉํ๊ธฐ
์ด ๋ฌธ์๋ ์ธ๋ถ ํด๋ผ์ด์ธํธ๊ฐ ํด๋ฌ์คํฐ์์ ์คํ ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๊ทผํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์ฟ ๋ฒ๋คํฐ์ค ์๋น์ค ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๋ค. ์๋น์ค๋ ์คํ ์ค์ธ ๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ํ ๋ก๋ ๋ฐธ๋ฐ์ฑ์ ์ ๊ณตํ๋ค.
์์ํ๊ธฐ ์ ์
์ฟ ๋ฒ๋คํฐ์ค ํด๋ฌ์คํฐ๊ฐ ํ์ํ๊ณ , kubectl ์ปค๋งจ๋-๋ผ์ธ ํด์ด ํด๋ฌ์คํฐ์ ํต์ ํ ์ ์๋๋ก ์ค์ ๋์ด ์์ด์ผ ํ๋ค. ์ด ํํ ๋ฆฌ์ผ์ ์ปจํธ๋กค ํ๋ ์ธ ํธ์คํธ๊ฐ ์๋ ๋ ธ๋๊ฐ ์ ์ด๋ 2๊ฐ ํฌํจ๋ ํด๋ฌ์คํฐ์์ ์คํํ๋ ๊ฒ์ ์ถ์ฒํ๋ค. ๋ง์ฝ, ์์ง ํด๋ฌ์คํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค๋ฉด, minikube๋ฅผ ์ฌ์ฉํด์ ์์ฑํ๊ฑฐ๋ ๋ค์ ์ฟ ๋ฒ๋คํฐ์ค ํ๋ ์ด๊ทธ๋ผ์ด๋ ์ค ํ๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
๋ชฉ์
- Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ธ์คํด์ค ๋ ๊ฐ๋ฅผ ์คํํ๋ค.
- ๋ ธ๋ ํฌํธ๋ฅผ ๋ ธ์ถํ๋ ์๋น์ค ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ค.
- ์คํ ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๊ทผํ๊ธฐ ์ํด ์๋น์ค ์ค๋ธ์ ํธ๋ฅผ ์ฌ์ฉํ๋ค.
๋ ๊ฐ์ ํ๋์์ ์คํ ์ค์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ํ ์๋น์ค ์์ฑํ๊ธฐ
๋ค์์ ์ ํ๋ฆฌ์ผ์ด์ ๋ํ๋ก์ด๋จผํธ(Deployment) ์ค์ ํ์ผ์ด๋ค.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
selector:
matchLabels:
run: load-balancer-example
replicas: 2
template:
metadata:
labels:
run: load-balancer-example
spec:
containers:
- name: hello-world
image: gcr.io/google-samples/node-hello:1.0
ports:
- containerPort: 8080
protocol: TCP
ํด๋ฌ์คํฐ ๋ด Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ์. ์ ํ์ผ์ ์ฌ์ฉํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ๋ํ๋ก์ด๋จผํธ๋ฅผ ์์ฑํ์.
kubectl apply -f https://k8s.io/examples/service/access/hello-application.yaml
์์ ๋ช ๋ น์ ๋ํ๋ก์ด๋จผํธ ์ค๋ธ์ ํธ์ ์ฐ๊ด๋ ๋ ํ๋ฆฌ์นด์ (ReplicaSet) ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ค. ๋ ํ๋ฆฌ์นด์ ์ ๋ ๊ฐ์ ํ๋๋ฅผ ๊ฐ๊ณ , ๊ฐ๊ฐ์ Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ค.
๋ํ๋ก์ด๋จผํธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค.
kubectl get deployments hello-world kubectl describe deployments hello-world
๋ ํ๋ฆฌ์นด์ ์ค๋ธ์ ํธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค.
kubectl get replicasets kubectl describe replicasets
๋ํ๋ก์ด๋จผํธ๋ฅผ ๋ ธ์ถํ๋ ์๋น์ค ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ค.
kubectl expose deployment hello-world --type=NodePort --name=example-service
์๋น์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค.
kubectl describe services example-service
๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
Name: example-service Namespace: default Labels: run=load-balancer-example Annotations: <none> Selector: run=load-balancer-example Type: NodePort IP: 10.32.0.16 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 31496/TCP Endpoints: 10.200.1.4:8080,10.200.2.5:8080 Session Affinity: None Events: <none>
์๋น์ค์ ๋ ธ๋ํฌํธ(NodePort) ๊ฐ์ ๋ฉ๋ชจํ์. ์๋ฅผ ๋ค์ด, ์์ ๊ฒฐ๊ณผ์์, ๋ ธ๋ํฌํธ ๊ฐ์ 31496์ด๋ค.
Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ด ์คํ ์ค์ธ ํ๋๋ฅผ ๋์ดํ๋ค.
kubectl get pods --selector="run=load-balancer-example" --output=wide
๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
NAME READY STATUS ... IP NODE hello-world-2895499144-bsbk5 1/1 Running ... 10.200.1.4 worker1 hello-world-2895499144-m1pwt 1/1 Running ... 10.200.2.5 worker2
Hello World ํ๋๊ฐ ์คํ ์ค์ธ ๋ ธ๋๋ค ์ค ํ๋์ ๋ ธ๋์ ๋ํด ๊ณต์ฉ IP ์ฃผ์๋ฅผ ์ป์. ์ด ์ฃผ์๋ฅผ ์ป๋ ๋ฐฉ๋ฒ์ ์ด๋ป๊ฒ ํด๋ฌ์คํฐ๋ฅผ ์ค์นํ๋์ง์ ๋ฐ๋ผ ๋ค๋ฅด๋ค. ์๋ฅผ ๋ค์ด, Minikube๋ฅผ ์ฌ์ฉํ๋ฉด,
kubectl cluster-info
๋ฅผ ์คํํ์ฌ ๋ ธ๋ ์ฃผ์๋ฅผ ์ ์ ์๋ค. Google Compute Engine ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋ฉด,gcloud compute instances list
๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ ธ๋๋ค์ ๊ณต์ฉ ์ฃผ์๋ฅผ ์ ์ ์๋ค.์ ํํ ๋ ธ๋์์ ๋ ธ๋ ํฌํธ์ ๋ํด TCP ํต์ ์ ํ์ฉํ๋๋ก ๋ฐฉํ๋ฒฝ ๊ท์น์ ์์ฑํ์. ์๋ฅผ ๋ค์ด, ์๋น์ค์ ๋ ธ๋ํฌํธ ๊ฐ์ด 31568์ธ ๊ฒฝ์ฐ, 31568 ํฌํธ๋ก TCP ํต์ ์ ํ์ฉํ๋๋ก ๋ฐฉํ๋ฒฝ ๊ท์น์ ์์ฑํ์. ๋ค๋ฅธ ํด๋ผ์ฐ๋ ๊ณต๊ธ์๋ ๋ฐฉํ๋ฒฝ ๊ท์น์ ์ค์ ํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ทผ์ ์ํด ๋ ธ๋ ์ฃผ์์ ๋ ธ๋ ํฌํธ๋ฅผ ์ฌ์ฉํ์.
curl http://<public-node-ip>:<node-port>
<public-node-ip>
๋ ๋ ธ๋์ ๊ณต์ฉ IP ์ฃผ์์ด๊ณ ,<node-port>
๋ ์๋น์ค์ ๋ ธ๋ํฌํธ ๊ฐ์ด๋ค. ์ฑ๊ณต์ ์ธ ์์ฒญ์ ๋ํ ์๋ต์ hello ๋ฉ์์ง์ด๋ค.Hello Kubernetes!
์๋น์ค ์ค์ ํ์ผ ์ฌ์ฉํ๊ธฐ
kubectl expose
๋ฅผ ์ฌ์ฉํ๋ ๋์ ,
์๋น์ค ์ค์ ํ์ผ์ ์ฌ์ฉํด
์๋น์ค๋ฅผ ์์ฑํ ์ ์๋ค.
์ ๋ฆฌํ๊ธฐ
์๋น์ค๋ฅผ ์ญ์ ํ๊ธฐ ์ํด ๋ค์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์.
kubectl delete services example-service
๋ํ๋ก์ด๋จผํธ, ๋ ํ๋ฆฌ์นด์ , Hello World ์ ํ๋ฆฌ์ผ์ด์ ์ด ์คํ ์ค์ธ ํ๋๋ฅผ ์ญ์ ํ๊ธฐ ์ํด ๋ค์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์.
kubectl delete deployment hello-world
๋ค์ ๋ด์ฉ
ํํ ๋ฆฌ์ผ ์๋น์ค์ ์ ํ๋ฆฌ์ผ์ด์ ์ฐ๊ฒฐํ๊ธฐ ๋ฐ๋ผํ๊ธฐ