1. 準(zhǔn)備條件
1.1 硬件配置
在阿里云上購買3臺輕量應(yīng)用服務(wù)器碌廓,均選擇香港節(jié)點洛搀,每臺配置選擇2cpu仲智,2G內(nèi)存良蒸,例如三臺服務(wù)器的網(wǎng)絡(luò)參數(shù):
- k8s-master (公) 8.210.221.246 (內(nèi)) 172.17.17.160
- k8s-worker1 (公) 47.242.153.51 (內(nèi)) 172.17.17.161
- k8s-worker2 (公) 8.210.226.220 (內(nèi)) 172.17.17.162
1.2 配置防火墻
參考文檔:https://kubernetes.io/docs/reference/networking/ports-and-protocols/
1.2.1 計算網(wǎng)段
將上述3臺服務(wù)器的私有IP地址轉(zhuǎn)換為二進(jìn)制链方,并計算出網(wǎng)段為172.17.17.160/30
10進(jìn)制 | 2進(jìn)制 |
---|---|
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 配置防火墻端口
根據(jù)參考文檔對3臺服務(wù)器依次配置防火墻。
2. 安裝containerd, kubeadm, kubelet, kubectl
以下步驟均需在上述3臺服務(wù)器上執(zhí)行
2.1 關(guān)閉 swap
$ sed -i '/swap/d' /etc/fstab
$ swapoff -a
2.2 安裝容器運行時
2.2.1 轉(zhuǎn)發(fā) IPv4 并讓 iptables 看到橋接流量
$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
$ modprobe overlay
$ modprobe br_netfilter
# 設(shè)置所需的 sysctl 參數(shù)涮拗,參數(shù)在重新啟動后保持不變
$ 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
# 應(yīng)用 sysctl 參數(shù)而不重新啟動
$ 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節(jié)點
耳幢!本節(jié)腳本只在 Master 節(jié)點上運行
參考文檔:https://learn-k8s-from-scratch.readthedocs.io/en/latest/k8s-install/kubeadm.html#master
3.1 初始化Master節(jié)點
$ kubeadm config images pull
# 替換--apiserver-advertise-address的值為master的IP地址
# 保存好該命令的輸出,后續(xù)會用
$ 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
# 檢查狀態(tài)
$ kubectl get nodes
$ kubectl get pods -A
3.2 安裝 Pod 網(wǎng)絡(luò)附加組件
部署Pod網(wǎng)絡(luò)的方案有很多,我們這里選擇Flannel绷蹲,該方案可以用于 Kubernetes 的 overlay 網(wǎng)絡(luò)提供者。
- Pod網(wǎng)絡(luò)方案匯總: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
# 訪問上述配置文件地址顾孽,確認(rèn)其中 net-conf.json 與前面配置的 pod-network-cidr 一致
$ kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
# 驗證結(jié)果祝钢,pod都是running的狀態(tài),說明我們的network方案部署成功
$ kubectl get pods -A
4. 添加Worker節(jié)點
! 本節(jié)腳本只需在每個 Worker 節(jié)點上運行
參考文檔:https://learn-k8s-from-scratch.readthedocs.io/en/latest/k8s-install/kubeadm.html#worker
# 在兩個工作節(jié)點中均執(zhí)行前面保存輸出中的最后一條提示命令
$ kubeadm join 172.17.17.160:6443 --token d66afp.9b33ck7gqhdpjhkc \
--discovery-token-ca-cert-hash sha256:7d59d93d30fe498ea6ce6ec6ad2d6b2fd4c0388ffc51de5c680962cf44d09370
# 在master中驗證結(jié)果
$ kubectl get nodes
如果token超時若厚,或忘記token和discovery-token-ca-cert-hash拦英,可以參考本節(jié)開頭的文檔。