본문 바로가기

devops/k8s

[k8s] 가상머신 환경에서 클러스터 구성하기

Forwarding IPv4 and letting iptables see bridged traffic

리눅스의 netfilter 를 이용하여 네트워크를 다루는데 k8s가 사용하기 때문에 해당 설정을 하는 것

 

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

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
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

# Apply sysctl params without reboot
sudo sysctl --system

 

Swap off

kubeadm 을 통해서 클러스터를 구축할 때 사전 준비가 필요하다는 문서를 보면 swap disabled 를 반드시 설정하라는 주의가 보인다. 이를 해결하기 위한 설정이다.

[WARNING Swap]: swap is enabled; production deployments should disable swap unless testing the NodeSwap feature gate of the kubelet

 

 

 

How do I disable swap?

I have some sensitive data in RAM that I prefer not to be on disk. How do I disable swap? I have more than enough RAM. If RAM consumption gets too high I have no problems with processes being term...

askubuntu.com

sudo swapoff -a # 현재 시스템에 적용(리부팅하면 재설정 필요)
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab # 리부팅 필수

 

Install containerd

내부 컨테이너를 컨트롤하기 위한 구현체로서 containerd를 사용하기 때문에 containerd 를 설치해야한다.

 

How to Install Containerd on Ubuntu 22.04 / Ubuntu 20.04 | ITzGeek

containerd is an industry-standard container runtime that manages the complete container lifecycle of its host system. It handles image

www.itzgeek.com

# 필요한 프로그램 설치
sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release

# apt 패키지 설치를 위한 저장소 등록
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list

# containerd 설치
sudo apt update
sudo apt install -y containerd.io
sudo systemctl status containerd

# containerd 설치 확인
sudo systemctl status containerd

# k8s를 위한 설정
cat <<EOF | sudo tee -a /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
EOF

sudo sed -i 's/^disabled_plugins \=/\#disabled_plugins \=/g' /etc/containerd/config.toml
sudo systemctl restart containerd

 

Kubernetes install

쿠버네티스 클러스터 구성에 필요한 패키지들 다음 공식 문서의 가이드를 통해서 설치한다.

 

Installing kubeadm

This page shows how to install the kubeadm toolbox. For information on how to create a cluster with kubeadm once you have performed this installation process, see the Creating a cluster with kubeadm page. Before you begin A compatible Linux host. The Kuber

kubernetes.io

# ubuntu 22.04 이전 버전은 필요함.
sudo mkdir /etc/apt/keyrings

# 필요한 프로그램 설치
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

# apt 패키지 저장소 등록
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

# kubelet, kubeadm, kubectl 관련 프로그램 설치
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

 

master node cluster start

클러스터를 구축한다.

sudo kubeadm init

 

worker node register(join)

1. 마스터 노드와 워커 노드 사이의 통신확인

ping을 통해서 서로 간에 네트워크가 연결된 상태인지를 확인한다.

 

2. kubeadm join 을 통해서 worker 등록

kubeadm을 이용해서 workder node를 클러스터에 합류시킨다.

 

+ 만약 clear 를 통해서 worker node join 토큰을 잊어버렸다면 다음 명령어를 통해서 필요한 커맨드를 알 수 있다.

#마스터 노드에서 워커 노드 조인 명령 출력 방법
kubeadm token create --print-join-command

 

CNI install

에드온을 설치하는 과정을 거쳐서 클러스터 구축을 완료한다. 에드온을 설치하지 않은 node 상태가 not ready 상태이기도 하고 coreDns 컴포넌트가 pending 상태에서 진행되지 않는다.

 

Use Cilium for NetworkPolicy

This page shows how to use Cilium for NetworkPolicy. For background on Cilium, read the Introduction to Cilium. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. I

kubernetes.io

curl -LO https://github.com/cilium/cilium-cli/releases/latest/download/cilium-linux-amd64.tar.gz
sudo tar xzvfC cilium-linux-amd64.tar.gz /usr/local/bin
rm cilium-linux-amd64.tar.gz
cilium install

 

삽질한 것들

1. workder node에서 kubectl 을 통한 질의가 안되는 현상.

 

kubectl 은 마스터 노드에 특정 요청을 전달하는 쿠버네티스의 CLI 도구이다. 이 도구를 사용하기 위해서는 사전 조건이 필요한데 그것은 CA 인증서이다. 

 

master node에서는 인증서 그런거 없이 명령을 전달했는데?????

 

그것은 context 라는 리소스를 통해서 kubectl 에 명령을 사용할 때 CA가 전달되어서 잘 작동하는 것이다. 

 

따라서 워커노드에서 kubectl 을 사용하게 하려면 CA 정보를 담은 인증서 정보를 kubectl 명령 인자로 전달하거나 홈디렉토리에서 ./kube 디렉터리에 context 정보를 담은 config 파일이 필요하다.

 

마스터 노드에서 kubeadm init 을 실행하고 나서 메시지에서 권고하는 명령어

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

이 것들도 root 유저가 아닌 일반 유저가 context를 사용하기 위해서 필요한 config 파일을 복사하는 절차인 것이다.

만약 나처럼  워커 노드에서 조인이 잘 실행되었는지 확인하기 위해서 kubectl을 사용해서 이런 에러가 발견되면 마스터가서 확인하길 바란다.

 

2. containred client tool : crictl setting

docker 에 의존하지 않고 containerd 만을 설치해서 클러스터를 구성하였기 때문에 클러스터 구축시 오류가 발생하면 어느 컨테이너 환경에서 문제가 있는지 발견하기위해서 containred cli interface 를 사용하려고 했을 때, 사전에 필요한 절차가 필요하다. 이를 해결하기 위한 가이드

 

GitHub - containerd/containerd: An open and reliable container runtime

An open and reliable container runtime. Contribute to containerd/containerd development by creating an account on GitHub.

github.com

 

3. 가상 머신 ip 주소 중첩 현상

서로다른 가상머신은 고유한 ip를 가져야하는데 복제를 하니까 ip 주소가 중첩이 되는 현상이 발견되었다. 이를 해결하기 위해서 mac 주소를 재설정하고 가상머신을 재시작하거나 완전한 복제를 하거나 하는 것을 다해도 ip가 변경이 되지 않는다. machine id 를 변경한 이후에 재부팅하니까 해결한 현상이었다.

 

Two VirtualBox VMs running in parallel assigned same IP

I had one VM in my VirtualBox and wanted to use it as a template/image so I cloned it. Then I launched both the original and the clone simultaneously (to simulate a server cluster for something I ...

superuser.com

 

4. ubuntu 22.04 버전 에러

ubuntu 22.04 버전에서 kubernetes component 들이 다운되는 현상이 발생해서 클러스터가 구축이 되지않았다. 이 현상은 ubuntu 20.04 버전에서는 발생하지 않았고 22.04 버전에서 발생한다.