k8s-1.20.14快速部署脚本

王书宸 2022年1月2日16:08:58
评论
392 8375字

docker-20.10.12+k8s-1.20.14

脚本·使用·说明:

使用此脚本可在线快速部署一主两从的经典k8s集群

当然可以使用此脚本部署:1mast+nnode集群

比如主机规划:

主机名 ip 内核 版本
master 192.168.1.201 3.10、4.19+(5.4) 最小化centos7.9.209
node1 192.168.1.202 3.10、4.19+(5.4) 最小化centos7.9.209
node2 192.168.1.203 3.10、4.19+(5.4) 最小化centos7.9.209

已经把脚本里面的设置主机名和添加hosts解析注释掉,使用时手动修改成需要部署的机子的主机名并且解析

1、master单独一个脚本

2、node单独一个脚本

3、复制粘贴执行脚本即可

手动升级内核

https://www.kernel.org/

查最新ELRepo仓库源:

http://elrepo.reloumirrors.net/kernel/el7/x86_64/RPMS

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-5.el7.elrepo.noarch.rpm

yum --enablerepo=elrepo-kernel install -y kernel-lt

grub2-set-default 0

部署脚本里面已经判断内核版本,会自动加载对应的模块

master

touch master_docker-20.10.12+k8s-1.20.14.sh

chmod +x master_docker-20.10.12+k8s-1.20.14.sh

vim master_docker-20.10.12+k8s-1.20.14.sh
#!/bin/bash

startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
#1、环境准备

#设置主机名
#hostnamectl set-hostname master

#添加hosts解析
#cat >> /etc/hosts << EOF
#192.168.1.201 master
#192.168.1.202 node1
#192.168.1.203 node2
#EOF

ping -c2 master
ping -c2 node1
ping -c2 node2

#同步时间
yum -y install ntp

systemctl start ntpd && systemctl enable ntpd && systemctl status ntpd

#关闭防火墙
systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld

#永久关闭seLinux(需重启系统生效)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

#关闭swap
swapoff -a  # 临时关闭
sed -i 's/.*swap.*/#&/g' /etc/fstab

#加载IPVS模块
yum -y install ipset ipvsadm

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
EOF

kernel_version=$(uname -r | cut -d- -f1)
echo $kernel_version

if [ `expr $kernel_version \> 4.19` -eq 1 ]
    then
        modprobe -- nf_conntrack
    else
        modprobe -- nf_conntrack_ipv4
fi

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack

#2、部署docker
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

# 1.20+需要开启br_netfilter
sudo modprobe overlay
sudo modprobe br_netfilter

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

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-20.10.12 docker-ce-cli-20.10.12

systemctl enable docker --now && systemctl status docker

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

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

systemctl daemon-reload && systemctl restart docker
#3、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
EOF

yum -y install kubeadm-1.20.14-0 kubelet-1.20.14 kubectl-1.20.14-0

systemctl enable --now kubelet

systemctl status kubelet

mkdir ~/kubeadm_init && cd ~/kubeadm_init

kubeadm config print init-defaults > kubeadm-init.yaml

cat > kubeadm-init.yaml << EOF
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: `hostname -i` #master_ip
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: master
  taints:
  - effect: "NoSchedule"
    key: "node-role.kubernetes.io/master"
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.20.14
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16  
scheduler: {}
--- 
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration 
mode: ipvs 
--- 
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration 
cgroupDriver: systemd
EOF

# 预拉取镜像
kubeadm config images pull --config kubeadm-init.yaml

kubeadm init --config=kubeadm-init.yaml | tee kubeadm-init.log

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

