[Kubernetes]Docker Desktop+MultiNodes +Rolling Update+Scalling[MacOS]

๐Ÿ‘‰๐Ÿป ๋กค๋ง ์—…๋ฐ์ดํŠธ / Rolling Update

โœ”๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฌด์ค‘๋‹จ์œผ๋กœ ์ ์ง„์ ์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฒ„์ „์œผ๋กœ ๊ต์ฒดํ•˜๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
It refers to the process of gradually replacing an application with a new version without interruption.

๐Ÿ‘‰๐Ÿป ์Šค์ผ€์ผ๋ง / Scaling

โœ”๏ธ ์‹œ์Šคํ…œ์ด๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ž์›์˜ ์–‘์„ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์—ฌ์„œ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์ด๋‚˜ ์„ฑ๋Šฅ์„ ์กฐ์ ˆํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
It refers to adjusting processing power or performance in a system or application by increasing or decreasing the amount of resources.

๐Ÿ‘‰๐Ÿป ClusterIP์™€ NodePort์˜ ์ฐจ์ด
Difference between ClusterIP and NodePort

โœ”๏ธ ClusterIP

— ๊ธฐ๋ณธ ์„œ๋น„์Šค ํƒ€์ž…์ž…๋‹ˆ๋‹ค. / This is the basic service type.

— ์™ธ๋ถ€์—์„œ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ณ , ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€ ํ†ต์‹ ์šฉ์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
It cannot be accessed directly from the outside and is primarily used for internal cluster communication.

โœ”๏ธ NodePort

— ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€๋ฟ ์•„๋‹ˆ๋ผ, ๊ฐ ์›Œ์ปค ๋…ธ๋“œ์—์„œ ์ง€์ •๋œ ํฌํŠธ(30000 ~ 32767 ๋ฒ”์œ„)๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ๋…ธ๋“œ IP์™€ ์ด ํฌํŠธ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
In addition to within the cluster, external access is possible through the node IP and the designated port (range 30000 to 32767) on each worker node.

— ClusterIP๋Š” ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€ ์ „์šฉ ๋„คํŠธ์›Œํฌ๋กœ ์„œ๋น„์Šค์— ์ ‘๊ทผํ•  ๋•Œ, NodePort๋Š” ์™ธ๋ถ€์—์„œ๋„(๋…ธ๋“œ IP + ์ง€์ • ํฌํŠธ๋กœ) ์ ‘๊ทผ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์„œ๋น„์Šค ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
ClusterIP is a service type that allows access to the service via a private network within the cluster, while NodePort enables access from the outside (via Node IP + specified port).


๐Ÿ‘‰๐Ÿป ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™•์žฅ(์Šค์ผ€์ผ๋ง) ๋ฐ ๋กค๋ง ์—…๋ฐ์ดํŠธ 
Application scaling and rolling updates

โœ”๏ธ nginx-deployment.yaml

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

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

kubectl apply -f nginx-deployment.yaml

โœ”๏ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™•์žฅ (Replica ์ˆ˜ ์กฐ์ ˆ)
Application expansion (controlling the number of replicas)

— ํ˜„์žฌ pod ์ˆ˜๊ฐ€ 1๊ฐœ๋ฅผ(replicas: 1) 3๊ฐœ๋กœ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.
Expands the current pod count from 1 (replicas: 1) to 3.

kubectl scale deployment nginx-deployment --replicas=3

โœ”๏ธ ๋กค๋ง ์—…๋ฐ์ดํŠธ / rolling updates

— nginx1.23๋ฒ„์ „์œผ๋กœ ๊ต์ฒด / Replaced with nginx version 1.23

— deployment(nginx-deployment)์—์„œ nginx๋ผ๋Š” ์ด๋ฆ„์˜ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๋ฅผ nginx:1.23์œผ๋กœ ๊ต์ฒดํ•ฉ๋‹ˆ๋‹ค.
Replace the container image named nginx in deployment(nginx-deployment) with nginx:1.23.

kubectl set image deployment/nginx-deployment nginx=nginx:1.23

— ์—…๋ฐ์ดํŠธ ์ง„ํ–‰์ƒํƒœ ๋ณด๊ธฐ / View update progress

kubectl rollout status deployment/nginx-deployment

