在裸机上安装 KubeSphere

介绍

KubeSphere 除了可以在云上安装,还可以在裸机上安装。由于在裸机上没有虚拟层,基础设施的开销大大降低,从而可以给部署的应用提供更多的计算和存储资源,硬件效率得到提高。以下示例介绍如何在裸机上安装 KubeSphere。

准备工作

  • 您需要了解如何在多节点集群中安装 KubeSphere。有关详情,请参见多节点安装
  • 您的环境中需要有足够的服务器和网络冗余。
  • 如果搭建生产环境,建议您提前准备持久化存储并创建 StorageClass。如果搭建开发测试环境,您可以直接使用集成的 OpenEBS 配置 LocalPV 存储服务。

准备 Linux 主机

本教程使用 3 台物理机,硬件配置为 DELL 620 Intel (R) Xeon (R) CPU E5-2640 v2 @ 2.00GHz (32G memory)。在这 3 台物理机上将安装 CentOS Linux release 7.6.1810 (Core) 操作系统,用于 KubeSphere 最小化安装。

安装 CentOS

请提前下载并安装 CentOS 镜像。请确保根目录已至少分配 200 GB 空间用于存储 Docker 镜像(如果 KubeSphere 仅用于测试,您可以跳过这一步)。

有关系统要求的更多信息,请参见系统要求

三台主机的角色分配如下,供参考。

主机 IP 地址 主机名 角色
192.168.60.152 master1 master1, etcd
192.168.60.153 worker1 worker
192.168.60.154 worker2 worker

设置网卡

  1. 清空网卡配置。

    ifdown em1
    
    ifdown em2
    
    rm -rf /etc/sysconfig/network-scripts/ifcfg-em1
    
    rm -rf /etc/sysconfig/network-scripts/ifcfg-em2
    
  2. 创建 bond 网卡。

    nmcli con add type bond con-name bond0 ifname bond0 mode 802.3ad ip4 192.168.60.152/24 gw4 192.168.60.254
    
  3. 设置 bond 模式。

    nmcli con mod id bond0 bond.options mode=802.3ad,miimon=100,lacp_rate=fast,xmit_hash_policy=layer2+3
    
  4. 将物理网卡绑定至 bond。

    nmcli con add type bond-slave ifname em1 con-name em1 master bond0
    
    nmcli con add type bond-slave ifname em2 con-name em2 master bond0
    
  5. 修改网卡模式。

    vi /etc/sysconfig/network-scripts/ifcfg-bond0
    BOOTPROTO=static
    
  6. 重启 Network Manager。

    systemctl restart NetworkManager
    
    nmcli con # Display NIC information
    
  7. 修改主机名和 DNS。

    hostnamectl set-hostname worker-1
    
    vim /etc/resolv.conf
    

设置时间

  1. 开启时间同步。

    yum install -y chrony
    
    systemctl enable chronyd
    
    systemctl start chronyd
    
    timedatectl set-ntp true
    
  2. 设置时区。

    timedatectl set-timezone Asia/Shanghai
    
  3. 检查 ntp-server 是否可用。

    chronyc activity -v
    

设置防火墙

执行以下命令停止并禁用 firewalld 服务:

iptables -F
systemctl status firewalld
systemctl stop firewalld
systemctl disable firewalld

更新系统包和依赖项

执行以下命令更新系统包并安装依赖项:

yum update
yum install openssl openssl-devel
yum install socat
yum install epel-release
yum install conntrack-tools

备注

取决于将要安装的 Kubernetes 版本,您可能不需要安装所有依赖项。有关更多信息,请参见依赖项要求

下载 KubeKey

KubeKey 是新一代 Kubernetes 和 KubeSphere 安装器,可帮助您以简单、快速、灵活的方式安装 Kubernetes 和 KubeSphere。

请按照以下步骤下载 KubeKey。

GitHub Release Page 下载 KubeKey 或使用以下命令:

curl -sfL https://get-kk.kubesphere.io | VERSION=v2.0.0 sh -

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

export KKZONE=cn

执行以下命令下载 KubeKey:

curl -sfL https://get-kk.kubesphere.io | VERSION=v2.0.0 sh -

备注

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

备注

执行以上命令会下载最新版 KubeKey (v2.0.0),您可以修改命令中的版本号下载指定版本。

kk 文件添加可执行权限。

chmod +x kk

创建多节点集群

