在 Ubuntu 22.04 上安裝 KubeSphere 實戰教程

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

前言

導圖

知識點

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

演示服務器配置

主機名 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

演示環境涉及軟件版本信息

  • 操作系統: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 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章