[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