您可用使用 KubeKey 同时安装 Kubernetes 和 KubeSphere,通过自定义配置文件中的参数创建多节点集群。

创建安装有 KubeSphere 的 Kubernetes 集群(例如使用 --with-kubesphere v3.2.1):

./kk create config --with-kubernetes v1.21.5 --with-kubesphere v3.2.1

备注

  • 安装 KubeSphere 3.2.1 的建议 Kubernetes 版本:v1.19.x、v1.20.x、v1.21.x 或 v1.22.x(实验性支持)。如果不指定 Kubernetes 版本,KubeKey 将默认安装 Kubernetes v1.21.5。有关受支持的 Kubernetes 版本的更多信息,请参见支持矩阵

  • 如果您在这一步的命令中不添加标志 --with-kubesphere,则不会部署 KubeSphere,只能使用配置文件中的 addons 字段安装 KubeSphere,或者在您后续使用 ./kk create cluster 命令时再次添加该标志。

  • 如果您添加标志 --with-kubesphere 时不指定 KubeSphere 版本,则会安装最新版本的 KubeSphere。

系统将创建默认的 config-sample.yaml 文件。您可以根据您的环境修改此文件。

vi config-sample.yaml
apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
  name: config-sample
spec:
  hosts:
  - {name: master1, address: 192.168.60.152, internalAddress: 192.168.60.152, user: root, password: P@ssw0rd}
  - {name: worker1, address: 192.168.60.153, internalAddress: 192.168.60.153, user: root, password: P@ssw0rd}
  - {name: worker2, address: 192.168.60.154, internalAddress: 192.168.60.154, user: root, password: P@ssw0rd}
  roleGroups:
    etcd:
    - master1
    control-plane:
    - master1
    worker:
    - worker1
    - worker2
  controlPlaneEndpoint:
    domain: lb.kubesphere.local
    address: ""                    
    port: 6443

执行以下命令使用自定义的配置文件创建集群:

./kk create cluster -f config-sample.yaml

验证安装

安装结束后,您可以执行以下命令查看安装日志:

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

如果返回欢迎日志,则安装成功。

**************************************************
#####################################################
###              Welcome to KubeSphere!           ###
#####################################################
Console: http://192.168.60.152:30880
Account: admin
Password: P@88w0rd
NOTES:
  1. After you log into the console, please check the
     monitoring status of service components in
     the "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             20xx-xx-xx xx:xx:xx
#####################################################

登录控制台

您可以使用默认的帐户和密码 admin/P@88w0rd 登录 KubeSphere 控制台并开始使用 KubeSphere。请在登录后修改默认密码。

启用可插拔组件(可选)

以上示例演示了默认的最小化安装流程。如需启用 KubeSphere 的其他组件,请参考启用可插拔组件

优化系统

  • 更新系统。

    yum update
    
  • 添加所需的内核引导参数。

    sudo /sbin/grubby --update-kernel=ALL --args='cgroup_enable=memory cgroup.memory=nokmem swapaccount=1'
    
  • 启用 overlay2 内核模块。

    echo "overlay2" | sudo tee -a /etc/modules-load.d/overlay.conf
    
  • 刷新动态生成的 grub2 配置。

    sudo grub2-set-default 0
    
  • 调整内核参数并使修改生效。

    cat <<EOF | sudo tee -a /etc/sysctl.conf
    vm.max_map_count = 262144
    fs.may_detach_mounts = 1
    net.ipv4.ip_forward = 1
    vm.swappiness=1
    kernel.pid_max =1000000
    fs.inotify.max_user_instances=524288
    EOF
    sudo sysctl -p
    
  • 调整系统限制。

    vim /etc/security/limits.conf
    *                soft    nofile         1024000
    *                hard    nofile         1024000
    *                soft    memlock        unlimited
    *                hard    memlock        unlimited
    root             soft    nofile         1024000
    root             hard    nofile         1024000
    root             soft    memlock        unlimited
    
  • 删除旧的限制配置。

    sudo rm /etc/security/limits.d/20-nproc.conf
    
  • 重启系统。

    reboot
    

通过邮件接收 KubeSphere 最新的技术博客与产品更新的通知


感谢您的反馈。如果您有关于如何使用 KubeSphere 的具体问题,请在 Slack 上提问。如果您想报告问题或提出改进建议,请在 GitHub 存储库中打开问题。