master: ip-1 k8s-master
worker-1: ip-2 k8s-worker-1
worker-2: ip-2 k8s-worker-2
sudo hostnamectl --static set-hostname k8s-master
sudo hostnamectl --static set-hostname k8s-worker-1
sudo hostnamectl --static set-hostname k8s-worker-2
sudo apt update
sudo apt -y full-upgrade
[ -f /var/run/reboot-required ] && sudo reboot -f
sudo swapoff -a
sudo sed -i '/swap/ s/^\(.*\)$/#\1/g' /etc/fstab
# -i 插入修改模式,g 标识符表示全局查找替换,表示注释掉swap的那一行。
free -h
[主机:master 和所有worker]
# Enable kernel modules
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# Add some settings to sysctl
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 重新加载 sysctl
sudo sysctl --system
[主机:master 和所有worker]
自 Kubernetes v1.24 起,Dockershim 已被删除
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
vi /etc/apt/sources.list 替换成如下:
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
sudo apt-get update
# 安装GPG证书
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get upgrade
sudo apt-get update
sudo apt install -y containerd.io
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo sed -i 's/k8s.gcr.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
apt-get update
Note: At time of writing this guide, Xenial is the latest Kubernetes repository but when repository is available for Ubuntu 22.04 (Jammy Jellyfish) then you need replace xenial word with ‘jammy’ in ‘apt-add-repository’ command.
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
#apt-mark 用于将软件包标记/取消标记为自动安装。 hold 选项用于将软件包标记为保留,以防止软件包被自动安装、升级或删除。
复制出来自节点worker1,worker2,
这样可以无需重复在worker机器上重复上面的步骤。
sudo kubeadm init \
--control-plane-endpoint=k8s-master \
--kubernetes-version=v1.25.0 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.24.0.0/16 \
--ignore-preflight-errors=Swap
在kubeadm init后的打印信息中 找到 kubernetes的配置信息提示
master中直接执行,
worker中不能直接执行
(需要将master中的etc/kubernetes/admin.conf 复制到worker的/etc/kubernetes路径下 才能执行)
$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
在kubeadm init后的打印信息中 找到对应的 worker节点的join信息 复制出在各个worker中执行
#例子 如下
kubeadm join k8s-master:6443 --token qankob.c47c3h9otd5mo9m0 --discovery-token-ca-cert-hash sha256:9a8e7922f82452f3add16a12ebb49d8a1c51b6b778c8f5ad4979c0ecf6cf6c1c
join信息执行后 在master 中可以 查看k8s集群信息
kubectl cluster-info
kubectl get nodes
curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
sed -i "s#192\.168\.0\.0/16#10\.244\.0\.0/16#" calico.yaml
kubectl apply -f calico.yaml
#查看k8s组件pod的状态
kubectl get pods -n kube-system
#查看nodes 状态
kubectl get nodes
https://blog.csdn.net/shelutai/article/details/126604857
https://blog.csdn.net/weixin_56752399/article/details/123217472
https://www.bilibili.com/video/BV1gV4y1p7CL?spm_id_from=333.337.search-card.all.click&vd_source=9d3f2a31f918888addf6f0d78cae7372
https://feisky.gitbooks.io/kubernetes/content/network/calico/calico.html
https://kubernetes.io/zh-cn/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/
https://kuboard.cn/learning/k8s-intermediate/service/cni.html