Kubernetesお勉強その1【準備】Shellスクリプトで(kubeadm+CRI-O)を構築
Dockerは一通り遊んだので、今はやりのコンテナの本命kubernetesに挑戦してみようと考えた。まぁクラウドのGKEとかEKSとかAKSとかクラウドのやつを使えばかなり楽に立てられるんだけどVMで動かすことになるので課金が発生し、そのままクラウド破産につながると怖いので課金が発生しないローカルのVM環境で構築することにした。もともとHyper-VでDocker入のUbuntu20.04LTSイメージを作っているのでそいつを使用する前提です。DockerがKubernetesの推奨CRIから外れるらしいので今回はCRI-Oを利用することにしました。後、なんか複数回使う可能性があるかもしれんので簡易Shellスクリプトにしています。相変わらずヨワヨワなので参考サイトのコピペばかりです。コマンド一発でできるので何回か試行錯誤できそうです。
◆Version等
20.04.3 LTS (Focal Fossa) Docker version 20.10.10, build b485636 docker-compose version 1.29.1, build c34c88b2 Version: 0.1.0 RuntimeName: cri-o RuntimeVersion: 1.22.0 RuntimeApiVersion: v1alpha2 Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"clean", BuildDate:"2021-10-27T18:41:28Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"} kubeadm version: &version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"clean", BuildDate:"2021-10-27T18:40:11Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"}
◆構築手順
1.初期設定用Shellスクリプト
Shellスクリプトを実行すると設定完了後に再起動するのでターミナルを閉じて再接続が必要
$ sudo vi previous_setting.sh ------------------------ #/bin/bash CMDNAME=`basename $0` if [ $# -ne 4 ]; then echo "Usage: ${CMDNAME} hostipadress gatewayip hostname domain" 1>&2 exit 1 fi HOSTIP=$1 GATEWAYIP=$2 HOSTNAME=$3 DOMAIN=$4 NETWORK=$(echo ${GATEWAYIP} | sed -e "s/\.\([^.]*\)$/.0\/24/") ## Firewall port open sudo ufw allow from ${NETWORK} to any port 6443 sudo ufw allow from ${NETWORK} to any port 10250 sudo ufw allow from ${NETWORK} to any port 10251 sudo ufw allow from ${NETWORK} to any port 10252 sudo ufw allow from ${NETWORK} to any port 8080 sudo ufw allow from ${NETWORK} to any proto tcp port 2379:2380 sudo ufw allow from ${NETWORK} to any proto tcp port 30000:32767 ## Replace ip addr sudo cp /dev/null /etc/netplan/00-installer-config.yaml sudo tee /etc/netplan/00-installer-config.yaml <<EOF # This is the network config written by 'subiquity' network: ethernets: eth0: addresses: - ${HOSTIP}/24 gateway4: ${GATEWAYIP} nameservers: addresses: - 8.8.8.8 - 8.8.4.4 version: 2 EOF ## Set hostname sudo hostnamectl set-hostname ${HOSTNAME}.${DOMAIN} sudo cp /dev/null /etc/hosts sudo tee /etc/hosts <<EOF 127.0.0.1 localhost 127.0.0.1 ${HOSTNAME} ${HOSTNAME}.${DOMAIN} 127.0.1.1 ${HOSTNAME} # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters EOF ## Update install package sudo apt update sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade ## swap off sudo swapoff -a sudo sed -i -e 's!/swap.img!#/swap.img!g' /etc/fstab ## Reflect setting echo " Close your terminal please " sudo netplan apply sudo shutdown -r now ------------------------ $ sudo chmod 755 previous_setting.sh $ sudo ./previous_setting.sh 192.168.0.100 192.168.0.1 kube-srv yourdomain.local
2.kubernetes準備用Shellスクリプト
OS再起動完了後に実行する。最後に出力されるTokenは忘れずにメモしておくこと
$ sudo vi kurbenetes_setting.sh ------------------------ #/bin/bash USER=$(echo ${SUDO_USER:-$USER}) UID=$(echo ${SUDO_UID:-$SUDO_UID}) GID=$(echo ${SUDO_GID:-$SUDO_GID}) HOME=/home/${USER} CMDNAME=`basename $0` if [ $# -ne 4 ]; then echo "Usage: ${CMDNAME} nodetype(master or sub) mainnodeip mainnodehostname domain " 1<&2 exit 1 fi NODETYPE=$1 MASTERIP=$2 MASTERNAME=$3 DOMAIN=$4 # Check the following site to find out the latest version of Kubernetes. # https://kubernetes.io/releases/notes/ OS=xUbuntu_20.04 VERSION=1.22 NETWORK=10.1.0.0/16 # Setting CRI-O echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/${OS}/ /" | sudo tee -a /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/${VERSION}/${OS}/ /" | sudo tee -a /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:${VERSION}.list curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:${VERSION}/${OS}/Release.key | sudo apt-key add - curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/${OS}/Release.key | sudo apt-key add - sudo apt-get -y update sudo apt-get install -y cri-o cri-o-runc sudo systemctl daemon-reload sudo systemctl start crio sudo systemctl enable crio # Install kubeadm echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl cri-o cri-o-runc # Setting kubeadm echo 'KUBELET_EXTRA_ARGS=--cgroup-driver=systemd --container-runtime=remote --container-runtime-endpoint="unix:///var/run/crio/crio.sock"' | sudo tee /etc/default/kubelet sudo systemctl daemon-reload sudo systemctl restart kubelet # Setting Node mkdir -p ${HOME}/.kube if [ "$NODETYPE" = "master" ]; then # Main node sudo kubectl taint nodes --all node-role.kubernetes.io/master- curl https://docs.projectcalico.org/manifests/calico.yaml -O cp -ap calico.yaml calico.yaml.org sed -i -e "s?192.168.0.0/16?${NETWORK}?g" calico.yaml sudo kubeadm init --cri-socket /var/run/crio/crio.sock --node-name ${MASTERNAME} --pod-network-cidr=${NETWORK} sudo cp -i /etc/kubernetes/admin.conf ${HOME}/.kube/config sudo chown ${UID}:${GID} ${HOME}/.kube/config kubectl apply -f calico.yaml kubectl get nodes else # Sub node echo "\n${MASTERIP} ${MASTERNAME} ${MASTERNAME}.${DOMAIN}" | sudo tee -a /etc/hosts fi ------------------------ $ sudo chmod 755 kurbenetes_setting.sh $ sudo ./kurbenetes_setting.sh master 192.168.0.100 kube-srv yourdomain.local
◆参考サイト
・kubernetes
https://kubernetes.io/ja/docs/concepts/overview/what-is-kubernetes/
https://snowsystem.net/container/kubernetes/kubernetes-build-env/
https://kubernetes.io/releases/notes/
https://blog.inductor.me/entry/2020/12/03/061329
・CRI-O関連
https://github.com/cri-o/cri-o
https://www.grugrut.net/posts/202007111344/
https://www.kimullaa.com/entry/2021/05/07/204706
・kubeadm、kubelet、kubectlのインストール
https://zenn.dev/onsd/articles/258d0af9251526
https://kubernetes.io/ja/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
https://kumano-te.com/activities/build-k8s-dev-cluster-with-kubeadm
https://qiita.com/t_ume/items/f0a1c1ae8f94add8f4ba
https://thinkit.co.jp/article/18188
・シェルスクリプト
https://qiita.com/seigot/items/f481445b9bd917e21f4a
https://atmarkit.itmedia.co.jp/ait/articles/1709/07/news016.html
・パッケージ自動更新
https://blog.jicoman.info/2017/01/autoupgrade_apt-get_dpkg/
これでまぁKubernetesのお勉強できる準備が整ったので、本格的にお勉強を開始していこうかなと企てています。次回はとりあえずネットワーク周りとかやっていこうと思ったりなんかしている。
コメント