Kubernetes

纯shell一键搭建k8s集群

本文主要是介绍纯shell一键搭建k8s集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

部署环境:三台centos7.9,其中一台master节点,两台node节点

docker版本:20.10.14

kubernetes版本:1.23.6

 master节点:

[root@k8s-master ~]# cat deploy_k8s_master.sh
#!/bin/bash
#1.configure domain name resolve
echo '192.168.121.50 k8s-master' >>/etc/hosts
echo '192.168.121.51 k8s-node1' >>/etc/hosts
echo '192.168.121.52 k8s-node2' >>/etc/hosts
#2.close firewall
systemctl disable firewalld.service --now
#3.close selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
#4.configure chrony
yum -y install chrony
sed -i -e 's/^server/#&/g' -e '2a server k8s-master iburst' /etc/chrony.conf
echo "allow all" >> /etc/chrony.conf
echo "local stratum 10" >> /etc/chrony.conf
#5.permanent close swap partition
sed -i 's/.*swap.*/#&/g' /etc/fstab
swapoff -a
#6.modify module parameter
modprobe br_netfilter
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
sysctl -p
#7.install docker-ce
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo=yum-config-manager --add-repo=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7.9/x86_64/stable/
mv /etc/yum.repos.d/mirrors.tuna.tsinghua.edu.cn_docker-ce_linux_centos_7.9_x86_64_stable_.repo /etc/yum.repos.d/docker-ce.repo
echo 'gpgcheck=0' >>/etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
systemctl enable docker --now
#8.configure docker image accelerator
tee /etc/docker/daemon.json <<-'EOF'
{
"insecure-registries" : ["0.0.0.0/0"],
"registry-mirrors": ["https://7o6gyryt.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
#9.configure k8s source and install k8s
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet --now
#10.k8s-master initialization
version=$(kubelet --version |cut -d'v' -f2)
docker --version
kubelet --version
kubeadm init --kubernetes-version=${version} --apiserver-advertise-address=192.168.121.50 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

--------------------------------------------------------------------------------------------------------------------------------------

node节点:

[root@k8s-node1 ~]# cat deploy_k8s_node.sh
#!/bin/bash
#1.configure domain name resolve
echo '192.168.121.50 k8s-master' >>/etc/hosts
echo '192.168.121.51 k8s-node1' >>/etc/hosts
echo '192.168.121.52 k8s-node2' >>/etc/hosts
#2.close firewall
systemctl disable firewalld.service --now
#3.close selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
#4.configure chrony
yum -y install chrony
sed -i -e 's/^server/#&/g' -e '2a server k8s-master iburst' /etc/chrony.conf
chronyc sources
#5.permanent close swap partition
sed -i 's/.*swap.*/#&/g' /etc/fstab
swapoff -a
#6.modify module parameter
modprobe br_netfilter
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
sysctl -p
#7.install docker-ce
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo=yum-config-manager --add-repo=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7.9/x86_64/stable/
mv /etc/yum.repos.d/mirrors.tuna.tsinghua.edu.cn_docker-ce_linux_centos_7.9_x86_64_stable_.repo /etc/yum.repos.d/docker-ce.repo
echo 'gpgcheck=0' >>/etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io
systemctl enable docker --now
#8.configure docker image accelerator
tee /etc/docker/daemon.json <<-'EOF'
{
"insecure-registries" : ["0.0.0.0/0"],
"registry-mirrors": ["https://7o6gyryt.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
#9.configure k8s source and install k8s
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet --now
#10.check version
docker --version
kubelet --version

master节点脚本执行成功后,如下图所示

 然后将绿色框选中的部分在master节点执行,蓝色框选中的部分在node节点执行

node节点加入master节点成功后如图所示

 然后我们在master节点上查看,此时状态都是NotReady(master节点是因为缺少网络插件,node节点是缺少网络插件对应的镜像和容器)

 然后通过GitHub下载flannel插件

并安装这个插件

 安装完插件之后,node节点就有了flannel插件相关的镜像和容器,并且k8s集群的状态都变成了Ready

这篇关于纯shell一键搭建k8s集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!