基于kubeadm和containerd部署单master k8s

文章内容:

  • 1、containerd的安装和使用
  • 2、基于kubeadm和containerd部署单master k8s

containerd的安装和使用

1、二进制安装containerd

下载containerd

https://github.com/containerd/containerd

# cd /usr/local/src
# wget https://github.com/containerd/containerd/releases/download/v1.6.6/containerd-1.6.6-linux-amd64.tar.gz
# tar  xvf containerd-1.6.6-linux-amd64.tar.gz
# cp bin/* /usr/local/bin/
service文件

https://github.com/containerd/containerd/blob/main/containerd.service

# vim /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
核对二进制文件路径
ExecStart=/usr/local/bin/containerd
配置文件
mkdir /etc/containerd/
# containerd config default  > /etc/containerd/config.toml
镜像加速 (153行)
     [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
          endpoint = ["https://9916w1ow.mirror.aliyuncs.com"]
启动服务containerd
# systemctl restart containerd
# systemctl enable containerd
# systemctl status  containerd
部署runc

https://github.com/opencontainers/runc

# wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64
# chmod +x runc.amd64
# cp runc.amd64 /usr/bin/runc
下载镜像并运行容器
# ctr images pull docker.io/library/alpine:latest

查看镜像
# ctr images ls

# ctr run -t --net-host  docker.io/library/alpine:latest container sh
containerd客户端工具扩展
crictl

https://github.com/kubernetes-sigs/cri-tools

下载crictl
# wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.24.2/crictl-v1.24.2-linux-amd64.tar.gz
# tar zxf crictl-v1.24.2-linux-amd64.tar.gz
# cp crictl /usr/local/bin/
配置crictl运行环境

默认链接 unix:///var/run/docker.sock
但是containerd在以下路径/run/containerd/containerd.sock

修改配置文件
runtime-endpoint: "unix:///run/containerd/containerd.sock"
image-endpoint: "unix:///run/containerd/containerd.sock"
timeout: 10
debug: false
下载并验证镜像
# crictl  pull nginx:1.20.2
# crictl images list
nerdctl 推荐使用

https://github.com/containerd/nerdctl

# wget https://github.com/containerd/nerdctl/releases/download/v0.22.0/nerdctl-0.22.0-linux-amd64.tar.gz

# tar xvf nerdctl-0.22.0-linux-amd64.tar.gz
nerdctl
containerd-rootless-setuptool.sh
containerd-rootless.sh
# cp  nerdctl /usr/local/bin/
安装cni

https://github.com/containernetworking/plugins

# mkdir -p /opt/cni/bin
# tar xvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
创建容器并指定端口
# nerdctl run -d -p 80:80 --name=nginx-web1 --restart=always nginx

# nerdctl ps


基于kubeadm和containerd部署单master k8s v1.24.x

准备3台虚拟机

一、下载安装containerd
# cd /usr/local/src/
# wget https://github.com/containerd/containerd/releases/download/v1.6.6/containerd-1.6.6-linux-amd64.tar.gz
# tar xvf containerd-1.6.6-linux-amd64.tar.gz
# cp bin/* /usr/local/bin/

# vim /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
# mkdir /etc/containerd/
# containerd config default  > /etc/containerd/config.toml

镜像加速 (153行)

      [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
          endpoint = ["https://9916w1ow.mirror.aliyuncs.com"]

sandbox_image(61行)

sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.7"
systemctl restart containerd && systemctl enable containerd && systemctl status containerd

部署runc

 # wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64
# chmod +x runc.amd64
# cp runc.amd64 /usr/bin/runc
二、安装kubeadm基础环境
2.1:安装kubeadm、kubectl、kubelet

Kubernetes镜像配置方法
https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.3e221b11n5yXhY

apt-get update && apt-get install -y apt-transport-https

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.listdeb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt-get update
apt-cache madison kubeadm

apt-get install -y kubelet=1.24.3-00 kubeadm=1.24.3-00 kubectl=1.24.3-00
安装nerdctl
# wget https://github.com/containerd/nerdctl/releases/download/v0.22.0/nerdctl-0.22.0-linux-amd64.tar.gz

# tar xvf nerdctl-0.22.0-linux-amd64.tar.gz
nerdctl
containerd-rootless-setuptool.sh
containerd-rootless.sh
# cp  nerdctl /usr/local/bin/
安装cni
# wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
# mkdir -p /opt/cni/bin
# tar xvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
三、初始化kubernetes (在管理节点)
3.1、下载镜像
# kubeadm config images list --kubernetes-version v1.24.3

下载镜像 ( 注意镜像下载空间 )
images-down.sh

#/bin/bash
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.3-0
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.8.6
3.2:初始化k8s集群 (在管理节点)
kubeadm init --apiserver-advertise-address=192.168.3.150 --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.aliyuncs.com/google_containers  --ignore-preflight-errors=swap
初始化报错 需要优化内核参数
root@k8s-master1:/usr/local/src# kubeadm init --apiserver-advertise-address=192.168.3.150 --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.aliyuncs.com/google_containers  --ignore-preflight-errors=swap
[init] Using Kubernetes version: v1.24.3
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables does not exist
    [ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
优化内核参数
modprobe br_netfilter
vim /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
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.3.150:6443 --token 837nq7.v32nzy3t88tz6qbz \
    --discovery-token-ca-cert-hash sha256:5b76c3398e18b1e517deaaf4a27eab86537f800a0f72b03c32ff8fa55a05a00a
安装网络组件
kubectl apply -f calico-ipip.yaml
添加node节点

node节点内核优化

modprobe br_netfilter

vim /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
kubeadm join 192.168.3.150:6443 --token 837nq7.v32nzy3t88tz6qbz \
    --discovery-token-ca-cert-hash sha256:5b76c3398e18b1e517deaaf4a27eab86537f800a0f72b03c32ff8fa55a05a00a

kubectl get node
检查pod
kubectl get pod -A


root@k8s-master1:/usr/local/src# kubectl  get node
NAME                      STATUS   ROLES           AGE     VERSION
k8s-master1.example.com   Ready    control-plane   16m     v1.24.3
k8s-master2.example.com   Ready    <none>          2m49s   v1.24.3
k8s-master3.example.com   Ready    <none>          2m44s   v1.24.3
# nerdctl  -n k8s.io images

# kubectl  describe pod calico-kube-controllers-56cdb7c587-q7psp  -n kube-system

报错

Warning  FailedCreatePodSandBox  11m                   kubelet            Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup network for sandbox "94674bddd9039d24174ef42e0afa37719b7e58a3d4460a9e326273a47847bc7d": plugin type="calico" failed (add): error creating calico client: stat /root/.kube/config: no such file or directory
  Warning  FailedCreatePodSandBox  2m56s (x38 over 10m)  kubelet            (combined from similar events): Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup network for sandbox "55da7de2de11eb60b2fd864ee86f0e5ff5f7beb6589f540471fa848007553ed7": plugin type="calico" failed (add): error creating calico client: stat /root/.kube/config: no such file or directory

解决
node节点 kube config文件

mkdir -p /root/.kube
scp /root/.kube/config  192.168.3.152:/root/.kube/
root@k8s-master1:/usr/local/src# kubectl  get pod -A
NAMESPACE     NAME                                              READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-56cdb7c587-q7psp          1/1     Running   0          24m
kube-system   calico-node-bzbfm                                 1/1     Running   0          17m
kube-system   calico-node-csmf7                                 1/1     Running   0          17m
kube-system   calico-node-s2ch7                                 1/1     Running   0          24m
kube-system   coredns-74586cf9b6-mjms6                          1/1     Running   0          30m
kube-system   coredns-74586cf9b6-zsrjf                          1/1     Running   0          30m
kube-system   etcd-k8s-master1.example.com                      1/1     Running   0          30m
kube-system   kube-apiserver-k8s-master1.example.com            1/1     Running   0          30m
kube-system   kube-controller-manager-k8s-master1.example.com   1/1     Running   0          30m
kube-system   kube-proxy-4xxcc                                  1/1     Running   0          17m
kube-system   kube-proxy-5dgw6                                  1/1     Running   0          17m
kube-system   kube-proxy-dppr7                                  1/1     Running   0          30m
kube-system   kube-scheduler-k8s-master1.example.com            1/1     Running   0          30m
测试及验证

k8s创建nginx服务

root@k8s-master1:/usr/local/src# kubectl create  ns myserver
namespace/myserver created
root@k8s-master1:/usr/local/src# kubectl  apply -f nginx.yaml
deployment.apps/myserver-nginx-deployment created
service/myserver-nginx-service created

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

推荐阅读更多精彩内容