centos7.9 + kubekey 3.x 安装k8s及kubesphere

本次计划安装k8s v1.28.15,它需要的系统内核必须 >= 5.4,所以要先升有centos的内核。

由于ElRepo在线源已经停止服务,所以不能通过在线仓库升级内核,需要通过下载离线包的方式升级内核。

 

【下载离线 RPM 包安装(在线源彻底不通时)】

centeos7安装6.9内核,逐条执行下面这 3 条wget令 (5.4与6.9内核安装期中一个即可)

#国内高速Coreix镜像,6.9.x主线
wget --no-check-certificate https://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-ml-6.9.7-1.el7.elrepo.x86_64.rpm
wget --no-check-certificate https://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-ml-devel-6.9.7-1.el7.elrepo.x86_64.rpm
wget --no-check-certificate https://mirrors.coreix.net/elrepo-archive-archive/kernel/el7/x86_64/RPMS/kernel-ml-headers-6.9.7-1.el7.elrepo.x86_64.rpm
# 2. 下载完直接安装,先删除旧headers
rpm -e --nodeps kernel-headers
yum localinstall -y *.rpm
# 3. 安装完设置默认启动 + 重启
#设置序号0为默认内核
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
# 查看内核 
uname -a

 

查看默认内核

awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg

 

【安装k8s前期准备】

 

准备三台centos服务器,每台服务器做好准备工作,每台执行以下命令:

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld

# 关闭 selinux
setenforce 0 
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config # 关闭虚拟内存 swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab # 重启生效 # systemctl reboot # 查看虚拟内存,swap那一行全部为0表示虚所内存已关闭 free -h # 给k8s的每个节点设置主机名,最好配三台电脑,每台电脑分别命名为: hostnamectl set-hostname k8s-master-01 hostnamectl set-hostname k8s-worker-01 hostnamectl set-hostname k8s-worker-02 # 刷新bash环境 exec bash # 查看主机名 hostname

 

设置时间同步

# 设置时区(东八区)
timedatectl set-timezone Asia/Shanghai

# 启动 chrony 时间同步
systemctl enable --now chronyd

# 立即同步一次
chronyc -a makestep

# 开启系统 NTP 自动同步
timedatectl set-ntp true

# 关闭 VMware 时间同步(避免冲突),非虚拟机不需要设置
vmware-toolbox-cmd timesync disable 2>/dev/null

 

查看时间同步状态

# 查看时间
date
hwclock

#查看chrony是否运行
systemctl status chronyd
#查看时间同步状态
timedatectl
# 或查看同步状态
timedatectl status
# 看 chrony 状态
chronyc sources

 

********** 不要用下面老旧的方法设置时间同步 **********
yum install ntpdate -y
ntpdate time.windows.com

 

添加hosts

# 添加hosts,ip需要修改成你自己机器的内网ip(在终端里执行命令,有几个k8s节点就添加几个,每个k8s节点做同样的设置)
# 如果只有k8s-master-01节点,就只添加一个

cat >> /etc/hosts << EOF
172.29.132.65 k8s-master-01
172.29.132.66 k8s-worker-01
172.29.132.67 k8s-worker-02 EOF # 查看hosts cat /etc/hosts

 

允许 iptables 检查桥接流量,每个k8s节点都要配置。

# 这是 K8s 必须配置的内核网络参数,目的是:
# 让 Linux 内核能正确转发、过滤容器之间的网络流量
# 不配置 → K8s 集群无法通信、Pod 不通、Service 失效。
cat << EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
#开启ip转发
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF

# 让上面的内核参数立即生效
sudo sysctl --system
# 让ip转发强制生效(CentOS7 专用)
echo 1 > /proc/sys/net/ipv4/ip_forward

 

SSH 免密登录(仅部署节点执行,多节点集群必须)

# 生成密钥
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
# 批量推送至所有节点(替换为你的节点IP)
ssh-copy-id root@172.29.132.66
ssh-copy-id root@172.29.132.67
ssh-copy-id root@172.29.132.68
# 测试连通
ssh root@192.168.1.101 hostname

 

【软件准备(所有节点)】

软件名称 是否安装
socat 必须安装
conntrack 必须安装

conntrack-tools

