Kubernetes是Google開源的一個容器編排引擎,簡稱“k8s”,它支援自動化部署、大規模可伸縮、應用容器化管理。在生産環境中部署一個應用程式時,通常要部署該應用的多個執行個體以便對應用請求進行負載均衡。
在Kubernetes中,我們可以建立多個容器,每個容器裡面運作一個應用執行個體,然後通過内置的負載均衡政策,實作對這一組應用執行個體的管理、發現、通路,而這些細節都不需要運維人員去進行複雜的手工配置和處理。
開篇堆張圖吧,主要是一直覺得她比較富态…
下面簡單介紹一下如何利用sealos部署及管理工具快速建構一個k8s實驗環境
硬體配置規劃:
這裡我們采用虛拟機模拟生産環境中的實體機(當然也可以在雲主機下部署k8s),具體配置規劃如下:
節點類型 | 節點名稱 | Cpu配置 | 記憶體配置 | 磁盤配置 | IP配置 | 作業系統 |
Master | dmster | 4c | 4GB | 80G | Ip:192.168.100.80/24 Gateway: 192.168.100.254 | Debian11 |
Node | Dnode1 | 2c | 3GB | 80G | Ip:192.168.100.81/24 Gateway: 192.168.100.254 | Debian11 |
Node | Dnode2 | 2c | 3GB | 80G | Ip:192.168.100.82/24 Gateway: 192.168.100.254 | Debian11 |
注:如果主控端硬體配置有限,那麼主節點也可以配置為2核心+3GB,node節點可配置1核心+2GB,存儲這塊每台vm配置30GB也夠用,當然,硬體配置越低,性能就越捉襟見肘。
ok,言歸正傳,下面開始幹活。
1 作業系統基礎環境準備
sealos 是一個簡單的 go 二進制檔案,可以安裝在大多數 Linux 作業系統中。
以下是一些基本的安裝要求:
n 每個叢集節點應該有不同的主機名。 主機名不要帶下劃線。
n 所有節點的時間同步。
n 在 Kubernetes 叢集的第一個節點上運作sealos run指令,目前叢集外的節點不支援叢集安裝。
n 建議使用幹淨的作業系統來建立叢集。不要自己裝 Docker。
n 支援大多數 Linux 發行版,例如:Ubuntu CentOS Rocky linux。
n 支援 DockerHub 中支援的 Kubernetes 版本。
n 支援使用 containerd 作為容器運作時。
n 在公有雲上請使用私有 IP。
1.1 安裝必要的軟體包
作業系統這塊咱們簡化安裝,盡量不要多餘安裝無關的軟體包,這樣跑起來才輕快。
首先我們更新一下deb軟體源,安裝一些sealos部署過程中用到或者有依賴關系的軟體包,具體到我的dedian11環境,需要補充安裝net-tools vim ntpdate ebtables ethtool socat iptables,當然,個人在選用及安裝作業系統方面或多或少會有些許差異,可能需要安裝的軟體包也各不相同,咱們不教條,根據實際狀況來吧。
root@dmaster:~# apt update 命中:1 http://mirrors.huaweicloud.com/debian bullseye InRelease 命中:2 http://mirrors.huaweicloud.com/debian bullseye-updates InRelease 命中:3 http://security.debian.org/debian-security bullseye-security InRelease 正在讀取軟體包清單... 完成 正在分析軟體包的依賴關系樹... 完成 正在讀取狀态資訊... 完成 所有軟體包均為最新。 root@dmaster:~# apt install -y net-tools vim ntpdate ebtables ethtool socat iptables |
注意:本篇全部操作都是在root賬戶下完成的,為了操作安全期間,建議在生産環境部署時盡量采用普通賬戶sudo來運作操作指令。
1.2 允許root使用者遠端ssh登入
Debian默禁止了root使用者ssh登入,我們需要打開它。在ssh服務配置檔案中添加如下兩行并儲存
vim /etc/ssh/sshd_config PermitRootLogin yes PasswordAuthentication yes |
重新開機ssh服務
systemctl restart ssh |
1.3 配置主機名
按照規劃配置節點主機名,確定各節點之間主機名不重複。
root@debian:~# hostnamectl set-hostname dmaster root@debian:~# cat /etc/hostname dmaster root@debian:~# vim /etc/hosts B 127.0.0.1 localhost 127.0.1.1 dmaster |
1.4 配置主機網絡
按照規劃配置就可以
root@dmaster:~# vim /etc/network/interfaces allow-hotplug ens18 auto ens18 iface ens18 inet static address 192.168.100.80 netmask 255.255.255.0 gateway 192.168.100.254 |
配置dns
root@dmaster:~# vim /etc/resolv.conf nameserver 114.114.114.114 nameserver 8.8.8.8 |
重新開機服務
root@dmaster:~# systemctl restart networking.service |
1.5 關閉交換分區
切記,安裝k8s之前一定要關閉交換分區,這個也是先決條件
swapoff -a |
root@k8s-master:~# vi /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # systemd generates mount units based on this file, see systemd.mount(5). # Please run 'systemctl daemon-reload' after making changes here. # # <file system> <mount point> <type> <options> <dump> <pass> # / was on /dev/vda2 during installation UUID=49dc03d8-5c7f-425e-842c-664e5de64d3b / ext4 errors=remount-ro 0 1 # /boot/efi was on /dev/vda1 during installation UUID=0664-8DBE /boot/efi vfat umask=0077 0 1 # swap was on /dev/vda3 during installation #UUID=411feef2-8862-4505-a3ff-c4d840d4cbac none swap sw 0 0 /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0 |
1.6 檢查系統時區配置
時區配置為Asia/Shanghai
root@k8s-master:~# timedatectl show Timezone=Asia/Shanghai LocalRTC=no CanNTP=yes NTP=yes NTPSynchronized=yes TimeUSec=Fri 2022-10-14 17:42:22 CST RTCTimeUSec=Fri 2022-10-14 17:42:23 CST |
檢查後如有問題可運作指令修改配置
timedatectl set-timezone Asia/Shanghai |
1.7 配置節點間時間同步
Sealos部署及k8s叢集都要求各節點之間的時間要保持同步,最好網内能有一台ntp伺服器,各節點都從ntp伺服器來同步時間,這裡偷個懶吧,直接都向阿裡雲時鐘伺服器看齊吧。
crontab -e */5 * * * * /usr/sbin/ntpdate -u ntp.aliyun.com |
2 部署K8S叢集
采用deb源來安裝sealos程式,這裡部署的是最新的4.1.3版本。
注意:生産環境建議不要這樣冒進,采用最新版本,當出現比較棘手的問題時,在網絡上很難擷取到現成的解決方案,建議選擇相對較新的穩定版本來使用。
root@dmaster:~# echo "deb [trusted=yes] https://apt.fury.io/labring/ /" | tee /etc/apt/sources.list.d/labring.list deb [trusted=yes] https://apt.fury.io/labring/ / root@dmaster:~# apt update 命中:1 http://mirrors.huaweicloud.com/debian bullseye InRelease 擷取:2 http://mirrors.huaweicloud.com/debian bullseye-updates InRelease [44.1 kB] 命中:3 http://security.debian.org/debian-security bullseye-security InRelease 忽略:4 https://apt.fury.io/labring InRelease 忽略:5 https://apt.fury.io/labring Release 忽略:6 https://apt.fury.io/labring Packages 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 擷取:6 https://apt.fury.io/labring Packages 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 忽略:7 https://apt.fury.io/labring Translation-en 忽略:8 https://apt.fury.io/labring Translation-zh 忽略:9 https://apt.fury.io/labring Translation-zh_CN 已下載下傳 46.4 kB,耗時 12秒 (3,840 B/s) 正在讀取軟體包清單... 完成 正在分析軟體包的依賴關系樹... 完成 正在讀取狀态資訊... 完成 所有軟體包均為最新。 root@dmaster:~# apt install sealos 正在讀取軟體包清單... 完成 正在分析軟體包的依賴關系樹... 完成 正在讀取狀态資訊... 完成 下列【新】軟體包将被安裝: sealos 更新了 0 個軟體包,新安裝了 1 個軟體包,要解除安裝 0 個軟體包,有 0 個軟體包未被更新。 需要下載下傳 21.0 MB 的歸檔。 解壓縮後會消耗 21.6 MB 的額外空間。 擷取:1 https://apt.fury.io/labring sealos 4.1.3 [21.0 MB] 已下載下傳 21.0 MB,耗時 5秒 (3,959 kB/s) 正在選中未選擇的軟體包 sealos。 (正在讀取資料庫 ... 系統目前共安裝有 36187 個檔案和目錄。) 準備解壓 .../sealos_4.1.3_amd64.deb ... 正在解壓 sealos (4.1.3) ... 正在設定 sealos (4.1.3) ... |
運作安裝指令,快速建構叢集環境,
注意:
Ø 這裡需要先安裝helm,再安裝kubernetes,這兩者之間有依賴關系,最後安裝CNI插件(calico:),run後的順序即是安裝順序
Ø 各主機密碼預設是root密碼,在部署時保持一緻
Ø Master節點也可以采用多台建構成高可用叢集,實驗環境囿于硬體資源限制,隻部署了一台
sealos run labring/helm:v3.8.2 labring/kubernetes:v1.25.0 labring/calico:v3.24.1 --masters 192.168.100.80 --nodes 192.168.100.81,192.168.100.82 -p 主機密碼 |
Ok,幾分鐘後,一個k8s叢集實驗環境就部署完成了,sealos号稱分鐘級建構k8s高可用叢集,名不虛傳!
3 K8s叢集環境測試
拉取一個nginx鏡像,并建立 nginx Pod
root@dmaster:~# kubectl create deployment nginx --image=nginx deployment.apps/nginx created root@dmaster:~# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-76d6c9b8c-927hj 1/1 Running 0 40s |
對外暴露 nginx 端口
root@dmaster:~# kubectl expose deployment nginx --port=80 --type=NodePort service/nginx exposed root@dmaster:~# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/nginx-76d6c9b8c-927hj 1/1 Running 0 4m6s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h service/nginx NodePort 10.96.1.98 <none> 80:31657/TCP 29s |
在外網中以任一 master或者node節點加上對外暴露端口31657通路web界面,看看k8s叢集是否工作正常
本章節到此結束,後續會單開一個章節講講k8s的可視化管理界面部署及基本操作。