쿠버네티스 기본기 다지기

이eun·2024년 12월 10일

kubernetes

  • 쿠버네티스를 배포하면 클러스터를 얻는다. 즉, 쿠버네티스를 실행 중이라는 건 클러스터를 실행하고 있다는 얘기와 같다.
  • 쿠버네티스 클러스터는 컨테이너화된 애플리케이션을 실행하는 노드(워커 머신)의 집합이다
  • 모든 클러스터는 최소 한 개 이상의 마스터 노드 및 워커 노드를 갖는다
  • minikube는 1개의 노드로 구성된 쿠버네티스 클러스터이며 개발 실습용으로 사용하기 용이하다

pods

  • 쿠버네티스 기능을 수행하는 시스템 내부 구성 요소
  • 1개의 pod는 1개 이상의 컨테이너로 구성
  • pod는 하나의 컴퓨터

예제 코드

// pod 생성
kubectl run [pod-name] --image=[image-name]

// pod 확인
kubectl get pods

// pod 자세히 보기
kubectl get pods -o wide

// 파드의 호스트네임 확인
kubectl exec [pod-name] --hostname

// pod의 쉘이 시작되지만 바로 종료
kubectl exec [pod-name] --sh

// 종료되지 않을려면 (ti : tty input 콘솔로 연결하는 옵션)
kubectl exec -ti [pod-name] --sh

# uname -a // 파드의 스펙이 나옴

// pod 삭제
kubectl delete pod/[pod-name]

deployment

  • deployment를 생성하면 pod가 생성됨
  • replicasets는 pod를 생성하며 pod의 개수를 유지함 (이게 self-healing)
  • replicaset컨트롤러로 pod의 스케일을 관리
  • scaling, self-healing, rollout 관리 등 pod를 쉽게 관리하는 기능을 제공

예제 코드

//deployment를 생성하면 pod가 생성됨
kubectl create deployment [deployment-name] --image=[image-name]

// deployment는 replicaset 컨트롤러로 pod의 스케일을 관리함
kubectl scale deployment/[deployment-name] --replicas=[복제할 숫자]

//expose명령으로 service생성 후, pod를 노출시킴
kubectl expose deployment/[deployment-name] --port=8080 --target-port=80

//service는 요청을 pod에 전달해서 처리하도록 함. 이때, 요청을 분배함
서비스로 노출된 deployment를 하나의 마이크로서비스라고 함
kubectl create ingress ingress --rule=/=[deployment-name]:8080

//deployment/nginx의 버전 history 확인
kubectl rollout history deployment/[deployment-name]

//rollback
kubectl rollout undo deployment/[deployment-name]

//deployment 삭제
kubectl delete deployment/[deployment-name]

service

  • 외부에서 접근할 수 있도록 제공
  • pod가 제공하는 기능을 사용하도록 노출해주는 역할
  • 로드밸런싱 기능 제공

예제코드

// 80번 port 외부 서비스(http)에서 8080번 port로 연결하도록 노출해줌
kubectl expose deployment/[deployment-name] --port=8080 -- target-port=80

// service가 pod를 노출하지만 쿠버네티스 내부에 있어서 외부에서는 연결할 수 없음 그래서 minikube 노드 내부의 service를 외부에 노출시킴
minkube service [service-name]

//어떤 pod와 어떤 서비스가 연결되어있는지 확인, 골고루 퍼져있다는 것을 알 수 있음 => 요청 분산하여 로드밸런싱함
kubectl logs -f [service-name]

// 요청
curl localhost:[port]

// service 삭제
kubectl delete service/[service-name]

ingress

  • API Gateway로 복수 서비스에 대한 단일 진입점을 제공
  • 쿠버네티스 클러스터 내에 다양한 기능을 가지고 있는 서비스가 있을 때 각각 노출시키는 것이 복잡하고 용이하지 않으므로 ingress가 API Gateway를 하여 라우팅하는 역할을 수행
// ingress를 사용하기 전 , 활성화 먼저해야 함
minikube addons enable ingress

// ingress 생성, 요청에 대해 service의 8080 port로 전달하도록 규칙을 정함
kubectl create ingress ingess--rule=/=[service-name]:8080

// ingress가 외부에 service를 노출하긴 하지만 minikube docker container에 연결하기 위해서 localhost로 터널링해야함 -> ingress를 localhost의 포트로 연결
minikube tunnel

// ingress 삭제
kubectl delete ingress/ingress

// deployment, service, ingress 모두 삭제
kubectl delete deployment, service, ingress --all

0개의 댓글