AWS EC 2にKubernetesをインストールする


実際、クボネディスはGKEに触れただけで、なぜかクボネディスの運営を直接インストールするプロジェクトがあった.インストール時に思わぬエラーが見られ、公式サイトでも作業方法があちこちにあるので、やり直したい、どこにあるかと思って、進行内容を整理しておきます.

EC 2インスタンスの準備


kubernetes master、workerノードの場合、デフォルトで推奨されるサイズはCPU 2以上、メモリ2 GB以上です.( https://kubernetes.io/ko/docs/setup/production-environment/tools/kubeadm/install-kubeadm/ )
AMIを選択した場合、Amazon Linuxを選択しないでください(docker、kubernetes repoにyumをインストール中にエラーが発生しました).

Red hatイメージを使用しました.

提案されたリソースを満たすために、t 2.mediumの選択

安全なインバウンド・ルールの設定


その後kubeadm joinにならず長い間うろうろしていたので、基本的にサブネット間で通信できるように設定しました.
6443->APIサーバポート(プライマリノード)
ファイルを22->SCPに転送する

プライマリノードの使用


準備作業
ブリッジトラフィックをiptablesに表示させる
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

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

1)Dockerのインストール


https://docs.docker.com/engine/install/#server
CentOS https://docs.docker.com/engine/install/centos/
古いバージョンの削除
 sudo yum remove docker \
                 docker-client \
                 docker-client-latest \
                 docker-common \
                 docker-latest \
                 docker-latest-logrotate \
                 docker-logrotate \
                 docker-engine
repo設定
sudo yum install -y yum-utils
sudo yum-config-manager \
   --add-repo \
   https://download.docker.com/linux/centos/docker-ce.repo
docker install
 sudo yum install docker-ce docker-ce-cli containerd.io
dockerの実行
 sudo systemctl start docker
けんさ
 sudo docker run hello-world
kubernetesの設定
Cgroup管理のためのSystemdの有効化
sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
 "exec-opts": ["native.cgroupdriver=systemd"],
 "log-driver": "json-file",
 "log-opts": {
   "max-size": "100m"
 },
 "storage-driver": "overlay2"
}
EOF
起動時にドッキングステーションを起動して再起動する設定(設定を適用)
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker
設定の確認
Cgroup Driverがsystemdであれば、それはよく応用されています.(デフォルトはcgroupfsですが、kubeletがこの設定と一致しない場合はエラーが発生します.)
docker info | grep Cgroup
Cgroup Driver: systemd
Cgroup Version: 1

2)Kubectl、Kubeadm、Kubeletを取り付ける


kubernetes repo設定とインストール
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

# permissive 모드로 SELinux 설정(효과적으로 비활성화)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

sudo systemctl enable --now kubelet

3)クベドインを行う


インストールが完了すると、kubeadm initを行うことができます.途中でエラーが発生した場合は、kubeadm resetを使用して初期化し、initを使用して操作します.
[root@i-00ffc2795d3930938 workspace]# kubeadm init
[init] Using Kubernetes version: v1.23.5
[preflight] Running pre-flight checks
       [WARNING FileExisting-tc]: tc not found in system path
[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] Using existing ca certificate authority
[certs] Using existing apiserver certificate and key on disk
[certs] Using existing apiserver-kubelet-client certificate and key on disk
[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 [i-00ffc2795d3930938.ap-northeast-2.compute.internal localhost] and IPs [10.0.6.100 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [i-00ffc2795d3930938.ap-northeast-2.compute.internal localhost] and IPs [10.0.6.100 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 7.503097 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kunsparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node i-00ffc2795d3930938.ap-northeast-2.compute.internal as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kernal-load-balancers]
[mark-control-plane] Marking the node i-00ffc2795d3930938.ap-northeast-2.compute.internal as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 7spbr0.xdteoukw4xqd0hjx
[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

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:

kubeadm join 10.0.6.100:6443 --token 7spbr0.xdteoukw4xqd0hjx \
       --discovery-token-ca-cert-hash sha256:7670b8b63583c50432855e26be0cf8038a4e9c3d666675c54799fa62f8cdbeb1
※tokenは24時間後に消失.
# kubeadm token create
# kubeadm token list

Workerノードの使用


メインノード操作1)~2)操作が行われた後、kubeadm initからkubeadm joinが出力される.
[root@i-05ea86c151589ec73 /]#kubeadm join 10.0.6.100:6443 --token 7spbr0.xdteoukw4xqd0hjx         --discovery-token-ca-cert-hash sha256:7670b8b63583c50432855e26be0cf8038a4e9c3d666675c54799fa62f8cdbeb1
[preflight] Running pre-flight checks
        [WARNING FileExisting-tc]: tc not found in system path
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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.
この状態を続行すると、control-planeはノードをクエリーし、workerノードはkubectl getノード情報に失敗します.(APIサーバ情報なし)
プライマリノードに作成されたプロファイルは$HOME/と同じです.kube/configにコピーする必要があります.
デフォルトでは、AWS EC 2では、パスワード入力によりログインがブロックされます.
プライマリノードからworkerノードに接続するためにパスワード検証を解放します.
[root@i-05ea86c151589ec73 ~]# mkdir -p /home/ec2-user/.kube
[root@i-05ea86c151589ec73 ~]# vim /etc/ssh/sshd_config
PasswordAuthentication no -> yes
他の新しいユーザーを作成するか、既存のec 2-userのパスワードを設定します.
(ec 2-userは高い権限を持っているので、新しいユーザーを作成するのは安全かもしれません.完了したら、新しいユーザーを再作成します)
パスワードの検証を阻止できます.)
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
sshデーモンプロセスを再起動します.
[root@i-05ea86c151589ec73 /]# systemctl restart sshd.service
プライマリノードにファイルをコピーします.
[root@i-00ffc2795d3930938 .kube]# scp $HOME/.kube/config [email protected]:/home/ec2-user/.kube
[email protected]'s password:
config      
kubectlコマンドの実行
[ec2-user@i-05ea86c151589ec73 ~]$ kubectl get nodes
NAME                                                  STATUS   ROLES                  AGE     VERSION
i-00ffc2795d3930938.ap-northeast-2.compute.internal   Ready    control-plane,master   3h31m   v1.23.5
i-05ea86c151589ec73.ap-northeast-2.compute.internal   Ready    <none>                 101m    v1.23.5
(他のユーザーは.kube/configファイルを同じ$HOMEディレクトリにコピーして他のユーザーが使用できるようにする必要があります)