ubuntu22.04一键部署k8s-v1.25.0集群
Ubuntu 22.04 LTS
k8s-v1.25.0
containerd-1.6.8
ntpdate cn.pool.ntp.orgapt-get install ntpdate
ssh-keygenfor i in master node{1..2}; do echo ">>> $i";ssh-copy-id $i;done
# Ubuntu 22.04 LTS(清华源)cat > /etc/apt/sources.list << 'EOF'# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse# 预发布软件源,不建议启用# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverseEOFapt update
一键通用安装脚本
cat > k8s-1.25.0.sh << 'eof'#!/bin/bashstart=$(date +%s)node=$1# 环境准备# 1、关闭防火墙echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 关闭防火墙 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "ufw disable"; done# 2、关闭swapecho -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 关闭 swap の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "swapoff -a"; donefor i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "sed -i 's/.*swap.*/#&/g' /etc/fstab"; done# 3、加载IPVS模块echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 加载IPVS模块 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"for i in ${node[*]}; do echo -e "\e[32;5m>>> $i\e[0m";ssh root@$i "apt install ipset ipvsadm -y"; donecat > /etc/modules-load.d/ipvs.conf << EOFmodprobe -- ip_vsmodprobe -- ip_vs_rrmodprobe -- ip_vs_wrrmodprobe -- ip_vs_shmodprobe -- nf_conntrackEOFfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/modules-load.d/ipvs.conf root@$i:/etc/modules-load.d;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "bash -x /etc/modules-load.d/ipvs.conf";done# 4、安装containerecho -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装 container.io-v1.6.8-1 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpgfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/apt/keyrings/docker.gpg root@$i:/etc/apt/keyrings;doneecho \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/nullfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/apt/sources.list.d/docker.list root@$i:/etc/apt/sources.list.d;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "apt-get update && apt-get install containerd.io=1.6.8-1";donecat <<EOF | sudo tee /etc/modules-load.d/containerd.confoverlaybr_netfilterEOFfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/modules-load.d/containerd.conf root@$i:/etc/modules-load.d;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "modprobe overlay && modprobe br_netfilter";donecat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.confnet.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1net.bridge.bridge-nf-call-ip6tables = 1EOFfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/sysctl.d/99-kubernetes-cri.conf root@$i:/etc/sysctl.d;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i sysctl --system;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "mkdir -p /etc/containerd && containerd config default > /etc/containerd/config.toml";done# 修改cgroup Driver为systemdecho -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 修改cgroup Driver为 systemd の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"for i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#' /etc/containerd/config.toml";done# 更改sandbox_image为pause:3.8echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 更改sandbox_image为pause:3.8 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"for i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "sed -ri 's#k8s.gcr.io\/pause:3.6#registry.aliyuncs.com\/google_containers\/pause:3.8#' /etc/containerd/config.toml";done# endpoint位置添加阿里云的镜像源for i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "sed -ri 's#https:\/\/registry-1.docker.io#https:\/\/registry.aliyuncs.com#' /etc/containerd/config.toml";donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "systemctl daemon-reload && systemctl restart containerd";done# 5、安装k8s-1.25.0echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装 kubelet-v1.25.0 kubelet-v1.25.0 kubectl-v1.25.0 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"curl -fsSL https://repo.huaweicloud.com/kubernetes/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/kubernetes-archive-keyring.gpgfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /usr/share/keyrings/kubernetes-archive-keyring.gpg root@$i:/usr/share/keyrings;donecat <<EOF >/etc/apt/sources.list.d/kubernetes.listdeb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/kubernetes/apt kubernetes-xenial mainEOFfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/apt/sources.list.d/kubernetes.list root@$i:/etc/apt/sources.list.d;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "apt-get update && apt-get install -y kubelet=1.25.0-00 kubeadm=1.25.0-00 kubectl=1.25.0-00";done# 设置crictlecho -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 设置crictl の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"cat << EOF >> /etc/crictl.yamlruntime-endpoint: unix:///var/run/containerd/containerd.sockimage-endpoint: unix:///var/run/containerd/containerd.socktimeout: 10debug: falseEOFfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp /etc/crictl.yaml root@$i:/etc/crictl.yaml;done# 初始化ymlmkdir ~/kubeadm_init && cd ~/kubeadm_initcat > kubeadm-init.yaml << EOFapiVersion: kubeadm.k8s.io/v1beta3bootstrapTokens:- groups:- system:bootstrappers:kubeadm:default-node-tokentoken: abcdef.0123456789abcdefttl: 24h0m0susages:- signing- authenticationkind: InitConfigurationlocalAPIEndpoint:advertiseAddress: `hostname -I` #master_ipbindPort: 6443nodeRegistration:criSocket: unix:///var/run/containerd/containerd.sockimagePullPolicy: IfNotPresentname: mastertaints:- effect: "NoSchedule"key: "node-role.kubernetes.io/master"---apiServer:timeoutForControlPlane: 4m0sapiVersion: kubeadm.k8s.io/v1beta3certificatesDir: /etc/kubernetes/pkiclusterName: kubernetescontrollerManager: {}dns: {}etcd:local:dataDir: /var/lib/etcdimageRepository: registry.aliyuncs.com/google_containerskind: ClusterConfigurationkubernetesVersion: v1.25.0networking:dnsDomain: cluster.localserviceSubnet: 10.96.0.0/12podSubnet: 10.244.0.0/16scheduler: {}---apiVersion: kubeproxy.config.k8s.io/v1alpha1kind: KubeProxyConfigurationmode: ipvs---apiVersion: kubelet.config.k8s.io/v1beta1kind: KubeletConfigurationcgroupDriver: systemdEOF# 预拉取镜像echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 预拉取镜像 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"kubeadm config images pull --config kubeadm-init.yamlfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "crictl pull registry.aliyuncs.com/google_containers/pause:3.8 && crictl pull registry.aliyuncs.com/google_containers/kube-proxy:v1.25.0";done# 初始化集群echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 初始化集群 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"kubeadm init --config=kubeadm-init.yaml | tee kubeadm-init.logmkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config# 加入集群echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 加入集群 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"cat ~/kubeadm_init/kubeadm-init.log |grep token |tail -2 >join.token.shfor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";scp ~/kubeadm_init/join.token.sh root@$i:/root/join.token.sh;donefor i in ${node[*]};doecho -e "\e[32;5m>>> $i\e[0m";ssh root@$i "bash /root/join.token.sh 1>/dev/null 2>&1";done# 安装flannelecho -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の 安装flannel の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"cat > ~/kube-flannel.yml << 'EOF'---kind: NamespaceapiVersion: v1metadata:name: kube-flannellabels:pod-security.kubernetes.io/enforce: privileged---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: flannelrules:- apiGroups:- ""resources:- podsverbs:- get- apiGroups:- ""resources:- nodesverbs:- list- watch- apiGroups:- ""resources:- nodes/statusverbs:- patch---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: flannelroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: flannelsubjects:- kind: ServiceAccountname: flannelnamespace: kube-flannel---apiVersion: v1kind: ServiceAccountmetadata:name: flannelnamespace: kube-flannel---kind: ConfigMapapiVersion: v1metadata:name: kube-flannel-cfgnamespace: kube-flannellabels:tier: nodeapp: flanneldata:cni-conf.json: |{"name": "cbr0","cniVersion": "0.3.1","plugins": [{"type": "flannel","delegate": {"hairpinMode": true,"isDefaultGateway": true}},{"type": "portmap","capabilities": {"portMappings": true}}]}net-conf.json: |{"Network": "10.244.0.0/16","Backend": {"Type": "vxlan"}}---apiVersion: apps/v1kind: DaemonSetmetadata:name: kube-flannel-dsnamespace: kube-flannellabels:tier: nodeapp: flannelspec:selector:matchLabels:app: flanneltemplate:metadata:labels:tier: nodeapp: flannelspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/osoperator: Invalues:- linuxhostNetwork: truepriorityClassName: system-node-criticaltolerations:- operator: Existseffect: NoScheduleserviceAccountName: flannelinitContainers:- name: install-cni-plugin#image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0command:- cpargs:- -f- /flannel- /opt/cni/bin/flannelvolumeMounts:- name: cni-pluginmountPath: /opt/cni/bin- name: install-cni#image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0command:- cpargs:- -f- /etc/kube-flannel/cni-conf.json- /etc/cni/net.d/10-flannel.conflistvolumeMounts:- name: cnimountPath: /etc/cni/net.d- name: flannel-cfgmountPath: /etc/kube-flannel/containers:- name: kube-flannel#image: flannelcni/flannel:v0.19.0 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.0command:- /opt/bin/flanneldargs:- --ip-masq- --kube-subnet-mgrresources:requests:cpu: "100m"memory: "50Mi"limits:cpu: "100m"memory: "50Mi"securityContext:privileged: falsecapabilities:add: ["NET_ADMIN", "NET_RAW"]env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: EVENT_QUEUE_DEPTHvalue: "5000"volumeMounts:- name: runmountPath: /run/flannel- name: flannel-cfgmountPath: /etc/kube-flannel/- name: xtables-lockmountPath: /run/xtables.lockvolumes:- name: runhostPath:path: /run/flannel- name: cni-pluginhostPath:path: /opt/cni/bin- name: cnihostPath:path: /etc/cni/net.d- name: flannel-cfgconfigMap:name: kube-flannel-cfg- name: xtables-lockhostPath:path: /run/xtables.locktype: FileOrCreateEOFkubectl apply -f ~/kube-flannel.yml# kubectl补全echo -e "\e[32;5m[=====♫ ♬ ♪ ♩ ♭ ♪ の kubectl补全 の ♪ ♭ ♩ ♪ ♬ ♫=====]\e[0m"apt install bash-completion -ysource /etc/profile.d/bash_completion.shecho "source <(crictl completion bash)" >> ~/.bashrcecho "source <(kubectl completion bash)" >> ~/.bashrcecho -e "\033[0;33m██╗ ██╗ █████╗ ███████╗██║ ██╔╝██╔══██╗██╔════╝█████╔╝ ╚█████╔╝███████╗██╔═██╗ ██╔══██╗╚════██║██║ ██╗╚█████╔╝███████║╚═╝ ╚═╝ ╚════╝ ╚══════ has been installed !!!\033[0m"# 脚本执行时间end=$(date +%s)take=$(( end - start ))echo -e "\e[32;5m[=======================]\e[0m"echo -e "\e[32;5m脚本执行时间为 ---> ${take} seconds \e[0m"echo -e "\e[32;5m[=======================]\e[0m"eof
chmod +x k8s-1.25.0.sh./k8s-1.25.0.sh "master node1 node2"su -
一主两从
master
node1
node2
如果需要多个node节点,在./k8s-1.25.0.sh “master node1 node2 node3 node4”后面加
