跳到主要内容

安装 KubeSphere 和 Kubernetes

概述

本文档详细介绍了如何在Ubuntu系统上安装KubeSphere和Kubernetes集群,包括系统配置、依赖安装、Kubernetes部署和KubeSphere安装等步骤。

目标

  • 成功部署Kubernetes集群
  • 安装并配置KubeSphere平台
  • 验证集群和平台功能正常运行

前置条件

  • 准备好数台Ubuntu服务器(建议20.04或更高版本)
  • 确保节点间网络连通
  • 具备root或sudo权限
  • 确保系统资源满足最低要求

步骤

步骤1:关闭swap

sudo swapoff -a  # 临时

sudo sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久

步骤2:设置主机名

`` sh

在192.168.192.180执行

sudo hostnamectl set-hostname ubuntu-k8s-master

在192.168.192.181执行

sudo hostnamectl set-hostname ubuntu-k8s-node1

在192.168.192.182执行

sudo hostnamectl set-hostname ubuntu-k8s-node2

在192.168.192.183执行

sudo hostnamectl set-hostname ubuntu-k8s-node3


### 步骤3:添加hosts

`` sh
cat <<EOF | sudo tee /etc/hosts
192.168.192.180 ubuntu-k8s-master
192.168.192.181 ubuntu-k8s-node1
192.168.192.182 ubuntu-k8s-node2
192.168.192.183 ubuntu-k8s-node3

10.123.123.180 ubuntu-k8s-master
10.123.123.181 ubuntu-k8s-node1
10.123.123.182 ubuntu-k8s-node2
10.123.123.183 ubuntu-k8s-node3
EOF

步骤4:配置网络参数

`` sh cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF

sudo modprobe overlay sudo modprobe br_netfilter

设置所需的 sysctl 参数,参数在重新启动后保持不变

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.ipv4.ip_forward = 1 EOF

应用 sysctl 参数而不重新启动

sudo sysctl --system

lsmod | grep br_netfilter lsmod | grep overlay

sudo sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

如果init时仍提示iptables错误请执行

echo "1">/proc/sys/net/bridge/bridge-nf-call-iptables echo "1">/proc/sys/net/ipv4/ip_forward


### 步骤5:时间同步

``` sh
sudo apt install chrony -y
sudo systemctl start chrony && sudo systemctl enable chrony

步骤6:安装 Kubernetes

1.安装Kubersphere和Kubernetes的依赖组件。

所有节点都安装

sudo apt install socat conntrack ebtables ipset -y

在其中一个节点执行:

2.下载KuberKey,并设置地域为中国,以免无法正常访问Google或Github。

export KKZONE=cn
# 下载并执行sh命令
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.1.6 sh -

如果上面下载不了就先下载downloadKubekey.sh之后再手动执行sh

wget https://github.com/kubesphere/kubekey/releases/download/v1.1.0/downloadKubekey.sh --no-check-certificate
sudo apt install mblaze -y
export VERSION=v3.1.6
sh downloadKubekey.sh

下载完成后当前目录下将生成 KubeKey 二进制文件 kk。

执行以下命令为 KubeKey 二进制文件 kk 添加执行权限:

chmod +x kk

执行以下命令创建安装配置文件 k8s-v1288.yaml:

./kk create config -f k8s-v1288.yaml --with-kubernetes v1.28.8
#./kk create config --with-kubernetes v1.28.8 --with-kubesphere v3.4.0 -f config.yaml

命令执行完毕后将生成安装配置文件 k8s-v1288.yaml。

vim k8s-v1288.yaml
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
name: sample
spec:
hosts:
- {name: ubuntu-k8s-master, address: 192.168.192.180, internalAddress: 192.168.192.180, user: wengtx, password: 密码}
- {name: ubuntu-k8s-node1, address: 192.168.192.181, internalAddress: 192.168.192.181, user: wengtx, password: 密码}
- {name: ubuntu-k8s-node2, address: 192.168.192.182, internalAddress: 192.168.192.182, user: wengtx, password: 密码}
- {name: ubuntu-k8s-node3, address: 192.168.192.183, internalAddress: 192.168.192.183, user: wengtx, password: 密码}
roleGroups:
etcd:
- ubuntu-k8s-master
control-plane:
- ubuntu-k8s-master
worker:
- ubuntu-k8s-node1
- ubuntu-k8s-node2
- ubuntu-k8s-node3
controlPlaneEndpoint:
#internalLoadbalancer: haproxy # 如需部署⾼可⽤集群,且⽆负载均衡器可⽤,可开启该参数,做集群内部负载均衡
domain: lb.kubesphere.local
# vip
address: ""
port: 6443
kubernetes:
version: v1.28.2
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
#enableMultusCNI: false
registry:
registryMirrors: []
insecureRegistries: []
addons: []
./kk create cluster -f k8s-v1288.yaml

步骤7:安装 KubeSphere

KubeSphere Core (ks-core) 是 KubeSphere 的核心组件,为扩展组件提供基础的运行环境。KubeSphere Core 安装完成后,即可访问 KubeSphere Web 控制台。

curl -k --resolve raw.githubusercontent.com:443:185.199.108.133 https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

在集群节点,执行以下命令安装 KubeSphere Core。

helm upgrade --install -n kubesphere-system --create-namespace ks-core https://charts.kubesphere.io/main/ks-core-1.1.2.tgz --debug --wait --set global.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks --set extension.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks

验证

  • 检查Kubernetes集群状态:kubectl get nodes
  • 验证KubeSphere控制台可访问性
  • 确认核心组件正常运行

故障排除

  • 如果安装过程中出现网络问题,请检查网络配置和防火墙设置
  • 如果KubeSphere无法访问,请检查相关服务状态和端口配置

最佳实践

  • 建议使用独立的节点部署master节点
  • 合理规划集群资源和网络配置
  • 定期备份重要数据和配置

参考资料