[Kubernetes]Docker Desktop+MultiNodes +Service[MacOS]

๐Ÿ‘‰๐Ÿป ์ด์ „ ๊ฒŒ์‹œ๋ฌผ์—์„œ ๋กค๋ง ์—…๋ฐ์ดํŠธ๋‚˜ ์Šค์ผ€์ผ๋ง์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ดค์Šต๋‹ˆ๋‹ค.
In the previous post, we looked at rolling updates and scaling.

๐Ÿ‘‰๐Ÿป ์ด์ „ ๊ฒŒ์‹œ๋ฌผ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ์…‹ํŒ…์— ์ถ”๊ฐ€๋กœ service๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
Let’s add a service to the settings used in the previous post.

๐Ÿ‘‰๐Ÿป ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋ฅผ ์™ธ๋ถ€์— ์ด์ „์ฒ˜๋Ÿผ(port-forward) ์™ธ๋ถ€์— ๋…ธ์ถœํ•  ๊ฒฝ์šฐ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋‚ด๋ถ€์—์„œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ์ด ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
If you expose Kubernetes externally as before (port-forward), load balancing within Kubernetes may not work.

๐Ÿ‘‰๐Ÿป port-forward๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ์—๋Š” ์ ํ•ฉํ•˜์ง€๋งŒ ์™ธ๋ถ€์—์„œ ์ ‘์†์‹œ ์ฒ˜์Œ ์ ‘์†ํ•œ ํŒŒ๋“œ์— ์ ‘์†์ด ๊ณ ์ •๋ฉ๋‹ˆ๋‹ค.
Using port-forward is suitable for testing, but when accessing from outside, the connection is fixed to the first connected pod.

โœ”๏ธ port-forward๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์™ธ๋ถ€์— ๋…ธ์ถœํ•œ๊ฒฝ์šฐ
When exposed externally using port-forward

MacBookAir nginx-test1 % kubectl port-forward svc/nginx-deployment 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

๐Ÿ‘‰๐Ÿป ๊ทธ๋ž˜์„œ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋‚ด๋ถ€์—์„œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ์ด ์ž‘๋™ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
Therefore, to enable load balancing within Kubernetes, you must use a service.

โœ”๏ธ nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080   # 30000~32767
      protocol: TCP

— yamlํŒŒ์ผ ์ ์šฉ / Apply YAML file

kubectl apply -f nginx-service.yaml

๐Ÿ‘‰๐Ÿป ์‹ค์ œ ์ž‘์—… / actual work

โœ”๏ธ ๋™์ผํ•œ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— nginx-deployment.yaml๊ณผ nginx-service.yamlํŒŒ์ผ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
Prepare the nginx-deployment.yaml and nginx-service.yaml files in the same directory.

โœ”๏ธ nginx-deployment.yaml ( ๋ฐ”๋กœ ์ด์ „๊ณผ ๋™์ผํ•œ ๋‚ด์šฉ / The exact same content as before)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

โœ”๏ธ nginx-service.yaml (์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ๋‚ด์šฉ)

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080   # 30000~32767
      protocol: TCP
nginx-test1 % ls
nginx-deployment.yaml	nginx-service.yaml	

โœ”๏ธ ๋„์ปค๋ฐ์Šคํฌํƒ‘์„ ์‹คํ–‰ํ•˜๊ณ  ํ„ฐ๋ฏธ์–ผ์—์„œ yamlํŒŒ์ผ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
Run Docker Desktop and execute the yaml file in the terminal.

โœ”๏ธ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์˜ ํŒŒ์ผ์„ ํ•œ๋ฒˆ์— ์ ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.
To apply the files in the directory all at once, do as follows.

acBookAir nginx-test1 % kubectl apply -f .
deployment.apps/nginx-deployment created
service/nginx-service created
MacBookAir nginx-test1 % 

โœ”๏ธ ๋งŒ์•ฝ์— ์ด๋ฏธ์ง€๊ฐ€ ์—†์œผ๋ฉด magePullBackOff๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
If there is no image, ImagePullBackOff appears .

โœ”๏ธ ์ด๋Ÿด ๊ฒฝ์šฐ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ๋‹ค์šด ๋ฐ›๊ณ  ์„ค์ •์„ ์žฌ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
In this case, download the image again and reapply the settings.

โœ”๏ธ nginx:latest์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ / nginx:latest image download

docker pull nginx:latest

โœ”๏ธ ์„ค์ • ์žฌ์ ์šฉ / Reapply settings

# kubectl rollout restart deployment/[๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ช… / Deployment Name]

MacBookAir nginx-test1 % kubectl rollout restart deployment/nginx-deployment 
deployment.apps/nginx-deployment restarted
MacBookAir nginx-test1 % 

โœ”๏ธ pod์ƒํƒœ ํ™•์ธ / Check pod status

— STATUS๊ฐ€ Running ์œผ๋กœ ๋ฐ”๋€๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can see that STATUS has changed to Running.

MacBookAir nginx-test1 % kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6f9664446b-72mlp   1/1     Running   0          10m
nginx-deployment-6f9664446b-g2b4x   1/1     Running   0          10m
nginx-deployment-6f9664446b-xrqhr   1/1     Running   0          10m
MacBookAir nginx-test1 % 

โœ”๏ธ ์„œ๋น„์Šค ์ƒํƒœํ™•์ธ / Check service status

# kubectl get service or kubectl get svc

MacBookAir nginx-test1 % kubectl get service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP        16h
nginx-service   NodePort    10.96.157.81   <none>        80:30080/TCP   13m

โœ”๏ธ ๋ธŒ๋ผ์šฐ์ €์ ‘์† / Browser access

— ๋ธŒ๋ผ์šฐ์ €์—์„œ 30080ํฌํŠธ๋กœ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.
Connect to port 30080 in your browser.

http://localhost:30080

Leave a Reply