쿠버네티스를 적용해보자! Building Kubernetes Cluster(feat.k3s)

🚀 쿠버네티스 클러스터 만들기: VirtualBox와 k3s로 홈랩 구축하기!

안녕하세요, 쿠버네티스 입문자 여러분! 오늘은 저와 함께 VirtualBox와 Vagrant를 사용해서 나만의 쿠버네티스 클러스터를 구축해 보는 시간을 가져볼게요. 경량 쿠버네티스 배포판인 k3s를 사용해서 마스터 노드와 워커 노드를 설정하고, 실제 애플리케이션도 배포해 볼 거예요. 너무 어렵게 생각하지 마세요. 차근차근 따라오시면 됩니다! 😊

🔧 환경 준비하기

먼저 필요한 환경을 설정했어요:

  • VirtualBox로 가상 머신 준비
  • Vagrant로 가상 머신 관리
  • k3s로 가벼운 쿠버네티스 클러스터 구축
  • 처음에는 Calico 네트워크 플러그인 사용 시도했으나, 문제 발생으로 기본 네트워크(Flannel) 사용

🌐 VirtualBox 네트워크 설정하기

가상 머신끼리 통신할 수 있도록 호스트 전용 네트워크를 설정했어요:

  1. VirtualBox에서 ‘환경설정’ > ‘네트워크’ > ‘호스트 전용 네트워크’ 클릭
  2. 다음 정보로 네트워크 설정:
    • 이름: vboxnet-vbox1
    • IP 범위: 192.168.56.0/24
    • DHCP 서버 활성화
    • IP 할당 범위: 192.168.56.2 ~ 192.168.56.254

이렇게 하면 VM들끼리 서로 통신할 수 있고, 호스트 PC에서도 VM에 접근할 수 있어요! 👍

💻 k3s 설치하기

마스터 노드 세팅

마스터 노드에 SSH로 접속:

vagrant ssh k8s-master

처음에는 Calico 네트워크 플러그인 설정으로 k3s 설치를 시도했어요:

curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--flannel-backend=none --disable-network-policy --cluster-cidr=192.168.0.0/16" sh -

하지만 Calico 설치 후 네트워크 플러그인 초기화 문제가 발생했어요. 😓 오류 로그:

Error syncing pod, skipping" err="network is not ready: container runtime network not ready: NetworkPluginNotReady

기본 네트워크로 다시 시작하기

문제 해결을 위해 k3s를 제거하고 기본 네트워크 설정으로 다시 설치했어요:

# k3s 제거
sudo /usr/local/bin/k3s-uninstall.sh

# 기본 설정으로 다시 설치
curl -sfL https://get.k3s.io | sh -

이렇게 하니 k3s가 기본 네트워크 플러그인인 Flannel을 사용해서 잘 작동하기 시작했어요! 🎉

워커 노드 연결하기

마스터 노드에서 토큰 확인:

sudo cat /var/lib/rancher/k3s/server/node-token

워커 노드에서 실행:

curl -sfL https://get.k3s.io | K3S_URL=https:/<IP>:6443 K3S_TOKEN="TOKEN" sh -

🎮 kubectl 설정하기

마스터 노드에서 kubectl을 사용할 수 있도록 설정했어요:

mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config

🔍 클러스터 확인하기

모든 노드가 잘 연결됐는지 확인:

sudo kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml get nodes

결과:

NAME         STATUS   ROLES                  AGE     VERSION
k8s-master   Ready    control-plane,master   5m55s   v1.32.3+k3s1
k8s-worker   Ready    <none>                 79s     v1.32.3+k3s1

🚢 첫 애플리케이션 배포하기

간단한 nginx 웹 서버 배포:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

서비스 확인:

kubectl get svc nginx

결과:

NAME    TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
nginx   NodePort   10.43.70.69   <none>        80:30723/TCP   105s

이제 http://<노드IP>:30723로 접속하면 nginx 웹 서버에 접근할 수 있어요! 🎉

🔧 문제 해결: VirtualBox GuestAdditions

가상 머신에서 공유 폴더 등의 기능을 사용하려면 GuestAdditions를 설치해야 해요:

# VM에 SSH로 접속
vagrant ssh k8s-master

# GuestAdditions 서비스 시작
sudo systemctl start vboxadd
sudo systemctl start vboxadd-service

