1. 准备条件
1.1 硬件配置
在阿里云上购买3台轻量应用服务器,均选择香港节点,每台配置选择2cpu,2G内存,例如三台服务器的网络参数:
- k8s-master (公) 8.210.221.246 (内) 172.17.17.160
- k8s-worker1 (公) 47.242.153.51 (内) 172.17.17.161
- k8s-worker2 (公) 8.210.226.220 (内) 172.17.17.162
1.2 配置防火墙
参考文档:https://kubernetes.io/docs/reference/networking/ports-and-protocols/
1.2.1 计算网段
将上述3台服务器的私有IP地址转换为二进制,并计算出网段为172.17.17.160/30
10进制 | 2进制 |
---|---|
172.17.17.160 | 10101100.00010001.00010001.10100000 |
172.17.17.161 | 10101100.00010001.00010001.10100001 |
172.17.17.162 | 10101100.00010001.00010001.10100010 |
1.2.2 配置防火墙端口
根据参考文档对3台服务器依次配置防火墙。
2. 安装containerd, kubeadm, kubelet, kubectl
以下步骤均需在上述3台服务器上执行
2.1 关闭 swap
$ sed -i '/swap/d' /etc/fstab
$ swapoff -a
2.2 安装容器运行时
2.2.1 转发 IPv4 并让 iptables 看到桥接流量
$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
$ modprobe overlay
$ modprobe br_netfilter
# 设置所需的 sysctl 参数,参数在重新启动后保持不变
$ cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 参数而不重新启动
$ sysctl --system
2.2.2 安装 containerd
参考文档:https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes/#containerd
# Set up the repository
$ apt-get update
$ apt-get install -y apt-transport-https ca-certificates curl
$ 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
$ 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
# Install Docker Engine
$ apt-get update
$ apt-get -y install containerd.io
$ containerd config default >/etc/containerd/config.toml
# 修改 cgroup 为 systemd
$ vim /etc/containerd/config.toml
# 修改为 SystemdCgroup = true, 如下
# [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
# ...
# [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
# SystemdCgroup = true
$ systemctl restart containerd
$ systemctl enable containerd
2.3 安装 kubeadm, kubelet, kubectl
$ curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | 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
# kubectl shell 自动补全
$ source <(kubectl completion bash)
2.4 验证
$ kubeadm version
$ kubelet --version
$ kubectl version
3. 配置Master节点
!本节脚本只在 Master 节点上运行
参考文档:https://learn-k8s-from-scratch.readthedocs.io/en/latest/k8s-install/kubeadm.html#master
3.1 初始化Master节点
$ kubeadm config images pull
# 替换--apiserver-advertise-address的值为master的IP地址
# 保存好该命令的输出,后续会用
$ kubeadm init --apiserver-advertise-address=172.17.17.160 --pod-network-cidr=10.244.0.0/16
# token
[init] Using Kubernetes version: v1.27.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
W0504 12:04:04.422870 310227 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.1, falling back to the nearest etcd version (3.5.7-0)
W0504 12:04:04.574874 310227 checks.go:835] detected that the sandbox image "registry.k8s.io/pause:3.6" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.k8s.io/pause:3.9" as the CRI sandbox image.
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [izj6ch7aknbnx00p8p712yz kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.17.17.160]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [izj6ch7aknbnx00p8p712yz localhost] and IPs [172.17.17.160 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [izj6ch7aknbnx00p8p712yz localhost] and IPs [172.17.17.160 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
W0504 12:04:08.075223 310227 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.1, falling back to the nearest etcd version (3.5.7-0)
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 7.001564 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node izj6ch7aknbnx00p8p712yz as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node izj6ch7aknbnx00p8p712yz as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: d66afp.9b33ck7gqhdpjhkc
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.17.17.160:6443 --token d66afp.9b33ck7gqhdpjhkc \
--discovery-token-ca-cert-hash sha256:7d59d93d30fe498ea6ce6ec6ad2d6b2fd4c0388ffc51de5c680962cf44d09370
要使非 root 用户可以运行 kubectl,请运行以下命令, 它们也是 kubeadm init 输出的一部分:
$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
# 检查状态
$ kubectl get nodes
$ kubectl get pods -A
3.2 安装 Pod 网络附加组件
部署Pod网络的方案有很多,我们这里选择Flannel,该方案可以用于 Kubernetes 的 overlay 网络提供者。
- Pod网络方案汇总:https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/addons/
- Flannel:https://github.com/flannel-io/flannel#deploying-flannel-manually
- 配置文件:https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
# 访问上述配置文件地址,确认其中 net-conf.json 与前面配置的 pod-network-cidr 一致
$ kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
# 验证结果,pod都是running的状态,说明我们的network方案部署成功
$ kubectl get pods -A
4. 添加Worker节点
! 本节脚本只需在每个 Worker 节点上运行
参考文档:https://learn-k8s-from-scratch.readthedocs.io/en/latest/k8s-install/kubeadm.html#worker
# 在两个工作节点中均执行前面保存输出中的最后一条提示命令
$ kubeadm join 172.17.17.160:6443 --token d66afp.9b33ck7gqhdpjhkc \
--discovery-token-ca-cert-hash sha256:7d59d93d30fe498ea6ce6ec6ad2d6b2fd4c0388ffc51de5c680962cf44d09370
# 在master中验证结果
$ kubectl get nodes
如果token超时,或忘记token和discovery-token-ca-cert-hash,可以参考本节开头的文档。