基于Ubuntu安裝Kubernetes集群指南

Kubernetes(K8S)做為容器編排的系統(tǒng),被廣泛應(yīng)用于容器、微服務(wù)、服務(wù)網(wǎng)格中。

想體驗、學(xué)習(xí)K8S的話,首先得把它裝好。但由于國內(nèi)的GFW網(wǎng)絡(luò)、公司內(nèi)部局域網(wǎng)的各種限制,安裝過程會出現(xiàn)一些坑。故整理一下新鮮出爐的步驟,盡量把坑填上。

在這安裝過程中,我準(zhǔn)備了兩臺Ubuntu 22.04的系統(tǒng),一臺做為Master,另一臺做為Worker,一起組成一個mini版的集群。

0. 證書問題 - 可選

如果你在公司內(nèi)部的局域網(wǎng),而且通過公司提供的代理服務(wù)器上網(wǎng),那么,多半需要此步驟。否則會遇到各種自簽名證書驗證失敗的問題。

此步驟,兩臺機器都需要做。

具體的證書下載,各個公司都不一樣,請咨詢你們家的IT人員。下載完之后,執(zhí)行:update-ca-certificates。

# 下載你家的自簽名證書
$ sudo cp 自簽名證書.crt /usr/local/share/ca-certificates/
$ sudo update-ca-certificates

這樣,可以避免后續(xù)的各種證書驗證失敗。

1. 安裝Docker

此步驟,兩臺機器都需要做。

具體安裝步驟請參考:

Install Docker Engine on Ubuntu | Docker Documentation

2. 禁用Swap

此步驟,兩臺機器都需要做。

分兩步,一步時臨時禁用,并馬上生效;另一步是永久性禁用。

$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

3. 加載內(nèi)核模塊

此步驟,兩臺機器都需要做。

$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

$ sudo modprobe overlay
$ sudo modprobe br_netfilter

4. 設(shè)置sysctl 參數(shù)

此步驟,兩臺機器都需要做。

$ 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

5. 安裝K8S

此步驟,兩臺機器都需要做。

$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl
$ sudo curl --insecure -fsSLo https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
$ sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl

目前最新版的K8S是1.27.1。這些步驟做完之后,會被安裝到兩臺機器上。

6. 修改containerd配置

此步驟,兩臺機器都需要做。

主要修改兩個地方,一個是從阿里云下載鏡像;另一個是啟用systemd。

第一個修改是啟用systemd:

$ containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
$ sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml

第二個修改是把遠(yuǎn)程下載地址從Google家的改為阿里云的:

$ sudo vim /etc/containerd/config.toml

把這行:sandbox_image = "registry.k8s.io/pause:3.6"

改成:sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"

其實用sed,跟第一個systemd的修改一起做了也行。

然后重啟containerd:

$ sudo systemctl restart containerd

這一步如果不做的話,后面的kubeadm init會因為無法下載鏡像而一直失敗。

7. 初始化Master節(jié)點

此步驟,僅需要在Master節(jié)點上操作。

sudo kubeadm init --apiserver-advertise-address 192.168.111.128 --pod-network-cidr 10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers

因為后面打算用Flannel來作為CNI,所以CIDR按要求配置了:10.244.0.0/16。

如果一切正常的話,可以看到這樣的輸出:

[init] Using Kubernetes version: v1.27.1
[preflight] Running pre-flight checks
[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'
W0421 15:26:43.924760    8817 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.1, falling back to the nearest etcd version (3.5.7-0)
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local ycwang-ubuntu] and IPs [10.96.0.1 192.168.111.128]
[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 [localhost ycwang-ubuntu] and IPs [192.168.111.128 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost ycwang-ubuntu] and IPs [192.168.111.128 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"
W0421 15:26:46.894146    8817 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.1, falling back to the nearest etcd version (3.5.7-0)
[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 5.502175 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" 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 ycwang-ubuntu as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node ycwang-ubuntu as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: sd27s0.dpnlrf96at6uwgl0
[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 192.168.111.128:6443 --token sd27s0.dpnlrf96at6uwgl0 \
??????--discovery-token-ca-cert-hash sha256:7a6acd******3e154c

這樣,集群的Master節(jié)點基本上好了。

8. 配置Master節(jié)點

此步驟,僅需要在Master節(jié)點上操作。

根據(jù)上面的輸出,在Master節(jié)點輸入命令,使得可以用非root用戶操作kubectl:

$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

繼續(xù)輸入這些命令,實現(xiàn)Shell命令自動補全:

$ echo 'source <(kubectl completion bash)' >> ~/.bashrc
$ source ~/.bashrc

9. 安裝Flannel網(wǎng)絡(luò)

此步驟,僅需要在Master節(jié)點上操作。

在Master節(jié)點上,安裝Flannel:

$ kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

至此,Master節(jié)點的安裝、配置都結(jié)束了。

下一步,把Worker節(jié)點加入集群。

10. Worker加入集群

此步驟,僅需要在Worker節(jié)點上操作。

如果你有多臺Worker節(jié)點,每個結(jié)點都需要做一次。

把第7步的join命令Copy出來,然后執(zhí)行:

$ sudo kubeadm join 192.168.111.128:6443 --token sd27s0.dpnlrf96at6uwgl0 \
??????--discovery-token-ca-cert-hash sha256:7a6acd******3e154c

一切正常的話,可以看到這樣的輸出,表明該機器已經(jīng)加入集群了:

[preflight] Running pre-flight checks
[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.

11. 查看集群信息

回到Master節(jié)點,可以看到集群已經(jīng)建立:

$ kubectl get nodes
NAME                   STATUS     ROLES           AGE   VERSION
ycwang-ubuntu          NotReady   control-plane   30m   v1.27.1
ycwang-ubuntu-worker   NotReady   <none>          43s   v1.27.1

一開始的時候,狀態(tài)都是NotReady。

稍等片刻,等需要的鏡像都下載、運行之后,就會全部變成Ready了。

$ kubectl get nodes
NAME                   STATUS   ROLES           AGE     VERSION
ycwang-ubuntu          Ready    control-plane   33m     v1.27.1
ycwang-ubuntu-worker   Ready    <none>          3m23s   v1.27.1

可以看到所有的Pod都在正常運行了:

$ kubectl get pods --all-namespaces -o wide
NAMESPACE      NAME                                    READY   STATUS    RESTARTS   AGE     IP                NODE                   NOMINATED NODE   READINESS GATES
kube-flannel   kube-flannel-ds-gl795                   1/1     Running   0          6m10s   192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-flannel   kube-flannel-ds-lq8p8                   1/1     Running   0          6m10s   192.168.111.129   ycwang-ubuntu-worker   <none>           <none>
kube-system    coredns-7bdc4cb885-lgsvw                1/1     Running   0          36m     10.244.1.3        ycwang-ubuntu-worker   <none>           <none>
kube-system    coredns-7bdc4cb885-ss4n5                1/1     Running   0          36m     10.244.1.2        ycwang-ubuntu-worker   <none>           <none>
kube-system    etcd-ycwang-ubuntu                      1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-apiserver-ycwang-ubuntu            1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-controller-manager-ycwang-ubuntu   1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-proxy-wtg74                        1/1     Running   0          6m48s   192.168.111.129   ycwang-ubuntu-worker   <none>           <none>
kube-system    kube-proxy-x4s95                        1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-scheduler-ycwang-ubuntu            1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>

這樣,整個集群搭建成功??梢蚤_始使用了。

驗證通過后,可以把上述命令合到一起,保存成一個Shell文件。以后每次執(zhí)行該文件即可。

歡迎來到微服務(wù)、云原生的時代!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容