본문 바로가기
Kubernetes

[Kubernetes] Ubuntu Linux - kubeadm, kubelet, kubectl 클러스터 설치

by 빅경 2025. 2. 1.
728x90
반응형

설치환경 - VMWare

  • Ubuntu v24.04
  • Kubernetes v1.32.1

필요 요소들 설치 및 구성

필수 포트 확인

$ nc 127.0.0.1 6443 -v

 

스왑 비활성화

kubelet의 기본 동작은 노드에서 스왑 메모리가 감지되면 시작에 실패합니다. 

따라서 이는 스왑을 비활성화하거나 kubelet이 스왑을 허용하도록 설정해야 합니다.

$ sudo swapoff -a && sudo sed -i '/ swap / s/^/#/' /etc/fstab

 

방화벽 해제(포트 개방)

$ sudo ufw disable

 

컨테이너 런타임 설치

참고: https://kubernetes.io/docs/setup/production-environment/container-runtimes/

https://kubernetes.io/ko/docs/setup/production-environment/container-runtimes/

 

Container Runtimes

Note: Dockershim has been removed from the Kubernetes project as of release 1.24. Read the Dockershim Removal FAQ for further details. You need to install a container runtime into each node in the cluster so that Pods can run there. This page outlines what

kubernetes.io

클러스터의 각 노드에 컨테이너 런타임을 설치해야 Pod가 해당 노드에서 실행될 수 있습니다. 

 

필수 요소들 설치 및 구성하기

쿠버네티스의 kube-proxy는 iptables를 의존하기 때문에 리눅스의 iptables 프록시가 올바르게 작동하도록 아래와 같이 설정한다. 

# IPv4를 포워딩하여 iptables가 브리지된 트래픽을 보게 하기
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 필요한 sysctl 파라미터를 설정하면, 재부팅 후에도 값이 유지된다.
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# 재부팅하지 않고 sysctl 파라미터 적용하기
sudo sysctl --system

 

 

Container runtimes

이 페이지는 Kubernetes와 함께 여러 일반적인 컨테이너 런타임을 사용하는 방법에 대한 개요를 제공합니다.

  • containerd
  • CRI-O
  • Docker Engine
  • Mirantis Container Runtime

다음 설치 방법은 containerd 방식 입니다.

Install using the apt repository

1. Set up Docker's apt repository.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

 

2. Install the Docker packages.

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

 

3. Verify that the installation is successful by running the hello-world image

$ sudo docker run hello-world

 

4. 참고

containerd를 패키지(예: RPM 또는 .deb)를 통해 설치한 경우, CRI 통합 플러그인이 기본적으로 비활성화되어 있을 수 있습니다. Kubernetes와 함께 containerd를 사용하려면 CRI 지원이 활성화되어 있어야 합니다. /etc/containerd/config.toml 파일 내의 disabled_plugins 목록에 cri가 포함되어 있지 않은지 확인하세요. 해당 파일을 변경했다면 containerd를 재시작해야 합니다.

초기 클러스터 설치 후 또는 CNI 설치 후 컨테이너 크래시 루프를 경험한다면, 패키지와 함께 제공된 containerd 구성에 호환되지 않는 구성 매개변수가 포함되어 있을 수 있습니다. getting-started.md에 명시된 대로 containerd config default > /etc/containerd/config.toml 명령을 사용하여 containerd 구성을 재설정하고, 위에서 지정한 구성 매개변수를 적절히 설정하는 것을 고려하세요.

$ sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.bak
$ sudo vi /etc/containerd/config.toml

SystemdCgroup = false를 true로 변경합니다.

5. containerd를 재시작합니다.

$ sudo systemctl restart containerd

 

 

cgroup 드라이버

참고: https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/configure-cgroup-driver/

 

cgroups(control groups의 약자)는 프로세스들의 자원의 사용(CPU, 메모리, 디스크 입출력, 네트워크 등)을 제한하고 격리시키는 리눅스 커널 기능입니다.

우리는 컨테이너를 활용하기 위해 컨테이너 기술 즉 Container Runtime을 통한 Linux Kernel을 사용합니다.

Container 기술을 구현하기 위해 Linux Kernel이 제공하는 주요 기능은 Namespace, C-Group, SELinux 등이 있다.

  • Namespace : 프로세스 격리 기술
  • cgroup (process 감사, 제어, 추적) : namespace + cgroup 형태로 구성되며, namespace에 대한 관리, 추적을 위한 역할

