基于kubeadm和containerd部署k8s

一、Containerd簡介和安裝

1.apt安裝containerd

(1)驗證倉庫版本

root@45b5bc5f4a38:/# apt update

root@45b5bc5f4a38:/# apt-cache madison containerd

(2)安裝containerd

root@45b5bc5f4a38:/# apt install containerd

(3)查看Service文件

root@ecs-67093:~# cat /lib/systemd/system/containerd.service

# Copyright The containerd Authors.

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#? ? http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

[Unit]

Description=containerd container runtime

Documentation=https://containerd.io

After=network.target local-fs.target

[Service]

ExecStartPre=-/sbin/modprobe overlay

ExecStart=/usr/bin/containerd

Type=notify

Delegate=yes

KillMode=process

Restart=always

RestartSec=5

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNPROC=infinity

LimitCORE=infinity

LimitNOFILE=infinity

# Comment TasksMax if your systemd version does not supports it.

# Only systemd 226 and above support this version.

TasksMax=infinity

OOMScoreAdjust=-999

[Install]

WantedBy=multi-user.target

(4)驗證runc環(huán)境

(5)配置文件

root@45b5bc5f4a38:/# containerd config default? #查看默認(rèn)配置

root@45b5bc5f4a38:/# mkdir /etc/containerd

root@45b5bc5f4a38:/# containerd config default > /etc/containerd/config.toml

root@45b5bc5f4a38:/#?systemctl restart containerd.service

(6)下載鏡像

root@ecs-67093:~# ctr images pull docker.io/library/alpine:latest

(7)驗證鏡像

root@ecs-67093:~# ctr images ls

(8)ctr客戶端創(chuàng)建測試容器

root@ecs-67093:~# ctr run -t --net-host docker.io/library/alpine:latest test-container sh

2.二進(jìn)制安裝containerd

(1)下載二進(jìn)制包

root@ecs-67093:~# tar xvf containerd-1.6.6-linux-amd64.tar.gz

root@ecs-67093:~# cp bin/* /usr/local/bin/

(2)創(chuàng)建service文件

root@ecs-67093:~# cat /lib/systemd/system/containerd.service

# Copyright The containerd Authors.

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#? ? http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

[Unit]

Description=containerd container runtime

Documentation=https://containerd.io

After=network.target local-fs.target

[Service]

ExecStartPre=-/sbin/modprobe overlay

ExecStart=/usr/local/bin/containerd

Type=notify

Delegate=yes

KillMode=process

Restart=always

RestartSec=5

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNPROC=infinity

LimitCORE=infinity

LimitNOFILE=infinity

# Comment TasksMax if your systemd version does not supports it.

# Only systemd 226 and above support this version.

TasksMax=infinity

OOMScoreAdjust=-999

[Install]

WantedBy=multi-user.target

(3)配置文件

root@ecs-67093:~# mkdir /etc/containerd

root@ecs-67093:/usr/local/src# containerd config default > /etc/containerd/config.toml

root@ecs-67093:~# cat /etc/containerd/config.toml

61? ? ? ? sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.7"

153? ? ? [plugins."io.containerd.grpc.v1.cri".registry.mirrors]

154? ? ? ? [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]

155? ? ? ? ? endpoint = ["https://e1bqjzsj.mirror.aliyuncs.com"]

root@ecs-67093:/usr/local/src# systemctl restart containerd

root@ecs-67093:/usr/local/src# systemctl enable containerd

root@ecs-67093:/usr/local/src# systemctl status containerd

(4)部署runc(v1.1.3)

root@ecs-67093:/usr/local/src# chmod +x runc.amd64

root@ecs-67093:/usr/local/src# cp runc.amd64 /usr/bin/runc

(5)下載鏡像并運行容器

root@ecs-67093:/usr/local/src# ctr images pull docker.io/library/alpine:latest

root@ecs-67093:/usr/local/src# ctr run -t --net-host docker.io/library/alpine:latest container1 sh

/ # ping www.baidu.com

PING www.baidu.com (110.242.68.3): 56 data bytes

64 bytes from 110.242.68.3: seq=0 ttl=47 time=12.022 ms

64 bytes from 110.242.68.3: seq=1 ttl=47 time=11.948 ms

64 bytes from 110.242.68.3: seq=2 ttl=47 time=11.914 ms

