#!/bin/bash

#--------------------------------------------
# 此脚本用于Centos8自动部署k8s服务
# 安装K8S前先去查看k8s仪表板最新支持的版本
# 因为仪表板支持的版本落后于最新版本
# 得根据仪表板支持的最新版本进行安装
# 2021年10月31日
#--------------------------------------------



#关闭seLinux
selinuxoff(){
 sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
 setenforce 0
}


#关闭交换分区
swapoff(){
 sed -i 's/.*swap.*/#&/' /etc/fstab
 sudo swapoff -a
}

#配置K8S内核参数
sysctl.k8s()
{
cat << EOF >> /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system
}

#修改系统源为国内源
change.repos.ali()
{
mv /etc/yum.repos.d/ /etc/yum.repos.d.initial.back
mkdir /etc/yum.repos.d/
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

cat << EOF >> /etc/yum.repos.d/kubernetes.repo
[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

}

#安装docker
install.docker(){
 yum -y erase podman buildah
 yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools
 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 yum install -y docker-ce 
 sed -i 's/--containerd=\/run\/containerd\/containerd.sock/& --exec-opt native.cgroupdriver=systemd/' /usr/lib/systemd/system/docker.service
 systemctl enable docker
 systemctl start docker
}

#安装kubectl、kubelet、kubeadm
install.k8s(){
 yum install -y kubectl kubelet kubeadm
 systemctl enable kubelet
 systemctl start kubelet
}

#初始化k8s
initial.k8s(){
 k8sversion=$(kubeadm version | grep -Eo 'v[0-9].[0-9]{2}.[0-9]{1,2}')

 kubeadm init \
 --apiserver-advertise-address=192.168.232.152 \
 --apiserver-cert-extra-sans=127.0.0.1 \
 --image-repository=registry.aliyuncs.com/google_containers \
 --ignore-preflight-errors=all \
 --kubernetes-version=$k8sversion \
 #k8s集群指定的service网段
 --service-cidr=10.10.0.0/16 \
 --pod-network-cidr=10.18.0.0/16
}


selinuxoff
swapoff
sysctl.k8s
change.repos.ali
install.docker
install.k8s
initial.k8s

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

#安装calico网络
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

#安装K8S仪表盘
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml
sed -i '0,/^spec:/s/^spec:/  type: NodePort/'  recommended.yaml
sed -i '/  type: NodePort/ i\spec:' recommended.yaml
sed -i '/      targetPort: 8443/ a\      nodePort: 30000' recommended.yaml
kubectl create -f recommended.yaml
Logo

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

更多推荐