Skip to content

使用 KubeKey 部署和运维 Kubernetes 和 KubeSphere 集群

第一章:KubeKey 介绍

1.1 kubeadm 等工具的缺点

  • ① 需要选择合适的部署工具(包括容器运行时,如:Docker 、Podman 等)。
  • ② 需要手动管理证书。
  • ③ 需要为 api-server 配置负载均衡器。
  • ④ 需要手动外置并备份 etcd 服务。
  • ⑤ 跨区域非常麻烦。
  • ⑥ 需要持续管理集群,如:手动增删节点等。
  • ⑦ ……

1.2 KubeKey 介绍

  • KubeKey 是一种全新的安装工具,是由 Go 语言开发的,提供了灵活的安全选择:
    • all-in-one 集群。
    • 高可用集群。
  • 从安装结果上来看,可以选择:
    • 仅安装 Kubernetes/K3s 。
    • 同时安装 Kubernetes/K3s 和 KubeSpere 。

1.3 KubeKey 的使用方式

1.3.1 概述

  • KubeKey  分为 all-in-one 模式和高级模式,其中高级模式可以部署高可用的 Kubernetes 集群。

1.3.2 all-in-one 模式(适合学习、测试使用)

  • 对于 Kubernetes >= 1.18 版本而言,建议安装如下依赖:
# 该命令只能在 redhat 系列的系统上运行
yum -y install dnf \
    && dnf -y update \
    && dnf -y install socat conntrack ebtables ipset ipvsadm

注意:对于 CentOS 7.9 而言,最低要求 2 核 CPU,4 GB 内存,40 GB 磁盘空间。

1679557609950-c9ccd70b-c550-4368-9650-6e262775b272.gif

  • 学习阶段请关闭防火墙:
systemctl stop firewalld \
    && systemctl disable firewalld

1679557609124-6b2bd2bc-a946-4d0e-b826-987fd39f00af.gif

  • 升级内核到主线版本:
# 在 CentOS 7.x 上启用 ELRepo 仓库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装长期支持内核版本
dnf -y --enablerepo=elrepo-kernel install kernel-lt
# 查看启动项
cat /etc/default/grub
# 修改配置
sed -i 's/^GRUB_DEFAULT=saved$/GRUB_DEFAULT=0/' /etc/default/grub
# 查看启动项
cat /etc/default/grub
# 重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 重启
reboot
# 查看当前系统的内核
uname -sr

1679557615715-8bc60543-185e-4323-b457-4edde69cf3e3.gif

  • 关闭 swap 分区和 SELinux :
# 查看 SELinux 是否开启
getenforce
# 查看 SELinux 是否开启
cat /etc/selinux/config
# 永久关闭 SELinux ,需要重启
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭当前会话的 SELinux ,重启之后无效
setenforce 0
# 查看 SELinux 是否开启
cat /etc/selinux/config
# 查看 swap 分区是否存在
free -h
# 关闭当前会话的 swap ,重启之后无效
swapoff -a
# 永久关闭 swap ,需要重启
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 查看 swap 分区是否存在
free -h

1679557623799-5e0e526e-c45e-4aba-928e-e1295ef98fc2.gif

  • 下载 KubeKey:
# 先执行以下命令以确保从正确的区域下载 KubeKey
export KKZONE=cn
# 执行以下命令下载 KubeKey
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.2 sh -
# 为 kk 添加可执行权限
chmod +x kk

1679557628086-7414e9bf-7856-4ca9-93ef-d30faeaa1403.gif

  • 创建集群:
./kk create cluster --with-kubernetes v1.21.10 --with-kubesphere v3.2.1

1679557634339-6f6e6074-a3f7-499a-a993-5deb52f2a737.gif

  • 查询安装进度:
watch -n 1 kubectl get pod -A
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

1679557641431-42c7294b-71ab-4a4c-913d-ef159eb3fcec.gif

  • 升级集群:
./kk upgrade --with-kubernetes v1.22.12 --with-kubesphere v3.3.1

1679557647653-d97b405c-78b1-47cd-924b-d401a418dd2b.gif

  • 删除集群:
./kk delete cluster

1679557678747-1b9213a7-a554-4c84-83e8-ca593e87bc49.gif

1.3.3 高级模式(生产建议使用)

  • 准备工作(所有机器节点均需执行):
# 该命令只能在 redhat 系列的系统上运行
yum -y install dnf \
    && dnf -y update \
    && dnf -y install socat conntrack ebtables ipset ipvsadm
systemctl stop firewalld \
    && systemctl disable firewalld
# 在 CentOS 7.x 上启用 ELRepo 仓库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装长期支持内核版本
dnf -y --enablerepo=elrepo-kernel install kernel-lt
# 查看启动项
cat /etc/default/grub
# 修改配置
sed -i 's/^GRUB_DEFAULT=saved$/GRUB_DEFAULT=0/' /etc/default/grub
# 查看启动项
cat /etc/default/grub
# 重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 重启
reboot
# 查看当前系统的内核
uname -sr
# 查看 SELinux 是否开启
getenforce
# 查看 SELinux 是否开启
cat /etc/selinux/config
# 永久关闭 SELinux ,需要重启
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭当前会话的 SELinux ,重启之后无效
setenforce 0
# 查看 SELinux 是否开启
cat /etc/selinux/config
# 查看 swap 分区是否存在
free -h
# 关闭当前会话的 swap ,重启之后无效
swapoff -a
# 永久关闭 swap ,需要重启
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 查看 swap 分区是否存在
free -h
  • 下载 KubeKey :
# 先执行以下命令以确保从正确的区域下载 KubeKey
export KKZONE=cn
# 执行以下命令下载 KubeKey
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.2 sh -
# 为 kk 添加可执行权限
chmod +x kk
  • 创建 KubeKey 的配置文件:
./kk create config --with-kubernetes v1.21.10 --with-kubesphere v3.2.1 -f config.yaml

注意:根据实际情况,编辑 config.yaml 中的节点的配置信息。

  • 安装集群依赖组件:
./kk init os -f config.yaml
  • 创建集群:
./kk create cluster -f config.yaml
  • 删除集群:
./kk delete cluster -f config.yaml
  • 升级集群:
./kk upgrade -f config.yaml

第二章:部署高可用的 Kubernetes 集群

2.1 概述

  • Kubernetes 高可用集群有两种:
    • ① 外部负载均衡。
    • ② 内部负载均衡。
  • 其中,外部负载均衡的方式就是通过负载均衡器监听默认端口 6443 作为 Kubernetes API 服务器。

1679557705897-146236de-dfde-4114-8251-2bcb9ff489b1.png

  • 其中,内部负载均衡就是每个 master 节点的 kubelet 连接本地的 kube-apiserver,每个 worker 节点的 kubelet 通过本地的反向代理连接 kube-apiserver。基于此,kubekey 将部署一个基于 haproxy 的代理,驻留在每个工作节点上作为本地反向代理。

1679557710479-2777c94b-0fe8-4d6e-9b1f-c95a6f3d85bf.png

2.2 主机规划

  • 本次采用内部负载均衡的方式来搭建 Kubernetes 高可用集群,其中主机规划如下:
角色 IP 地址 操作系统 配置 hostname
master 192.168.68.10 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-master1
master 192.168.68.11 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-master2
master 192.168.68.12 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-master3
node 192.168.68.13 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-node1
node 192.168.68.14 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-node2
node 192.168.68.15 CentOS 7.9 2核CPU,3G内存,50G硬盘 k8s-node3

2.3 搭建步骤

  • 准备工作(所有机器节点均需执行):
# 替换为国内镜像源
# 备份
cp -a /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 下载
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
# 安装 dnf 软件包管理器,清除缓存并重新构建缓存
yum -y install dnf \
    && dnf clean all \
    && dnf makecache

1679557735069-97a8decf-18ee-4912-8c34-be249b9c2354.gif

# 安装必须依赖
dnf -y update \
    && dnf -y install socat conntrack ebtables ipset ipvsadm

1679557749787-01821cb6-6c50-4201-aa1b-293785c1dd61.gif

