Fastapi框架+KubeSphere3.1.0系列(1): kubesphere安装部署+节点添加实战

1,273 阅读10分钟

一、 说明

从前面几个小节陆陆续续学习了一些关于K8S的一些概念的东西,也尝试了一些小试验。但操作起来始终不解其中一些奥义和精髓,于是乎打算从kubesphere反过来来入门学习我们的K8S,有了一些基本的k8s的基础概念的话,想来kubesphere也不应该是非常的难以理解。所以接下来的系列笔记,主要是记录我如果把Fastapi部署到我们的kubesphere来展开.

首先主要目的是我了学习而已,所以对应生成环境上的话,相关服务器的要求不一样,我的环境主要是基于本地搭建的虚拟机来练习为主。所以可能和生产环境搭建有所出路。

另外其实kubesphere的官网提供了非常友好的操作文档手册,其实大部分的实践都可以按官网提供的操作手机进行即可,我这里主要是为了加深自己的理解,参考官网的提供的手册,补充自己的操作笔记记录。仅供参考!

二、环境

2.1 虚拟机准备

首先就是虚拟机的内存和处理器等配置信息:

image.png

这里因为最小化安装后续的时候,相关安装监控什么,默认会安装,建议给与的内存大一些。避免安装后,内存不足,部分的组件安装调度的时候回出现失败的情况。

image.png

2.1 单节点安装kubesphere

PS:一下的操作步骤,多数源自官网提供的步骤!

官网操作来源地址: kubesphere.com.cn/docs/quick-…

2.1.1 linux机器准备和基本要求

image.png

因为默认我的是使用一个干净的虚拟机来实践。所以容器运行时的环境,我也没安装,使用官网提供安装脚本,它会默认自动帮你安装Docker.

2.1.2 在linux上安装Kubernetes和KubeSphere的基础准备

image.png

ps:基本准备很关键,因为安装的时候会对相关环境要求进行检测,比如防火墙和swap还 有Selinux状态等这些都有进行关闭处理一下。而且我再时间的过程中遇到了一个我问题,我发现:

即时我的使用:sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' >/etc/selinux/config 关闭了selinux,并且重启了安装的时候还是会提示出现

Failed to get etcdctl: Failed to exec command: sudo -E /bin/sh -c >"docker run --rm -v /usr/local/bin:/systembindir的错误问题,最后的处理方式:只是临时关闭就可以了!

步骤1:关闭防火墙或者开放必要的[防火墙端口]

[root@k81-master01 ~]#systemctl disable firewalld                                                                                                        
[root@k81-master01 ~]#systemctl stop firewalld                                                                                                           
[root@k81-master01 ~]#systemctl status firewalld  

步骤2: 关闭swap分区

[root@k81-master01 ~]#swapoff -a                                                                                                                         
[root@k81-master01 ~]#echo "vm.swappiness=0" >> /etc/sysctl.conf                                                                                         
[root@k81-master01 ~]#sysctl -p /etc/sysctl.conf   
vm.swappiness = 0

步骤3: 关闭Selinux状态

[root@k81-master01 ~]sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config    
[root@k81-master01 ~]reboot
[root@k81-master01 ~]### 查看Selinux状态                                                                                       
[root@k81-master01 ~]#getenforce                                                                                                                  
Disabled                          
[root@k81-master01 ~]# 再执行一次临时关闭的操作
[root@k81-master01 ~]# setenforce 0

步骤4:配置epel源--非必选

[root@k81-master01 ~]#rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm

步骤5: 更新软件包(建议执行)

[root@k81-master01 ~]#yum -y update

步骤6: 安装基础依赖组件

[root@k81-master01 ~]#yum install -y ebtables socat ipset conntrack 

2.1.3 使用kk正式进行单节点的安装

KubeKey 是kubesphere提供的一键安装的工具!非常nice! 下面的步骤基本就是按部就班的跟着官网的给的文档操作就有可以了!我这里就直接的抄了过来了!哈哈

步骤 1:下载 KubeKey

先执行以下命令以确保您从正确的区域下载 KubeKey。

