搭建KubeSphere+k8s环境
本文字数 0 | 阅读时长 ≈ 0 分钟
category
技术工具
tags
云原生
k8s
实用教程
type
Post
slug
kuberkey-kubesphere-k8s
summary
kubekey和kubesphere真是k8s的利器(All-in-One 及 Mutl-Node两种模式)
status
Published
date
Jul 13, 2022
icon
password
前言
这里将借用KubeKey 和KubeSphere官方提供的安装脚本,快速搭建一个带KubeSphere平台的K8s集群。KubeSphere的面板相比Kubernetes的DashBoard更加直观并且实用。


准备服务器
- 虚拟机要开启时间同步
# 矫正时间
yum install -y ntp
# 新增以下看两行内容
echo "server ntp1.aliyun.com prefer" >> /etc/ntp.conf
echo "server ntp2.aliyun.com" >> /etc/ntp.conf
# 设置时区
timedatectl set-timezone Asia/Shanghai
chkconfig ntpd on- 提前修改pip的conf ,否则KubeSphere安装脚本中的默认清华源会提示警告错误,耽误时间
cat > ~/.pip/pip.conf << EOF
[global]
index-url=https://mirrors.aliyun.com/pypi/simple
EOF- 偶尔会出现packagekit服务阻塞yum服务的正常安装,推荐关闭此服务。
# 停掉 正在运行的packagekit (如果有的话)
systemctl stop packagekit
systemctl disable packagekit- 建议安装过程打几个快照,尤其是在纯净系统的时候打快照,以便于重新安装或测试。(用空间换取时间)
一、使用kubekey (kk)安装 (推荐)
为什么选择 KubeKey
- 以前基于 ansible 的安装程序依赖于许多软件,例如 Python。KubeKey 由 Go 语言开发,可以消除在多种环境中出现的问题,确保成功安装。
- KubeKey 支持多种安装选项,例如 All-in-One、多节点安装以及离线安装。
- KubeKey 使用 Kubeadm 在节点上尽可能多地并行安装 Kubernetes 集群,使安装更简便,提高效率。与旧版的安装程序相比,它极大地节省了安装时间。
- KubeKey 提供内置高可用模式,支持一键安装高可用 Kubernetes 集群。
- KubeKey 旨在将集群作为对象来进行安装,即 CaaO。
All-in-One 脚本示例
以下是我从一个纯净的CentOS系统,一步步执行成功安装AllinOne的KubeSphere+K8s环境的脚本。
- 配置基础网络环境
# hostname
hostnamectl set-hostname master
# 防火墙
systemctl disable firewalld
systemctl stop firewalld
# 关闭swap
swapoff -a
echo "vm.swappiness=0" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
sed -i 's$/dev/mapper/centos-swap$#/dev/mapper/centos-swap$g' /etc/fstab
# 检查确认
free -m
# 检查关闭enforce
getenforce
# cat /etc/selinux/config
# cat /etc/sysconfig/selinux
# 设置为 disabled后重启
reboot- 安装 必备组件 socat conntrack
# 安装需要环境
yum install -y ebtables socat ipset conntrack wget- 安装Docker
# 安装docker
## 卸载旧版 yum remove docker-ce* && yum remove containerd.io.x86_64
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9
systemctl enable docker && systemctl start docker
systemctl status docker
# 镜像加速(可选)如果服务器在国外,则此项没必要。
#cat <<EOF > /etc/docker/daemon.json
#{"registry-mirrors": ["https://gqk8w9va.mirror.aliyuncs.com"]}
#EOF
systemctl restart docker
systemctl status docker
docker info | grep "Registry Mirrors" -A 1
# 检查 dns
cat /etc/resolv.conf- 安装kk工具
- 如果您的服务器能正常访问Github/Googleapis
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 sh -# 安装kk准备
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum makecache fast
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 sh -
开始创建环境
chmod +x kk
./kk create cluster --with-kubernetes v1.22.12 --with-kubesphere v3.3.2
## 使用1.22.12的kubernetes版本 + 3.3.2的kubesphere版本安装完成
根据你的网络情况,安装过程可能需要几十分钟,不要着急。安装完成后控制台会显示出你的额KubeSphere访问面板地址。

