使用kubeadm安装kubernetes集群脚本(更新日期:2024.7.19)
目前kubernetes
使用的 CRI(容器运行时)主流的是docker
和containerd
,以下是一个自动化安装kubernetes
集群的脚本,并且支持两种运行时,按照如下步骤操作即可。
系统版本为centos 7.9,docker
的安装方式我已经试验过没问题了
1. 添加脚本
vi /usr/local/bin/kubestart
将以下内容复制进去
#! /bin/bash
###############################################
## Simple Install Your K8S
###############################################
kube_pod_subnet="10.244.0.0/16"
# kubernetes版本
kube_version="1.23.9"
kube_image_server="registry.cn-hangzhou.aliyuncs.com/google_containers"
crictl_url="https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.21.0/"
crictl_name="crictl-v1.21.0-linux-amd64.tar.gz"
# 关闭SElinux
function init-env-disable-selinux()
{
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
}
# 禁用防火墙
function init-env-disable-firewalld()
{
sudo systemctl stop firewalld
sudo systemctl disable --now firewalld
}
# 添加 kubernetes.repo 文件和一些必要工具
function init-env-repository()
{
# kubernetes.repo
sudo echo -e "[kubernetes] \nname=Kubernetes \nbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ \nenabled=1 \ngpgcheck=1 \nrepo_gpgcheck=1 \ngpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg" > /etc/yum.repos.d/kubernetes.repo
# replace base.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
echo '# CentOS-Base.repo' > CentOS-Base.repo
echo '#' >> CentOS-Base.repo
echo '# The mirror system uses the connecting IP address of the client and the' >> CentOS-Base.repo
echo '# update status of each mirror to pick mirrors that are updated to and' >> CentOS-Base.repo
echo '# geographically close to the client. You should use this for CentOS updates' >> CentOS-Base.repo
echo '# unless you are manually picking other mirrors.' >> CentOS-Base.repo
echo '#' >> CentOS-Base.repo
echo '# If the mirrorlist= does not work for you, as a fall back you can try the ' >> CentOS-Base.repo
echo '# remarked out baseurl= line instead.' >> CentOS-Base.repo
echo '#' >> CentOS-Base.repo
echo '#' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
echo '[base]' >> CentOS-Base.repo
echo 'name=CentOS-$releasever - Base - mirrors.aliyun.com' >> CentOS-Base.repo
echo 'failovermethod=priority' >> CentOS-Base.repo
echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/' >> CentOS-Base.repo
echo 'gpgcheck=1' >> CentOS-Base.repo
echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
echo '#released updates ' >> CentOS-Base.repo
echo '[updates]' >> CentOS-Base.repo
echo 'name=CentOS-$releasever - Updates - mirrors.aliyun.com' >> CentOS-Base.repo
echo 'failovermethod=priority' >> CentOS-Base.repo
echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/' >> CentOS-Base.repo
echo 'gpgcheck=1' >> CentOS-Base.repo
echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
echo '#additional packages that may be useful' >> CentOS-Base.repo
echo '[extras]' >> CentOS-Base.repo
echo 'name=CentOS-$releasever - Extras - mirrors.aliyun.com' >> CentOS-Base.repo
echo 'failovermethod=priority' >> CentOS-Base.repo
echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/' >> CentOS-Base.repo
echo 'gpgcheck=1' >> CentOS-Base.repo
echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
echo '#additional packages that extend functionality of existing packages' >> CentOS-Base.repo
echo '[centosplus]' >> CentOS-Base.repo
echo 'name=CentOS-$releasever - Plus - mirrors.aliyun.com' >> CentOS-Base.repo
echo 'failovermethod=priority' >> CentOS-Base.repo
echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/' >> CentOS-Base.repo
echo 'gpgcheck=1' >> CentOS-Base.repo
echo 'enabled=0' >> CentOS-Base.repo
echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
echo '#contrib - packages by Centos Users' >> CentOS-Base.repo
echo '[contrib]' >> CentOS-Base.repo
echo 'name=CentOS-$releasever - Contrib - mirrors.aliyun.com' >> CentOS-Base.repo
echo 'failovermethod=priority' >> CentOS-Base.repo
echo 'baseurl=https://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
echo ' https://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/' >> CentOS-Base.repo
echo 'gpgcheck=1' >> CentOS-Base.repo
echo 'enabled=0' >> CentOS-Base.repo
echo 'gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' >> CentOS-Base.repo
echo ' ' >> CentOS-Base.repo
# docker repo
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cd /etc/yum.repos.d
for file in *.repo; do
if [[ "$file" != "CentOS-Base.repo" && "$file" != "kubernetes.repo" && "$file" != "docker-ce.repo" ]]; then
mv "$file" "$file.bak"
fi
done
yum clean all
yum makecache
# tools
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
}
# 安装 kubelet kubeadm kubectl
function init-install-kube()
{
sudo yum install -y kubeadm-${kube_version} kubectl-${kube_version} kubelet-${kube_version} --disableexcludes=kubernetes
}
# 安装docker
function init-docker()
{
sudo yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
# 更改 cgroupdriver 为 systemd,设置代理
echo -e "{" > /etc/docker/daemon.json
echo -e " \"exec-opts\": [\"native.cgroupdriver=systemd\"]," >> /etc/docker/daemon.json
echo -e " \"registry-mirrors\": [\"https://docker.m.daocloud.io\"]" >> /etc/docker/daemon.json
echo -e "}" >> /etc/docker/daemon.json
systemctl restart docker
systemctl enable kubelet
}
# 安装containerd
function init-containerd()
{
sudo yum install -y wget containerd.io openvswitch* certbot
mkdir -p /etc/containerd
cd /home
wget $crictl_url$crictl_name
tar zxf $crictl_name
mv crictl /usr/local/bin/
rm -rf $crictl_name
echo "overlay" > /etc/modules-load.d/containerd.conf
echo "br_netfilter" >> /etc/modules-load.d/containerd.conf
sudo modprobe overlay
sudo modprobe br_netfilter
echo "net.bridge.bridge-nf-call-iptables = 1" > /etc/sysctl.d/99-kubernetes-cri.conf
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/99-kubernetes-cri.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.d/99-kubernetes-cri.conf
sudo sysctl --system
sudo containerd config default > /etc/containerd/config.toml
sed -i 's/k8s.gcr.io\/pause:3.1/registry.cn-hangzhou.aliyuncs.com\/google_containers\/pause:3.2/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/sysconfig/kubelet
echo "runtime-endpoint: unix:///run/containerd/containerd.sock" > /etc/crictl.yaml
echo "image-endpoint: unix:///run/containerd/containerd.sock" >> /etc/crictl.yaml
echo "timeout: 10" >> /etc/crictl.yaml
echo "debug: false" >> /etc/crictl.yaml
sudo systemctl restart kubelet
sudo systemctl enable kubelet
}
function init-env-kubeconfig()
{
mkdir /etc/kubernetes
echo -e "apiVersion: kubeadm.k8s.io/v1beta3" > /etc/kubernetes/kubeadm.yaml
echo -e "kind: InitConfiguration" >> /etc/kubernetes/kubeadm.yaml
echo -e "bootstrapTokens:" >> /etc/kubernetes/kubeadm.yaml
echo -e " - ttl: \"0\"" >> /etc/kubernetes/kubeadm.yaml
echo -e "---" >> /etc/kubernetes/kubeadm.yaml
echo -e "apiVersion: kubeadm.k8s.io/v1beta3" >> /etc/kubernetes/kubeadm.yaml
echo -e "kind: ClusterConfiguration" >> /etc/kubernetes/kubeadm.yaml
echo -e "networking:" >> /etc/kubernetes/kubeadm.yaml
echo -e " podSubnet: \"${kube_pod_subnet}\"" >> /etc/kubernetes/kubeadm.yaml
echo -e "kubernetesVersion: \"v${kube_version}\"" >> /etc/kubernetes/kubeadm.yaml
echo -e "imageRepository: \"${kube_image_server}\"" >> /etc/kubernetes/kubeadm.yaml
}
function init-env-kubecomp()
{
# default calico cni
echo -e "https://docs.projectcalico.org/manifests/calico.yaml" > /etc/kubernetes/kubeenv.list
# flannel: cni config url
# echo -e "https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml" > /etc/kubernetes/kubeenv.list
}
function init-env()
{
init-env-disable-selinux
init-env-disable-firewalld
init-env-repository
init-install-kube
if [[ -z $2 ]]
then
init-docker
elif [[ $2 == "docker" ]]
then
init-docker
elif [[ $2 == "containerd" ]]
then
init-containerd
else
echo "only support docker or containerd"
exit 1
fi
init-env-kubeconfig
init-env-kubecomp
}
function run-kube()
{
sudo swapoff -a && sysctl -w vm.swappiness=0
sudo sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
systemctl restart kubelet
echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
echo "1" > /proc/sys/net/ipv4/ip_forward
kubeadm init --config /etc/kubernetes/kubeadm.yaml
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
iptables -P FORWARD ACCEPT
while read line
do
kubectl apply -f $line
done < /etc/kubernetes/kubeenv.list
}
function help()
{
echo -e "Commands:"
echo -e " init-env :\t(Init): Initialize the environment configuration, simplify configuring node, such as disable selinux, install docker or containerd..."
echo -e " run-kube :\t(Init): deploy Kubernetes as your want by editing /etc/kubernetes/kubeenv.list. Now it includes calico, flannel"
}
case $1 in
"init-env")
init-env $*
;;
"run-kube")
run-kube $*
;;
"--help")
help
;;
*)
help
;;
esac
2. 修改为可执行文件
sudo chmod +x /usr/local/bin/kubestart
3. 支持的命令
然后你就可以使用以下两条命令去创建你的k8s集群了
# 初始化各种环境配置
kubestart init-env
# 启动集群,应用cni
kubestart run-kube
# 帮助命令
kubestart help
4. 可能遇到的问题
- 关于镜像拉取,目前dockerhub已经不能使用,脚本里面设置了镜像代理:
https://docker.m.daocloud.io
,拉不下来的可以自己使用docker pull命令多拉几次,仍然不好使或者您的集群可以顺利从dockerhub拉去镜像不需要代理的话,可以去掉这个配置:
vi /etc/docker/daemon.json
# 删除掉这行
"registry-mirrors": ["https://docker.m.daocloud.io"]
# 删除掉之后注意保持json文件格式正确,上一行末尾的逗号去掉,然后保存执行
systemctl restart docker
systemctl enable kubelet
PS 脚本说明:
- 脚本开头的几个参数,是关于你想使用的 k8s 版本以及 crictl 安装包,想要使用其他版本的可以在开头改一下。
想要查看目前支持哪些版本可以使用以下命令:
yum list --showduplicates kubeadm --disableexcludes=kubernetes
-
kubestart init-env
命令后面还可以添加其他参数,选择容器运行时,可以使用docker
或者containerd
,默认是 docker -
执行
kubeadm init
命令使用的是/etc/kubernetes/kubeadm.yaml
配置文件启动的,如果有特殊需要,在执行完init-env
命令后去修改这个文件内容就可以,其实启动集群的方式就是这条命令:
kubeadm init --config /etc/kubernetes/kubeadm.yaml
- 默认使用的是
calico
作为 cni 应用,你如果想使用其他的,去修改一下脚本里面的init-env-kubecomp
函数即可。
热门相关:绝天武帝 我真不是学神 今宵大厦2粤 极品明君 婚婚欲睡:腹黑老公请节制