[root@k81-master01 ~]# export KKZONE=cn
# 主要是避免从国外jnx下载,贼慢滴!

执行以下命令下载 KubeKey。

curl -sfL https://get-kk.kubesphere.io | VERSION=v1.1.1 sh -

备注

在您下载 KubeKey 后,如果您将其传至新的机器,且访问 Googleapis 同样受限,在您>执行以下步骤之前请务必再次执行 export KKZONE=cn 命令。

为 kk 添加可执行权限:

chmod +x kk

步骤 2:开始一键安装kubernetes和kubesphere

[root@k81-master01 ~]# ./kk create cluster --with-kubernetes v1.20.4 --with-kubesphere v3.1.1

[root@localhost ~]# ./kk create cluster --with-kubernetes v1.20.4 --with-kubesphere v3.1.1
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+
| name                                | sudo | curl | openssl | ebtables | socat | ipset | conntrack | docker  | nfs client | ceph client | glusterfs client | time         |
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+
| localhost.localdomain.cluster.local | y    | y    | y       | y        | y     | y     | y         | 20.10.8 |            |             |                  | CST 17:33:14 |
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+

This is a simple check of your environment.
Before installation, you should ensure that your machines meet all requirements specified at
https://github.com/kubesphere/kubekey#requirements-and-recommendations

Continue this installation? [yes/no]: yes

步骤 3:等待安装···

比较慢~去喝杯咖啡吧!

步骤 4:安装完成后

出现如下图示:


#####################################################
###              Welcome to KubeSphere!           ###
#####################################################

Console: http://192.168.219.143: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             2021-09-13 17:45:52
#####################################################
INFO[17:46:05 CST] Installation is complete.

Please check the result using the command:

       kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f


步骤 5:验证安装结果

[root@k81-master01 ~]# kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f

输出信息会显示 Web 控制台的 IP 地址和端口号,默认的 NodePort 是 30880。现在,您可以使用默认的帐户和密码 (admin/P@88w0rd) 通过 <NodeIP>:30880 访问控制台。

步骤 6:登入系统体验

image.png

image.png

image.png

2.1.4 使用KubeKey 来新增一个工作节点(组成集群)

ps:自 KubeSphere v3.0.0 起,您可以使用全新的安装程序 KubeKey 将新节点添加到集群。从根本上说,该操作是基于 Kubelet 的注册机制。换言之,新节点将自动加入现有的 Kubernetes 集群。KubeSphere 支持混合环境,这意味着新添加的主机操作系统可以是 CentOS 或者 Ubuntu。

关于节点管理官网文档地址: kubesphere.com.cn/docs/instal…

准备新的节点我的虚拟服务器:

image.png

image.png

2.1.4.1 和前面一样做相关的Kubernetes和KubeSphere的基础准备

  • 步骤1:关闭防火墙或者开放必要的[防火墙端口]
  • 步骤2: 关闭swap分区
  • 步骤3: 关闭Selinux状态 (reboot)
  • 步骤4:配置epel源--非必选
  • 步骤5: 更新软件包(建议执行)(可以在虚拟机安装好后优先执行,再处理关闭Selinux状态 )
  • 步骤6: 安装基础依赖组件

2.1.4.2 工作节点下载 KubeKey

下载KubeKey:方式一:

先执行以下命令以确保您从正确的区域下载 KubeKey。

[root@k81-master01 ~]# export KKZONE=cn
# 主要是避免从国外jnx下载,贼慢滴!

执行以下命令下载 KubeKey。

curl -sfL https://get-kk.kubesphere.io | VERSION=v1.1.1 sh -

下载KubeKey:方式二: 从主节点直接的拷贝之前已经下载好的,避免再次的下载相关的组件。

拷贝kk到新工作节点上:


[root@localhost ~]# scp kk root@192.168.219.144:/root/
The authenticity of host '192.168.219.144 (192.168.219.144)' can't be established.
ECDSA key fingerprint is SHA256:96v+n8/ZY/wbVqH+R03ZAC9aHwAkG7q15Z7ggmX8f/g.
ECDSA key fingerprint is MD5:de:51:eb:c0:ee:61:47:2b:b5:93:d2:a2:10:a5:ff:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.219.144' (ECDSA) to the list of known hosts.
root@192.168.219.144's password:
kk                                                                                                             100%   13MB  23.9MB/s   00:00
You have new mail in /var/spool/mail/root
[root@localhost ~]#

拷贝kubekey到新工作节点上:

# 压缩

[root@localhost ~]# tar -cvzf kubekey.tar.gz kubekey
kubekey/
kubekey/v1.20.4/
kubekey/v1.20.4/amd64/
kubekey/v1.20.4/amd64/kubeadm
kubekey/v1.20.4/amd64/kubelet
kubekey/v1.20.4/amd64/kubectl
kubekey/v1.20.4/amd64/helm
kubekey/v1.20.4/amd64/cni-plugins-linux-amd64-v0.8.6.tgz
kubekey/config-localhost.localdomain.cluster.local
kubekey/network-plugin.yaml
[root@localhost ~]# ll
total 125676
-rw-------. 1 root root      1247 Sep 13 16:08 anaconda-ks.cfg
-rwxr-xr-x  1 1001  121  13668116 Jul 12 15:02 kk
drwxr-xr-x  3 root root        98 Sep 13 16:57 kubekey
-rw-r--r--  1 root root 101624288 Sep 14 11:48 kubekey.tar.gz
-rw-r--r--  1 root root  13341858 Sep 13 16:26 kubekey-v1.1.1-linux-amd64.tar.gz
-rw-r--r--  1 1001  121     22906 Jul 12 15:00 README.md
-rw-r--r--  1 1001  121     22845 Jul 12 15:00 README_zh-CN.md

拷贝

[root@localhost ~]# scp kubekey.tar.gz root@192.168.219.144:/root/
root@192.168.219.144's password:
kubekey.tar.gz                                                                                                                                                             100%   97MB  42.9MB/s   00:02
[root@localhost ~]#

查看新工作节点的文件信息:


[root@localhost ~]# ll
total 112596
-rw-------. 1 root root      1247 Sep 14 11:15 anaconda-ks.cfg
-rwxr-xr-x  1 root root  13668116 Sep 14 11:49 kk
-rw-r--r--  1 root root 101624288 Sep 14 11:54 kubekey.tar.gz
[root@localhost ~]#


解压:

[root@localhost ~]# tar -xzvf kubekey.tar.gz kubekey
kubekey/
kubekey/v1.20.4/
kubekey/v1.20.4/amd64/
kubekey/v1.20.4/amd64/kubeadm
kubekey/v1.20.4/amd64/kubelet
kubekey/v1.20.4/amd64/kubectl
kubekey/v1.20.4/amd64/helm
kubekey/v1.20.4/amd64/cni-plugins-linux-amd64-v0.8.6.tgz
kubekey/config-localhost.localdomain.cluster.local
kubekey/network-plugin.yaml
[root@localhost ~]# ll
total 112596
-rw-------. 1 root root      1247 Sep 14 11:15 anaconda-ks.cfg
-rwxr-xr-x  1 root root  13668116 Sep 14 11:49 kk
drwxr-xr-x  3 root root        98 Sep 13 16:57 kubekey
-rw-r--r--  1 root root 101624288 Sep 14 11:54 kubekey.tar.gz
[root@localhost ~]# cd kubekey
[root@localhost kubekey]# ll
total 192
-rw-r--r-- 1 root root   5583 Sep 13 17:36 config-localhost.localdomain.cluster.local
-rw-r--r-- 1 root root 187179 Sep 13 17:36 network-plugin.yaml
drwxr-xr-x 3 root root     19 Sep 13 16:26 v1.20.4
[root@localhost kubekey]#

2.1.5 添加工作节点步骤

步骤1. 在Master节点上使用 KubeKey 检索集群信息。

