harukin721

主に学習記録

Ubuntu PC 1台で kubeadm を使って Kubernetes クラスターを構築

Ubuntu PC 1台で kubeadm を使って Kubernetes クラスターを構築

Ubuntu PC 1台の上で Multipass を使い、仮想マシンVM)上に Kubernetes クラスターを構築する。 構成は Master 1台 + Worker 3台(計4台)、kubeadm を使用し、ネットワークプラグインは Calico を採用。 コンテナランタイムとして Docker を利用する。

ref. Calico - IBM Documentation

Ubuntu PC

# CPU
haruki@node01:~$ nproc
4

# メモリ
haruki@node01:~$ free -g
               total        used        free      shared  buff/cache   available
Mem:              11           0          10           0           0          10
Swap:              1           0           1

# ストレージ
haruki@node01:~$ df -Th /
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sda2      ext4  468G   20G  425G   5% /

Multipass のインストール

haruki@node01:~$ sudo snap install multipass
multipass 1.15.1 from Canonical✓ installed
haruki@node01:~$ multipass version
multipass   1.15.1
multipassd  1.15.1
haruki@node01:~$ multipass list
No instances found.

仮想マシンVM)の作成

ノード名 CPU メモリ ストレージ
k8s-master 2コア 4GB 20GB
k8s-worker-01 1コア 2GB 10GB
k8s-worker-02 1コア 2GB 10GB
k8s-worker-03 1コア 2GB 10GB
haruki@node01:~$ multipass launch --name k8s-master --cpus 2 --mem 4G --disk 20G
Warning: the "--mem" long option is deprecated in favour of "--memory". Please update any scripts, etc.
Launched: k8s-master
haruki@node01:~$ multipass launch --name k8s-worker-01 --cpus 1 --mem 2G --disk 10G
Warning: the "--mem" long option is deprecated in favour of "--memory". Please update any scripts, etc.
Launched: k8s-worker-01
haruki@node01:~$ multipass launch --name k8s-worker-02 --cpus 1 --mem 2G --disk 10G
Warning: the "--mem" long option is deprecated in favour of "--memory". Please update any scripts, etc.
Launched: k8s-worker-02
haruki@node01:~$ multipass launch --name k8s-worker-03 --cpus 1 --mem 2G --disk 10G
Warning: the "--mem" long option is deprecated in favour of "--memory". Please update any scripts, etc.
Launched: k8s-worker-03
haruki@node01:~$ multipass list
Name                    State             IPv4             Image
k8s-master              Running           10.105.167.32    Ubuntu 24.04 LTS
k8s-worker-01           Running           10.105.167.224   Ubuntu 24.04 LTS
k8s-worker-02           Running           10.105.167.63    Ubuntu 24.04 LTS
k8s-worker-03           Running           10.105.167.9     Ubuntu 24.04 LTS

各 Node に Docker & Kubernetes をインストール

#  kubelet がメモリ管理をする際、スワップが有効だと挙動が不安定になることがある
ubuntu@k8s-master:~$ sudo swapoff -a
ubuntu@k8s-master:~$ sudo sed -i '/ swap / s/^/#/' /etc/fstab

Docker インストール

ubuntu@k8s-master:~$ sudo apt update && sudo apt install -y docker.io
ubuntu@k8s-master:~$ sudo systemctl enable docker
ubuntu@k8s-master:~$ sudo systemctl start docker
ubuntu@k8s-master:~$ sudo systemctl status docker

cgroupdriver=systemd にすることで、kubelet が Docker のリソース管理と統一した方法で動作

ubuntu@k8s-master:~$ cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl restart docker

Ubuntu 24.04(Noble)は、Ubuntu 22.04(Jammy)と互換性

ubuntu@k8s-master:~$ sudo apt update
ubuntu@k8s-master:~$ sudo apt install -y apt-transport-https ca-certificates curl
ubuntu@k8s-master:~$ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo tee /etc/apt/trusted.gpg.d/kubernetes.asc
ubuntu@k8s-master:~$ echo "deb https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
ubuntu@k8s-master:~$ sudo apt update

Kubernetes のインストール

ubuntu@k8s-master:~$ sudo apt install -y kubelet kubeadm kubectl
ubuntu@k8s-master:~$ sudo systemctl enable kubelet

Master Node で Kubernetes クラスターを作成

# Worker Node を追加するための kubeadm join コマンドが表示される
ubuntu@k8s-master:~$ sudo kubeadm init --pod-network-cidr=192.168.0.0/16
I0310 18:35:35.035243    4576 version.go:256] remote version is much newer: v1.32.2; falling back to: stable-1.30
--- snip ---
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:

sudo kubeadm join <master-ip>:6443 --token <token> \
    --discovery-token-ca-cert-hash sha256:<hash>

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

# 2025/03/10 時点の latest
ubuntu@k8s-master:~$ kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.2/manifests/calico.yaml

Worker Node を Kubernetes クラスターに追加

sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
ubuntu@k8s-master:~$ kubectl get nodes
NAME            STATUS   ROLES           AGE   VERSION
k8s-master      Ready    control-plane   84m   v1.30.10
k8s-worker-01   Ready    <none>          69m   v1.30.10
k8s-worker-02   Ready    <none>          35m   v1.30.10
k8s-worker-03   Ready    <none>          34m   v1.30.10

お掃除

いろいろ遊び終わったら削除

haruki@node01:~$ multipass stop k8s-master k8s-worker-01 k8s-worker-02 k8s-worker-03
haruki@node01:~$ multipass delete k8s-master k8s-worker-01 k8s-worker-02 k8s-worker-03
haruki@node01:~$ multipass list
Name                    State             IPv4             Image
k8s-master              Deleted           --               Ubuntu 24.04 LTS
k8s-worker-01           Deleted           --               Ubuntu 24.04 LTS
k8s-worker-02           Deleted           --               Ubuntu 24.04 LTS
k8s-worker-03           Deleted           --               Ubuntu 24.04 LTS
haruki@node01:~$ multipass purge
haruki@node01:~$ multipass list
No instances found.
haruki@node01:~$

その他

github.com