RaspberryPi 4 に Kubernetes v1.19 をインストールする
はじめに
RaspberryPi4にOS, Dockerのインストールが完了したので、"kubeadm"を利用したKubernetesクラスターを構築していきます。
今回はMaster Node x1 / Worker Node x3 の構築した記録になります。本家のページ非常にわかりやすい手順があるので、細かなところは是非参考にしてください。
インストール環境
HW : Raspberry Pi 4 x4
OS : Ubuntu 20.04.1 LTS
Master node x1 : kube-mstr-01
Worker node x3 : kube-node-[01-03]
Step1. Kubernetes インストール
Kubernetesの各コンポーネントをインストールしていきます。
user$ sudo su
root# apt update && apt install -y apt-transport-https curl
root# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
-----
OK
-----
root# cat <<EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
------------------------------------------------------
deb https://apt.kubernetes.io/ kubernetes-xenial main
------------------------------------------------------
root# apt update
root# apt install -y kubelet kubeadm kubectl
root# apt-mark hold kubelet kubeadm kubectl
-------------------------------------------
kubelet set on hold.
kubeadm set on hold.
kubectl set on hold.
-------------------------------------------
Step2. イニシャルセットアップ @Master node
Master nodeでのみ実行してください。
"kubeadm init"コマンドより、セットアップの事前チェックから始まり、各種必要な設定を行ってくれます。
root# kubeadm init --pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address=192.168.0.90
-----
W1009 15:46:36.350640 7084 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.19.2
[preflight] Running pre-flight checks
[WARNING SystemVerification]: missing optional cgroups: hugetlb
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kube-mstr-01 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.0.90]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [kube-mstr-01 localhost] and IPs [192.168.0.90 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [kube-mstr-01 localhost] and IPs [192.168.0.90 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 39.008793 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node kube-mstr-01 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node kube-mstr-01 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[kubelet-check] Initial timeout of 40s passed.
[bootstrap-token] Using token: zwf2hb.72wghm64m1rtkw8t
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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
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.90:6443 --token zwf2hb.72wghm64m1rtkw8t \
--discovery-token-ca-cert-hash sha256:dea79f21103edf92d6dd7837202ba5c083dc7bb8c25f1571f409b29aebcc031f
↑の標準出力結果に、"kubeadm join ..."の記載があります。
次のStep3にて利用するので消さないでください。
特定のユーザーでkubectlコマンドを実行できように、下記のコマンドを実行し、configファイルをコピーします。
root# exit
user$ mkdir -p $HOME/.kube
user$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
user$ chown $(id -u):$(id -g) $HOME/.kube/config
Master nodeでインストールが完了したので、ステータスを確認します。
"NotReady"状態であることがわかります。
user$ kubectl get node
-----
NAME STATUS ROLES AGE VERSION
kube-mstr-01 NotReady master 99s v1.19.2
次に、Pod間の通信を可能にするアドオンを入れます。
いくつか選択肢がありますが、flannel を利用します。
下記のリンクから、"kubectl apply -f .....” で導入してください。
※下のマニュフェストファイルは、ローカル環境に用意して実行してます。
user$ kubectl apply -f kube-system/manifest/kube-flannel.yaml
-----
podsecuritypolicy.policy/psp.flannel.unprivileged 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
実行完了し、Podの状態を確認してみます。
全てのPodが「Running」状態なら次のステップに進みます。
user$ kubectl get pods -A
-----
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-f9fd979d6-6kxsz 1/1 Running 0 5m7s
kube-system coredns-f9fd979d6-kjszj 1/1 Running 0 5m7s
kube-system etcd-kube-mstr-01 1/1 Running 0 5m12s
kube-system kube-apiserver-kube-mstr-01 1/1 Running 0 5m12s
kube-system kube-controller-manager-kube-mstr-01 1/1 Running 0 5m12s
kube-system kube-flannel-ds-x72b4 1/1 Running 0 65s
kube-system kube-proxy-n7qdd 1/1 Running 0 5m7s
kube-system kube-scheduler-kube-mstr-01 1/1 Running 0 5m12s
Master nodeの状態も「Ready」になっていることを確認します。
user$ kubectl get nodes
-----
NAME STATUS ROLES AGE VERSION
kube-mstr-01 Ready master 5m47s v1.19.2
Step3. クラスタジョイン @Worker node
Worker node にて、クラスターに追加すれば完了です。
先ほど登場した、"kubeadm join ...."コマンドを 実行します。
完了後に、クラスターに登録されていれば完了です。
root# kubeadm join 192.168.0.90:6443 --token zwf2hb.72wghm64m1rtkw8t \
--discovery-token-ca-cert-hash sha256:dea79f21103edf92d6dd7837202ba5c083dc7bb8c25f1571f409b29aebcc031f
-----
[preflight] Running pre-flight checks
[WARNING SystemVerification]: missing optional cgroups: hugetlb
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
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.
おわりに
Master nodeからクラスターの状態を確認します。
各Nodeがクラスターに所属されていることを確認して完了です。
user$ kubectl get nodes -o wide
-----
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
kube-mstr-01 Ready master 38m v1.19.2 192.168.0.90 <none> Ubuntu 20.04.1 LTS 5.4.0-1019-raspi docker://19.3.13
kube-node-01 Ready <none> 30m v1.19.2 192.168.0.91 <none> Ubuntu 20.04.1 LTS 5.4.0-1019-raspi docker://19.3.13
kube-node-02 Ready <none> 30m v1.19.2 192.168.0.92 <none> Ubuntu 20.04.1 LTS 5.4.0-1019-raspi docker://19.3.13
kube-node-03 Ready <none> 28m v1.19.2 192.168.0.93 <none> Ubuntu 20.04.1 LTS 5.4.0-1019-raspi docker://19.3.13
”kubeadm”を利用した、Kubernetesクラスター以上で完了です。
正式ドキュメントもかなり充実しているので、かなり簡単に導入することができます。
この記事が気に入ったらサポートをしてみませんか?