kubectl get node -owide 
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`

sumTime=$[ $endTime_s - $startTime_s ]

echo "$startTime ---> $endTime" "Total:$sumTime seconds"

node

touch node_docker-20.10.12+k8s-1.20.14.sh

chmod +x node_docker-20.10.12+k8s-1.20.14.sh

vim node_docker-20.10.12+k8s-1.20.14.sh
#!/bin/bash

startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
#1、环境准备

#设置主机名
#hostnamectl set-hostname master

#添加hosts解析
#cat >> /etc/hosts << EOF
#192.168.1.201 master
#192.168.1.202 node1
#192.168.1.203 node2
#EOF

ping -c2 master
ping -c2 node1
ping -c2 node2

#同步时间
yum -y install ntp

systemctl start ntpd && systemctl enable ntpd && systemctl status ntpd

#关闭防火墙
systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld

#永久关闭seLinux(需重启系统生效)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

#关闭swap
swapoff -a  # 临时关闭
sed -i 's/.*swap.*/#&/g' /etc/fstab

#加载IPVS模块
yum -y install ipset ipvsadm

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
EOF

kernel_version=$(uname -r | cut -d- -f1)
echo $kernel_version

if [ `expr $kernel_version \> 4.19` -eq 1 ]
    then
        modprobe -- nf_conntrack
    else
        modprobe -- nf_conntrack_ipv4
fi

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack

#2、部署docker
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

# 1.20+需要开启br_netfilter
sudo modprobe overlay
sudo modprobe br_netfilter

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

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-20.10.12 docker-ce-cli-20.10.12

systemctl enable docker --now && systemctl status docker

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

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

systemctl daemon-reload && systemctl restart docker

#3、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
EOF

yum -y install kubeadm-1.20.14-0 kubelet-1.20.14-0 kubectl-1.20.14-0

systemctl enable --now kubelet

systemctl status kubelet

# 预拉取镜像
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.20.14
docker pull registry.aliyuncs.com/google_containers/coredns:1.7.0
docker pull registry.aliyuncs.com/google_containers/pause:3.2

endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`

sumTime=$[ $endTime_s - $startTime_s ]

echo "$startTime ---> $endTime" "Total:$sumTime seconds"

安装flannel

https://github.com/flannel-io/flannel

在线

mkdir ~/kubernetes-flannel && cd ~/kubernetes-flannel

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml

离线

创建文件夹,把 yml 跟 镜像文件 上传到文件夹,导入镜像

mkdir ~/kubernetes-flannel && cd ~/kubernetes-flannel

# 导入镜像
ls *tar |xargs -i docker load -i {}

kubectl apply -f kube-flannel.yml

让node节点使用kubectl命令行工具

mkdir -p $HOME/.kube
scp master:/$HOME/.kube/config $HOME/.kube/config

chown $(id -u):$(id -g) $HOME/.kube/config

<table><tr><td bgcolor="red">补充:docker一键部署最新版本

安装docker那部分,可使用国内==道客==docker一键部署最新版本

==道客官网==:https://get.daocloud.io

#一键部署最新版本docker
curl -sSL https://get.daocloud.io/docker | sh

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

systemctl daemon-reload && systemctl restart docker
#上面脚本使用的docker是20.10.12,需要最新版本,把脚本安装docker那部分换成下面内容即可
curl -sSL https://get.daocloud.io/docker | sh

systemctl daemon-reload

systemctl enable docker --now && systemctl status docker

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

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

systemctl daemon-reload && systemctl restart docker
继续阅读
王书宸
  • 本文由 发表于 2022年1月2日16:08:58
  • 转载请务必保留本文链接:https://www.wscyun.com/3001
k8s-1.21.8快速部署脚本 kubernetes

k8s-1.21.8快速部署脚本

docker-20.10.12+k8s-1.21.8 脚本·使用·说明: 使用此脚本可在线快速部署一主两从的经典k8s集群 当然可以使用此脚本部署:1mast+nnode集群 比如主机规划: 主机名 ...
k8s-1.19.16快速部署脚本 kubernetes

k8s-1.19.16快速部署脚本

docker-19.03.15+k8s-1.19.16 脚本·使用·说明: 使用此脚本可在线快速部署一主两从的经典k8s集群 当然可以使用此脚本部署:1mast+nnode集群 比如主机规划: 主机名...
k8s-1.23.1快速部署脚本 kubernetes

k8s-1.23.1快速部署脚本

k8s-1.23.1快速部署脚本 containerd-1.4.12+k8s-1.23.1 脚本·使用·说明: 使用此脚本可在线快速部署一主两从的经典k8s集群 当然可以使用此脚本部署:1mast+n...