우리집에 Kubernetes Cluster 만들기

집에 쓸만한 쿠버네티스 클러스터 꾸미기

Thomas K
11 min readMar 14, 2024

For English, please follow the link

구형 컴퓨터의 재활용하여 마음 놓고 쓰는 테스트 및 학습용 클러스터를 꾸며보자

일전에 다른 일로 쓰다가 남아 있는 좀 오래된 작은 컴퓨터가 몇대 있는데, 여유시간이 생겨서 이걸로 집에 쿠버네티스 클러스터를 꾸며 봤어요. 요즘엔 가정용 인터넷 속도도 제법 빠르고(up/down 1Gbps), 이렇게 만든 클러스터는 기능 테스트 또는 Hobby Project를 위해 사용하는데 편리하군요.

A stack of my old small PCs

사진처럼, 다양한 업체들에서 나온 녀석들인데, 크기가 작아 공간을 적게 차지하고, 저전력 i5, RAM 16GB, SSD 500GB 정도는 되는지라, 가벼운 커뮤니티 웹사이트 또는 VPN 서버, PoC용 각종 API 서버들을 위해 사용하는데는 충분하네요.

이글에서는 설치 과정 만을 설명합니다. 쿠버네티스에 대한 기본적인 정보는 공식 사이트의 소개문서를 참조해 주세요.

Host OS 설치

우선, 이전에 깔려 있는 OS 를 싹 밀어내고, 모두 Ubuntu Server 22.04 LTS 버전(현재 시점의 최신 LTS version)을 다운로드 해서 설치합니다. (설치 과정 자세한 안내는 링크 참조)

Select the 1st option to install Ubuntu server

쿠버네티스 노드에서는 필요없는 모듈은 설치하지 않기 위해서 Ubuntu Server (minimized) 를 선택합니다.(Ubuntu Server 를 선택해도 상관없지만, 저성능 컴퓨터에서 부담을 최소화 하자는 생각에서)

microk8s, canonical-livepatch를 선택하여 OS 설치하면서 함께 설치합니다.

추가로 SSH server 설치를 선택, 원격 SSH로 로그인 해서 이후 나머지 셋업 절차를 진행할 수 있도록 합니다.

쿠버네티스 기본 셋업 (모든 Node)

일단 호스트 OS 설치가 끝나면, SSH로 접속하여 모든 컴퓨터에 다음과 같은 절차를 실행합니다.

  1. 모든 Node에 적용할 절차
# Set hostname for each machine
sudo hostnamectl set-hostname "{your-hostname}.local"
exec bash

# Apply any updates and reboot (*)
sudo apt update
sudo apt upgrade
sudo reboot

# Disable swap for better performance
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# Add settings to containerd.conf
# overlay (for using overlayfs),
# br_netfilter (for ipvlan, macvlan, external SNAT of service IPs)
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter

# Add settings to kubernetes.conf
# Allow IPv4, IPv6 and IP forwarding
sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

# Reload updated config
sudo sysctl --system

# Install required tools and CA certificates
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

# Add Docker repository
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# Then, install containerd
sudo apt update
sudo apt install -y containerd.io
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd

# Add Kubernetes repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/kubernetes-xenial.gpg
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

# Then, install Kubernetes components
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

2. /etc/hosts 파일에 각 Node의 IP 주소와 hostname 등록

# Install text editor 'nano'
sudo apt install nano
sudo nano /etc/hosts

이제 /etc/hosts 파일에 클러스터로 사용될 모든 컴퓨터들의 로컬 IP 주소들을 추가한 다음, 저장합니다. (Note: 다음의 IP address들은 예시, 실제 hostname 은 각자 환경에 따라 다름, 각 컴퓨터의 실제 IP address 는 모든 Node에서 ifconfig 명령으로 확인해야함, i.e. ifconfig | grep inet ⏎)

# k8s cluster nodes
192.168.1.150 master.local master
192.168.1.151 worker1.local worker1
192.168.1.152 worker2.local worker2
192.168.1.153 worker3.local worker3

3. Install Docker Community Edition

# k8s cluster nodes
sudo apt-get install docker-ce

4. Open TCP port for K8s API communication (default 6443)

# Open TCP port for K8s API (default 6443)
sudo iptables -A INPUT -p tcp --dport 6443 -j ACCEPT

# To diable swap
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# open /etc/fstab file to check swap is commented out
sudo nano /etc/fstab

5. swap.img 라인이 아직 주석으로 되어 있지 않다면, 첫글자로 #를 넣어 주석으로 바꿔 줍니다.

6. Reboot 후 swap 영역이 없음을 재확인 합니다.

# reboot 
sudo reboot

# after reboot, check the swap is gone
free -h

Master 노드 설정

이제 Master 노드 셋업을 할 차례입니다.

# only for master node
sudo kubeadm config images pull
sudo reboot

# note: '--ignore-preflight-errors=all' is added
# due to initialization stops with some minor errors
sudo kubeadm init --control-plane-endpoint=leno.k8s.local --ignore-preflight-errors=all

Master 노드 셋업이 잘 마무리 되었다면 아래와 같은 화면이 나옵니다.

successful installation

위 화면에 설명되어 있는 것 처럼, 추가 절차를 수행하고 나면 Master Node 에 대한 설정은 끝납니다.

# Copy /etc/kubernetes/admin.conf for using the node 
# as a Non-root user
# Create .kube/config
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# Install Calico Network Plugin
# Currently (v3.25.0) is the latest, check the latest first for yours
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml

Worker 노드들을 Master 노드에 연결

Master 노드에 worker 노드 연결하는 방법은 간단합니다. Worker 노드로 사용할 컴퓨터에 login 해서 (SSH or 직접 로그인) 다음을 수행합니다.

# '--ignore-preflight-errors=all' used to bypass on minor errors 
sudo kubeadm join master.local:6443 --token wf.......REDACTED..........u8b \
--discovery-token-ca-cert-hash sha256:5b7............REDACTED.......................2038 --ignore-preflight-errors=all

# reboot
sudo reboot

Worker 노드를 재부팅한 다음, Master 노드쪽에서 제대로 연결되었는지 확인합니다.

# check the worker node is connected
kubectl get nodes

NAME STATUS ROLES AGE VERSION
worker1.local Ready <none> 2m6s v1.28.2
master.local Ready control-plane 33m v1.28.2


# list all pods in the system namespace (kube-system)
kubectl get pods -n kube-system

이제 기본적인 Kubernetes Cluster 구성은 끝났습니다. 다음 글에서는 클러스터에서 사용할 인터넷 도메인을 설정해 보도록 하겠습니다.

--

--

Thomas K

Software engineer (working on Android, iOS and API design), system engineer on Windows, Linux, Mac and still love programming in embedded OSes with no boundary