— ํ˜„์žฌ ๋ฐฐํฌ(Deployment)์˜ ์ƒํƒœ๋ฅผ ์ด์ „ ์„ฑ๊ณต์ ์ด์—ˆ๋˜ ๋ฒ„์ „์œผ๋กœ ๋˜๋Œ๋ฆฌ๋Š” ๋กค๋ฐฑ ๋ช…๋ น์ž…๋‹ˆ๋‹ค.
This is a rollback command that reverts the current deployment state to a previously successful version.

kubectl rollout undo deployment/nginx-deployment

โœ”๏ธ ์‹ค์ œ ์ž‘์—… / actual work

–์ปจํŠธ๋กค ํ”Œ๋ ˆ์ธ์—์„œ ๋„์ปค์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ(nginx:1.23์ด ์—†๋Š” ๊ฒฝ์šฐ)
Download Docker image from the control plane (if nginx:1.23 is not available)

docker pull nginx:1.23

–๋‹ค์šด๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ํ™•์ธ / Check downloaded images
(nginx:1.23 f5747a42e3ad 205MB 55.6MB )

MacBookAir nginx-test1 % docker images
                                                                                                                                                         i Info โ†’   U  In Use
IMAGE                                                                                                   ID             DISK USAGE   CONTENT SIZE   EXTRA
docker/desktop-cloud-provider-kind:v0.5.0                                                               4ad59ce20658        574MB          150MB    U   
docker/desktop-containerd-registry-mirror:v0.0.3                                                        179a9ea3ae47       48.8MB         15.5MB    U   
docker/desktop-kubernetes:kubernetes-v1.34.1-cni-v1.7.1-critools-v1.33.0-cri-dockerd-v0.3.20-1-debian   12d6673564e0        552MB          169MB        
docker/desktop-storage-provisioner:v3.0                                                                 57d2b6ad1c6f       75.4MB         21.9MB        
docker/desktop-vpnkit-controller:v4.0                                                                   bdaff3408b1c         50MB         10.7MB        
envoyproxy/envoy:v1.36.4                                                                                08acc53db6f4        233MB         58.8MB        
kindest/node:v1.31.1                                                                                    cd224d8da58d       1.47GB          413MB        
kindest/node:v1.35.1                                                                                    05d7bcdefbda       1.26GB          346MB    U   
nginx:1.23                                                                                              f5747a42e3ad        205MB         55.6MB        
nginx:latest                                                                                            6e23479198b9        258MB         64.2MB        
registry.k8s.io/coredns/coredns:v1.11.3                                                                 9caabbf6238b       81.8MB         16.9MB        
registry.k8s.io/coredns/coredns:v1.12.1                                                                 e8c262566636       95.8MB         20.4MB        
registry.k8s.io/etcd:3.5.16-0                                                                           c6a9d11cc5c0        213MB         67.9MB        
registry.k8s.io/etcd:3.6.4-0                                                                            e36c08168342        306MB         98.2MB        
registry.k8s.io/kube-apiserver:v1.32.2                                                                  c47449f3e751        123MB         26.2MB        
registry.k8s.io/kube-apiserver:v1.34.1                                                                  b9d7c117f8ac        112MB         24.5MB        
registry.k8s.io/kube-controller-manager:v1.32.2                                                         399aa50f4d13        114MB           24MB        
registry.k8s.io/kube-controller-manager:v1.34.1                                                         2bf47c1b01f5       95.5MB         20.6MB        
registry.k8s.io/kube-proxy:v1.32.2                                                                      83c025f0faa6        128MB         27.4MB        
registry.k8s.io/kube-proxy:v1.34.1                                                                      913cc83ca0b5        101MB         22.8MB        
registry.k8s.io/kube-scheduler:v1.32.2                                                                  45710d74cfd5       90.1MB         18.9MB        
registry.k8s.io/kube-scheduler:v1.34.1                                                                  6e9fbc4e25a5       69.6MB         15.7MB        
registry.k8s.io/pause:3.10                                                                              ee6521f290b2        786kB          266kB        

— ๋กค๋ง ์—…๋ฐ์ดํŠธ ์‹คํ–‰ / Run rolling update

kubectl set image deployment/nginx-deployment nginx=nginx:1.23

— ๋ชจ๋“ pod์— nginx:1.23์— ์ ์šฉ๋๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ(์ด๋ฏธ์ง€ ์ ์šฉ)
Check if nginx:1.23 has been applied to all pods (apply image)