# 禁用防火墙
systemctl stop firewalld \
    && systemctl disable firewalld

1679557748624-085cad2a-38c5-4fd8-bc08-62ea3a070246.gif

# 升级内核
# 在 CentOS 7.x 上启用 ELRepo 仓库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装长期支持内核版本
dnf -y --enablerepo=elrepo-kernel install kernel-lt
# 查看启动项
cat /etc/default/grub
# 修改配置
sed -i 's/^GRUB_DEFAULT=saved$/GRUB_DEFAULT=0/' /etc/default/grub
# 查看启动项
cat /etc/default/grub
# 重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 重启
reboot
# 查看当前系统的内核
uname -sr

1679557782815-18ac737f-93b0-4374-b6d2-16fe28dc613e.gif

# 关闭 SELinux
# 查看 SELinux 是否开启
getenforce
# 查看 SELinux 是否开启
cat /etc/selinux/config
# 永久关闭 SELinux ,需要重启
sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭当前会话的 SELinux ,重启之后无效
setenforce 0
# 查看 SELinux 是否开启
cat /etc/selinux/config

1679557791175-bc8cc3c3-a64c-4539-972b-9534ff1dfc62.gif

# 关闭 swap 分区
# 查看 swap 分区是否存在
free -h
# 关闭当前会话的 swap ,重启之后无效
swapoff -a
# 永久关闭 swap ,需要重启
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 查看 swap 分区是否存在
free -h

1679557804796-fbee1167-ff4f-4537-98b0-cc867923f68c.gif

  • 开启 ipvs:
# 执行如下脚本
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
# 授权、运行、检查是否加载
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules

1679623534740-814b6d12-d0a3-449c-b847-d998baee30f4.gif

  • 设置主机名:
# 192.168.68.10
hostnamectl set-hostname k8s-master1
# 192.168.68.11
hostnamectl set-hostname k8s-master2
# 192.168.68.12
hostnamectl set-hostname k8s-master3
# 192.168.68.13
hostnamectl set-hostname k8s-node1
# 192.168.68.14
hostnamectl set-hostname k8s-node2
# 192.168.68.15
hostnamectl set-hostname k8s-node3

1679557814437-3f69f2cf-a07d-48b2-9432-9caf4b4daa69.gif

  • 主机名解析:
cat >> /etc/hosts << EOF
127.0.0.1   $(hostname)
192.168.68.10 k8s-master1
192.168.68.11 k8s-master2
192.168.68.12 k8s-master3
192.168.68.13 k8s-node1
192.168.68.14 k8s-node2
192.168.68.15 k8s-node3
EOF

1679557821951-36de075f-523c-4260-a5a7-ecc7baa2dbc6.gif

  • k8s-master1 节点下载 KubeKey :
# 先执行以下命令以确保从正确的区域下载 KubeKey
export KKZONE=cn
# 执行以下命令下载 KubeKey
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.2 sh -
# 为 kk 添加可执行权限
chmod +x kk

1679557856990-b8ffc490-7019-4488-91b9-eb8edab885e0.gif

  • 创建 KubeKey 的配置文件:
./kk create config --with-kubernetes v1.21.10 --with-kubesphere v3.2.1 -f config.yaml

1679557879646-ba1b4c48-148c-4e4e-adeb-bc88b58aa753.gif

  • 修改 KubeKey 的配置文件:
vim config.yaml
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  - {name: k8s-master1, address: 192.168.68.10, internalAddress: 192.168.68.10, user: root, password: "123456"}
  - {name: k8s-master2, address: 192.168.68.11, internalAddress: 192.168.68.11, user: root, password: "123456"}
  - {name: k8s-master3, address: 192.168.68.12, internalAddress: 192.168.68.12, user: root, password: "123456"}
  - {name: k8s-node1, address: 192.168.68.13, internalAddress: 192.168.68.13, user: root, password: "123456"}
  - {name: k8s-node2, address: 192.168.68.14, internalAddress: 192.168.68.14, user: root, password: "123456"}
  - {name: k8s-node3, address: 192.168.68.15, internalAddress: 192.168.68.15, user: root, password: "123456"}
  roleGroups:
    etcd:
    - k8s-master1
    - k8s-master2
    - k8s-master3
    control-plane: 
    - k8s-master1
    - k8s-master2
    - k8s-master3
    worker:
    - k8s-node1
    - k8s-node2
    - k8s-node3
  controlPlaneEndpoint:
    ## Internal loadbalancer for apiservers 
    internalLoadbalancer: haproxy # 内部负载均衡,如果是外部负载均衡,将此句注释掉

    domain: lb.kubesphere.local
    address: "" # 外部负载均衡 vip 的地址
    port: 6443
  kubernetes:
    version: v1.21.10
    clusterName: cluster.local
    autoRenewCerts: true
    containerManager: docker
  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: []



