在 Ubuntu 22.04 上安裝 KubeSphere 實戰教程

KubeSphere發表於2023-05-19
作者:老 Z,中電信數智科技有限公司山東分公司運維架構師,雲原生愛好者,目前專注於雲原生運維,雲原生領域技術棧涉及 Kubernetes、KubeSphere、DevOps、OpenStack、Ansible 等。

前言

導圖

知識點

  • 定級:入門級
  • KubeKey 安裝部署 KubeSphere 和 Kubernetes
  • Ubuntu 作業系統的基本配置
  • Kubernetes 常用工作負載的建立

演示伺服器配置

主機名IPCPU記憶體系統盤資料盤用途
ks-master-0192.168.9.9141640200KubeSphere/k8s-master/k8s-worker
ks-master-1192.168.9.9241640200KubeSphere/k8s-master/k8s-worker
ks-master-2192.168.9.9341640200KubeSphere/k8s-master/k8s-worker
合計31248120600

演示環境涉及軟體版本資訊

  • 作業系統:Ubuntu 22.04.2 LTS
  • KubeSphere:3.3.2
  • Kubernetes:v1.25.5
  • KubeKey: v3.0.7

簡介

本文介紹瞭如何在 Ubuntu 22.04 LTS 伺服器上部署 KubeSphere 和 Kubernetes 叢集。我們將使用 KubeSphere 開發的 KubeKey 工具實現自動化部署,在三臺伺服器上實現高可用模式最小化部署 Kubernetes 叢集和 KubeSphere。我們將提供詳細的部署說明,以便讀者輕鬆地完成部署過程。

作業系統基礎配置

請注意,以下操作無特殊說明時需在所有 Ubuntu 伺服器上執行。本文只選取其中一臺伺服器作為演示,並假定其餘伺服器都已按照相同的方式進行配置和設定。

建立使用者

在安裝 KubeSphere 和 Kubernetes 之前,需要建立一個普通使用者,該使用者可以是安裝 Ubuntu 系統時預設建立的初始使用者 ubuntu,也可以是新建立的使用者。在本文中,我們將使用新建使用者 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

命令執行效果如下:

ubuntu@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:tQzVX/3bF87KzKjYnZcm6M3wdMRRlKyixFtCTQOcBks kube@ks-master-0
The key's randomart image is:
+--[ED25519 256]--+
|       Eo.*+  ooo|
|      . .* .o .oo|
|       .= .  o...|
|         B +..o..|
|        S B .oo +|
|         o  .  +o|
|         ...=.o .|
|        o.BooO   |
|       ..+.*+    |
+----[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:XMc3FWjoTJY6TFH7BK3S6bRmLt+ZxoPvdUh9Dz3ENgk.
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 ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

# 增加國內的 ntp 伺服器,或是指定其他常用的時間伺服器
server ntp.api.bz iburst

重啟 chrony 服務:

sudo systemctl restart chrony

驗證 chrony 同步狀態:

# 執行檢視命令
sudo chronyc sourcestats -v

# 正常的輸出結果如下
kube@ks-master-0:~/kubekey$ 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
==============================================================================
185.53.177.52               0   0     0     +0.000   2000.000     +0ns  4000ms

安裝部署 KubeSphere 和 Kubernetes

下載 KubeKey

本文將 master-0 節點作為部署節點,把 KubeKey (下文簡稱 kk) 最新版(3.0.7)二進位制檔案下載到該伺服器。具體 KubeKey 版本號可以在 KubeKey 發行頁面檢視。

  • 下載最新版的 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$ ll
total 111996
drwxrwxr-x 2 kube kube     4096 May 17 07:19 ./
drwxr-x--- 5 kube kube     4096 May 17 07:17 ../
-rwxr-xr-x 1 kube kube 78901793 Jan 18 01:59 kk*
-rw-rw-r-- 1 kube kube 35769576 May 17 07:19 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.25.5,同時,指定配置檔名稱為 kubesphere-v3.3.2.yaml,如果不指定,預設的檔名為 config-sample.yaml

./kk create config -f kubesphere-v3.3.2.yaml --with-kubernetes v1.25.5 --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.25.5 --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: "P@88w0rd"}
  - {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.25.5
    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


 _   __      _          _   __
| | / /     | |        | | / /
| |/ / _   _| |__   ___| |/ /  ___ _   _
|    \| | | | '_ \ / _ \    \ / _ \ | | |
| |\  \ |_| | |_) |  __/ |\  \  __/ |_| |
\_| \_/\__,_|_.__/ \___\_| \_/\___|\__, |
                                    __/ |
                                   |___/