MacBookAir nginx-test1 % kubectl set image deployment/nginx-deployment nginx=nginx:1.23
deployment.apps/nginx-deployment image updated
MacBookAir nginx-test1 % kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7878ff98b4-597k4   1/1     Running   0          3s
nginx-deployment-7878ff98b4-mqm62   1/1     Running   0          7s
nginx-deployment-7878ff98b4-nwn9r   1/1     Running   0          5s
MacBookAir nginx-test1 % kubectl describe pod nginx-deployment-7878ff98b4-597k4

... ์ค‘๊ฐ„ ์ƒ๋žต / skipping the middle ...

Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  41s   default-scheduler  Successfully assigned default/nginx-deployment-7878ff98b4-597k4 to desktop-worker
  Normal  Pulling    41s   kubelet            Pulling image "nginx:1.23"
  Normal  Pulled     41s   kubelet            Successfully pulled image "nginx:1.23" in 8ms (8ms including waiting). Image size: 55571911 bytes.
  Normal  Created    41s   kubelet            Container created
  Normal  Started    41s   kubelet            Container started

๐Ÿ‘‰๐Ÿป ์›Œ์ปค๋…ธ๋“œ ์ ‘์† ํ™•์ธ ํ•˜๊ธฐ / Check worker node connection

โœ”๏ธ NodePort๋กœ deployment ๋…ธ์ถœํ•˜๊ธฐ / Exposing a deployment via NodePort

MacBookAir nginx-test1 % kubectl expose deployment nginx-deployment --type=NodePort --port=80

โœ”๏ธ nodeIP ํ™•์ธํ•˜๊ธฐ / Check nodeIP

MacBookAir ~ % kubectl get nodes -o wide
NAME                    STATUS   ROLES           AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                       KERNEL-VERSION     CONTAINER-RUNTIME
desktop-control-plane   Ready    control-plane   28h   v1.35.1   172.18.0.3    <none>        Debian GNU/Linux 13 (trixie)   6.12.69-linuxkit   containerd://2.2.1
desktop-worker          Ready    <none>          28h   v1.35.1   172.18.0.6    <none>        Debian GNU/Linux 13 (trixie)   6.12.69-linuxkit   containerd://2.2.1
desktop-worker2         Ready    <none>          28h   v1.35.1   172.18.0.4    <none>        Debian GNU/Linux 13 (trixie)   6.12.69-linuxkit   containerd://2.2.1

— ์„œ๋น„์Šค ์ƒ์„ฑํ™•์ธ(ํฌํŠธ๋ฒˆํ˜ธ 32497 ํ™•์ธ)
Verify service creation (verify port number 32497)

MacBookAir nginx-test1 % kubectl get svc
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes         ClusterIP   10.96.0.1      <none>        443/TCP        28h
nginx-deployment   NodePort    10.96.49.237   <none>        80:32497/TCP   2m43s

— ์›Œ์ปค๋…ธ๋“œ ์ ‘์† / Worker node connection

# worker node 1
MacBookAir nginx-test1 % docker exec -it desktop-worker bash

# worker node 2
MacBookAir nginx-test1 % docker exec -it desktop-worker2 bash

— ์›Œ์ปค ๋…ธ๋“œ์—์„œ nginx์— ์ ‘์†ํ•ด ๋ณด๊ธฐ / Trying to access nginx from a worker node

root@desktop-worker:/# curl 172.18.0.6:32497
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>

— ์›Œ์ปค ๋…ธ๋“œ2๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. / Worker Node 2 connects as follows.

# curl 172.18.0.4:32497

๐Ÿ‘‰๐Ÿป๋ธŒ๋ผ์šฐ์ € ์ ‘์†ํ•˜๊ธฐ / Access browser

โœ”๏ธ ํ„ฐ๋ฏธ๋„์—์„œ 8080ํฌํŠธ๋กœ NodePort 80ํฌํŠธ์™€ ์—ฐ๊ฒฐ
Connect NodePort 80 to port 8080 in the terminal

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

โœ”๏ธ ๋ธŒ๋ผ์šฐ์ €์—์„œ localhost:8080์œผ๋กœ ์ ‘์†ํ•˜๋ฉด “Welcome to nginx”ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
If you access localhost:8080 in your browser, you can see the “Welcome to nginx” screen.

Leave a Reply