在阿里云上使用kubeadm安裝1.27版本的k8s

官方安裝文檔:https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#check-required-ports

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 看到橋接流量

參考文檔:https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes/#轉(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

參考文檔:https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-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ò)提供者。

 # 訪問上述配置文件地址顾孽,確認(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é)開頭的文檔。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末测秸,一起剝皮案震驚了整個濱河市疤估,隨后出現(xiàn)的幾起案子灾常,更是在濱河造成了極大的恐慌,老刑警劉巖铃拇,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件钞瀑,死亡現(xiàn)場離奇詭異,居然都是意外死亡慷荔,警方通過查閱死者的電腦和手機雕什,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來显晶,“玉大人贷岸,你說我怎么就攤上這事×坠停” “怎么了偿警?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長唯笙。 經(jīng)常有香客問我螟蒸,道長,這世上最難降的妖魔是什么睁本? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任尿庐,我火速辦了婚禮,結(jié)果婚禮上呢堰,老公的妹妹穿的比我還像新娘抄瑟。我一直安慰自己,他們只是感情好枉疼,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布皮假。 她就那樣靜靜地躺著,像睡著了一般骂维。 火紅的嫁衣襯著肌膚如雪惹资。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天航闺,我揣著相機與錄音褪测,去河邊找鬼。 笑死潦刃,一個胖子當(dāng)著我的面吹牛侮措,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乖杠,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼分扎,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了胧洒?” 一聲冷哼從身側(cè)響起畏吓,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤墨状,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后菲饼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肾砂,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年巴粪,在試婚紗的時候發(fā)現(xiàn)自己被綠了通今。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡肛根,死狀恐怖辫塌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情派哲,我是刑警寧澤臼氨,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站芭届,受9級特大地震影響储矩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜褂乍,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一持隧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧逃片,春花似錦屡拨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至损离,卻和暖如春哥艇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背僻澎。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工貌踏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窟勃。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓祖乳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親拳恋。 傳聞我的和親對象是個殘疾皇子凡资,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容