老 Z,運維架構師,云原生愛好者,目前專注于云原生運維,云原生領域技術堆疊涉及 Kubernetes、KubeSphere、DevOps、OpenStack、Ansible 等,
前言
知識點
- 定級:入門級
- KubeKey 安裝部署 KubeSphere 和 Kubernetes
- Debian 作業系統的基本配置
- Kubernetes 常用作業負載的創建
- KubeSphere 控制臺操作入門
演示服務器配置
| 主機名 | IP | CPU | 記憶體 | 系統盤 | 資料盤 | 用途 |
|---|---|---|---|---|---|---|
| ks-master-0 | 192.168.9.91 | 4 | 16 | 40 | 200 | KubeSphere/k8s-master/k8s-worker |
| ks-master-1 | 192.168.9.92 | 4 | 16 | 40 | 200 | KubeSphere/k8s-master/k8s-worker |
| ks-master-2 | 192.168.9.93 | 4 | 16 | 40 | 200 | KubeSphere/k8s-master/k8s-worker |
| 合計 | 3 | 12 | 48 | 120 | 600 |
演示環境涉及軟體版本資訊
- 作業系統:Debian 12
- KubeSphere:3.3.2
- Kubernetes:v1.26.0
- KubeKey: v3.0.7
簡介
Debian 曾經的開源 Linux 發行版的王者,唯一的電信級開源作業系統,我曾經在 IDC 作業時,所有的核心業務、自建防火墻都運行在 Debian 之上,穩如磐石,
基于多種原因 Debian 的用戶不斷減少,尤其是在國內,已經很少有人談及 Debian了,在當今尋求 CentOS 替代品的背景下,Debian 也是一種選擇,
本文基于我個人對 Debian 的情懷,寫了一篇測驗驗證檔案,僅僅適用于學習、測驗環境,生產環境需要進行充分的調研、測驗、驗證后再做決定,
當前 Debian 的穩定版版本號是 12,開發代號為 bookworm,發布于 2023年06月10日,生產環境使用,可以使用 Debian 10,開發代號 Buster,現在的 LTS 發行版本,
本文介紹了如何在 Debian 12 服務器上部署 KubeSphere 和 Kubernetes 集群,我們將使用 KubeSphere 開發的 KubeKey 工具實作自動化部署,在三臺服務器上實作高可用模式最小化部署 Kubernetes 集群和 KubeSphere,我們將提供詳細的部署說明,以便讀者輕松地完成部署程序,
作業系統基礎配置
請注意,以下操作無特殊說明時需在所有 Debian 服務器上執行,本文只選取其中一臺服務器作為演示,并假定其余服務器都已按照相同的方式進行配置和設定,
創建用戶
在安裝 KubeSphere 和 Kubernetes 之前,需要創建一個普通用戶,該用戶可以是安裝 Debian 系統時默認創建的初始用戶 debian,也可以是新創建的用戶,在本文中,我們將使用新建用戶 kube 作為示例,
- 創建用戶并設定密碼
通過執行以下命令,創建一個名為 kube 的新用戶,并設定該用戶的密碼,在提示輸入密碼時,請輸入所需的密碼并進行確認,
sudo useradd -m -s /bin/bash kube
sudo passwd kube
- 配置用戶免密碼執行命令
為了安裝和部署 KubeSphere 和 Kubernetes,需要將 kube 用戶配置為無需密碼即可使用 sudo 切換到 root 用戶執行系統命令,
要讓 kube 用戶無需密碼即可使用 sudo 命令,請執行以下命令,
cat <<EOF | sudo tee /etc/sudoers.d/kube
Defaults:kube !fqdn
Defaults:kube !requiretty
kube ALL=(ALL) NOPASSWD: ALL
EOF
配置主機名
sudo hostnamectl hostname ks-master-0
配置 hosts 檔案
編輯 /etc/hosts 檔案,將規劃的服務器 IP 和主機名添加到檔案中,
192.168.9.91 ks-master-0
192.168.9.92 ks-master-1
192.168.9.93 ks-master-2
配置基于 SSH 密鑰的身份驗證
KubeKey 支持在自動化部署 KubeSphere 和 Kubernetes 服務時,利用密碼和密鑰作為遠程服務器的連接驗證方式,本文會演示同時使用密碼和密鑰的配置方式,因此,需要為部署用戶 kube 配置免密碼 SSH身份驗證,
本小節為可選配置項,如果你使用純密碼的方式作為服務器遠程連接認證方式,可以忽略本節內容,
本文將 master-0 節點作為部署節點,下面的操作需要在 master-0 節點操作,
以 kube 用戶登陸系統,然后使用 ssh-keygen 命令生成一個新的 SSH 密鑰對,命令完成后,SSH 公鑰和私鑰將存盤在 /home/kube/.ssh 目錄中,
su - kube
ssh-keygen -t ed25519
命令執行效果如下:
debian@ks-master-0:~$ su - kube
Password:
kube@ks-master-0:~$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/kube/.ssh/id_ed25519):
Created directory '/home/kube/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kube/.ssh/id_ed25519
Your public key has been saved in /home/kube/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:mMmyz1xem5VbgMgSydTrr/P3Wqp3c3BcnoOJ12qSdQM kube@ks-master-0
The key's randomart image is:
+--[ED25519 256]--+
| .. |
| o .. |
| + . |
| . *.. . E .|
| . *.S . o =oo|
| o .. . B.*+|
| . ... * =oo|
| + o...*oBo .|
| + o+=+*+.o |
+----[SHA256]-----+
接下來,輸入以下命令將 SSH 公鑰從 master-0 節點發送到其他節點,命令執行時輸入 yes,以接受服務器的 SSH 指紋,然后在出現提示時輸入 kube 用戶的密碼,
ssh-copy-id kube@ks-master-0
下面是密鑰復制時,正確的輸出結果:
kube@ks-master-0:~$ ssh-copy-id kube@ks-master-0
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/kube/.ssh/id_ed25519.pub"
The authenticity of host 'ks-master-0 (192.168.9.91)' can't be established.
ED25519 key fingerprint is SHA256:1hcstZu/Ml3ghElXuZLRj+lF6EzlWXV6EaRr3/C1L/Y.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
kube@ks-master-0's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'kube@ks-master-0'"
and check to make sure that only the key(s) you wanted were added.
添加并上傳 SSH 公鑰后,您現在可以執行下面的命令驗證,通過 kube 用戶連接到所有服務器,無需密碼驗證,
kube@ks-master-0:~$ ssh kube@ks-master-1
# 登陸輸出結果 略
安裝系統依賴
在所有節點上,以 kube 用戶登陸系統(非必需,其他系統用戶也可),執行下面的命令為 Kubernetes 安裝系統基本依賴包,
sudo apt-get update
sudo apt-get install curl socat conntrack ebtables ipset ipvsadm
配置服務器時區
配置服務器時區為 Asia/Shanghai:
sudo timedatectl set-timezone Asia/Shanghai
安裝 chrony 配置時間同步
安裝 chrony 作為時間同步軟體:
sudo apt-get install chrony
修改組態檔 /etc/chrony/chrony.conf,修改 ntp 服務器配置:
sudo vi /etc/chrony/chrony.conf
# 洗掉所有的 pool 配置
pool 2.debian.pool.ntp.org iburst
# 增加國內的 ntp 服務器,或是指定其他常用的時間服務器
server ntp.api.bz iburst
重啟 chrony 服務:
sudo systemctl restart chrony
驗證 chrony 同步狀態:
# 執行查看命令
sudo chronyc sourcestats -v
# 正常的輸出結果如下
kube@ks-master-0:~$ sudo chronyc sourcestats -v
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
ec2-3-64-163-50.eu-centr> 0 0 0 +0.000 2000.000 +0ns 4000ms
安裝部署 KubeSphere 和 Kubernetes
下載 KubeKey
本文將 master-0 節點作為部署節點,把 KubeKey (下文簡稱 kk) 最新版(3.0.7)二進制檔案下載到該服務器,具體 kk 版本號可以在 kk 發行頁面查看,
- 下載最新版的 KubeKey
cd ~
mkdir kubekey
cd kubekey/
# 選擇中文區下載(訪問github受限時使用)
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | sh -
# 也可以使用下面的命令指定具體版本
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 sh -
# 正確的執行效果如下
kube@ks-master-0:~$ cd ~
kube@ks-master-0:~$ mkdir kubekey
kube@ks-master-0:~$ cd kubekey/
kube@ks-master-0:~/kubekey$ export KKZONE=cn
kube@ks-master-0:~/kubekey$ curl -sfL https://get-kk.kubesphere.io | sh -
Downloading kubekey v3.0.7 from https://kubernetes.pek3b.qingstor.com/kubekey/releases/download/v3.0.7/kubekey-v3.0.7-linux-amd64.tar.gz ...
Kubekey v3.0.7 Download Complete!
kube@ks-master-0:~/kubekey$ ls -l
總計 111988
-rwxr-xr-x 1 kube kube 78901793 1月18日 09:59 kk
-rw-r--r-- 1 kube kube 35769576 6月16日 10:07 kubekey-v3.0.7-linux-amd64.tar.gz
- 查看 KubeKey 支持的 Kubernetes 版本串列
./kk version --show-supported-k8s
# 輸出如下
kube@ks-master-0:~/kubekey$ ./kk version --show-supported-k8s
v1.19.0
v1.19.8
v1.19.9
v1.19.15
v1.20.4
v1.20.6
v1.20.10
v1.21.0
v1.21.1
v1.21.2
v1.21.3
v1.21.4
v1.21.5
v1.21.6
v1.21.7
v1.21.8
v1.21.9
v1.21.10
v1.21.11
v1.21.12
v1.21.13
v1.21.14
v1.22.0
v1.22.1
v1.22.2
v1.22.3
v1.22.4
v1.22.5
v1.22.6
v1.22.7
v1.22.8
v1.22.9
v1.22.10
v1.22.11
v1.22.12
v1.22.13
v1.22.14
v1.22.15
v1.22.16
v1.22.17
v1.23.0
v1.23.1
v1.23.2
v1.23.3
v1.23.4
v1.23.5
v1.23.6
v1.23.7
v1.23.8
v1.23.9
v1.23.10
v1.23.11
v1.23.12
v1.23.13
v1.23.14
v1.23.15
v1.24.0
v1.24.1
v1.24.2
v1.24.3
v1.24.4
v1.24.5
v1.24.6
v1.24.7
v1.24.8
v1.24.9
v1.25.0
v1.25.1
v1.25.2
v1.25.3
v1.25.4
v1.25.5
v1.26.0
創建 Kubernetes 和 KubeSphere 部署組態檔
創建集群組態檔,本示例中,選擇 KubeSphere v3.3.2 和 Kubernetes v1.26.0,同時,指定組態檔名稱為 kubesphere-v3.3.2.yaml,如果不指定,默認的檔案名為 config-sample.yaml,
./kk create config -f kubesphere-v3.3.2.yaml --with-kubernetes v1.26.0 --with-kubesphere v3.3.2
命令執行成功后,在當前目錄會生成檔案名為 kubesphere-v3.3.2.yaml 的組態檔,
kube@ks-master-0:~/kubekey$ ./kk create config -f kubesphere-v3.3.2.yaml --with-kubernetes v1.26.0 --with-kubesphere v3.3.2
Generate KubeKey config file successfully
kube@ks-master-0:~/kubekey$ ls
kk kubekey-v3.0.7-linux-amd64.tar.gz kubesphere-v3.3.2.yaml
本文示例采用 3 個節點作為 control-plane 節點同時復用為 worker 節點,
編輯組態檔 kubesphere-v3.3.2.yaml,修改 hosts 和 roleGroups 等資訊,修改說明如下,
- hosts 指定節點的 IP、ssh 用戶、ssh 密碼、ss h密鑰,示例演示了同時使用密碼和密鑰的配置方法
- roleGroups 指定 ks-master-0、ks-master-1、ks-master-2 作為 etcd、control-plane、worker節點
- internalLoadbalancer 啟用內置的 HAProxy 負載均衡器
修改后的示例如下:
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
name: sample
spec:
hosts:
- {name: ks-master-0, address: 192.168.9.91, internalAddress: 192.168.9.91, user: kube, password: "Qcloud@123"}
- {name: ks-master-1, address: 192.168.9.92, internalAddress: 192.168.9.92, user: kube, privateKeyPath: "~/.ssh/id_ed25519"}
- {name: ks-master-2, address: 192.168.9.93, internalAddress: 192.168.9.93, user: kube, privateKeyPath: "~/.ssh/id_ed25519"}
roleGroups:
etcd:
- ks-master-0
- ks-master-1
- ks-master-2
control-plane:
- ks-master-0
- ks-master-1
- ks-master-2
worker:
- ks-master-0
- ks-master-1
- ks-master-2
controlPlaneEndpoint:
## Internal loadbalancer for apiservers
internalLoadbalancer: haproxy
domain: lb.kubesphere.local
address: ""
port: 6443
kubernetes:
version: v1.26.0
clusterName: cluster.local
autoRenewCerts: true
containerManager: containerd
etcd:
type: kubekey
network:
plugin: calico
kubePodsCIDR: 10.233.64.0/18
kubeServiceCIDR: 10.233.0.0/18
## multus support. https://github.com/k8snetworkplumbingwg/multus-cni
multusCNI:
enabled: false
registry:
privateRegistry: ""
namespaceOverride: ""
registryMirrors: []
insecureRegistries: []
addons: []
部署 KubeSphere 和 Kubernetes
接下來我們執行下面的命令,使用上面生成的組態檔部署 KubeSphere 和 Kubernetes,
./kk create cluster -f kubesphere-v3.3.2.yaml
上面的命令執行后,首先 kk 會檢查部署 Kubernetes 的依賴及其他詳細要求,檢查合格后,系統將提示您確認安裝,輸入 yes 并按 ENTER 繼續部署,
kube@ks-master-0:~/kubekey$ ./kk create cluster -f kubesphere-v3.3.2.yaml
_ __ _ _ __
| | / / | | | | / /
| |/ / _ _| |__ ___| |/ / ___ _ _
| \| | | | '_ \ / _ \ \ / _ \ | | |
| |\ \ |_| | |_) | __/ |\ \ __/ |_| |
\_| \_/\__,_|_.__/ \___\_| \_/\___|\__, |
__/ |
|___/
14:01:18 CST [GreetingsModule] Greetings
14:01:19 CST message: [ks-master-2]
Greetings, KubeKey!
14:01:19 CST message: [ks-master-0]
Greetings, KubeKey!
14:01:20 CST message: [ks-master-1]
Greetings, KubeKey!
14:01:20 CST success: [ks-master-2]
14:01:20 CST success: [ks-master-0]
14:01:20 CST success: [ks-master-1]
14:01:20 CST [NodePreCheckModule] A pre-check on nodes
14:01:20 CST success: [ks-master-0]
14:01:20 CST success: [ks-master-1]
14:01:20 CST success: [ks-master-2]
14:01:20 CST [ConfirmModule] Display confirmation form
+-------------+------+------+---------+----------+-------+-------+---------+-----------+--------+--------+------------+------------+-------------+------------------+--------------+
| name | sudo | curl | openssl | ebtables | socat | ipset | ipvsadm | conntrack | chrony | docker | containerd | nfs client | ceph client | glusterfs client | time |
+-------------+------+------+---------+----------+-------+-------+---------+-----------+--------+--------+------------+------------+-------------+------------------+--------------+
| ks-master-0 | y | y | y | y | y | y | y | y | y | | y | | | | CST 14:01:20 |
| ks-master-1 | y | y | y | y | y | y | y | y | y | | y | | | | CST 14:01:20 |
| ks-master-2 | y | y | y | y | y | y | y | y | y | | y | | | | CST 14:01:20 |
+-------------+------+------+---------+----------+-------+-------+---------+-----------+--------+--------+------------+------------+-------------+------------------+--------------+
This is a simple check of your environment.
Before installation, ensure that your machines meet all requirements specified at
https://github.com/kubesphere/kubekey#requirements-and-recommendations
Continue this installation? [yes/no]:
注意:上文命令輸出中有一點很詭異,containerd 的檢查結果居然是 y,我在系統中找了一百遍也沒發現跟 containerd 有關的任何檔案,暫時擱置后面看看有沒有影響再說,
部署完成需要大約 10-30 分鐘左右,具體看網速和機器配置,
部署完成后,您應該會在終端上看到類似于下面的輸出,提示部署完成的同時,輸出中還會顯示用戶登陸 KubeSphere 的默認管理員用戶和密碼,
clusterconfiguration.installer.kubesphere.io/ks-installer created
14:14:52 CST skipped: [ks-master-2]
14:14:52 CST skipped: [ks-master-1]
14:14:52 CST success: [ks-master-0]
#####################################################
### Welcome to KubeSphere! ###
#####################################################
Console: http://192.168.9.91:30880
Account: admin
Password: P@88w0rd
NOTES:
1. After you log into the console, please check the
monitoring status of service components in
"Cluster Management". If any service is not
ready, please wait patiently until all components
are up and running.
2. Please change the default password after login.
#####################################################
https://kubesphere.io 2023-06-16 14:27:39
#####################################################
14:27:42 CST skipped: [ks-master-2]
14:27:42 CST skipped: [ks-master-1]
14:27:42 CST success: [ks-master-0]
14:27:42 CST Pipeline[CreateClusterPipeline] execute successfully
Installation is complete.
Please check the result using the command:
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f
部署驗證
- 驗證 KubeSphere 管理控制臺
我們打開瀏覽器訪問 master-0 節點的 Ip 地址和埠 "30880",可以看到 KubeSphere 管理控制臺的登錄頁面,
輸入默認用戶 admin 和默認密碼 P@88w0rd,然后點擊「登錄」,