PS:上面的命令注意是在Master节点上进行操作

  1. 使用 KubeKey 检索集群信息。以下命令会创建配置文件 (sample.yaml)。

    [root@localhost ~]#  ./kk create config --from-cluster
    Notice: /root/sample.yaml has been created. Some parameters need to be filled in by yourself, please complete it.
    

    备注

    如果您的机器上已有配置文件,就可以跳过此步骤。例如,若要将节点添加到由 KubeKey 设置的多节点集群,如果您没有删除该集群,则可能仍拥有该配置文件。

  2. 在配置文件中,将新节点的信息放在 hosts 和 roleGroups 之下。该示例添加了两个新节点(即 node1 )。这里的 master1 是现有节点。

  • 192.168.219.143 是master
  • 192.168.219.144 是node1

image.png

修改一下Node节点主机名:

[root@localhost ~]# hostnamectl ``set``-hostname node1

编辑文件sample.yaml

apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
  name: sample
spec:
  hosts: 
  # You should complete the ssh information of the hosts
  - {name: localhost.localdomain.cluster.local, address: 192.168.219.143, internalAddress: 192.168.219.143}
  - {name: node1, address: 192.168.219.144, internalAddress: 192.168.219.144, user: root, password: 123456}
  roleGroups:
    etcd:
    - localhost.localdomain.cluster.local
    master:
    - localhost.localdomain.cluster.local
    worker:
    - localhost.localdomain.cluster.local
    - node1
  controlPlaneEndpoint:
    # If loadbalancer was used, 'address' should be set to loadbalancer's ip.
    domain: lb.kubesphere.local
    address: ""
    port: 6443
  kubernetes:
    version: v1.20.4
    clusterName: cluster.local
    proxyMode: ipvs
    masqueradeAll: false
    maxPods: 110
    nodeCidrMaskSize: 24
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
  registry:
    privateRegistry: ""

文件注意修改点:

image.png

  • 主要master主机名已经定下了的话,不要再修改咯,所以最好一开的时候修改下
  • 主要edct配置也需要指定,使用的主节点上的主机(当然后续你可以单独的分类etcd的主机)
步骤2. 执行以下命令:
[root@localhost ~]# ./kk add nodes -f sample.yaml
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+
| name                                | sudo | curl | openssl | ebtables | socat | ipset | conntrack | docker  | nfs client | ceph client | glusterfs client | time         |
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+
| node1                               | y    | y    | y       | y        |       | y     |           |         |            |             |                  | CST 12:36:43 |
| localhost.localdomain.cluster.local | y    | y    | y       | y        | y     | y     | y         | 20.10.8 |            |             |                  | CST 12:31:12 |
+-------------------------------------+------+------+---------+----------+-------+-------+-----------+---------+------------+-------------+------------------+--------------+
node1: conntrack is required.

然后再执行一下:

[root@localhost ~]# ./kk add nodes -f sample.yaml

结果如下图示:直接的yes即可:

image.png

image.png

image.png

步骤3. 等待工作节点的安装处理:
这里建议工作节点先直接的安装好Docker,如果让kk安装的,会有点慢!需要耐心等!

PS:如果很久还是不行,终止一次再执行一下吧!!!

INFO[13:58:51 CST] Joining nodes to cluster
[node1 192.168.219.144] MSG:
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.8. Latest validated version: 19.03
[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'
W0914 14:04:24.425611   18613 utils.go:69] The recommended value for "clusterDNS" in "KubeletConfiguration" is: [10.233.0.10]; the provided value is: [169.254.25.10]
[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.
[node1 192.168.219.144] MSG:
node/node1 labeled
INFO[13:59:09 CST] Congratulations! Scaling cluster is successful.
[root@localhost ~]#

提示上面的问题之后, 查看节点添加成功了!!

image.png

步骤4. 查看节点信息:

image.png


以上仅仅是个人结合自己的实际需求,做学习的实践笔记!如有笔误!欢迎批评指正!感谢各位大佬!

结尾

END

简书:www.jianshu.com/u/d6960089b…

掘金:juejin.cn/user/296393…

公众号:微信搜【小儿来一壶枸杞酒泡茶】

小钟同学 | 文 【欢迎一起学习交流】| QQ:308711822