본문 바로가기

리눅스

[draft] 우분투에 쿠버네티스 싱글 노드를 구성하는 방법

728x90

우분투에 쿠버네티스(k8s) 싱글 노드를 구성하는 방법

컨트롤 플레인(Control Plane)/워커 노드(worker nodes) 적용

컨트롤 플레인 control-plane
워커 노드 worker1, worker2, worker3
컨테이너 런타임 Containerd
네트워크 플러그인 Calico

1. 호스트명 변경

sudo hostnamectl set-hostname control-plane
sudo hostnamectl set-hostname worker1

2. 호스트(/etc/hosts) 파일 수정

 

cat <<EOF >> /etc/hosts

# kubernetes
192.168.0.131 control-plane
192.168.0.142 worker1
EOF

3. Swap 비활성화(스왑 끄기)

sudo swapoff -a
sudo sed -i '/\/swap\.img[[:space:]]\+none[[:space:]]\+swap[[:space:]]\+sw[[:space:]]\+0[[:space:]]\+0/s/^/#/' /etc/fstab
swapon -s

4. 방화벽(firewalld) 비활성화

sudo systemctl stop ufw && sudo systemctl disable ufw

5. Kernel Parameters 추가

br_netfilter, overlay 모듈 로드

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

모듈 확인

lsmod | grep "overlay\|br_netfilter"

IP 포워딩 및 iptables 설정

sudo tee /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

IP 포워딩 활성화

sudo echo 1 > /proc/sys/net/ipv4/ip_forward

sysctl 설정 적용

sudo sysctl -p

sysctl 설정 확인

sudo sysctl -a | grep 'net.bridge.bridge-nf-call-ip6tables\|net.bridge.bridge-nf-call-iptables\|net.ipv4.ip_forward'

6. 시스템 패키지 업데이트 및 필수 패키지 설치

sudo apt-get update
sudo apt-get install -y gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release curl
728x90

7. containerd 설치 및 설정

Docker 리포지토리 추가

sudo rm -f /etc/apt/trusted.gpg.d/docker.gpg
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 -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

containerd 설치

sudo apt-get update
sudo apt-get install -y containerd.io
sudo systemctl --now enable containerd

containerd 버전 정보 확인

$ containerd --version
containerd github.com/containerd/containerd 1.7.12

containerd 설정 파일 생성

sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml > /dev/null

containerd 설정 파일 수정

  • config.toml 파일에서 SystemdCgroup = true로 설정합니다.
sudo sed -i 's/^\([[:blank:]]*\)SystemdCgroup = false/\1SystemdCgroup = true/' /etc/containerd/config.toml
cat /etc/containerd/config.toml | grep SystemdCgroup

containerd 서비스 재시작

sudo systemctl restart containerd
sudo systemctl status containerd --no-pager -l

8. 쿠버네티스 설치

sudo rm -f /etc/apt/keyrings/kubernetes-apt-keyring.gpg

구글 클라우드의 공개 사이닝 키를 다운로드

KUBERNETES_VERSION="v1.30"
sudo mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/${KUBERNETES_VERSION}/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

쿠버네티스 apt 리포지터리를 추가

echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/${KUBERNETES_VERSION}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

모든 서버에서 kubeadm, kubelet, kubectl을 설치

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

kubelet 자동 업데이트 방지

sudo apt-mark hold kubelet kubeadm kubectl
sudo systemctl --now enable kubelet
sudo systemctl status kubelet --no-pager -l

9. 컨트롤 플레인 노드 초기화

컨트롤 플레인 노드(control-plane)에서 kubeadm을 사용하여 클러스터를 초기화합니다.

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 | tee $HOME/kubeadm_init_output.log
...
Your Kubernetes control-plane has initialized successfully!

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.0.131:6443 --token n1g8os.hvqvxhg3fj864qvd \
        --discovery-token-ca-cert-hash sha256:7d28b2687f805239d170e280a3cee6079241a53fe4900196366d4b93d88dcd07

명령이 성공하면 kubeadm join 명령이 출력됩니다. 이 명령은 워커 노드를 클러스터에 추가할 때 사용됩니다.

10. kubeconfig 설정

컨트롤 플레인 노드에서 kubectl을 사용하려면 일반 사용자로 kubeconfig 파일을 설정해야 합니다.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.0.131:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: DATA+OMITTED
    client-key-data: DATA+OMITTED

11. 네트워크 플러그인 설치(CNI 설정 - 마스터 노드)

쿠버네티스는 네트워킹을 위해 추가 플러그인이 필요합니다. 가장 많이 사용하는 플러그인 중 하나인 Calico를 설치합니다.

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
poddisruptionbudget.policy/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
serviceaccount/calico-node created
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
deployment.apps/calico-kube-controllers created
더보기

---

플란넬(Flannel) 사용

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
namespace/kube-flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

---

싱글 노드 클러스터를 위한 설정

kubectl taint nodes --all node-role.kubernetes.io/control-plane-
$ kubectl taint nodes --all node-role.kubernetes.io/control-plane-
node/control-plane untainted

클러스터 상태 확인

kubectl get nodes
kubectl get pods --all-namespaces

12. 워커 노드 클러스터 구성(워커 노드 join)

각 워커 노드에서 다음 명령을 실행하여 클러스터에 추가합니다. 이 명령은 kubeadm init 명령 실행 시 출력된 kubeadm join 명령입니다.

sudo kubeadm join <control-plane_IP>:6443 \
--token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
kubeadm join 192.168.10.111:6443 \
--token 10jxz4.s9dcedm8uv325kj5 \
--discovery-token-ca-cert-hash sha256:304c09b3cc23311d71a778149ba8c54a4af19205fca240daae71e2c673a221f8

각 워커 노드에서 위 명령을 실행하여 클러스터에 조인합니다.

...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

클러스터 상태 확인

  • 모든 노드가 정상적으로 클러스터에 조인되었는지 확인합니다.
kubectl get nodes

 

참고URL

- phoenixNAP : https://phoenixnap.com/kb/how-to-install-kubernetes-on-centos

- docker docs : Install using the convenience script

- kubernetes docs : kubeadm 설치하기

 

728x90