---
apiVersion: installer.kubesphere.io/v1alpha1
kind: ClusterConfiguration
metadata:
  name: ks-installer
  namespace: kubesphere-system
  labels:
    version: v3.2.1
spec:
  persistence:
    storageClass: ""
  authentication:
    jwtSecret: ""
  zone: ""
  local_registry: ""
  namespace_override: ""
  # dev_tag: ""
  etcd:
    monitoring: true
    endpointIps: localhost
    port: 2379
    tlsEnable: true
  common:
    core:
      console:
        enableMultiLogin: true
        port: 30880
        type: NodePort
    # apiserver:
    #  resources: {}
    # controllerManager:
    #  resources: {}
    redis:
      enabled: false
      volumeSize: 2Gi
    openldap:
      enabled: false
      volumeSize: 2Gi
    minio:
      volumeSize: 20Gi
    monitoring:
      # type: external
      endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
      GPUMonitoring:
        enabled: false
    gpu:
      kinds:         
      - resourceName: "nvidia.com/gpu"
        resourceType: "GPU"
        default: true
    es:
      # master:
      #   volumeSize: 4Gi
      #   replicas: 1
      #   resources: {}
      # data:
      #   volumeSize: 20Gi
      #   replicas: 1
      #   resources: {}
      logMaxAge: 7
      elkPrefix: logstash
      basicAuth:
        enabled: false
        username: ""
        password: ""
      externalElasticsearchHost: ""
      externalElasticsearchPort: ""
  alerting:
    enabled: false
    # thanosruler:
    #   replicas: 1
    #   resources: {}
  auditing:
    enabled: false
    # operator:
    #   resources: {}
    # webhook:
    #   resources: {}
  devops:
    enabled: false
    jenkinsMemoryLim: 2Gi
    jenkinsMemoryReq: 1500Mi
    jenkinsVolumeSize: 8Gi
    jenkinsJavaOpts_Xms: 512m
    jenkinsJavaOpts_Xmx: 512m
    jenkinsJavaOpts_MaxRAM: 2g
  events:
    enabled: false
    # operator:
    #   resources: {}
    # exporter:
    #   resources: {}
    # ruler:
    #   enabled: true
    #   replicas: 2
    #   resources: {}
  logging:
    enabled: false
    containerruntime: docker
    logsidecar:
      enabled: true
      replicas: 2
      # resources: {}
  metrics_server:
    enabled: false
  monitoring:
    storageClass: ""
    # kube_rbac_proxy:
    #   resources: {}
    # kube_state_metrics:
    #   resources: {}
    # prometheus:
    #   replicas: 1
    #   volumeSize: 20Gi
    #   resources: {}
    #   operator:
    #     resources: {}
    #   adapter:
    #     resources: {}
    # node_exporter:
    #   resources: {}
    # alertmanager:
    #   replicas: 1
    #   resources: {}
    # notification_manager:
    #   resources: {}
    #   operator:
    #     resources: {}
    #   proxy:
    #     resources: {}
    gpu:
      nvidia_dcgm_exporter:
        enabled: false
        # resources: {}
  multicluster:
    clusterRole: none 
  network:
    networkpolicy:
      enabled: false
    ippool:
      type: none
    topology:
      type: none
  openpitrix:
    store:
      enabled: false
  servicemesh:
    enabled: false
  kubeedge:
    enabled: false   
    cloudCore:
      nodeSelector: {"node-role.kubernetes.io/worker": ""}
      tolerations: []
      cloudhubPort: "10000"
      cloudhubQuicPort: "10001"
      cloudhubHttpsPort: "10002"
      cloudstreamPort: "10003"
      tunnelPort: "10004"
      cloudHub:
        advertiseAddress:
          - ""
        nodeLimit: "100"
      service:
        cloudhubNodePort: "30000"
        cloudhubQuicNodePort: "30001"
        cloudhubHttpsNodePort: "30002"
        cloudstreamNodePort: "30003"
        tunnelNodePort: "30004"
    edgeWatcher:
      nodeSelector: {"node-role.kubernetes.io/worker": ""}
      tolerations: []
      edgeWatcherAgent:
        nodeSelector: {"node-role.kubernetes.io/worker": ""}
        tolerations: []

