简易k8s实验小集群安装脚本

#!/bin/bash

read -e -p "本脚本在master1节点上使用"  test
yum install -y epel-release 
yum install -y ansible

echo "----------通用安装--------------"
echo "masterIP、(最多3个,无需输入本机ip)"
read -e c1 c2 

echo "workerIP(最多4个)"
read -e a1 a2 a3 a4

zhuip=$(cat /etc/sysconfig/network-scripts/ifcfg-ens33 |grep IPADDR |awk -F= '{print$2}')

zong=($zhuip $c1 $c2 $a1 $a2 $a3 $a4)

#镜像文件脚本
cat >> jingxiang.bash << EOF
#!/bin/bash
tar -xf k8s-v1.23.15-images.tgz
for i in \$(ls /root/k8s-v1.23.15-images) ;do docker load < /root/k8s-v1.23.15-images/\$i ;done
EOF

#批量改master主机名
for i in $zhuip $c1 $c2 $c3 $c4
do
a=0
a=$b
b=$(($a+1))
ssh $i hostnamectl set-hostname master-$b
done

#批量改node节点主机名
for i in $a1 $a2 $a3 $a4
do
c=0
c=$d
d=$(($c+1))
ssh $i hostnamectl set-hostname worker-$d
done

#定义ansible主机组
cat > /etc/ansible/hosts << EOF
[master]
$zhuip
$c1
$c2
[worker]
$a1
$a2
$a3
$a4
[zong]
$zhuip
$c1
$c2
$a1
$a2
$a3
$a4
[masterling]
$c1
$c2
EOF

#改hosts文件  
#关闭防火墙 selinux 禁用swap分区  
#将桥接的 IPv4、IPv6 流量传递到iptables的链: 路由转发规则   
#安装docker
#配置yum仓库   安装k8s组件
cat >> tongyong.bash << eee
#!/bin/bash
cat >> /etc/hosts << AAA
$zhuip master-1
$c1 master-2
$c2 master-3
$c3 master-4
$c4 master-5

$a1 worker-1
$a2 worker-2
$a3 worker-3
$a4 worker-4
$zhuip api.zhao.com
AAA
systemctl disable firewalld; systemctl stop firewalld
setenforce 0
sed -i --follow-symlinks 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
swapoff -a; sed -i '/swap/d' /etc/fstab
cat > /etc/sysctl.d/kubernetes.conf << BBB
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
BBB
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.con
sysctl --system
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-20.10.24-3.el7.x86_64 docker-ce-cli-20.10.24-3.el7.x86_64 containerd.io docker-buildx-plugin-0.10.5-1.el7.x86_64.rpm   docker-compose-plugin
systemctl enable --now docker
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << ccc
{
    "exec-opts": ["native.cgroupdriver=systemd"]
}
ccc
systemctl daemon-reload && systemctl restart docker
cat > /etc/yum.repos.d/kubernetes.repo << ddd
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
         http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
ddd
yum install -y kubeadm-1.23.15-0 kubelet-1.23.15-0 kubectl-1.23.15-0
systemctl enable --now kubelet
mkdir /root/chajian
eee

#ansible执行初始化脚本   导入镜像 
ansible zong -m script -a './tongyong.bash'
ansible zong -m copy -a 'src=~/k8s-v1.23.15-images.tgz dest=/root/'
ansible zong -m copy -a 'src=~/ingress-controller-image.tgz dest=/root/'
ansible zong -m script -a './jingxiang.bash'

#master节点
echo "-----------masker安装----------"
sed -i "s/192.168.136.61/$zhuip/g" kubeadm-config.yaml
kubeadm init --config=kubeadm-config.yaml --upload-certs > jilu.conf
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
cd /root/chajian
mkdir calico
cd calico
curl https://projectcalico.docs.tigera.io/archive/v3.24/manifests/calico.yaml -O
kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f calico.yaml
cd
cat jilu.conf |grep -B3 certificate-key |grep kubeadm -A3 > /root/master.lingpan
ansible masterling -m script -a './master.lingpan'

#worker节点
echo "-----------worker-----------"
kubeadm token create --print-join-command > /root/yanz.cof
ansible worker -m script -a './yanz.cof'

#ingres节点
echo "------------ingres----------------"
cat >> ingress.conf << www
#!/bin/bash
mkdir /root/chajian/ingress
cd /root/chajian/ingress
mv /root/deploy.yaml .
mv /root/ingress-controller-image.tgz .
tar -xf ingress-controller-image.tgz
docker load < kube-webhook-certgen-v20220916.img
docker load < controller-v1.6.4.img
cd
www
ansible zong -m script -a './ingress.conf'

cd /root/chajian/ingress
kubectl apply -f deploy.yaml
cd

#helm安装
echo "-----------helm---------------"
mkdir /root/chajian/helm
cd /root/chajian/helm
mv /root/helm-v3.12.3-linux-amd64.tar.gz .
tar -xf helm-v3.12.3-linux-amd64.tar.gz
mv linux-amd64/helm /usr/bin/
cd

echo "----------给其他节点添加config文件,设置bash-com-----------"
ansible masterling  -m shell -a 'mkdir -p /root/.kube'
ansible worker -m shell -a 'mkdir -p /root/.kube'

ansible masterling  -m copy -a 'src=/etc/kubernetes/admin.conf dest=/root/'
ansible worker -m copy -a 'src=/etc/kubernetes/admin.conf dest=/root/'

cat >> tyu.conf << yui
#!/bin/bash
sudo cp -i /root/admin.conf \$HOME/.kube/config
sudo chown \$(id -u):\$(id -g) \$HOME/.kube/config
yui
ansible masterling  -m script -a './tyu.conf'
ansible worker -m script -a './tyu.conf'

cat >> tap.conf << TTT
#!/bin/bash
yum -y install bash-completion 
echo "source <(kubectl completion bash)" >> ~/.bashrc        
source  ~/.bashrc
rm -rf ingress.conf k8s.bash k8s-v1.23.15-images k8s-v1.23.15-images.tgz tongyong.bash worker.bash yanz.cof yhua.conf tap.conf
rm -rf jilu.conf jingxiang.bash k8s.conf k8s.tar.gz kubeadm-config.yaml master.lingpan 
TTT
ansible zong -m script -a './tap.conf'

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值