检验安装结果
你可以随时输入以下指令,查看KubeSphere平台 安装、更新的结果:
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-installer -o jsonpath='{.items[0].metadata.name}') -f单节点扩展为多结点
参照文档编辑你的节点配置文件
- 创建一个实例脚本
./kk create config --from-cluster- 编辑脚本:在脚本中添加你的节点配置
vim ~/sample.yml
apiVersion: kubekey.kubesphere.io/v1alpha1
kind: Cluster
metadata:
name: sample
spec:
hosts:
# You should complete the ssh information of the hosts
- {name: master, address: 192.168.1.25, internalAddress: 192.168.1.25}
- {name: node1, address: 192.168.1.54, internalAddress: 192.168.1.54, user:root, password: root}
roleGroups:
etcd:
- master
master:
- master
worker:
- node1
controlPlaneEndpoint:
# If loadbalancer was used, 'address' should be set to loadbalancer's ip.
domain: lb.kubesphere.local
address: ""
port: 6443
kubernetes:
version: v1.17.9
imageRepo: kubesphere
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: ""然后执行以下命令即可
./kk add nodes -f sample.yaml二、ks-installer安装器安装
三、可插拔组件的安装
- 你可以随时用命令行修改ks的ConfigMap文件,从而实现更新ks-installer。
kubectl edit cm -n kubesphere-system ks-installer修改完成后用以下命令检查更新结果
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath={.items[0].metadata.name}) -f- 更可以在kubesphere控制台中直接修改 ks.installer 的配置文件


示例-KubeSpereh增加ES日志查询
示例 - KubeSphere增加NFS卷类型
默认安装的KuberSphere只有 一个Local文件类型,这里可以安装一个NFS卷类型:
1. 系统安装NFS服务
- 执行以下脚本,安装NFS服务,并准备将/data目录作为服务目录:
yum install -y nfs-utils
systemctl enable rpcbind && systemctl start rpcbind
systemctl enable nfs && systemctl start nfs
mkdir /data
chmod 755 /data- 执行
vi /etc/exports修改NFS的目录配置 /data: 共享目录位置。192.168.31.0/24: 客户端 IP 范围,*代表所有,即没有限制。rw: 权限设置,可读可写。sync: 同步共享目录。no_root_squash: 可以使用 root 授权。no_all_squash: 可以使用普通用户授权。
文本内容:
/data/ 192.168.31.0/24(rw,sync,no_root_squash,no_all_squash)说明:
- 修改后重启NFS
systemctl restart nfs
- 执行
showmount -elocalhost可以查看当前被作为NFS服务的目录文件夹
2. 客户端 配置
- 开启rpc服务
systemctl enable rpcbind && systemctl start rpcbind
- 读取NFS服务器可挂载的文件夹列表
showmount -e 192.168.31.39 - 注意master需要关闭防火墙
- 客户端创建相同目录
mkdir /data
- 挂载
mount -t nfs 192.168.31.39:/data /data
3.更新KubeSphere环境
- 理想情况下,首次使用一键脚本安装配置
如果你在首次执行
./install.sh安装器部署带有KubeSphere环境前,已经安装好NFS环境,并按照以下配置修改 conf/common.yaml , 这时你安装的k8s是默认具有NFS卷类型支持的,并且NFS被设置为是默认的卷类型。# Local Volume
local_volume_enabled: true
local_volume_is_default_class: false # 先将集群默认的存储类型 Local Volume 设置为 false
# NFS CONFIGURATION
# KubeSphere can use existing nfs service as backend storage service.
# change to true to use nfs.
nfs_client_enabled: true # 设置为 true 开启客户端的安装
nfs_client_is_default_class: true # 设置为默认的存储类型
# Hostname of the NFS server(ip or hostname)
nfs_server: 192.168.31.39 # 替换为您 NFS server 实际的 IP 地址或 hostname
# Basepath of the mount point
nfs_path: /data # NFS server 的共享目录
nfs_vers3_enabled: false
nfs_archiveOnDelete: false- 但如果你已经安装好kubesphere+k8s环境,且没有使用一键安装脚本、而是使用kuberkey等方式,需要安装NFS-client的方式进行:
- 若你是用installer脚本一键安装可以使用修改
ConfigMap的方式来更新集群
执行以下命令,并参照以上的common.yaml内容配置ks-installer的内容。修改后自动生效并重新配置KubeSphere
kubectl edit cm -n kubesphere-system ks-installer
示例- 安装devops
官方推荐,在muti-node模式下,需要一台主机节点大于8G,才可以安装,否则可能效果并不理想,开启DevOps参考以下文档,此处不再赘述。
参考文档
KubeSphere文档
Kubekey文档
在 Kubernetes集群中 安装 KubeSphere2.1
一些QA
- 安装过程可能出现pip源的警告。这是由于ks-installer安装器默认的清华源有问题,需要按照文章上面的方法修改为默认使用阿里源。
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple, https://mirrors.aliyun.com/pypi/simple
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)'),)': /simple/pip/- 可能出现安装失败
使用ks-installer安装器或KubeKey都要求系统是完全纯净的,如果装了其他库依赖例如自己安装了Docker的其他版本,都会导致安装错误,这时候利用你的服务器快照回滚到最初的版本,重新安装试一下。
- Author:tangly1024
- URL:https://tangly1024.com/article/kuberkey-kubesphere-k8s
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts :
Tags:
云原生
k8s
实用教程