16:24:04 CST [GreetingsModule] Greetings
16:24:05 CST message: [ks-master-2]
Greetings, KubeKey!
16:24:06 CST message: [ks-master-0]
Greetings, KubeKey!
16:24:08 CST message: [ks-master-1]
Greetings, KubeKey!
16:24:08 CST success: [ks-master-2]
16:24:08 CST success: [ks-master-0]
16:24:08 CST success: [ks-master-1]
16:24:08 CST [NodePreCheckModule] A pre-check on nodes
16:24:08 CST success: [ks-master-0]
16:24:08 CST success: [ks-master-2]
16:24:08 CST success: [ks-master-1]
16:24:08 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      |        |            |            |             |                  | CST 16:24:08 |
| ks-master-1 | y    | y    | y       | y        | y     | y     | y       | y         | y      |        |            |            |             |                  | CST 16:24:08 |
| ks-master-2 | y    | y    | y       | y        | y     | y     | y       | y         | y      |        |            |            |             |                  | CST 16:24:08 |
+-------------+------+------+---------+----------+-------+-------+---------+-----------+--------+--------+------------+------------+-------------+------------------+--------------+

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]:

部署完成需要大約 10-30 分鐘左右,具體看網速和機器配置。

部署完成後,您應該會在終端上看到類似於下面的輸出。提示部署完成的同時,輸出中還會顯示使用者登陸 KubeSphere 的預設管理員使用者和密碼。

clusterconfiguration.installer.kubesphere.io/ks-installer created
16:36:44 CST skipped: [ks-master-2]
16:36:44 CST skipped: [ks-master-1]
16:36:44 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-05-17 16:48:38
#####################################################
16:48:43 CST skipped: [ks-master-2]
16:48:43 CST skipped: [ks-master-1]
16:48:43 CST success: [ks-master-0]
16:48:43 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

部署驗證

  1. 驗證 KubeSphere 管理控制檯

我們開啟瀏覽器訪問 master-0 節點的 Ip 地址和埠 "30880",可以看到 KubeSphere 管理控制檯的登入頁面。

輸入預設使用者 admin 和預設密碼 P@88w0rd,然後點選「登入」。

登入後,系統會要求您更改 KubeSphere 預設使用者 admin 的預設密碼,輸入新的密碼並點選「提交」。

提交完成後,系統會跳轉到 KubeSphere admin 使用者工作臺頁面,該頁面顯示了當前 KubeSphere 版本為 v3.3.2,可用的 Kubernetes 叢集數量為 1。

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

進入叢集管理介面,在該頁面可以檢視叢集的基本資訊,包括叢集資源用量、Kubernetes 狀態、節點資源用量 Top、系統元件、工具箱等內容。

單擊左側「節點」選單,點選「叢集節點」可以檢視 Kubernetes 叢集可用節點的詳細資訊。

單擊左側「系統元件」選單,可以檢視已安裝元件的詳細資訊。目前叢集採用的最小化安裝,僅包含 KubeSphere、Kubernetes、監控三個類別的元件。

  1. 命令列驗證叢集資訊

在 master-0 節點執行 kubectl 命令獲取 Kubernetes 叢集上的可用節點列表

kubectl get nodes