二、Containerd客戶端工具擴(kuò)展

1.crictl

(1)下載crictl

root@ecs-67093:/usr/local/src# tar xvf crictl-v1.24.2-linux-amd64.tar.gz

root@ecs-67093:/usr/local/src# cp crictl /usr/local/bin/

(2)配置crictl運行工具

默認(rèn)連接unix:///var/run/dockershim.sock

但是containerd在以下路徑:

root@ecs-67093:/usr/local/src# ls /run/containerd/containerd.sock

/run/containerd/containerd.sock

所以,修改配置文件

root@ecs-67093:/usr/local/src# cat /etc/crictl.yaml

runtime-endpoint: "unix:///run/containerd/containerd.sock"

image-endpoint: "unix:///run/containerd/containerd.sock"

timeout: 10

debug: false

(3)下載并驗證鏡像

root@ecs-67093:/usr/local/src# crictl pull nginx:1.20.2

Image is up to date for sha256:50fe74b50e0d0258922495297efbb9ebc3cbd5742103df1ca54dc21c07d24575

root@ecs-67093:/usr/local/src# crictl images ls

IMAGE? ? ? ? ? ? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? SIZE

docker.io/library/nginx? 1.20.2? ? ? ? ? ? ? 50fe74b50e0d0? ? ? 56.7MB

2.nerdctl-推薦使用

(1)安裝nerdctl

root@ecs-67093:/usr/local/src# tar xvf nerdctl-0.18.0-linux-amd64.tar.gz

nerdctl

containerd-rootless-setuptool.sh

containerd-rootless.sh

root@ecs-67093:/usr/local/src# cp nerdctl /usr/local/bin/

root@ecs-67093:/usr/local/src# nerdctl version

(2)安裝cni

root@ecs-67093:/usr/local/src# mkdir /opt/cni/bin -p

root@ecs-67093:/usr/local/src# tar xvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/

(3)創(chuàng)建容器并指定端口

nginx:

root@ecs-67093:/usr/local/src# nerdctl run -d -p 80:80 --name=nginx-web1 --restart=always nginx:1.20.2

root@ecs-67093:/usr/local/src# nerdctl ps

root@ecs-67093:/usr/local/src# nerdctl exec -it c1b91b522807 bash

tomcat:

root@ecs-67093:/usr/local/src# nerdctl run -d -p 8080:8080 --name=tomcat-web1 --restart=always tomcat:7.0.88-alpine

root@ecs-67093:/usr/local/src# nerdctl ps

三、基于kubeadm和containerd部署k8s

1.服務(wù)器環(huán)境準(zhǔn)備

(1)開機(jī)啟動br_netfilter模塊

root@ecs-67093:~# cat << EOF >> /etc/rc.local

> #!/bin/bash

> modprobe br_netfilter

> EOF

root@ecs-67093:~# chmod a+x /etc/rc.local

root@ecs-67093:~# systemctl restart rc-local

root@ecs-67093:~# lsmod | grep br_netfilter

(2)優(yōu)化內(nèi)核參數(shù)

root@ecs-67093:~# cat << EOF >> /etc/sysctl.conf

> net.bridge.bridge-nf-call-iptables = 1? #容器啟動后會創(chuàng)建網(wǎng)橋坛猪,內(nèi)核監(jiān)聽網(wǎng)橋上有過的報文,以實現(xiàn)對報文的安全控制、規(guī)則檢查毒费,ingress、egress就是通過對報文的檢查來決定允許通行或禁止通行

> net.ipv4.ip_forward = 1? #把Linux當(dāng)作路由器使用愈魏,使其具備路由功能觅玻,基于路由表做地址轉(zhuǎn)發(fā)、報文轉(zhuǎn)發(fā)培漏、源地址替換等溪厘,否則無法跨主機(jī)通信

> EOF

root@ecs-67093:~# sysctl -p

2.安裝kubeadm基礎(chǔ)環(huán)境

(1)安裝kubeadm、kubectl牌柄、kubelet

master上安裝kubeadm畸悬、kubectl、kubelet珊佣;node上安裝kubeadm蹋宦、kubelet,kubectl可以不安裝

root@ecs-67093:~# apt-get update && apt-get install -y apt-transport-https? #支持https源

root@ecs-67093:~# curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -? #導(dǎo)入key

