Ubuntu上安装部署k8s集群
此处非常感谢 江城琉璃梦同学对我的帮助。
一、基础环境准备
(一)环境说明
ubutun 20.04 TLS
kubernetes: v1.25.0
- kubectl v1.25.0
- kubeadm v1.25.0
- kubelet v1.25.0
Docker:20.10.21
1.主机说明
#master
master-100 192.168.16.100#worker
worker-200 192.168.16.200
(二)环境操作
Tips:所有主机都需要进行以下操作
1. 设置Master与工作节点的机器名称及配置
#IP地址和主机名称修改
hostname $hostname
sudo hostnamectl set-hostname master-100
2. 解析主机
sudo gedit /etc/hosts
3. 写入以下内容(注意IP地址和主机名换成自己的):
192.168.16.100 master-100
192.168.16.200 node-200
4. 虚拟内存swap分区关闭
sudo swapoff -a && sudo sed -i 's/^\/swap.img\(.*\)$/#\/swap.img \1/g' /etc/fstab && free
5. 开启防火墙的端口
很多教程是直接关闭防火墙,事实上,关闭防火墙会在kubeadm过程中出现超时现象。
sudo ufw enable
sudo ufw allow 6443
sudo ufw status
重启主机。。。重启后注意查看swap是否关闭。。。
6. 开启IPv4转发
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
7. 设置时间同步
sudo apt install -y chrony
sudo systemctl restart chrony
sudo systemctl status chrony
chronyc sources
8. 在各个主机中安装 docker软件
(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
9. 安装 cri-dockerd 组件
一般来说下载最新的就行:
进入以下网址(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
10. 安装Kubernetes
(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/kubelet
复制以下内容,保存退出
KUBELET_KUBEADM_ARGS="--container-runtime=remote --container-runtime-endpoint=/run/cri-dockerd.sock
(7)启动kubelet
systemctl enable --now kubelet
以上内容是所有节点都要执行的,下面的操作步骤会分master节点和Node节点
二、单实例K8s集群部署
(一)Master节点初始化
1. 查看初始化需要的镜像
sudo kubeadm config images list --image-repository registry.aliyuncs.com/google_containers
2. 拉取镜像
sudo kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers --cri-socket unix:///run/cri-dockerd.sock
3. 初始化节点
注意这里的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节点加入集群。
4. 配置环境变量
还是在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
5. 切换普通模式
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
(二)安装网络插件flannel
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
(三)Node节点,加入集群
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