必须安装
ebtables 可选,但推荐安装
ipset 可选,但推荐安装
ipvsadm 可选,但推荐安装

 

 
 
 
 
 
 
 
 
 
 
# 每台节点都要安装
yum -y install socat conntrack conntrack-tools ebtables ipset ipvsadm

 

设置KKZONE为国源内,永久生效(所有节点执行)

先查看一下如果/etc/profile 里有 export KKZONE=cn  这一句配置,就勿需再执行了。

echo "export KKZONE=cn" >> /etc/profile
source /etc/profile

 

【安装kubekey 3.1.9】 

【在线安装】kubekey 3.1.9是目前kubekey 3.x里最新版,最稳定,长期版本。

export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.1.9 sh -
cp kk /usr/local/bin/
kk version

 

【手动二进制包下载】kubekey 3.1.9

wget https://github.com/kubesphere/kubekey/releases/download/v3.1.9/kubekey-v3.1.9-linux-amd64.tar.gz
tar -zxvf kubekey-v3.1.9-linux-amd64.tar.gz
chmod +x kk
cp kk /usr/local/bin/
kk version

 

查看当前版本kubekey支持哪些k8s版本

./kk version --show-supported-k8s

 

生成集群配置文件

mkdir /data/kubekey
cd /data/kubekey
# 生成自定义配置文件 cluster.yaml,指定K8s版本,kubekey 3.1.9 最高支持 k8s v1.33.0
kk create config --with-kubernetes v1.33.0 -f k8s-cluster.yaml

 

修改 k8s-cluster.yaml 配置文件,根据现有模板文件修改。

apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  #若已做 ssh 免密登录,password 字段可删除。
  - {name: k8s-master-01, address: 172.29.132.65, internalAddress: 172.29.132.65, user: root, password: "你服务器的root密码"}
  - {name: k8s-worker-01, address: 172.29.132.66, internalAddress: 172.29.132.66, user: root, password: "你服务器的root密码"}
  - {name: k8s-worker-02, address: 172.29.132.67, internalAddress: 172.29.132.67, user: root, password: "你服务器的root密码"}
  roleGroups:
    etcd:
    - k8s-master-01
    control-plane:
    - k8s-master-01
    worker:
    - k8s-worker-01
    - k8s-worker-02
  controlPlaneEndpoint:
    # 单master不需要开启内置lb,注释保持原样
    # internalLoadbalancer: haproxy
    domain: lb.kubesphere.local
    address: ""
    port: 6443
  kubernetes:
    version: v1.33.0
    clusterName: cluster.local
    autoRenewCerts: true
    containerManager: containerd
  etcd:
    type: kubekey
  network:
    plugin: calico
    kubePodsCIDR: 10.244.0.0/16    #这个地方要改,兼容kubesphere,它默认是 10.233.64.0/18
    kubeServiceCIDR: 10.96.0.0/16   #这个地方要改,兼容kubesphere,它默认是 10.233.0.0/18
    multusCNI:
      enabled: false
  registry:
    privateRegistry: ""
    namespaceOverride: ""
    # 国内镜像加速,解决拉取镜像慢/超时
    registryMirrors:
      - https://mirror.baidubce.com
      - https://hub-mirror.c.163.com
      - https://docker.mirrors.ustc.edu.cn
    insecureRegistries: []
  addons: []

 

验证yaml文件是否有语法错误

python3 -c "import yaml; yaml.safe_load(open('k8s-cluster.yaml')); print('YAML语法正常')"

输出 YAML语法正常表示语法正确。

 

修改所有节点 sshd 允许环境变量

vi /etc/ssh/sshd_config

找到这两行,确保如下配置(没有就新增)

AcceptEnv LANG LC_* KKZONE *
PermitUserEnvironment yes

或者先判断不存在再追加(推荐,一键执行无重复)

# 匹配整行,不存在才写入
grep -q "^AcceptEnv LANG LC_\* KKZONE \*" /etc/ssh/sshd_config || echo "AcceptEnv LANG LC_* KKZONE *" >> /etc/ssh/sshd_config
grep -q "^PermitUserEnvironment yes" /etc/ssh/sshd_config || echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config

 

重启 sshd 服务

# CentOS/RHEL
systemctl restart sshd
# Ubuntu/Debian
systemctl restart ssh

 

 安装k8s 和 kubesphere(Ubuntu 云服务器必加 KKZONE=cn)

