1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KubernetesをUbuntuに構築

Posted at

前置き

ここではkubeadmによるKubernetesのインストール手順を解説します。
今回はUbuntu 22.04を利用します。
クリーンな環境でないと動かないかもしれないです。
CNIも同時に設定します。
CNIをcilium以外にしたい方は最後のhelmのとことより適宜設定する必要があります

手順

Swap

sudo sed -i '/swap/d' /etc/fstab
sudo swapoff -a

Kernel

# 起動時に読み込むカーネルモジュールの設定
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
# カーネルモジュールの読み込み
sudo modprobe overlay
sudo modprobe br_netfilter
# カーネルパラメーターの設定
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
vm.overcommit_memory                = 1
vm.panic_on_oom                     = 0
kernel.panic                        = 10
kernel.panic_on_oops                = 1
kernel.keys.root_maxkeys            = 1000000
kernel.keys.root_maxbytes           = 25000000
EOF
# カーネルパラメーターの設定を適用
sudo sysctl --system

Containerd

# Dockerからcontainerdをインストール
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install -y containerd.io
# containerdの設定
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
if grep -q "SystemdCgroup = true" "/etc/containerd/config.toml"; then
  echo "Config found, skip rewriting..."
else
  sudo sed -i -e "s/SystemdCgroup \= false/SystemdCgroup \= true/g" /etc/containerd/config.toml
fi
# 設定の適用
sudo systemctl restart containerd

Kubernetes

# kubeadmなどをインストール
sudo apt-get install -y apt-transport-https
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
# 構成のconfigを作成
mkdir -p $HOME/.kube
cat <<EOF | tee $HOME/.kube/init_config.yaml
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
bootstrapTokens:
- token: "$(openssl rand -hex 3).$(openssl rand -hex 8)"
  description: "kubeadm bootstrap token"
  ttl: "24h"
nodeRegistration:
  criSocket: "unix:///var/run/containerd/containerd.sock"
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
controllerManager:
  extraArgs:
    bind-address: "0.0.0.0" # Used by Prometheus Operator
scheduler:
  extraArgs:
    bind-address: "0.0.0.0" # Used by Prometheus Operator
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: "systemd"
protectKernelDefaults: true
EOF
# kubernetesを構築
sudo kubeadm init --config ~/.kube/init_config.yaml
# kubectlを使えるように設定
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# helmをインストール
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
# helmでcilium(cni)をインストール
helm repo add cilium https://helm.cilium.io/
helm install cilium cilium/cilium --namespace kube-system

最終確認

nodeの確認

kubectl get node

下のように表示されればOK!

NAME        STATUS   ROLES           AGE   VERSION
master-01   Ready    control-plane   1m   v1.30.2

podの確認

kubectl get pods -A

下のように表示されればOK!
シングルノードの時はciliumが1つPendingになっているのは正常です

NAMESPACE     NAME                                READY   STATUS    RESTARTS   AGE
kube-system   cilium-bgtn7                        1/1     Running   0          6m7s
kube-system   cilium-operator-65496b9554-fgcnz    1/1     Running   0          6m7s
kube-system   cilium-operator-65496b9554-s9x5t    0/1     Pending   0          6m7s
kube-system   coredns-7db6d8ff4d-7cx7n            1/1     Running   0          11m
kube-system   coredns-7db6d8ff4d-d56zn            1/1     Running   0          11m
kube-system   etcd-ap-hsg-01                      1/1     Running   0          11m
kube-system   kube-apiserver-ap-hsg-01            1/1     Running   0          11m
kube-system   kube-controller-manager-ap-hsg-01   1/1     Running   0          11m
kube-system   kube-proxy-mb4d9                    1/1     Running   0          11m
kube-system   kube-scheduler-ap-hsg-01            1/1     Running   0          11m

適宜設定

シングルノードだとmasterしかいないため、通常のdeploymentはスケジュールできないです。その制限を下のコマンドで外せます。

kubectl get node -o json \
  | jq '.items[].metadata.name' \
  | awk 'system("kubectl taint node "$1" node-role.kubernetes.io/control-plane:NoSchedule-")'

まとめ

Kubernetesは意外と簡単に構築できます
これを機にぜひお試しを〜

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?