CentOS 7.9 环境下搭建k8s集群(一主两从)
目录[*]一、硬件准备(假造主机)
[*]二、环境准备
[*]1、所有机器关闭防火墙
[*]2、所有机器关闭selinux
[*]3、所有机器关闭swap
[*]4、所有机器上添加主机名与ip的对应关系
[*]5、在所有主机上将桥接的ipv4流量传递到iptables的链
[*]三、为所有节点安装docker
[*]四、集群部署
[*]1、为所有节点修改堆栈,安装kubeadm、kubelet、kubectl
[*]2、修改docker的配置(所有节点)
[*]3、部署master节点(主节点k8s-master)
[*](1)、遇到报错:
[*](2)、解决办法:
[*]4、按照指示实行:
[*]5、查看kubelet.service状态
[*]6、查看节点状态为notready
[*]7、安装网络插件,官方文档:https://github.com/flannel-io/flannel
[*]8、添加node节点
一、硬件准备(假造主机)
角色主机名ip地点masterk8s-master192.168.112.10nodek8s-node1192.168.112.20nodek8s-node2192.168.112.30CentOS Linux release 7.9.2009 (Core)
至少2核CPU、3GB以上内存
使用命令hostnamectl set-hostname临时修改主机名
二、环境准备
1、所有机器关闭防火墙
[*]systemctl stop firewalld #关闭
systemctl disable firewalld #开机不自启
systemctl status firewalld #查看状态https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183155692-1685112634.png
2、所有机器关闭selinux
[*]sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183155258-1401573694.png
3、所有机器关闭swap
[*]swapoff -a # 临时关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab#永久关闭https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183154857-1074478114.png
4、所有机器上添加主机名与ip的对应关系
[*]vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.112.10 k8s-master
192.168.112.20 k8s-node1
192.168.112.30 k8s-node2https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183154501-1297636540.png
5、在所有主机上将桥接的ipv4流量传递到iptables的链
<ul>cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF这里从节点的kubelet.service状态报code=exited, status=1/FAILURE是正常的
3、部署master节点(主节点k8s-master)
yum install wget.x86_64 -y
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce-20.10.11 -y
systemctl start docker
systemctl enable docker记得保存好这段命令是用于将一个工作节点(worker node)加入到已存在的 Kubernetes 集群中的过程。
https://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183153314-1021253735.png
(1)、遇到报错:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install kubelet-1.22.2 kubeadm-1.22.2 kubectl-1.22.2 -y
systemctl enable kubelet && systemctl start kubelet(2)、解决办法:
# cat > /etc/docker/daemon.json <<EOF
> {
> "exec-opts": ["native.cgroupdriver=systemd"]
> }
> EOF
# systemctl daemon-reload
# systemctl restart docker.service
# systemctl restart kubelet.service
# Here is one example how you may list all Kubernetes containers running in docker:
- 'docker ps -a | grep kube | grep -v pause'
Once you have found the failing container, you can inspect its logs with:
- 'docker logs CONTAINERID'
error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
To see the stack trace of this error execute with --v=5 or higher4、按照指示实行:
kubeadm init \
--apiserver-advertise-address=192.168.112.10 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.2 \
--control-plane-endpoint k8s-master \
--service-cidr=172.16.0.0/16 \
--pod-network-cidr=10.244.0.0/165、查看kubelet.service状态
Here is one example how you may list all Kubernetes containers running in docker:
- 'docker ps -a | grep kube | grep -v pause'
Once you have found the failing container, you can inspect its logs with:
- 'docker logs CONTAINERID'
error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
To see the stack trace of this error execute with --v=5 or higherhttps://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183152960-1615503121.png
6、查看节点状态为notready
rm -rf /etc/containerd/config.toml
systemctl restart containerdhttps://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183152582-1298511207.png
7、安装网络插件,官方文档:https://github.com/flannel-io/flannel
# 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.conf8、添加node节点
systemctl status kubelet.servicehttps://img2023.cnblogs.com/blog/3332572/202403/3332572-20240314183151757-2073402572.png
至此一个简单的k8s集群安装完成
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]