在輸出結果中可以看到,當前的 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   36m   v1.25.5   192.168.9.91   <none>        Ubuntu 22.04.2 LTS   5.15.0-70-generic   containerd://1.6.4
ks-master-1   Ready    control-plane,worker   35m   v1.25.5   192.168.9.92   <none>        Ubuntu 22.04.2 LTS   5.15.0-70-generic   containerd://1.6.4
ks-master-2   Ready    control-plane,worker   35m   v1.25.5   192.168.9.93   <none>        Ubuntu 22.04.2 LTS   5.15.0-70-generic   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-75c594996d-mfl2c       1/1     Running   0          37m
kube-system                    calico-node-2xhqh                              1/1     Running   0          37m
kube-system                    calico-node-6vcl9                              1/1     Running   0          37m
kube-system                    calico-node-l9p9s                              1/1     Running   0          37m
kube-system                    coredns-67ddbf998c-t2z87                       1/1     Running   0          38m
kube-system                    coredns-67ddbf998c-xff4b                       1/1     Running   0          38m
kube-system                    kube-apiserver-ks-master-0                     1/1     Running   0          38m
kube-system                    kube-apiserver-ks-master-1                     1/1     Running   0          37m
kube-system                    kube-apiserver-ks-master-2                     1/1     Running   0          37m
kube-system                    kube-controller-manager-ks-master-0            1/1     Running   0          38m
kube-system                    kube-controller-manager-ks-master-1            1/1     Running   0          37m
kube-system                    kube-controller-manager-ks-master-2            1/1     Running   0          37m
kube-system                    kube-proxy-czmpt                               1/1     Running   0          37m
kube-system                    kube-proxy-mddtt                               1/1     Running   0          37m
kube-system                    kube-proxy-vjdzl                               1/1     Running   0          37m
kube-system                    kube-scheduler-ks-master-0                     1/1     Running   0          38m
kube-system                    kube-scheduler-ks-master-1                     1/1     Running   0          37m
kube-system                    kube-scheduler-ks-master-2                     1/1     Running   0          37m
kube-system                    nodelocaldns-5zpnj                             1/1     Running   0          37m
kube-system                    nodelocaldns-8849d                             1/1     Running   0          38m
kube-system                    nodelocaldns-v97cd                             1/1     Running   0          37m
kube-system                    openebs-localpv-provisioner-57d95c6875-lwvqt   1/1     Running   0          37m
kube-system                    snapshot-controller-0                          1/1     Running   0          35m
kubesphere-controls-system     default-http-backend-5d5c44d77f-kst9l          1/1     Running   0          31m
kubesphere-controls-system     kubectl-admin-788ff75cd7-4wqtc                 1/1     Running   0          25m
kubesphere-monitoring-system   kube-state-metrics-6cc9478499-9fr7h            3/3     Running   0          28m
kubesphere-monitoring-system   node-exporter-dzqw5                            2/2     Running   0          28m
kubesphere-monitoring-system   node-exporter-f6wvj                            2/2     Running   0          28m
kubesphere-monitoring-system   node-exporter-kqtj8                            2/2     Running   0          28m
kubesphere-monitoring-system   prometheus-k8s-0                               2/2     Running   0          28m
kubesphere-monitoring-system   prometheus-k8s-1                               2/2     Running   0          28m
kubesphere-monitoring-system   prometheus-operator-6bd484db88-l6666           2/2     Running   0          28m
kubesphere-system              ks-apiserver-6797fc669-dr9gs                   1/1     Running   0          31m
kubesphere-system              ks-console-6f9d57b699-whg9w                    1/1     Running   0          31m
kubesphere-system              ks-controller-manager-6656d4f57d-gfc5x         1/1     Running   0          31m
kubesphere-system              ks-installer-55b8b4c4d9-9gvjh                  1/1     Running   0          37m

至此,我們已經部署了具有三個 Ubuntu 伺服器節點的最小化的 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

驗證 Nginx Deployment 和 Pod

執行以下命令檢視建立的 Deployment 和 Pod 資源。

kubectl get deployment -o wide
kubectl get pods -o wide

檢視結果如下:

kube@ks-master-0:~$ kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx   2/2     2            2           72s   nginx        nginx:alpine   app=nginx

kube@ks-master-0:~$ kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP             NODE          NOMINATED NODE   READINESS GATES
nginx-55f494c486-85zsc   1/1     Running   0          78s   10.233.103.5   ks-master-1   <none>           <none>
nginx-55f494c486-hpzlr   1/1     Running   0          78s   10.233.102.7   ks-master-2   <none>           <none>

驗證 Nginx Service

執行一下命令檢視可用的服務列表,在列表中我們可以看到 Nginx 服務型別 為 Nodeport,並在 Kubernetes 主機上開放了 32710 埠。

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        16h     <none>
nginx        NodePort    10.233.10.40   <none>        80:32710/TCP   6m21s   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.10.40

# 訪問結果同上,略
  • 驗證訪問 Nodeport
curl 192.168.9.91:32710

# 訪問結果同上,略

在管理控制檯檢視

接下來我們回到 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 的詳細資訊。

常見問題

ssh 連線報錯

  • 報錯資訊
# ssh 伺服器時報錯如下
ssh: connect to host 192.168.9.91 port 22: Connection refused
  • 解決方案
# 問題原因 ubuntu 伺服器預設安裝後 openssh 服務啟動失敗, 需要執行如下命令後再啟動 sshd 服務

# 切換到 root 使用者
sudo -i

# 執行命令
ssh-keygen -A

# 啟動 sshd 服務
systemctl start sshd

結束語

本文主要介紹了在三臺 Ubuntu 22.04 伺服器上利用 KubeKey 自動化部署 KubeSphere 和 Kubernetes 高可用叢集的詳細過程。

部署完成後,我們還利用 KubeSphere 管理控制檯和 kubectl 命令列,檢視並驗證了 KubeSphere 和 Kubernetes 叢集的狀態。

最終我們透過在 Kubenetes 叢集上部署 Nginx Web 伺服器驗證了 Kubernetes 叢集和 KubeSphere 的可用性,並透過在 KubeSphere 管理控制檯檢視 Nginx Pod 和 服務狀態的操作,瞭解了 KubeSphere 的基本用法。

本文由部落格一文多發平臺 OpenWrite 釋出!

相關文章