kubelet과 Docker는 cgroupfs를 사용하고 나머지 프로세스는 systemd를 사용하도록 노드가 설정된 경우, 리소스가 부족할 때 불안정해지는 현상이 발생할 수 있습니다. 이로 인해 시스템 리소스 부족 현상이 발생할 수 있으므로 리눅스 init 시스템이 사용하는 cgroups 드라이버와 docker, kubelet의 드라이버를 맞춰주는 것이 효율적입니다.

 

Identify the cgroup version on Linux Nodes

stat -fc %T /sys/fs/cgroup/
For cgroup v2, the output is cgroup2fs.
For cgroup v1, the output is tmpfs.

 

Docker Cgroup 확인

$ sudo docker info | grep "Cgroup Driver"
Cgroup Driver: systemd

 

 

kubeadm, kubelet 및 kubectl 설치

참고: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-kubectl

모든 머신에 다음 패키지들을 설치한다.

  • kubeadm: 클러스터를 부트스트랩하는 명령이다.
  • kubelet: 클러스터의 모든 머신에서 실행되는 파드와 컨테이너 시작과 같은 작업을 수행하는 컴포넌트이다.
  • kubectl: 클러스터와 통신하기 위한 커맨드 라인 유틸리티이다.

 

다음은 Kubernetes control-plane 설치 방법 입니다.
1. Update the apt package index and install packages needed to use the Kubernetes apt repository:
sudo apt-get update

# apt-transport-https may be a dummy package; if so, you can skip that package
$ sudo apt-get install -y apt-transport-https ca-certificates curl gpg

 

2. Download the public signing key for the Kubernetes package repositories. The same signing key is used for all repositories so you can disregard the version in the URL:

# If the directory `/etc/apt/keyrings` does not exist, it should be created before the curl command, read the note below.
$ sudo mkdir -p -m 755 /etc/apt/keyrings
$ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.32/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

 

3. Add the appropriate Kubernetes apt repository. Please note that this repository have packages only for Kubernetes 1.32; for other Kubernetes minor versions, you need to change the Kubernetes minor version in the URL to match your desired minor version (you should also check that you are reading the documentation for the version of Kubernetes that you plan to install).

# This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list
$ echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.32/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list


4. Update the apt package index, install kubelet, kubeadm and kubectl, and pin their version:

$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl


5. (Optional) Enable the kubelet service before running kubeadm:

$ sudo systemctl enable --now kubelet

 

6. kubeadm 실행합니다. 

$ sudo kubeadm init
# $ sudo kubeadm init --config kubeadm-config.yaml
# $ sudo kubeadm init --pod-network-cidr=10.244.0.0/16

 

7. To start using your cluster, you need to run the following as a regular user:

$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
  
$ KUBECONFIG=$HOME/.kube/config

# You should now deploy a pod network to the cluster.
# Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
#   https://kubernetes.io/docs/concepts/cluster-administration/addons/
## worker 노드는 이미 조인이 되어있어두 되고 이후에 해도 관계없다.
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

## 정상적으로 설치되었다면 /run/flannel/subnet.env 파일이 생성되어야한다.
cat <<EOF | sudo tee /run/flannel/subnet.env 
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
EOF


# Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.80.149:6443 --token 9ko5np.3vzyqdb7ya7uc9lr \
	--discovery-token-ca-cert-hash sha256:d15a3fec241a40adb5747ecd36ce4e757ccce914b7717a97809691c4cea0b358


8. Kubelet Cgroup 확인

$ cat /var/lib/kubelet/config.yaml  |grep cgroup

 

 

설치 확인

# 버전 확인
$ kubeadm version
$ kubelet --version
$ kubectl version
$ kubectl version --client
$ kubectl version --client --output=yaml 
$ kubectl get node

# 클러스터 정보 확인
$ kubectl config view

# 클러스터 정보 확인
kubectl cluster-info

 

 

Woker 노드  구축

worker 노드에 1~4번까지 설치하고 다음을 등록 합니다.

kubeadm join 192.168.80.149:6443 --token 9ko5np.3vzyqdb7ya7uc9lr \
	--discovery-token-ca-cert-hash sha256:d15a3fec241a40adb5747ecd36ce4e757ccce914b7717a97809691c4cea0b358
    
cat <<EOF | sudo tee /run/flannel/subnet.env 
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
EOF

 

 

쿠버네티스 편의 기능 설치

다음은 쿠버네티스를 사용하는데 있어 편리한 기능들을 설치합니다.

# kubectl 자동 완성 기능
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ echo 'alias k=kubectl' >>~/.bashrc
$ echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
$ source ~/.bashrc


kubectl get node -> k get node로 간편하게 사용 가능합니다.

728x90
반응형