ubutun 20.04 TLS
kubernetes: v1.25.0
- kubectl v1.25.0
- kubeadm v1.25.0
- kubelet v1.25.0
Docker:20.10.21
#master
master-100 192.168.16.100#worker
worker-200 192.168.16.200
Tips:所有主机都需要进行以下操作
#IP地址和主机名称修改
hostname $hostname
sudo hostnamectl set-hostname master-100
sudo gedit /etc/hosts
192.168.16.100 master-100
192.168.16.200 node-200
sudo swapoff -a && sudo sed -i 's/^\/swap.img\(.*\)$/#\/swap.img \1/g' /etc/fstab && free
很多教程是直接关闭防火墙,事实上,关闭防火墙会在kubeadm过程中出现超时现象。
sudo ufw enable
sudo ufw allow 6443
sudo ufw status
重启主机。。。重启后注意查看swap是否关闭。。。
sudo cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
sudo 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
sudo sysctl --system
sudo apt install -y chrony
sudo systemctl restart chrony
sudo systemctl status chrony
chronyc sources
(1)卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
(2)更新apt包索引并安装包以允许apt在HTTPS上使用存储库
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
(3)添加Docker官方GPG密钥 # -fsSL
curl https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
(4)设置稳定存储库
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
(5)安装特定版本的Docker引擎,请在repo中列出可用的版本
apt-cache madison docker-ce
docker-ce | 5:20.10.21~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.20~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.19~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.18~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.17~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.16~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.8~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.7~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.6~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.5~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.4~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.3~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.2~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.1~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:20.10.0~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.15~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.14~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.13~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.12~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.11~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.10~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
docker-ce | 5:19.03.9~3-0~ubuntu-focal | https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
然后
sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io
(6)修改docker启动项
mkdir -vp /etc/docker/
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://reg-mirror.qiniu.com",
"https://registry.docker-cn.com"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
(7)保存退出,更新设置
sudo systemctl daemon-reload
sudo systemctl start docker
sudo systemctl enable docker
(8)查看docker版本和状态
sudo docker version
sudo systemctl status docker
一般来说下载最新的就行:
进入以下网址(https://github.com/Mirantis/cri-dockerd/releases),下载cri组件(版本看自己选择),这里选择0.26。
找到想要的版本,点进去,找到对印的安装包下载,Ubuntu应该下载deb包,CentOS是rpm包。
同时,还要注意版本代号,Ubuntu18.04代号是Bionic,22.04.1是jammy。
注意包名!! 一般输入到cri三个字母以后使用TAB键补全即可。
sudo dpkg -i cri-dockerd_0.2.6.3-0.ubuntu-jammy_amd64.deb
(1)调整启动参数,注意此处,后续报错可能会回来改
sudo sed -i -e 's#ExecStart=.*#ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7#g' /lib/systemd/system/cri-docker.service
(2)设置开机自启动
sudo systemctl daemon-reload
sudo systemctl enable cri-docker
(3)查看cri-docker状态
sudo systemctl status cri-docker
(1)安装依赖
sudo apt-get install -y apt-transport-https ca-certificates curl
(2)安装GPG密匙
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg
sudo echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] http://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
(3)更新软件源
sudo apt-get update
(4)查看Kubernetes可用版本
apt-cache madison kubeadm
注意:如果Ubuntu使用不同的软件源,对软件版本的命名会有所不同,aliyun的源在结尾会有 -00下载对应版本的kubernetes组件,这里下载1.25.0-00, 有需要可以更换其他版本
sudo apt-get install -y kubelet=1.25.0-00 kubeadm=1.25.0-00 kubectl=1.25.0-00
(5)标记软件包,防止自动更新
sudo apt-mark hold kubelet kubeadm kubectl
(6)配置kubelet
mkdir /etc/sysconfig
sudo gedit /etc/sysconfig/jubelet
复制以下内容,保存退出
KUBELET_KUBEADM_ARGS="--container-runtime=remote --container-runtime-sudoendpoint=/run/cri-dockerd.sock
(7)启动kubelet
systemctl enable --now kubelet
以上内容是所有节点都要执行的,下面的操作步骤会分master节点和Node节点
sudo kubeadm config images list --image-repository registry.aliyuncs.com/google_containers
sudo kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers --cri-socket unix:///run/cri-dockerd.sock
注意这里的ip地址要换成你的master节点的ip地址,k8s的版本也要换成你下载的版本(此处不要加-00,尽管你下载的包名带-00)。切换到root模式再执行:
sudo su
sudo kubeadm init --control-plane-endpoint=192.168.16.100 --kubernetes-version=v1.25.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --token-ttl=0 --cri-socket unix:///run/cri-dockerd.sock --upload-certs --image-repository registry.aliyuncs.com/google_containers
此时出现如下界面就配置好了:
记住kubeadm join那条指令,后续用于node节点加入集群。
还是在su环境下:
export KUBECONFIG=/etc/kubernetes/admin.conf
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
wget https://github.com/flannel-io/flannel/releases/download/v0.20.1/flanneld-amd64
sudo mkdir /opt/bin
sudo cp flanneld-amd64 /opt/bin/flanneld
sudo chmod +x /opt/bin/flanneld
继续执行:
sudo kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
等待一会,查看状态(root模式执行)结果应该会变成ready
kubectl get nodes
wget https://github.com/flannel-io/flannel/releases/download/v0.20.1/flanneld-amd64
mkdir /opt/bin
cp flanneld-amd64 /opt/bin/flanneld
chmod +x /opt/bin/flanneld
加入节点,利用刚才保存的命令:(末尾加上–cri-socket unix:///run/cri-dockerd.sock)
kubeadm join 192.168.16.100:6443 --token hkzgsi.omynmdnk9vy0wkxt \
--discovery-token-ca-cert-hash sha256:aee605c8ac29e7b8ea678020ca410833715a8d01a262f506b6dc6a53fb24be5d \
--cri-socket unix:///run/cri-dockerd.sock
kubeadm join 192.168.16.100:6443 --token hkzgsi.omynmdnk9vy0wkxt \
--discovery-token-ca-cert-hash sha256:aee605c8ac29e7b8ea678020ca410833715a8d01a262f506b6dc6a53fb24be5d \
--control-plane --certificate-key e5d40240c1d77691602358bfface8dc44dd98fe2c8d75c513f484c4ceafd7584
不出问题的话,主节点在root下执行
kubectl get nodes