# 国内镜像加速环境变量
export KKZONE=cn
# 一条命令安装k8s及kubesphere,需要带上本地存储插件
kk create cluster -f k8s-cluster.yaml --with-local-storage --with-kubesphere

 

仅安装k8s( 以后单独安装kubesphere很难安装成功,不建议仅单独安装k8s)

# 国内镜像加速环境变量
export KKZONE=cn
# 一条命令安装k8s及kubesphere,需要带上本地存储插件
kk create cluster -f k8s-cluster.yaml

 

安装完成

image

 

安装完成后,验证集群是否安装成功

kubectl get nodes
kubectl get pod -A

 

image

 

【单独安装kubesphere】

官司方明确说明,已有k8s集群不能用kubekey单独安装 kubesphere,只能用 helm 仓库安装。

添加国内 Helm 仓库

helm repo add kubesphere https://charts.kubesphere.io/main --force-update
helm repo update

 

使用官方华为云镜像安装kubesphere

helm upgrade --install ks-core oci://hub.kubesphere.com.cn/kse/ks-core \
  --namespace kubesphere-system \
  --create-namespace \
  --version 1.2.3 \
  --reset-values \
  --set global.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks \
  --set extension.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks \
  --set console.nodePort=30880 \
  --debug

 

或用使用阿里云镜像安装kubesphere

helm upgrade --install ks-core oci://hub.kubesphere.com.cn/kse/ks-core \
  --namespace kubesphere-system \
  --create-namespace \
  --version 1.2.3 \
  --reset-values \
  --set global.imageRegistry=registry.cn-beijing.aliyuncs.com/kubesphereio \
  --set extension.imageRegistry=registry.cn-beijing.aliyuncs.com/kubesphereio \
  --set console.nodePort=30880 \
  --debug

华为、阿里两个镜像都试过,安装不成功。建议通过一条命令一次性安装k8s及kubesphere。 

 

删除节点

kk delete node k8s-worker-02 -f k8s-cluster.yaml

 

添加节点,先在k8s-cluster.yaml里把节点添加到hosts 及 worker 节点里,然后

kk add nodes -f k8s-cluster.yaml

 

删除集群

kk delete cluster -f k8s-cluster.yaml --ignore-err -y

 

卸载后全节点深度清理(必做,否则重装可能会报错)

# 停止服务
systemctl stop kubelet
systemctl disable kubelet

# 卸载kubelet临时挂载卷
mount | grep /var/lib/kubelet | awk '{print $3}' | xargs -r umount -l

# 删除k8s核心目录
rm -rf /etc/kubernetes
rm -rf /var/lib/kubelet
rm -rf /var/lib/etcd
rm -rf /etc/cni
rm -rf /opt/cni
rm -rf /run/calico /var/lib/calico
rm -rf ~/.kube
rm -rf /data/openebs/local/*  # 清理OpenEBS本地存储数据

# 清理 containerd 全部镜像/容器
crictl rm -f $(crictl ps -q) 2>/dev/null
crictl rmi $(crictl images -q) 2>/dev/null

# 清空iptables/ipvs网络规则,避免calico冲突
iptables -F && iptables -t nat -F && iptables -t mangle -F
iptables -X && iptables -t nat -X && iptables -t mangle -X
ipvsadm -C 2>/dev/null

# 删除calico虚拟网卡
ip link delete cni0 2>/dev/null
ip link delete flannel.1 2>/dev/null
ip link delete tunl0 2>/dev/null

# 重启containerd
systemctl restart containerd

 

清理本地 kk 缓存(部署机执行)

# 删除kk本地缓存、证书、节点缓存
rm -rf ./kubekey
rm -rf /root/.kube

 

单独只卸载 KubeSphere(保留 K8s 集群)

# 卸载ks核心
helm uninstall ks-core -n kubesphere-system
# 删除ks命名空间与所有组件
kubectl delete ns kubesphere-system kubesphere-monitoring-system kubesphere-devops-system kubesphere-logging-system
# 删除ks相关CRD
kubectl get crd | grep kubesphere | awk '{print $1}' | xargs kubectl delete crd

 

 

 

 

posted @ 2026-06-20 18:14  民工黑猫  阅读(23)  评论(0)    收藏  举报