在本篇教學中,我們將介紹如何在 Oracle Linux 9 上安裝和設定 Kubernetes 環境。本文將引導你逐步完成 Docker、Kubeadm、Kubectl 和 Kubelet 的安裝,並使用 Kubeadm 設定 Kubernetes Cluster。
首先,確保你的 Oracle Linux 9 系統是最新版本。使用以下指令來進行系統更新:
sudo dnf upgrade -y
Kubernetes 需要一個 Container Runtimes,我們將使用 Docker 作為 Container Runtimes。首先,添加 Docker 儲存庫:
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
接著安裝 Docker:
sudo dnf install docker-ce docker-ce-cli containerd.io -y
本範例中安裝的版本與套件如下所示:
Docker CE Stable - x86_64 131 kB/s | 53 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
containerd.io x86_64 1.7.19-3.1.el9 docker-ce-stable 43 M
docker-ce x86_64 3:27.1.1-1.el9 docker-ce-stable 27 M
docker-ce-cli x86_64 1:27.1.1-1.el9 docker-ce-stable 7.8 M
Installing weak dependencies:
docker-buildx-plugin x86_64 0.16.1-1.el9 docker-ce-stable 14 M
docker-ce-rootless-extras x86_64 27.1.1-1.el9 docker-ce-stable 4.0 M
docker-compose-plugin x86_64 2.29.1-1.el9 docker-ce-stable 13 M
Transaction Summary
================================================================================
Install 6 Packages
Total download size: 109 M
Installed size: 422 M
安裝後,修改 /etc/containerd/config.toml
檔案,避免 CRI 被停用,並確保使用所需的 sandbox image(registry.k8s.io/pause:3.9
):
containerd config default | tee /etc/containerd/config.toml
sed -i 's/sandbox_image = "registry.k8s.io\/pause:3.8"/sandbox_image = "registry.k8s.io\/pause:3.9"/g' /etc/containerd/config.toml
啟動並啟用 Docker:
sudo systemctl enable --now docker
驗證 Docker 安裝是否成功:
sudo docker run hello-world
如果安裝成功,你將看到以下的執行結果示例:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
首先,添加 Kubernetes 儲存庫:
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/repodata/repomd.xml.key
EOF
接著,將 SELinux 設置為 Permissive 模式,因為 Kubernetes 不完全支援啟用的 SELinux:
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
然後安裝 Kubeadm、Kubectl 和 Kubelet:
sudo dnf install -y kubelet kubeadm kubectl
我們的安裝使用版本範例如下所示:
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
kubeadm x86_64 1.30.3-150500.1.1 kubernetes 10 M
kubectl x86_64 1.30.3-150500.1.1 kubernetes 10 M
kubelet x86_64 1.30.3-150500.1.1 kubernetes 17 M
Installing dependencies:
conntrack-tools x86_64 1.4.7-2.el9 ol9_appstream 261 k
cri-tools x86_64 1.30.0-150500.1.1 kubernetes 8.6 M
kubernetes-cni x86_64 1.4.0-150500.1.1 kubernetes 6.7 M
libnetfilter_cthelper x86_64 1.0.0-22.el9 ol9_appstream 23 k
libnetfilter_cttimeout x86_64 1.0.0-19.el9 ol9_appstream 23 k
libnetfilter_queue x86_64 1.0.5-1.el9 ol9_appstream 29 k
socat x86_64 1.7.4.1-5.el9_4.2 ol9_appstream 328 k
Transaction Summary
================================================================================
Install 10 Packages
Total download size: 54 M
Installed size: 293 M
啟用並立即執行 Kubelet 服務:
sudo systemctl enable --now kubelet
使用 Kubeadm 初始化控制平面(Control Plane):
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
為了讓非 root 用戶能使用 kubectl,請執行以下指令設定 kubectl 指令行工具:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
部署 Pod 網路外掛模組(例如 Flannel):
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
成功執行後的結果如下所示:
namespace/kube-flannel created
serviceaccount/flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
你也可以使用 ifconfig
檢查網路狀態,會發現增加了 flannel.1,結果如下所示:
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.244.0.0 netmask 255.255.255.255 broadcast 0.0.0.0
inet6 fe80::f054:2bff:fecd:c852 prefixlen 64 scopeid 0x20<link>
ether f2:54:2b:cd:c8:52 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 13 overruns 0 carrier 0 collisions 0
如果你有其他節點想要加入 Cluster,可以在控制平面上取得加入的指令,並在工作節點上運行該指令:
kubeadm token create --print-join-command
驗證所有節點是否已就緒:
kubectl get nodes
執行後,你將看到當前狀態如下:
NAME STATUS ROLES AGE VERSION
k8s-cp Ready control-plane 13d v1.30.3
驗證所有系統 Pod 是否運行正常:
kubectl get pods -n kube-system
到目前為止,你已成功在 Oracle Linux 9 上安裝並設定了 Kubernetes 環境,並學會了如何使用 Kubeadm 來初始化和設定 Kubernetes Cluster。接下來,你可以開始創建和管理各種 Kubernetes 資源,並進一步學習 Kubernetes 的進階功能。這些步驟將為你提供一個穩定的 Kubernetes 環境,使你能夠進一步學習和實踐 Kubernetes 的各種功能。