基于Ubuntu安裝Kubernetes集群指南

Kubernetes(K8S)做為容器編排的系統(tǒng)别凤,被廣泛應(yīng)用于容器、微服務(wù)领虹、服務(wù)網(wǎng)格中规哪。

想體驗、學(xué)習(xí)K8S的話塌衰,首先得把它裝好诉稍。但由于國內(nèi)的GFW網(wǎng)絡(luò)、公司內(nèi)部局域網(wǎng)的各種限制最疆,安裝過程會出現(xiàn)一些坑杯巨。故整理一下新鮮出爐的步驟,盡量把坑填上努酸。

在這安裝過程中服爷,我準(zhǔn)備了兩臺Ubuntu 22.04的系統(tǒng),一臺做為Master获诈,另一臺做為Worker仍源,一起組成一個mini版的集群。

0. 證書問題 - 可選

如果你在公司內(nèi)部的局域網(wǎng)烙荷,而且通過公司提供的代理服務(wù)器上網(wǎng)镜会,那么檬寂,多半需要此步驟终抽。否則會遇到各種自簽名證書驗證失敗的問題。

此步驟桶至,兩臺機器都需要做昼伴。

具體的證書下載,各個公司都不一樣镣屹,請咨詢你們家的IT人員圃郊。下載完之后,執(zhí)行:update-ca-certificates女蜈。

# 下載你家的自簽名證書
$ sudo cp 自簽名證書.crt /usr/local/share/ca-certificates/
$ sudo update-ca-certificates

這樣持舆,可以避免后續(xù)的各種證書驗證失敗色瘩。

1. 安裝Docker

此步驟,兩臺機器都需要做逸寓。

具體安裝步驟請參考:

Install Docker Engine on Ubuntu | Docker Documentation

2. 禁用Swap

此步驟居兆,兩臺機器都需要做。

分兩步竹伸,一步時臨時禁用泥栖,并馬上生效;另一步是永久性禁用勋篓。

$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

3. 加載內(nèi)核模塊

此步驟吧享,兩臺機器都需要做。

$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

$ sudo modprobe overlay
$ sudo modprobe br_netfilter

