傻瓜式搭建k8s集群(Kubernetes v1.27.3 + Containerd + Calico v3.25)

1.服務(wù)器準備

準備三臺主機

IP OS Hostname
172.16.1.101 Ubuntu 22.04 k8s-master
172.16.1.102 Ubuntu 22.04 k8s-worker1
172.16.1.103 Ubuntu 22.04 k8s-worker2
1.1 設(shè)置/etc/hosts及各主機的hostname
cat << EOF | sudo tee -a /etc/hosts
172.16.1.101 k8s-master
172.16.1.102 k8s-worker1
172.16.1.103 k8s-worker2
EOF

# 設(shè)置主節(jié)點hostname庆聘,如果是worker節(jié)點钥飞,按上面的名稱進行替換
sudo hostnamectl hostname k8s-master
1.2 主機時間同步
sudo apt install -y chrony
sudo systemctl start chrony
sudo systemctl enable chrony
1.3 各節(jié)點防火墻設(shè)定
sudo ufw disable  && sudo ufw status
1.4 禁用Swap設(shè)備
sudo swapoff -a
sudo sed -ri 's/.*swap.*/#&/' /etc/fstab
1.5 Forwarding IPv4 and letting iptables see bridged traffic
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
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

# Apply sysctl params without reboot
sudo sysctl --system

2.安裝containerd

sudo apt update
sudo apt-get install ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y containerd.io

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
sudo sed -i 's/registry.k8s.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml

sudo systemctl restart containerd
sudo systemctl enable containerd

參考:
https://github.com/containerd/containerd/blob/main/docs/getting-started.md
https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd

3.安裝kubeadm践图、kubelet、kubectl

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

如果主機都是通過虛擬機創(chuàng)建的挡鞍,那么到了這一步,可以采用克隆方式創(chuàng)建其余的主機酌住,再修改IP和hostname叼旋,省去很多麻煩伴挚。

4.初始化主節(jié)點

sudo kubeadm init \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.27.3 \
--control-plane-endpoint=k8s-master \
--pod-network-cidr=10.10.0.0/16

完成后結(jié)果如下靶衍,根據(jù)提示完成進一步的操作。

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master:6443 --token y24d2k.3prnyxd9ltafe01b \
        --discovery-token-ca-cert-hash sha256:f056a04a1105b98929a005322971bb2060fcfa5c29a04a39bfc9d3d6a5a6523f \
        --control-plane

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-master:6443 --token y24d2k.3prnyxd9ltafe01b \
        --discovery-token-ca-cert-hash sha256:f056a04a1105b98929a005322971bb2060fcfa5c29a04a39bfc9d3d6a5a6523f

5.加入worker節(jié)點

