0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ubuntu20.04でk8sクラスタを構築(kubeadm)

Posted at

ホスト名設定

hostnamectl set-hostname k8s1
# hostnamectl set-hostname k8s2
# hostnamectl set-hostname k8s3
# hostnamectl set-hostname k8s4

IPアドレスとホスト名の紐付け

cat << _EOF_ | sudo tee -a /etc/hosts
172.24.20.40  k8s1
172.24.20.41  k8s2
172.24.20.42  k8s3
172.24.20.43  k8s4
_EOF_

Docker/kubernetesを動作させるための設定

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

sudo sysctl --system

Cgroupの設定

ラズパイに設定しないのであればいらない?

依存する関連パッケージのインストール

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common socat conntrack ipset

プロキシの設定

apt-getやaptのインストールが出来ない場合がある。以下の設定を入れる

nano /etc/hosts

Acquire::http::No-Cache true;
Acquire::http::Pipeline-Depth 0;

containerdのインストール

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | 

##アーキテクチャの設定は適切に
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt update
sudo apt-get -y install containerd.io

再起動後にもcontainerdで利用するカーネルモジュールを有効

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

カーネルモジュールのロード、アンロードを行う

sudo modprobe overlay
sudo modprobe br_netfilter

containerdの設定ファイルを編集

containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd

Kubernetesパッケージのインストール

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat << _EOF_ | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
_EOF_

sudo apt-get update
apt-get install -y kubelet=1.23.3-00 kubeadm=1.23.3-00 kubectl=1.23.3-00
apt-mark hold kubelet kubeadm kubectl

swapオフ

sudo swapoff -a

kubeadmの設定

nano /etc/systemd/system/kubelet.service.d/10-allow-swap.conf

[Service]
Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"

systemctl daemon-reload

Kubeletの設定

systemctl start kubelet
systemctl enable kubelet

ファイアーウォールの無効化(状況に応じて)

systemctl stop ufw

Kubernetesクラスタの初期化(MASTER)

MasterNodeだけで初期化コマンドを実行する。

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint=k8s1 --apiserver-cert-extra-sans=k8s1

kubeletに関するエラー

ここでkubeletに関するエラーが出た。

kubeadmのリセット

kubeadm reset
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint=k8s1 --apiserver-cert-extra-sans=k8s1

出力

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s1:6443 --token 867qpj.1dwsg5x8e003ihyi \
        --discovery-token-ca-cert-hash sha256:2dc50ac606f40bb12d4eb1ac0af2bfc852a47be83f3ad1b4f8f75c69338dca1c \
        --control-plane

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

kubeadm join k8s1:6443 --token 867qpj.1dwsg5x8e003ihyi \
        --discovery-token-ca-cert-hash sha256:2dc50ac606f40bb12d4eb1ac0af2bfc852a47be83f3ad1b4f8f75c69338dca1c

出力されたコマンドをWorkerに打ち込む
コントロールプレーンを追加したかったら別途上のコマンドを打ち込む

kubectl利用する認証情報のファイルをデフォルトで読み込まれるパスにコピー

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

Workerノードの組み込み

クラスタへの参加コマンドを実行する。

kubeadm join k8s1:6443 --token 867qpj.1dwsg5x8e003ihyi \
        --discovery-token-ca-cert-hash sha256:2dc50ac606f40bb12d4eb1ac0af2bfc852a47be83f3ad1b4f8f75c69338dca1c

MasterのみFlannelの設定

sudo sysctl net.bridge.bridge-nf-call-iptables=1

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?