登錄后,系統會要求您更改 KubeSphere 默認用戶 admin 的默認密碼,輸入新的密碼并點擊「提交」,

提交完成后,系統會跳轉到 KubeSphere admin 用戶作業臺頁面,該頁面顯示了當前 KubeSphere 版本為 v3.3.2,可用的 Kubernetes 集群數量為 1,

接下來,單擊左上角的「平臺管理」選單,選擇「集群管理」,

進入集群管理界面,在該頁面可以查看集群的基本資訊,包括集群資源用量、Kubernetes 狀態、節點資源用量 Top、系統組件、工具箱等內容,

單擊左側「節點」選單,點擊「集群節點」可以查看 Kubernetes 集群可用節點的詳細資訊,

單擊左側「系統組件」選單,可以查看已安裝組件的詳細資訊,目前集群采用的最小化安裝,僅包含 KubeSphere、Kubernetes、監控三個類別的組件,

- 命令列驗證集群資訊
在 master-0 節點運行 kubectl 命令獲取 Kubernetes 集群上的可用節點串列,
kubectl get nodes -o wide
在輸出結果中可以看到,當前的 Kubernetes 集群有三個可用節點、節點的內部 IP、節點角色、節點的 Kubernetes 版本號、容器運行時及版本號、作業系統型別及內核版本等資訊,
kube@ks-master-0:~/kubekey$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
ks-master-0 Ready control-plane,worker 25m v1.26.0 192.168.9.91 <none> Debian GNU/Linux 12 (bookworm) 6.1.0-9-amd64 containerd://1.6.4
ks-master-1 Ready control-plane,worker 25m v1.26.0 192.168.9.92 <none> Debian GNU/Linux 12 (bookworm) 6.1.0-9-amd64 containerd://1.6.4
ks-master-2 Ready control-plane,worker 25m v1.26.0 192.168.9.93 <none> Debian GNU/Linux 12 (bookworm) 6.1.0-9-amd64 containerd://1.6.4
輸入以下命令獲取在 Kubernetes 集群上運行的 Pod 串列:
kubectl get pods --all-namespaces
在輸出結果中可以看到, Kubernetes 集群上有多個可用的命名空間 kube-system、kubesphere-control-system、kubesphere-monitoring-system 和 kubesphere-system,所有 pod 都在運行,
kube@ks-master-0:~/kubekey$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-7f576895dd-fh5sq 1/1 Running 0 25m
kube-system calico-node-7j6qd 1/1 Running 0 25m
kube-system calico-node-rkhpz 1/1 Running 0 25m
kube-system calico-node-wpfqj 1/1 Running 0 25m
kube-system coredns-d9d84b5bf-jbzxw 1/1 Running 0 25m
kube-system coredns-d9d84b5bf-s4d9m 1/1 Running 0 25m
kube-system kube-apiserver-ks-master-0 1/1 Running 0 26m
kube-system kube-apiserver-ks-master-1 1/1 Running 0 25m
kube-system kube-apiserver-ks-master-2 1/1 Running 0 25m
kube-system kube-controller-manager-ks-master-0 1/1 Running 0 26m
kube-system kube-controller-manager-ks-master-1 1/1 Running 0 25m
kube-system kube-controller-manager-ks-master-2 1/1 Running 0 25m
kube-system kube-proxy-mfqgj 1/1 Running 0 25m
kube-system kube-proxy-mkwsv 1/1 Running 0 25m
kube-system kube-proxy-tlff8 1/1 Running 0 25m
kube-system kube-scheduler-ks-master-0 1/1 Running 0 26m
kube-system kube-scheduler-ks-master-1 1/1 Running 0 25m
kube-system kube-scheduler-ks-master-2 1/1 Running 0 25m
kube-system nodelocaldns-42x9v 1/1 Running 0 25m
kube-system nodelocaldns-dskst 1/1 Running 0 25m
kube-system nodelocaldns-z4h6t 1/1 Running 0 25m
kube-system openebs-localpv-provisioner-5c7fdd7bd9-khzz2 1/1 Running 0 25m
kube-system snapshot-controller-0 1/1 Running 0 22m
kubesphere-controls-system default-http-backend-767cdb5fdc-ntspc 1/1 Running 0 19m
kubesphere-controls-system kubectl-admin-5656cd6dfc-h9wvt 1/1 Running 0 12m
kubesphere-monitoring-system kube-state-metrics-5b8c487d5-wd55c 3/3 Running 0 16m
kubesphere-monitoring-system node-exporter-47b28 2/2 Running 0 16m
kubesphere-monitoring-system node-exporter-7rhlk 2/2 Running 0 16m
kubesphere-monitoring-system node-exporter-8lvc9 2/2 Running 0 16m
kubesphere-monitoring-system prometheus-k8s-0 2/2 Running 0 15m
kubesphere-monitoring-system prometheus-k8s-1 2/2 Running 0 15m
kubesphere-monitoring-system prometheus-operator-6fb9967754-64lnm 2/2 Running 0 16m
kubesphere-system ks-apiserver-579fb669c7-xwbkt 1/1 Running 0 19m
kubesphere-system ks-console-b5855d9f5-ws488 1/1 Running 0 19m
kubesphere-system ks-controller-manager-b677bdb48-7jvph 1/1 Running 0 19m
kubesphere-system ks-installer-5b5cc7f6c5-dnphc 1/1 Running 0 25m
至此,我們已經部署了具有三個 Debian 服務器節點的最小化的 Kubernetes 集群和 KubeSphere,我們還通過 KubeSphere 管理控制臺和命令列界面查看了集群的狀態,
接下來我們將在 Kubernetes 集群上部署一個簡單的 Nginx Web 服務器,測驗驗證Kubernetes 和 KubeSphere 正常可用,
部署測驗資源
本示例使用命令列工具在 Kubernetes 集群上部署一個 Nginx Web服務器并利用 KubeSphere 圖形化管理控制臺查看部署的資源資訊,
創建 Nginx Deployment
運行以下命令創建一個部署 Nginx Web 服務器的 Deployment,此示例中,我們將創建具有兩個副本基于 nginx:alpine 鏡像的 Pod,
kubectl create deployment nginx --image=nginx:alpine --replicas=2
創建 Nginx Service
創建一個新的 Kubernetes 服務,服務名稱 nginx,服務型別 Nodeport,對外的服務埠 80,
kubectl create service nodeport nginx --tcp=80:80
4.3 驗證 Nginx Deployment 和 Pod
運行以下命令查看創建的 Deployment 和 Pod 資源,
kubectl get deployment -o wide
kubectl get pods -o wide
查看結果如下:
kube@ks-master-0:~/kubekey$ kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 2/2 2 2 33m nginx nginx:alpine app=nginx
kube@ks-master-0:~$ kubectl get pods -o wide
root@ks-master-0:~# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6c557cc74d-89bpf 1/1 Running 0 33m 10.233.103.5 ks-master-1 <none> <none>
nginx-6c557cc74d-psgsc 1/1 Running 0 33m 10.233.102.7 ks-master-2 <none> <none>
驗證 Nginx Service
運行一下命令查看可用的服務串列,在串列中我們可以看到 nginx 服務型別 為 Nodeport,并在 Kubernetes 主機上開放了 31719 埠,
kubectl get svc -o wide
查看結果如下:
kube@ks-master-0:~$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 61m <none>
nginx NodePort 10.233.55.227 <none> 80:31719/TCP 33m app=nginx
訪問 Nginx 服務
運行以下命令訪問部署的 Nginx 服務,驗證服務是否成功部署,
- 驗證直接訪問 Pod
curl 10.233.103.5
# 訪問結果如下
kube@ks-master-0:~$ curl 10.233.103.5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- 驗證訪問 Service
curl 10.233.55.227
# 訪問結果同上,略
- 驗證訪問 Nodeport
curl 192.168.9.91:31719
# 訪問結果同上,略
在管理控制臺查看
接下來我們回到 KubeSphere 管理控制臺,在管理控制臺查看已經創建的資源,
說明: KubeSphere 的管理控制臺具有友好地、圖形化創建 Kubernetes 各種資源的功能,主要是截圖太麻煩了,所以本文采用了命令列的方式簡單的創建了測驗資源,
只是在查看的時候給大家演示一下 KubeSphere 管理控制臺的基本功能,實際使用中,大家可以使用圖形化方式創建和管理 Kubernetes 資源,
- 登錄 KubeSphere 管理控制臺,點擊「平臺管理」,選擇「集群管理」,
- 單擊集群管理頁面左側的「應用負載」,點擊「作業負載」,默認會看到所有型別為部署的作業負載,
我們使用的是 admin 賬戶,因此可以看到所有的作業負載,在搜索框輸入 Nginx,只顯示 Nginx 部署作業負載,