$ sudo kubeadm join k8s-master:6443 --token y24d2k.3prnyxd9ltafe01b \
        --discovery-token-ca-cert-hash sha256:f056a04a1105b98929a005322971bb2060fcfa5c29a04a39bfc9d3d6a5a6523f
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: missing optional cgroups: blkio
[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.

回到主節(jié)點茎芋,使用kubectl查看集群中的節(jié)點

$ kubectl get nodes
NAME          STATUS      ROLES           AGE   VERSION
k8s-master    NotReady    control-plane   45m   v1.27.3
k8s-worker1   NotReady    <none>          44m   v1.27.3
k8s-worker2   NotReady    <none>          44m   v1.27.3

STATUS為NotReady狀態(tài)颅眶,這是由于沒有為集群配置網(wǎng)絡(luò)插件。

6.配置網(wǎng)絡(luò)插件

curl https://docs.tigera.io/archive/v3.25/manifests/calico.yaml -O
sed -i "s#192\.168\.0\.0/16#10\.10\.0\.0/16#" calico.yaml
kubectl apply -f calico.yaml

calico.yamlCALICO_IPV4POOL_CIDR需要與k8s集群初始化時指定的pod-network-cidr一致田弥,如果pod-network-cidr剛好為calico中的默認值192.168.0.0/16涛酗,則無需調(diào)整calico.yml。

此外,需要注意一下煤杀,如果calico.yaml文件中以下兩行被注釋掉了眷蜈,那么需要手動取消注釋。

- name: CALICO_IPV4POOL_CIDR
  value: "10.10.0.0/16"

安裝完成后沈自,查看kube-system命名空間下的pod,結(jié)果如下

$ kubectl get pod -n kube-system
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-6c99c8747f-wmz9s   1/1     Running   0          17m
calico-node-65kc4                          1/1     Running   0          17m
calico-node-kk75c                          1/1     Running   0          17m
calico-node-qqm8b                          1/1     Running   0          17m
coredns-7bdc4cb885-8d9bq                   1/1     Running   0          48m
coredns-7bdc4cb885-dhxz2                   1/1     Running   0          48m
etcd-master                                1/1     Running   3          48m
kube-apiserver-master                      1/1     Running   3          49m
kube-controller-manager-master             1/1     Running   3          48m
kube-proxy-7pdx5                           1/1     Running   0          47m
kube-proxy-g7h9c                           1/1     Running   0          47m
kube-proxy-l2kqh                           1/1     Running   0          48m
kube-scheduler-master                      1/1     Running   3          48m

再次查看集群中的節(jié)點辜妓,都已經(jīng)是Ready狀態(tài)了

$ kubectl get nodes
NAME      STATUS   ROLES           AGE   VERSION
master    Ready    control-plane   50m   v1.27.3
worker1   Ready    <none>          49m   v1.27.3
worker2   Ready    <none>          48m   v1.27.3

這里有個注意點枯途,如果使用的是云主機,可能云廠商默認只開放了部分端口籍滴,那么會出現(xiàn)如下情況

$ kubectl get pod -n kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
calico-kube-controllers-7bdbfc669-bxn9g   1/1     Running   0          7m53s
calico-node-5m5x8                         0/1     Running   0          7m53s
calico-node-bpphv                         0/1     Running   0          7m53s
calico-node-lbvq8                         0/1     Running   0          7m53s
coredns-5bbd96d687-8xvvq                  1/1     Running   0          16m
coredns-5bbd96d687-pjwrc                  1/1     Running   0          16m
etcd-master.test.com                      1/1     Running   0          16m
kube-apiserver-master.test.com            1/1     Running   0          16m
kube-controller-manager-master.test.com   1/1     Running   0          16m
kube-proxy-5qjvp                          1/1     Running   0          14m
kube-proxy-87bpn                          1/1     Running   0          16m
kube-proxy-bp6zz                          1/1     Running   0          14m
kube-scheduler-master.test.com            1/1     Running   0          16m

那么只要在所有節(jié)點開放179端口即可酪夷。


至此,k8s集群基本搭建完成了孽惰,除了后續(xù)的Ingress Controller晚岭。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市勋功,隨后出現(xiàn)的幾起案子坦报,更是在濱河造成了極大的恐慌,老刑警劉巖狂鞋,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件片择,死亡現(xiàn)場離奇詭異,居然都是意外死亡骚揍,警方通過查閱死者的電腦和手機字管,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來信不,“玉大人嘲叔,你說我怎么就攤上這事〕榛睿” “怎么了硫戈?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長酌壕。 經(jīng)常有香客問我掏愁,道長,這世上最難降的妖魔是什么卵牍? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任果港,我火速辦了婚禮,結(jié)果婚禮上糊昙,老公的妹妹穿的比我還像新娘辛掠。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布萝衩。 她就那樣靜靜地躺著回挽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪猩谊。 梳的紋絲不亂的頭發(fā)上千劈,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機與錄音牌捷,去河邊找鬼墙牌。 笑死,一個胖子當著我的面吹牛暗甥,可吹牛的內(nèi)容都是我干的喜滨。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼撤防,長吁一口氣:“原來是場噩夢啊……” “哼虽风!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起寄月,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤辜膝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后剥懒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體内舟,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年初橘,在試婚紗的時候發(fā)現(xiàn)自己被綠了验游。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡保檐,死狀恐怖耕蝉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夜只,我是刑警寧澤垒在,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站扔亥,受9級特大地震影響场躯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜旅挤,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一踢关、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧粘茄,春花似錦签舞、人聲如沸秕脓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吠架。三九已至,卻和暖如春搂鲫,著一層夾襖步出監(jiān)牢的瞬間傍药,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工默穴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怔檩,地道東北人。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓蓄诽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親媒吗。 傳聞我的和親對象是個殘疾皇子仑氛,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容