使用shell脚本搭建k8s集群

#前置免密登录脚本,执行脚本时需连续输入3次回车后,再根据提示输入yes及对应服务器密码

nopassword_login.sh

#!/bin/bash ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.11
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.12
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.13
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.14
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.15
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.16
一 创建目录:/tmp/k8s_install,编写脚本,为了降低耦合性及提高维护性,共创建以下几个脚本

  1. k8s_cluster_install.sh

#!/bin/bash
#关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

#关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config

setenforce 0

关闭swapoff

swapoff -a

#脚本动态根据服务器IP设置主机名

cd /tmp/k8s_install

sh ip_sethostname.sh

#绑定IP和主机名

cat >> /etc/hosts << EOF

10.0.0.17 master

10.0.0.11 k8s-node1

10.0.0.12 k8s-node1

10.0.0.13 k8s-node3

10.0.0.14 k8s-node4

10.0.0.15 k8s-node5

10.0.0.16 k8s-node6

EOF

#设置流量转发

cat > /etc/sysctl.d/k8s.conf << EOF

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF

#执行生效

sysctl --system

#配置阿里源并安装docker,docker版本一定要是18以上

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

yum -y install docker-ce-18.06.1.ce-3.el7

systemctl enable docker && systemctl start docker

docker --version

#安装必要依赖

yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git socat 
#同步服务器时间,这一步很重要,如果用的是云服务器,则找对应服务器厂商的ntp服务即可

ntpdate time.windows.com

#配置阿里镜像加速器

mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'

{

"registry-mirrors": ["https://vkayqpp3.mirror.aliyuncs.com"]

}

EOF

systemctl daemon-reload

systemctl restart docker

#添加阿里云yum源

cat > /etc/yum.repos.d/kubernetes.repo << EOF

[kubernetes]

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 clean all

yum -y makecache

#安装k8s相关组件,最新的稳定版为1.19.0,此次先安装1.17.0,验证脚本无误后,后期尝试1.19.0

yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0

#设置开机启动

systemctl enable kubelet

#初始化k8s

#master节点输出的加入命令需要手动复制

cd /tmp/k8s_install

sh k8s_clusters_init1.sh
sh k8s_clusters_init2.sh
sh k8s_clusters_init3.sh

#脚本在主节点执行完成之后,将join命令贴到join.sh脚本中,再将脚本发送到各个node节点



#查看节点状态

kubectl get nodes -n kube-system

2 ip_sethostname.sh


#!/bin/bash

#LC_ALL=C ifconfig  | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

ip=`ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`

#根据固定字符串拼接ip最后一位来设置节点主机名

ip_end=${ip##*.}

echo $ip_end

host_code=${ip_end:1:2}

echo $ip_end

if [ $ip_end -lt 17 ]

then

	host_name="k8s-node"+$host_code

else

	host_name="master"

fi

`hostnamectl set-hostname $host_name`

echo hostname

3 三个文件
k8s_clusters_init1.sh

#!/bin/bash
#LC_ALL=C ifconfig  | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
ip=`ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`
#ip_end=${ip:-1}
ip_end=${ip##*.}

#如果是主节点,则初始化后,执行固定配置命令
if [ $ip_end -lt 17 ]
then
	echo "当前节点非master"
else
	kubeadm init \
	--apiserver-advertise-address=10.0.0.17 \
	--image-repository registry.aliyuncs.com/google_containers \
	--kubernetes-version v1.17.0 \
	--service-cidr=10.1.0.0/16 \
	--pod-network-cidr=10.244.0.0/16 
fi

k8s_clusters_init2.sh

#该脚本需输入y确认

#!/bin/bash
#LC_ALL=C ifconfig  | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
ip=`ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`
#ip_end=${ip:-1}
ip_end=${ip##*.}

if [ $ip_end -lt 17 ]
then
	echo "跳过当前步骤"
else
	mkdir -p $HOME/.kube
	sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
	sudo chown $(id -u):$(id -g) $HOME/.kube/config
fi

k8s_clusters_init3.sh

#!/bin/bash
#LC_ALL=C ifconfig  | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
ip=`ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`
#ip_end=${ip:-1}
ip_end=${ip##*.}

if [ $ip_end -lt 17 ]
then
	sh join.sh
else
	echo "当前为主节点"
	kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
fi

4 join.sh

#!/bin/bash

#LC_ALL=C ifconfig  | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

ip=`ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`

#ip_end=${ip:-1}

ip_end=${ip##*.}

echo $ip_end

host_code=${ip_end:1:2}

echo $ip_end

if [ $ip_end -lt 17 ]

then

	kubeadm join 10.0.0.17:6443 --token upd9ll.n2695flanykqy3rk \

    --discovery-token-ca-cert-hash sha256:fda6328b*************380e10d92789d73c39b0640652f7f99

else

	echo "当前为主节点“

fi

echo hostname

二 主节点安装成功后复制join指令到join.sh中后,,执行scp.sh将脚本发送到其他各个node 执行

scp.sh


#!/bin/bash

scp -r /tmp/k8s_install 10.0.0.11:/tmp

scp -r /tmp/k8s_install 10.0.0.12:/tmp

scp -r /tmp/k8s_install 10.0.0.13:/tmp

scp -r /tmp/k8s_install 10.0.0.14:/tmp

scp -r /tmp/k8s_install 10.0.0.15:/tmp

scp -r /tmp/k8s_install 10.0.0.16:/tmp

scp -r /etc/kubernetes 10.0.0.11:/etc

scp -r /etc/kubernetes 10.0.0.12:/etc

scp -r /etc/kubernetes 10.0.0.13:/etc

scp -r /etc/kubernetes 10.0.0.14:/etc

scp -r /etc/kubernetes 10.0.0.15:/etc

scp -r /etc/kubernetes 10.0.0.16:/etc

注: node节点如果报错The connection to the server localhost:8080 was refused - did you specif,则执行kubeadmq8080.sh即可。

执行脚本后依旧报错,则直接在命令行手动执行脚本中命令。

kubeadmq8080.sh


#!/bin/bash

echo export KUBECONFIG=/etc/kubernetes/admin.conf >> ~/.bash_profile

source ~/.bash_profile

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