ちょっと検証することが有り、CPが1台、Workerが1台の小さいKubernetesクラスターを作成したのでメモ。
VMなど
・CP、WorkerともにCPU 2個、メモリ 8GB、HDD 20GB。VMware Workstation上の仮想マシン。
・ホスト名はc1とw1。IPアドレスはc1:192.168.0.204/24、w1:192.168.0.205/24。
上記をOSインストール画面中で設定済み。OSインストール手順は割愛。
・インターネット接続有り。
CPノードへのKubernetesインストール
kubeadmでKubernetesをインストール。現時点Kubernetesの最新版は1.28だが、例によってcri-oが最新版を提供しないのでそれに引きずられて1.27。
https://v1-27.docs.kubernetes.io/docs/setup/production-environment/tools/kubeadm/
ネットワークプラグインにCalicoを使うが、仮想マシンのIPアドレスが192.168.0.0/24なのでKuberntesの内部アドレスは172.16.0.0/16にする。
c1サーバーにrootでssh接続して、以下を実行。
# dnf remove -y zram-generator-defaults
# swapoff -a
# systemctl disable --now firewalld
# cat <<EOF | sudo tee /etc/modules-load.d/k8s.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.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# sudo sysctl --system
# export VERSION=1.27
# dnf module enable -y cri-o:$VERSION
# dnf install -y cri-o
# systemctl enable --now crio
# 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
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
# setenforce 0
# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# export VERSION=1.27.6-0.x86_64
# yum install -y kubelet-$VERSION kubeadm-$VERSION kubectl-$VERSION --disableexcludes=kubernetes
# systemctl enable --now kubelet
# kubeadm init --pod-network-cidr=172.16.0.0/16
kubeinitの以下のような出力を取っておく。
...
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 192.168.0.204:6443 --token jnxqqu.l4o0yexgvg4edwj2 \
--discovery-token-ca-cert-hash sha256:676773b011a4b8db932c5bbeb690bbc3e4a5f0f61de73e04d513b1ad59025f36
続けてネットワークプラグインのインストール。以下を実行。プラグインはCalico。
# mkdir -p $HOME/.kube
# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# chown $(id -u):$(id -g) $HOME/.kube/config
7つのPodがRunningになるのを待つ。
[root@c1 ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-5d78c9869d-hsffj 1/1 Running 0 3m13s
kube-system coredns-5d78c9869d-pfmrc 1/1 Running 0 3m13s
kube-system etcd-c1 1/1 Running 0 3m26s
kube-system kube-apiserver-c1 1/1 Running 0 3m26s
kube-system kube-controller-manager-c1 1/1 Running 0 3m26s
kube-system kube-proxy-vb782 1/1 Running 0 3m13s
kube-system kube-scheduler-c1 1/1 Running 0 3m26s
Calicoオペレーターをインストールする。
https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart
以下を実行。
# kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.3/manifests/tigera-operator.yaml
カスタムリソースは、読み込ませる前にサブネットを修正する。
# wget https://raw.githubusercontent.com/projectcalico/calico/v3.26.3/manifests/custom-resources.yaml
# sed -i -e "s/192\.168\.0\.0/172.16.0.0/" custom-resources.yaml
# kubectl create -f custom-resources.yaml
しばらく、calico-apiserverが起動するまで待つ。
[root@c1 ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
calico-apiserver calico-apiserver-dff568db5-p7bmm 1/1 Running 0 32s
calico-apiserver calico-apiserver-dff568db5-zk2q8 1/1 Running 0 32s
calico-system calico-kube-controllers-7df47db7f-lphl6 1/1 Running 0 2m7s
calico-system calico-node-plpsl 1/1 Running 0 2m7s
calico-system calico-typha-66c6b9fb45-lfvsc 1/1 Running 0 2m7s
calico-system csi-node-driver-qvs6x 2/2 Running 0 2m7s
kube-system coredns-5d78c9869d-hsffj 1/1 Running 0 12m
kube-system coredns-5d78c9869d-pfmrc 1/1 Running 0 12m
kube-system etcd-c1 1/1 Running 0 12m
kube-system kube-apiserver-c1 1/1 Running 0 12m
kube-system kube-controller-manager-c1 1/1 Running 0 12m
kube-system kube-proxy-vb782 1/1 Running 0 12m
kube-system kube-scheduler-c1 1/1 Running 0 12m
tigera-operator tigera-operator-f6bb878c4-nbzdv 1/1 Running 0 5m
WorkerノードへのKubernetesインストール
w1サーバーにrootでssh接続して、以下を実行。
kubeadm join
の行は先のkubeadm initの出力からコピーしたもの。
# dnf remove -y zram-generator-defaults
# swapoff -a
# systemctl disable --now firewalld
# cat <<EOF | sudo tee /etc/modules-load.d/k8s.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.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# sudo sysctl --system
# export VERSION=1.27
# dnf module enable -y cri-o:$VERSION
# dnf install -y cri-o
# systemctl enable --now crio
# 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
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
# setenforce 0
# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# export VERSION=1.27.6-0.x86_64
# yum install -y kubelet-$VERSION kubeadm-$VERSION kubectl-$VERSION --disableexcludes=kubernetes
# systemctl enable --now kubelet
# kubeadm join 192.168.0.204:6443 --token jnxqqu.l4o0yexgvg4edwj2 \
--discovery-token-ca-cert-hash sha256:676773b011a4b8db932c5bbeb690bbc3e4a5f0f61de73e04d513b1ad59025f36
kubeadm joinの出力の最後は以下のようになる。別にこの出力を取っておく必要は無いが。
...
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.
動作確認
CPノードのsshプロンプトで以下を実行し、ノードが2台あることを確認する。
# kubectl get node
NAME STATUS ROLES AGE VERSION
c1 Ready control-plane 26m v1.27.6
w1 Ready <none> 3m8s v1.27.6
nginxのPodを1つ作ってみる。
# kubectl create ns test
# cat << EOF > pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: test
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
EOF
# kubectl create -f pod.yaml
Podが動いていることを確かめる。
# kubectl get pod -n test -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 49s 172.16.190.65 w1 <none> <none>
NodePortでKubernetesサービスを作る。
# cat << EOF > svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: test
spec:
type: NodePort
selector:
app: nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
EOF
# kubectl create -f svc.yaml
curlで手元のPCのコマンドプロンプトからnginxにアクセスしてみる。
# curl http://192.168.0.205:30080
...
<h1>Welcome to nginx!</h1>
...
「192.168.0.205」はWorkerノードだが、CPノードの「192.168.0.204」に対してもアクセスは出来る。
# curl http://192.168.0.204:30080
...
<title>Welcome to nginx!</title>
...
Ingressコントローラー等の導入は、今回の目的になっている検証では使用しないので割愛。