4. 設(shè)置sysctl 參數(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

sudo sysctl --system

5. 安裝K8S

此步驟,兩臺機器都需要做孤荣。

$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl
$ sudo curl --insecure -fsSLo https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
$ sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl

目前最新版的K8S是1.27.1甸陌。這些步驟做完之后,會被安裝到兩臺機器上盐股。

6. 修改containerd配置

此步驟钱豁,兩臺機器都需要做。

主要修改兩個地方疯汁,一個是從阿里云下載鏡像牲尺;另一個是啟用systemd。

第一個修改是啟用systemd:

$ 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

第二個修改是把遠程下載地址從Google家的改為阿里云的:

$ sudo vim /etc/containerd/config.toml

把這行:sandbox_image = "registry.k8s.io/pause:3.6"

改成:sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"

其實用sed幌蚊,跟第一個systemd的修改一起做了也行谤碳。

然后重啟containerd:

$ sudo systemctl restart containerd

這一步如果不做的話,后面的kubeadm init會因為無法下載鏡像而一直失敗溢豆。

7. 初始化Master節(jié)點

此步驟蜒简,僅需要在Master節(jié)點上操作。

sudo kubeadm init --apiserver-advertise-address 192.168.111.128 --pod-network-cidr 10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers

因為后面打算用Flannel來作為CNI漩仙,所以CIDR按要求配置了:10.244.0.0/16搓茬。

如果一切正常的話,可以看到這樣的輸出:

[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'
W0421 15:26:43.924760    8817 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)
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local ycwang-ubuntu] and IPs [10.96.0.1 192.168.111.128]
[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 [localhost ycwang-ubuntu] and IPs [192.168.111.128 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost ycwang-ubuntu] and IPs [192.168.111.128 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"
W0421 15:26:46.894146    8817 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 5.502175 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 ycwang-ubuntu 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 ycwang-ubuntu as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: sd27s0.dpnlrf96at6uwgl0
[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 192.168.111.128:6443 --token sd27s0.dpnlrf96at6uwgl0 \
??????--discovery-token-ca-cert-hash sha256:7a6acd******3e154c

這樣队他,集群的Master節(jié)點基本上好了卷仑。

8. 配置Master節(jié)點

此步驟,僅需要在Master節(jié)點上操作麸折。

根據(jù)上面的輸出锡凝,在Master節(jié)點輸入命令,使得可以用非root用戶操作kubectl:

$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

繼續(xù)輸入這些命令垢啼,實現(xiàn)Shell命令自動補全:

$ echo 'source <(kubectl completion bash)' >> ~/.bashrc
$ source ~/.bashrc

9. 安裝Flannel網(wǎng)絡(luò)

此步驟窜锯,僅需要在Master節(jié)點上操作张肾。

在Master節(jié)點上,安裝Flannel:

$ kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

至此锚扎,Master節(jié)點的安裝捌浩、配置都結(jié)束了。

下一步工秩,把Worker節(jié)點加入集群尸饺。

10. Worker加入集群

此步驟,僅需要在Worker節(jié)點上操作助币。

如果你有多臺Worker節(jié)點浪听,每個結(jié)點都需要做一次。

把第7步的join命令Copy出來眉菱,然后執(zhí)行:

$ sudo kubeadm join 192.168.111.128:6443 --token sd27s0.dpnlrf96at6uwgl0 \
??????--discovery-token-ca-cert-hash sha256:7a6acd******3e154c

一切正常的話迹栓,可以看到這樣的輸出,表明該機器已經(jīng)加入集群了:

[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

11. 查看集群信息

回到Master節(jié)點俭缓,可以看到集群已經(jīng)建立:

$ kubectl get nodes
NAME                   STATUS     ROLES           AGE   VERSION
ycwang-ubuntu          NotReady   control-plane   30m   v1.27.1
ycwang-ubuntu-worker   NotReady   <none>          43s   v1.27.1

一開始的時候克伊,狀態(tài)都是NotReady。

稍等片刻华坦,等需要的鏡像都下載愿吹、運行之后,就會全部變成Ready了惜姐。

$ kubectl get nodes
NAME                   STATUS   ROLES           AGE     VERSION
ycwang-ubuntu          Ready    control-plane   33m     v1.27.1
ycwang-ubuntu-worker   Ready    <none>          3m23s   v1.27.1

可以看到所有的Pod都在正常運行了:

$ kubectl get pods --all-namespaces -o wide
NAMESPACE      NAME                                    READY   STATUS    RESTARTS   AGE     IP                NODE                   NOMINATED NODE   READINESS GATES
kube-flannel   kube-flannel-ds-gl795                   1/1     Running   0          6m10s   192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-flannel   kube-flannel-ds-lq8p8                   1/1     Running   0          6m10s   192.168.111.129   ycwang-ubuntu-worker   <none>           <none>
kube-system    coredns-7bdc4cb885-lgsvw                1/1     Running   0          36m     10.244.1.3        ycwang-ubuntu-worker   <none>           <none>
kube-system    coredns-7bdc4cb885-ss4n5                1/1     Running   0          36m     10.244.1.2        ycwang-ubuntu-worker   <none>           <none>
kube-system    etcd-ycwang-ubuntu                      1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-apiserver-ycwang-ubuntu            1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-controller-manager-ycwang-ubuntu   1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-proxy-wtg74                        1/1     Running   0          6m48s   192.168.111.129   ycwang-ubuntu-worker   <none>           <none>
kube-system    kube-proxy-x4s95                        1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>
kube-system    kube-scheduler-ycwang-ubuntu            1/1     Running   0          36m     192.168.111.128   ycwang-ubuntu          <none>           <none>

這樣犁跪,整個集群搭建成功〈踉可以開始使用了坷衍。

驗證通過后,可以把上述命令合到一起条舔,保存成一個Shell文件枫耳。以后每次執(zhí)行該文件即可。

歡迎來到微服務(wù)孟抗、云原生的時代迁杨!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市夸浅,隨后出現(xiàn)的幾起案子仑最,更是在濱河造成了極大的恐慌扔役,老刑警劉巖帆喇,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異亿胸,居然都是意外死亡坯钦,警方通過查閱死者的電腦和手機预皇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來婉刀,“玉大人吟温,你說我怎么就攤上這事⊥患眨” “怎么了鲁豪?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長律秃。 經(jīng)常有香客問我爬橡,道長,這世上最難降的妖魔是什么棒动? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任糙申,我火速辦了婚禮,結(jié)果婚禮上船惨,老公的妹妹穿的比我還像新娘柜裸。我一直安慰自己,他們只是感情好粱锐,可當(dāng)我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布疙挺。 她就那樣靜靜地躺著,像睡著了一般怜浅。 火紅的嫁衣襯著肌膚如雪衔统。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天海雪,我揣著相機與錄音锦爵,去河邊找鬼。 笑死奥裸,一個胖子當(dāng)著我的面吹牛险掀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播湾宙,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼樟氢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了侠鳄?” 一聲冷哼從身側(cè)響起埠啃,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伟恶,沒想到半個月后碴开,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年潦牛,在試婚紗的時候發(fā)現(xiàn)自己被綠了眶掌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡巴碗,死狀恐怖朴爬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情橡淆,我是刑警寧澤召噩,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站逸爵,受9級特大地震影響蚣常,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜痊银,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一抵蚊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧溯革,春花似錦贞绳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至抖单,卻和暖如春萎攒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背矛绘。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工耍休, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人货矮。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓羊精,卻偏偏與公主長得像,于是被迫代替她去往敵國和親囚玫。 傳聞我的和親對象是個殘疾皇子喧锦,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,689評論 2 354

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