1679557894979-0913459b-6d3a-4f80-8159-cd7565543a53.gif

  • 创建集群:
./kk create cluster -f config.yaml

注意:安装成功之后,根据日志信息,通过浏览器登录到 kubesphere 平台即可。

1679557965038-a08d2fad-8ab7-42f6-8402-7690f3c14713.gif

  • 安装成功的日志:

1679557973857-d8bcdf4e-ff85-4b43-ab3b-fcd692b8a5ea.png

  • 登录 kubesphere :

1679557995541-fae6ee39-cf07-48bc-8294-fae5eaa01500.gif

  • 删除集群:
./kk delete cluster -f config.yaml
  • 升级集群:
./kk upgrade -f config.yaml

第三章:Kubernetes 增删集群节点

3.1 概述

  • 在实际应用场景中,有的时候,我们需要根据实际情况,对 Kubernetes 进行增删节点,而 KubeKey 就提供了这种功能。

3.2 增加节点

  • 步骤:
    • ① 修改 config.yaml 文件,在其中配置要增加的节点。
    • ② 执行 ./kk add nodes -f config.yaml 即可。
    • ③ 登录到 kubesphere 平台查看节点即可。

注意:增加的节点同样要执行相应的准备工作,如:关闭防火墙,关闭 swap 分区等,此处就不再次阐述了!

  • 示例:

1679558009401-01a0b1d1-af2d-48f9-b984-9ee4bc8983fa.gif

3.3 删除节点

  • 步骤:执行 ./kk delete node <nodeName> -f config-sample.yaml 即可。

  • 示例:略。

第四章:Kubernetes 集群证书管理

5.1 概述

  • 默认情况下,使用 kubeadm 安装 Kubernetes 集群的证书的有效期是 1 年,但是 KubeKey 能自动检测 Kubernetes 集群证书的有效期,我们也可以使用 KubeKey 来帮助我们延长 Kubernetes 集群证书的有效期。

5.2 Kubernetes 集群证书管理

  • 查看集群证书的有效期:
./kk certs check-expiration

1679558017668-be6d356d-751e-4a6e-8548-0fd9db85dd79.gif

  • 手动更新证书有效期:
./kk certs renew -f config.yaml

1679558028037-c990fba5-e280-4bb7-a55f-6eb43d10c523.gif

第六章:KubeSphere 启用可插拔组件

6.1 概述

  • 默认情况下,KubeSphere 将一些组件做成可插拔,目的就是为了减轻对硬件的压力;当然,如果我们需要,可以手动开启。

6.2 启用可插拔组件

  • ① 通过 admin 账户登录到 KubeSphere 平台:略。
  • 平台管理 --> 集群管理 --> CRD --> 输入 ClusterConfiguration --> 编辑 ks-installer 即可。

1679558105291-deb5cc29-e4c4-453c-9748-47b708b36a68.gif

第七章:Kubernetes 节点管理

  • 可以通过 KubeSphere 对 Kubernetes 的节点做一些操作,如:打标签,设置污点、停止调度等。

  • 示例:

1679558100479-37335676-5055-4d27-a518-ffd9bb172833.gif

更新: 2023-03-24 02:07:05
原文: https://www.yuque.com/fairy-era/yg511q/iwtm32krf5g55sl4

Comments