基于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环境

(5)配置文件

root@45b5bc5f4a38:/# containerd config default  #查看默认配置

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客户端创建测试容器

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

2.二进制安装containerd

(1)下载二进制包

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

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

(2)创建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客户端工具扩展

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运行工具

默认连接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)创建容器并指定端口

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.服务器环境准备

(1)开机启动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)优化内核参数

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

> net.bridge.bridge-nf-call-iptables = 1  #容器启动后会创建网桥,内核监听网桥上有过的报文,以实现对报文的安全控制、规则检查,ingress、egress就是通过对报文的检查来决定允许通行或禁止通行

> net.ipv4.ip_forward = 1  #把Linux当作路由器使用,使其具备路由功能,基于路由表做地址转发、报文转发、源地址替换等,否则无法跨主机通信

> EOF

root@ecs-67093:~# sysctl -p

2.安装kubeadm基础环境

(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 -  #导入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)配置认证文件

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)安装网络组件

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

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

(5)添加node节点

root@k8s-node01:/usr/local/src# kubeadm join 192.168.0.173:6443 --token pa2l6x.6mp1hbewr1hr5rwu --discovery-token-ca-cert-hash sha256:d71b81c437de1790cc3aed44b2dd65669d08175bd518b947af28351fe5b5e337  #在node节点执行

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

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

四、部署时遇到的问题

1.添加节点时,calico容器报错

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,189评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,577评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,857评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,703评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,705评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,620评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,995评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,656评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,898评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,639评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,720评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,395评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,982评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,953评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,195评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,907评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,472评论 2 342

推荐阅读更多精彩内容