# 서비스 상태 확인
sudo systemctl status vboxadd
sudo systemctl status vboxadd-service

# 필요시 재설치
sudo apt-get update
sudo apt-get install -y dkms build-essential linux-headers-$(uname -r)
sudo /opt/VBoxGuestAdditions*/init/vboxadd setup

# VM 재부팅
vagrant reload k8s-master

이렇게 해서 간단한 쿠버네티스 클러스터를 구축해 보았어요! 처음에는 Calico를 사용하려 했지만, 기본 네트워크도 잘 작동하니 걱정마세요. 다음에는 더 복잡한 애플리케이션을 배포해보도록 할게요. 😄

🚀 Building a Kubernetes Cluster: Home Lab with VirtualBox and k3s!

Hello, Kubernetes beginners! Today, we’ll build our own Kubernetes cluster using VirtualBox and Vagrant. We’ll set up a lightweight Kubernetes distribution called k3s to configure master and worker nodes, and even deploy actual applications. Don’t worry if it seems complicated – just follow along step by step! 😊

🔧 Preparing the Environment

First, I set up the necessary environment:

  • Virtual machines with VirtualBox
  • VM management with Vagrant
  • Lightweight Kubernetes cluster with k3s
  • Initially tried Calico network plugin, but switched to the default network (Flannel) due to issues

🌐 VirtualBox Network Configuration

I configured a host-only network so virtual machines can communicate:

  1. In VirtualBox, click ‘Preferences’ > ‘Network’ > ‘Host-only Network’
  2. Set up the network with these details:
    • Name: vboxnet-vbox1
    • IP range: 192.168.56.0/24
    • Enable DHCP server
    • IP assignment range: 192.168.56.2 ~ 192.168.56.254

This allows VMs to communicate with each other and the host PC to access the VMs! 👍

💻 Installing k3s

Setting up the Master Node

SSH into the master node:

vagrant ssh k8s-master

Initially, I tried installing k3s with Calico network plugin settings:

curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--flannel-backend=none --disable-network-policy --cluster-cidr=192.168.0.0/16" sh -

But after installing Calico, I encountered network plugin initialization issues. 😓 Error log:

Error syncing pod, skipping" err="network is not ready: container runtime network not ready: NetworkPluginNotReady

Starting Over with Default Network

To solve the problem, I removed k3s and reinstalled with default network settings:

# Remove k3s
sudo /usr/local/bin/k3s-uninstall.sh

# Reinstall with default settings
curl -sfL https://get.k3s.io | sh -

This way, k3s started working well using the default network plugin Flannel! 🎉

Connecting the Worker Node

Check the token on the master node:

sudo cat /var/lib/rancher/k3s/server/node-token

Run on the worker node:

curl -sfL https://get.k3s.io | K3S_URL=https://<IP>:6443 K3S_TOKEN="TOKEN" sh -

🎮 Configuring kubectl

I set up kubectl on the master node:

mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config

🔍 Verifying the Cluster

Check if all nodes are connected properly:

sudo kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml get nodes

Result:

NAME         STATUS   ROLES                  AGE     VERSION
k8s-master   Ready    control-plane,master   5m55s   v1.32.3+k3s1
k8s-worker   Ready    <none>                 79s     v1.32.3+k3s1

🚢 Deploying Your First Application

Deploy a simple nginx web server:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

Check the service:

kubectl get svc nginx

Result:

NAME    TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
nginx   NodePort   10.43.70.69   <none>        80:30723/TCP   105s

Now you can access the nginx web server at http://<node-ip>:30723! 🎉

🔧 Troubleshooting: VirtualBox GuestAdditions

To use shared folders and other features in virtual machines, install GuestAdditions:

# SSH into VM
vagrant ssh k8s-master

# Start GuestAdditions services
sudo systemctl start vboxadd
sudo systemctl start vboxadd-service

# Check service status
sudo systemctl status vboxadd
sudo systemctl status vboxadd-service

# Reinstall if needed
sudo apt-get update
sudo apt-get install -y dkms build-essential linux-headers-$(uname -r)
sudo /opt/VBoxGuestAdditions*/init/vboxadd setup

# Reboot VM
vagrant reload k8s-master

That’s how I built a simple Kubernetes cluster! Although I initially tried to use Calico, the default network works fine too, so don’t worry. Next time, we’ll try deploying more complex applications. 😄

답글 남기기