Created
November 27, 2023 04:44
-
-
Save crazygit/fe4ad930c82e881083b8a866334b7f4f to your computer and use it in GitHub Desktop.
Setup K8S Cluster with Vagrant and Kubeadm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -eux | |
# 目前写死的是ubuntu 22.04 arm架构的源 | |
function use_tsinghua_mirror() { | |
cat >/etc/apt/sources.list <<EOF | |
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 | |
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse | |
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse | |
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse | |
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse | |
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse | |
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse | |
deb http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse | |
# deb-src http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse | |
# 预发布软件源,不建议启用 | |
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse | |
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse | |
EOF | |
} | |
# 安装常用软件 | |
function install_software() { | |
apt-get update && apt-get install -y vim net-tools | |
} | |
function install_containerd() { | |
apt-get update && apt-get install -y ca-certificates curl gnupg | |
install -m 0755 -d /etc/apt/keyrings | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg | |
chmod a+r /etc/apt/keyrings/docker.gpg | |
# Add the repository to Apt sources: | |
echo \ | |
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ | |
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | | |
sudo tee /etc/apt/sources.list.d/docker.list >/dev/null | |
apt-get update | |
apt-get install -y containerd.io | |
apt-mark hold containerd.io | |
} | |
function install_kubeadm() { | |
apt-get update | |
apt-get install -y apt-transport-https ca-certificates curl | |
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg | |
# This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list | |
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list | |
apt-get update && apt-get install -y kubelet kubeadm kubectl | |
apt-mark hold kubelet kubeadm kubectl | |
} | |
function disable_swap() { | |
swapoff -a | |
rm /swap.img | |
sed -i '/swap/s/^\(.*\)$/# \1/g' /etc/fstab | |
} | |
function config_system() { | |
modprobe br_netfilter | |
echo 1 | tee /proc/sys/net/ipv4/ip_forward | |
echo 1 | tee /proc/sys/net/bridge/bridge-nf-call-iptables | |
# 默认的containerd的配置(通过Docker安装的默认配置)是没有enable cri服务的,需要更新配置文件 | |
containerd config default | sudo tee /etc/containerd/config.toml | |
# 设置containerd使用正确的cgroup, 不然创建集群后,会看到k8s的系统组件的容器一直在重启 | |
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml | |
# 配置crictl模式使用的runtime,生成的配置文件在/etc/crictl.yaml,可以随时修改。 | |
crictl config runtime-endpoint unix:///var/run/containerd/containerd.sock | |
systemctl enable containerd.service | |
systemctl enable kubelet.service | |
systemctl restart containerd.service | |
systemctl restart kubelet.service | |
} | |
function main() { | |
export DEBIAN_FRONTEND=noninteractive | |
disable_swap | |
use_tsinghua_mirror | |
install_software | |
# k8s 1.24版本开始不再使用docker,而是默认使用containerd | |
install_containerd | |
install_kubeadm | |
config_system | |
} | |
main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
Vagrant.configure("2") do |config| | |
# common config for master and node | |
config.vm.provision "shell", path: "init.sh" | |
config.vm.box = "gyptazy/ubuntu22.04-arm64" | |
config.vm.box_check_update = false | |
# master | |
config.vm.define "master" do |master| | |
master.vm.network "private_network", ip: "10.0.0.101" | |
master.vm.hostname = "master.k8s.local" | |
master.vm.provider "vmware_desktop" do |v| | |
v.vmx["memsize"] = "4096" | |
v.vmx["numvcpus"] = "2" | |
end | |
end | |
# node | |
(1..2).each do |i| | |
config.vm.define "node#{i}" do |node| | |
node.vm.network "private_network", ip: "10.0.0.20#{i}" | |
node.vm.hostname = "node#{i}.k8s.local" | |
node.vm.provider "vmware_desktop" do |v| | |
v.vmx["memsize"] = "2048" | |
v.vmx["numvcpus"] = "2" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup up k8s cluster by kubeadmin on mac with Vagrant
master节点配置
先连接到主机
vagrant ssh master
初始化master节点