欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s脚本安装kubeadm集群

程序员文章站 2022-07-14 09:10:03
...

#------------------------------ master节点脚本 ----------------------------
#!/bin/bash

#环境准备
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
iptables -F
swapoff -a



#检查到错误退出脚本

error() {

if [ $? -ne 0 ];then
echo -e "\033[31m 检查到此处有错误,已退出脚本! \033[0m"
exit 0
fi

}

#加载 ip_vs 模块
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done >& /dev/null

#修改主机名
read -p "请设置你的主机名:" name
hostnamectl set-hostname $name

#在此处添加hosts文件

cat /etc/hosts | grep master

if [ $? -ne ];then

cat  >> /etc/hosts << EOF
#此处写入设置hosts文件
#如192.168.x.x  test

EOF

else

continue

fi

#调整内核参数
cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF

#生效参数
sysctl --system 

#-------------------- 所有节点安装docker --------------------

#设置镜像仓库

yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo



#生成缓存

yum makecache fast

yum install -y yum-utils device-mapper-persistent-data lvm2
yum install -y docker-ce

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  }
}
EOF


#重载/重启docker
systemctl daemon-reload

systemctl restart docker.service

systemctl enable docker.service

error

#-------------------- 所有节点安装kubeadm,kubelet和kubectl ----------------
#定义kubernetes源
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=0
repo_gpgcheck=0
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 -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1

#开机自启kubelet
systemctl enable kubelet.service

error

#-------------------- 部署K8S集群 -------------------- 

#查看初始化需要的镜像
kubeadm config images list

#在 master 节点上传 kubeadm-basic.images.tar.gz 压缩包至 /opt 目录

cd /opt/
tar zxvf kubeadm-basic.images.tar.gz

for i in $(ls /opt/kubeadm-basic.images/*.tar); do docker load -i $i; done >& /dev/null

#复制镜像和脚本到 node 节点,并在 node 节点上执行脚本 bash /opt/load-images.sh

echo -e "\033[31m 复制镜像和脚本到 node01节点!: \033[0m"
scp -r kubeadm-basic.images [email protected]${node01的ip}:/opt

echo -e "\033[31m 复制镜像和脚本到 node02节点!: \033[0m"
scp -r kubeadm-basic.images [email protected]${node02的ip}:/opt

#初始化kubeadm
kubeadm config print init-defaults > /opt/kubeadm-config.yaml

#修改kubeadm-config.yaml文件

ip=`ip a | grep ens33 | awk 'NR==2' | awk -F " " '{print$2}' | awk -F "/" '{print$1}'`
sed -i "12c   advertiseAddress: $ip" /opt/kubeadm-config.yaml
sed -i "12s/^/  /" /opt/kubeadm-config.yaml
sed -i "34c kubernetesVersion: v1.15.1" /opt/kubeadm-config.yaml
sed -i "37c podSubnet: \"10.244.0.0/16\"" /opt/kubeadm-config.yaml
sed -i "38i serviceSubnet: 10.96.0.0/12" /opt/kubeadm-config.yaml

echo 'apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs' >> /opt/kubeadm-config.yaml

#设定kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config


#所有节点上传flannel镜像 flannel.tar 到 /opt 目录,master节点上传 kube-flannel.yml 文件

cd /opt/
docker load < flannel.tar

#在 master 节点创建 flannel 资源
kubectl apply -f kube-flannel.yml

#加入集群(master节点),阿里源在线获取
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.12

echo -e "\033[31m k8s安装完成,请在node节点输入master生成的指令加入集群命令! \033[0m"


#----------------------------node 节点脚本----------------------------

#!/bin/bash
#环境准备
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
iptables -F
swapoff -a

#检查到错误退出脚本

error() {
if [ $? -ne 0 ];then
echo -e "\033[31m 检查到此处有错误,已退出脚本! \033[0m"
exit 0
fi

}

#加载 ip_vs 模块
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done >& /dev/null

#修改主机名
read -p "请设置你的主机名:" name
hostnamectl set-hostname $name

#在此处添加hosts文件
cat  >> /etc/hosts << EOF
#写入hosts文件
EOF

#调整内核参数
cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF

#生效参数
sysctl --system 

#-------------------- 所有节点安装docker --------------------

#设置镜像仓库

yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum install -y yum-utils device-mapper-persistent-data lvm2
yum install -y docker-ce

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  }
}
EOF

#重载/重启docker
systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service

error

#-------------------- 所有节点安装kubeadm,kubelet和kubectl ----------------
#定义kubernetes源
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=0
repo_gpgcheck=0
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 -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1

#开机自启kubelet
systemctl enable kubelet.service

error

#-------------------- 部署K8S集群 -------------------- 

#初始化kubeadm
kubeadm config print init-defaults > /opt/kubeadm-config.yaml

#修改kubeadm-config.yaml文件

ip=`ip a | grep ens33 | awk 'NR==2' | awk -F " " '{print$2}' | awk -F "/" '{print$1}'`
sed -i "12c   advertiseAddress: $ip" /opt/kubeadm-config.yaml
sed -i "12s/^/  /" /opt/kubeadm-config.yaml
sed -i "34c kubernetesVersion: v1.15.1" /opt/kubeadm-config.yaml
sed -i "37c podSubnet: \"10.244.0.0/16\"" /opt/kubeadm-config.yaml
sed -i "38i serviceSubnet: 10.96.0.0/12" /opt/kubeadm-config.yaml

echo 'apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
featureGates:
  SupportIPVSProxyMode: true
mode: ipvs' >> /opt/kubeadm-config.yaml

#设定kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

#所有节点上传flannel镜像 flannel.tar 到 /opt 目录,master节点上传 kube-flannel.yml 文件
cd /opt/
docker load < flannel.tar

echo -e "\033[31m k8s安装完成,请输入master生成的加入集群命令! \033[0m"