Kubernetes 클러스터 구성과 관련된 설정, 배포, 모니터링, Pod 관리 및 리소스 관리 등에 대해 다룹니다.
1. Kubernetes 클러스터 설정 (KIND & kubectl 설치)
KIND 설치
KIND는 Docker 위에서 Kubernetes 클러스터를 간단히 구성할 수 있게 해 주는 도구입니다. (Docker가 사전 설치되어 있어야 합니다.)
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.24.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
kubectl 설치
kubectl은 쿠버네티스 클러스터와 통신하기 위한 필수 CLI 도구입니다.
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
2. Kubernetes 클러스터 생성 및 확인
KIND로 생성된 기본 클러스터에는 Control-Plane 노드가 1개, Worker 노드가 1개 있습니다. 필요한 경우, 다중 노드 구성을 위해 아래 kind-example-config.yml 파일을 사용하여 클러스터를 확장할 수 있습니다.
기본 클러스터 생성
기본적으로 컨트롤 플레인 노드 1개와 워커 노드 1개로 구성된 클러스터를 생성합니다.
kind create cluster
다중 노드 클러스터 생성
여러 노드 구성을 위해 아래와 같은 YAML 파일을 작성하여 다중 노드를 생성할 수 있습니다.
kind-example-config.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
클러스터 생성
kind create cluster --config kind-example-config.yml
클러스터 상태 확인
Docker 컨테이너와 Kubernetes 노드를 확인할 수 있습니다.
docker ps # Docker 컨테이너 상태 확인
kubectl get nodes # Kubernetes 노드 상태 확인
3. Kubernetes에서 Pod 생성
기본 Pod 생성
아래는 간단한 Busybox 컨테이너를 실행하는 Pod 설정 파일입니다.
apiVersion: v1
kind: Pod
metadata:
name: core-k8s
labels:
app: my-example-app
spec:
containers:
- name: any-name
image: busybox:latest
command: ['sleep', '10000']
Pod 생성
kubectl create -f pod.yml
Pod 상태 확인
kubectl get pods -o wide
4. 리소스 사용량 제한 설정
pod에 사용할 CPU와 메모리를 제한하여 설정할 수 있습니다. 아래와 같이 리소스 제한이 설정된 Pod.yml 파일을 작성합니다.
apiVersion: v1
kind: Pod
metadata:
name: core-k8s
labels:
app: my-example-app
spec:
containers:
- image: nginx
name: nginx
resources:
requests:
cpu: "1"
memory: "1G"
Pod 생성 및 상태 확인
kubectl apply -f pod.yml
5. 네트워크 네임스페이스 및 Volume 설정
네임스페이스 격리 및 Volume 설정
컨테이너 내부 데이터 공유를 위한 hostPath 볼륨을 마운트하고 독립적인 네임스페이스에서 실행할 수 있도록 설정합니다.
네임스페이스 디렉토리 생성
sudo mkdir -p /home/namespace/box/proc
파일 시스템 마운트
sudo mount -t proc proc /home/namespace/box/proc
격리된 환경에서 Bash 실행
프로세스의 네트워크를 독립적으로 관리하기 위해 unshare 명령을 사용할 수 있습니다.
sudo unshare -p -f -n chroot /home/namespace/box /bin/bash
Volume 마운트
컨테이너에 hostPath 볼륨을 마운트하여 데이터 공유를 설정합니다.
pod-volume.yml 파일을 생성하고 아래 내용을 추가합니다.
Pod 생성
오류 : Pod 업데이트 시 필드 변경 오류
Pod 업데이트 시 QoS, Volumes 등의 필드는 변경이 불가하여 오류가 발생합니다.
Pod을 삭제 후 재생성하여 설정을 업데이트합니다.
기존 Pod 삭제:
먼저 기존 core-k8s Pod을 삭제합니다.
새로운 Pod 생성:
pod-volume.yml 파일을 다시 적용하여 새로운 Pod을 생성합니다.
kubectl delete pod core-k8s
kubectl apply -f pod.yml
6. 시스템 모니터링 도구 설정 (Prometheus 설치)
Prometheus 설치
1. Kubernetes에 Prometheus를 배포하여 모니터링합니다.
2. Prometheus 설정 파일을 작성하거나 Helm Chart를 사용해 설치할 수 있습니다.
Prometheus Helm Chart를 이용한 설치
Helm 설치
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Prometheus 설치
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/prometheus
'BackEnd > DEVOPS' 카테고리의 다른 글
[AWS] AWS ECS와 ECR을 활용한 CI/CD 구현 (1) | 2024.11.18 |
---|---|
[ArgoCD] ArgoCD 설치 (1) | 2024.11.15 |
[Kubernetes] Master Node, Worker Node 연결 (0) | 2024.11.15 |
[Kubernetes] Spring Boot 애플리케이션 배포: CNI, SDN, NodePort 서비스 설정 (1) | 2024.10.29 |
[Docker] Dockerfile 작성 및 이미지 빌드 (5) | 2024.10.16 |