- 單擊部署串列中的 Nginx,可以查看更詳細的資訊,并且管理 Nginx 部署(Deployment),

- 回到「平臺管理」-「集群管理」頁面,單擊集群管理頁面左側的「應用負載」,點擊「服務」,默認會看到所有型別為服務的作業負載,
我們使用的是 admin 賬戶,因此可以看到所有的作業負載,在搜索框輸入 nginx,只顯示 nginx 服務作業負載,


- 單擊服務串列中的 nginx,可以查看更詳細的資訊,并且管理 nginx 服務(Service),

至此,我們實作了將 Nginx Web 服務器部署到 Kubernetes 集群,并通過 KubeSphere 管理控制臺查看、驗證了部署的 Deployment、Pod、Service 的詳細資訊,
結束語
本文主要介紹了在三臺 Debian 12 服務器上利用 KubeKey 自動化部署 KubeSphere 和 Kubernetes 高可用集群的詳細程序,
部署完成后,我們還利用 KubeSphere 管理控制臺和 kubectl 命令列,查看并驗證了 KubeSphere 和 Kubernetes 集群的狀態,
最終我們通過在 Kubenetes 集群上部署 Nginx Web服務器驗證了 Kubernetes 集群和 KubeSphere 的可用性,并通過在 KubeSphere 管理控制臺查看 Nginx Pod 和 服務狀態的操作,了解了 KubeSphere 的基本用法,
本文由博客一文多發平臺 OpenWrite 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/555573.html
標籤:其他
下一篇:返回列表
