ubuntu 20.4安装k8s 1.24.0、1.28.0(使用containerd)
ubuntu 20.4安装k8s 1.24.0
目录
前言
环境:ubuntu20.4 、k8s 1.24、containerd
本篇来讲解如何在ubuntu20.4下使用kubeadm安装部署k8s 1.24集群,经过验证,部署1.28.0版本本文档依然合适。
服务器初始化、环境准备
准备3台虚拟机,1个master,2个node节点。
主机 | 说明 |
---|---|
192.168.118.145 | master节点,能连外网,ubuntu 20.04版本,至少2核CPU,2G内存 |
192.168.118.146 | node1节点,能连外网,ubuntu 20.04版本,至少2核CPU,2G内存 |
192.168.118.147 | node2节点,能连外网,ubuntu 20.04版本,至少2核CPU,2G内存 |
3台主机都根据实际情况做如下6大步骤配置
注意:3台虚拟机使用的都是root账号,所以命令前面均没有加sudo 命令
# 1、关闭防火墙
#ufw查看当前的防火墙状态:inactive状态是防火墙关闭状态 active是开启状态
ufw status
#启动、关闭防火墙
ufw disable
# 2、禁用selinux
#默认ubunt默认是不安装selinux的,如果没有selinux命令和配置文件则说明没有安装selinux,则下面步骤就不用做了
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#3、关闭swap分区(必须,因为k8s官网要求)
#注意:最好是安装虚拟机时就不要创建swap交换分区**
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
# 4、设置主机名
cat >> /etc/hosts <<EOF
192.168.118.145 master
192.168.118.146 node1
192.168.118.147 node2
EOF
# 5、时间同步
#查看时区,时间
date
#先查看时区是否正常,不正确则替换为上海时区
timedatectl set-timezone Asia/Shanghai
#安装chrony,联网同步时间
apt install chrony -y && systemctl enable --now chronyd
# 6、将桥接的IPv4流量传递到iptables的链
#(有一些ipv4的流量不能走iptables链,因为linux内核的一个过滤器,每个流量都会经过他,然后再匹配是否可进入当前应用进程去处理,所以会导致流量丢失),配置k8s.conf文件(k8s.conf文件原来不存在,需要自己创建的)
touch /etc/sysctl.d/k8s.conf
cat >> /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
vm.swappiness=0
EOF
sysctl --system
# 7、设置服务器之间免密登陆(3台彼此之间均设置)
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.118.146
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.118.147
ssh node1
ssh node2
使用kubeadm安装k8s(本篇讲解使用kubeadm安装k8s)
以上6大步骤在每一台虚拟机做完之后,开始安装k8s。本篇讲解使用kubeadm安装k8s),kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具,这个工具能通过两条指令完成一个kubernetes集群的部署。
1、创建一个master节点,kubeadm init。
2、将node节点加入kubernetes集群,kubeadm join <master_IP:port >。
步骤一、安装containerd
(在所有节点服务器上都执行,因为k8s 1.24版本默认CRI为containerd,cri称之为容器运行时插件)
containerd的官网:https://containerd.io/downloads/
containerd官网安装教程:https://github.com/containerd/containerd/blob/main/docs/getting-started.md
,官网安装文档提供了源码包安装和普通的yum、apt-get安装,这里使用源码包安装。
#安装containerd
wget -c https://github.com/containerd/containerd/releases/download/v1.6.8/containerd-1.6.8-linux-amd64.tar.gz
tar -xzvf containerd-1.6.8-linux-amd64.tar.gz
#解压出来一个bin目录,containerd可执行文件都在bin目录里面
mv bin/* /usr/local/bin/
#使用systemcd来管理containerd
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
mv containerd.service /usr/lib/systemd/system/
systemctl daemon-reload && systemctl enable --now containerd
systemctl status containerd
#安装runc
#runc是容器运行时,runc实现了容器的init,run,create,ps...我们在运行容器所需要的cmd:
curl -LO https://github.com/opencontainers/runc/releases/download/v1.1.1/runc.amd64 && \
install -m 755 runc.amd64 /usr/local/sbin/runc
#安装 CNI plugins
wget -c https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
#根据官网的安装步骤来,创建一个目录用于存放cni插件
mkdir -p /opt/cni/bin
tar -xzvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
#修改containerd的配置,因为containerd默认从k8s官网拉取镜像
#创建一个目录用于存放containerd的配置文件
mkdir -p /etc/containerd
#把containerd配置导出到文件
containerd config default | sudo tee /etc/containerd/config.toml
#修改配置文件
vim /etc/containerd/config.toml
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2" #搜索sandbox_image,把原来的k8s.gcr.io/pause:3.6改为"registry.aliyuncs.com/google_containers/pause:3.2"
SystemdCgroup = true #搜索SystemdCgroup,把这个false改为true
config_path = "/etc/containerd/certs.d" #搜索config_path,配置镜像加速地址(这是一个目录下面创建)
#创建镜像加速的目录
mkdir /etc/containerd/certs.d/docker.io -pv
#配置加速
cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://b9pmyelo.mirror.aliyuncs.com"]
capabilities = ["pull", "resolve"]
EOF
#加载containerd的内核模块
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
#重启containerd
systemctl restart containerd
systemctl status containerd
#拉取镜像,测试containerd是否能创建和启动成功
ctr i pull docker.io/library/nginx:alpine #能正常拉取镜像说明没啥问题
ctr images ls #查看镜像
ctr c create --net-host docker.io/library/nginx:alpine nginx #创建容器
ctr task start -d nginx #启动容器,正常说明containerd没啥问题
ctr containers ls #查看容器
ctr tasks kill -s SIGKILL nginx #终止容器
ctr containers rm nginx #删除容器
步骤二、配置kubernetes的阿里云apt源(所有节点服务器都需要执行)
#更改源,ubuntu 20.04(focal) (选做)
cp /etc/apt/sources.list /etc/apt/sources.list.backup
cat > /etc/apt/sources.list <<EOF
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF
apt update
apt install apt-transport-https ca-certificates -y
apt install vim lsof net-tools zip unzip tree wget curl bash-completion pciutils gcc make lrzsz tcpdump bind9-utils -y
# 编辑镜像源文件,文件末尾加入阿里云k8s镜像源配置
echo 'deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main' >> /etc/apt/sources.list
#更新证书
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
#更新源
apt update
步骤三、yum安装kubeadm、kubelet、kubectl(所有节点都执行)
#在3台虚拟机上都执行安装kubeadm、kubelet、kubectl
#查看apt可获取的kubeadm版本,这里安装1.24.0版本,不指定版本的话默认安装最新版本
apt-cache madison kubeadm
#在所有节点上安装kubeadm、kubelet、kubectl
apt install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00
#设置kubelet开机自启(先不用启动,也起不了,后面kubeadm init初始化master时会自动拉起kubelet)
systemctl enable kubelet
步骤四、初始化master节点的控制面板
#列出所需镜像,可以提前拉取镜像
kubeadm config images list --kubernetes-version=v1.28.0 --image-repository=registry.aliyuncs.com/google_containers
kubeadm config images pull --kubernetes-version=v1.28.0 --image-repository=registry.aliyuncs.com/google_containers
#下载的镜像默认存放在containerd的k8s.io命名空间
#kubeadm init --help可以查看命令的具体参数用法
kubeadm init \
--apiserver-advertise-address=192.168.118.145 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.24.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
#最后kubeadm init初始化成功,提示信息如下:
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.118.145:6443 --token nrefdp.2mtcwkkshizkj1qa \
--discovery-token-ca-cert-hash sha256:564dbb8ec1993f3e38f3b757c324ad6190950156f30f89f7f7d4b244d2b29ec7
#我们根据输入的提示信息复制粘贴照着做即可
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
步骤五、将node节点加入k8s集群
在步骤四初始化完成master节点之后会提示你在node节点执行如下的命令来将node节点加入k8s集群,如下所示,复制它到node节点执行即可;
注意:这段kubeamd join命令的token只有24h,24h就过期,需要执行kubeadm token create --print-join-command
重新生成。
#node节点执行
kubeadm join 192.168.118.145:6443 --token nrefdp.2mtcwkkshizkj1qa \
--discovery-token-ca-cert-hash sha256:564dbb8ec1993f3e38f3b757c324ad6190950156f30f89f7f7d4b244d2b29ec7
步骤六、部署容器网络,CNI网络插件
#下载calico
wget https://docs.projectcalico.org/manifests/calico.yaml
#编辑文件,找到下面这两句,去掉注释,修改ip为当前你设置的pod ip段
vim calico.yaml
- name: CALICO_IPV4POOL_CIDR
value: "10.244.0.0/16"
#镜像拉取没有问题的话最好
kubectl apply -f calico.yaml
calico镜像拉取出现问题
(k8s 1.28.0无此问题,不知道是不是因为部署1.28版本是containerd配置文件没有配置镜像加速器的原因)
calico的镜像总是拉取不下来,报错如下:
Dec 02 21:58:49 node2 containerd[7285]: time="2022-12-02T21:58:49.627179374+08:00" level=error msg="Failed to handle backOff event &ImageCreate{Name:docker.io/calico/cni:v3.24.5,Labels:map[string]string{io.cri-containerd.image: managed,},XXX_unrecognized:[],} for docker.io/calico/cni:v3.24.5" error="update image store for \"docker.io/calico/cni:v3.24.5\": get image info from containerd: get image diffIDs: unexpected media type text/html for sha256:0238205317023105e6589e4001e2a3d81b84c71740b0d9563f6157ddb32c4ea4: not found"
Dec 02 21:58:50 node2 containerd[7285]: time="2022-12-02T21:58:50.627858264+08:00" level=info msg="ImageCreate event &ImageCreate{Name:docker.io/calico/kube-controllers:v3.24.5,Labels:map[string]string{io.cri-containerd.image: managed,},XXX_unrecognized:[],}"
Dec 02 21:58:50 node2 containerd[7285]: time="2022-12-02T21:58:50.627954441+08:00" level=error msg="Failed to handle backOff event &ImageCreate{Name:docker.io/calico/kube-controllers:v3.24.5,Labels:map[string]string{io.cri-containerd.image: managed,},XXX_unrecognized:[],} for docker.io/calico/kube-controllers:v3.24.5" error="update image store for \"docker.io/calico/kube-controllers:v3.24.5\": get image info from containerd: get image diffIDs: unexpected media type text/html for sha256:0238205317023105e6589e4001e2a3d81b84c71740b0d9563f6157ddb32c4ea4: not found"
Dec 02 21:59:01 node2 containerd[7285]: time="2022-12-02T21:59:01.446288924+08:00" level=info msg="Events for \"docker.io/calico/cni:v3.24.5\" is in backoff, enqueue event &ImageDelete{Name:docker.io/calico/cni:v3.24.5,XXX_unrecognized:[],}"
使用ctr命令能正常拉取下来,但是使用ctr拉取下来的镜像即使放到了k8s.io命名空间,仍是不能使用的:
ctr -n k8s.io i pull docker.io/calico/cni:v3.24.5
ctr -n k8s.io i pull docker.io/calico/kube-controllers:v3.24.5
ctr -n k8s.io i pull docker.io/calico/node:v3.24.5
必须要使用crictl 命令拉取的镜像,k8s才能使用,这一点有待研究,但是使用crictl 命令总是报错:
crictl pull docker.io/calico/cni:v3.24.5
crictl pull docker.io/calico/kube-controllers:v3.24.5
crictl pull docker.io/calico/node:v3.24.5
crictl pull docker.io/calico/node:v3.24.5 #总是报这种错误
E1202 21:25:47.261446 7560 remote_image.go:242] "PullImage from image service failed" err="rpc error: code = NotFound desc = failed to pull and unpack image \"docker.io/calico/node:v3.24.5\": failed to unpack image on snapshotter overlayfs: unexpected media type text/html for sha256:0c95980b6412e81bd466d0f2f7846f828c62417db9880081fef92f3b9d9d11f5: not found" image="docker.io/calico/node:v3.24.5"
FATA[0008] pulling image: rpc error: code = NotFound desc = failed to pull and unpack image "docker.io/calico/node:v3.24.5": failed to unpack image on snapshotter overlayfs: unexpected media type text/html for sha256:0c95980b6412e81bd466d0f2f7846f828c62417db9880081fef92f3b9d9d11f5: not found
解决办法:
尝试将
vim /etc/containerd/certs.d/docker.io/hosts.toml
server = "https://docker.io"
[host."https://xxxmyelo.mirror.aliyuncs.com"] #这个配置为自己阿里云的镜像加速地址
capabilities = ["pull", "resolve"]
root@node2:~# systemctl stop containerd.service && systemctl start containerd.service
还是不行。
听网友说,删掉了步骤一中的配置的containerd镜像加速器
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "" #改回空地址
root@node2:~# systemctl stop containerd.service && systemctl start containerd.service
居然可以了,这是为什么呢,百思不得其解,反正现在是正常了,calico的镜像能正常拉取了。
步骤七、配置kubectl命令自动补全
# kubectl 配置命令自动补全,master节点配置即可
apt install -y bash-completion
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc
kubectl describe nodes
步骤八、测试k8s集群
在k8s中创建一个pod,验证是否正常运行:
#创建一个httpd服务测试
kubectl create deployment httpd --image=httpd
#暴露服务,端口就写80,如果你写其他的可能防火墙拦截了
kubectl expose deployment httpd --port=80 --type=NodePort
#查看pod是否是Running状态,查看service/httpd的端口
kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/httpd-757fb56c8d-w42l5 1/1 Running 0 39s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpd NodePort 10.109.29.1 <none> 80:32569/TCP 42s #外部端口32569
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h22m
#网页测试访问,使用master节点的IP或者node节点的IP都可以访问,端口就是32569
http://192.168.118.145:32569/
It works! #成功了
更多推荐
所有评论(0)