root@ecs-67093:~# cat <<EOF >/etc/apt/sources.list.d/kubernetes.list

> deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main

> EOF? #配置源

root@ecs-67093:~# apt-get update

root@ecs-67093:~# apt-cache madison kubeadm

root@ecs-67093:~# apt-get install -y kubeadm=1.24.3-00 kubectl=1.24.3-00 kubelet=1.24.3-00

(2)安裝nerdctl

(3)安裝cni

3.初始化kubernetes

(1)下載鏡像

root@ecs-67093:~# kubeadm config images list --kubernetes-version v1.24.3

k8s.gcr.io/kube-apiserver:v1.24.3

k8s.gcr.io/kube-controller-manager:v1.24.3

k8s.gcr.io/kube-scheduler:v1.24.3

k8s.gcr.io/kube-proxy:v1.24.3

k8s.gcr.io/pause:3.7

k8s.gcr.io/etcd:3.5.3-0

k8s.gcr.io/coredns/coredns:v1.8.6

root@ecs-67093:~# cat image-down.sh

%s/k8s.gcr.io/nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com\/google_containers/g

(2)初始化k8s集群

root@ecs-67093:~# kubeadm init --apiserver-advertise-address=192.168.0.75 --apiserver-bind-port=6443 --kubernetes-version=v1.24.3 --pod-network-cidr=10.100.0.0/16 --service-cidr=10.200.0.0/16 --service-dns-domain=cluster.local --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --ignore-preflight-errors=swap

......

Your Kubernetes control-plane has initialized successfully!

......

(3)配置認(rèn)證文件

root@ecs-67093:~# mkdir -p $HOME/.kube

root@ecs-67093:~# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

root@ecs-67093:~# sudo chown $(id -u):$(id -g) $HOME/.kube/config

root@ecs-67093:~# kubectl get node

(4)安裝網(wǎng)絡(luò)組件

root@ecs-67093:~# kubectl apply -f calico-ipip.yaml

root@k8s-master01:~# nerdctl -n k8s.io images

(5)添加node節(jié)點

root@k8s-node01:/usr/local/src# kubeadm join 192.168.0.173:6443 --token pa2l6x.6mp1hbewr1hr5rwu --discovery-token-ca-cert-hash sha256:d71b81c437de1790cc3aed44b2dd65669d08175bd518b947af28351fe5b5e337? #在node節(jié)點執(zhí)行

root@k8s-node01:/usr/local/src# mkdir /root/.kube

root@k8s-master01:~# scp /root/.kube/config 192.168.0.42:/root/.kube/

四咒锻、部署時遇到的問題

1.添加節(jié)點時冷冗,calico容器報錯

root@ecs-67093:~# find / -name *ubeadm*

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市惑艇,隨后出現(xiàn)的幾起案子蒿辙,更是在濱河造成了極大的恐慌,老刑警劉巖滨巴,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件思灌,死亡現(xiàn)場離奇詭異,居然都是意外死亡恭取,警方通過查閱死者的電腦和手機(jī)泰偿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秽荤,“玉大人甜奄,你說我怎么就攤上這事柠横。” “怎么了课兄?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵牍氛,是天一觀的道長。 經(jīng)常有香客問我烟阐,道長搬俊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任蜒茄,我火速辦了婚禮唉擂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘檀葛。我一直安慰自己玩祟,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布屿聋。 她就那樣靜靜地躺著空扎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪润讥。 梳的紋絲不亂的頭發(fā)上转锈,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機(jī)與錄音楚殿,去河邊找鬼撮慨。 笑死,一個胖子當(dāng)著我的面吹牛脆粥,可吹牛的內(nèi)容都是我干的砌溺。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼冠绢,長吁一口氣:“原來是場噩夢啊……” “哼抚吠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起弟胀,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喊式,沒想到半個月后孵户,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡岔留,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年夏哭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片献联。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡竖配,死狀恐怖何址,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情进胯,我是刑警寧澤用爪,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站胁镐,受9級特大地震影響偎血,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜盯漂,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一颇玷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧就缆,春花似錦帖渠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至羞延,卻和暖如春渣淳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背伴箩。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工入愧, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人嗤谚。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓棺蛛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親巩步。 傳聞我的和親對象是個殘疾皇子旁赊,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

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