本文采用的kk(KubeKey)工具进行kubesphere及k8s安装,笔者已在生产和VM成功部署使用,请放心使用哦~
kubesphere官方文档:https://v3-1.docs.kubesphere.io/zh/docs/ 多节点安装文档:https://v3-1.docs.kubesphere.io/zh/docs/installing-on-linux/introduction/multioverview/ 离线安装官方文档:https://v3-1.docs.kubesphere.io/zh/docs/installing-on-linux/introduction/air-gapped-installation/
vm机器配置
主机名 | IP地址 | 最低要求 | 说明 |
---|---|---|---|
master14 | 192.168.239.14 | CPU: 2 核,内存:4 G,硬盘:80 G | master14节点 |
node22 | 192.168.239.22 | CPU: 1 核,内存:1 G,硬盘:50 G | node22节点 |
node23 | 192.168.239.23 | CPU: 1 核,内存:1 G,硬盘:50 G | node23节点 |
node24 | 192.168.239.23 | CPU: 1 核,内存:1 G,硬盘:50 G | harbor私服 |
温馨提示:这是针对于本地通过vm采用离线部署的最低配置(配置vm硬盘50G,并不代表直接就分配50,是虚拟的哦~)
一句话,预留一个100G的硬盘就足矣玩转我这个配置
在足够安全的外部防护下,可以永久关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
docker在线安装详细教程---安装
温馨提示:以下为政务内网的离线安装~
19.3.8+
选择合适的docker版本
tar -zxvf docker-19.03.9.tgz
解压的docker文件夹全部移动至/usr/bin目录
cp docker/* /usr/bin/
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
#依次执行以下脚本
vi /etc/systemd/system/docker.service
chmod +x /etc/systemd/system/docker.service
systemctl daemon-reload && systemctl start docker && systemctl enable docker.service
docker -v
vi /etc/docker/daemon.json填入以下内容:
{
"registry-mirrors": [
"https://sq9p56f6.mirror.aliyuncs.com" #镜像加速器
],
"insecure-registries": ["IP:8088"], #若镜像仓库为http,需要加入安全策略中
"exec-opts":["native.cgroupdriver=systemd"] #将cgroup改为systemd模式,与linux同步,docker默认为cgroupfs。
}
Docker-Compose下载路径:https://github.com/docker/compose/releases/download/1.24.1/docker-compose-Linux-x86_64
选择合适的docker版本
#依次执行一下命令
#重命名
mv docker-compose-Linux-x86_64 docker-compose
#修改权限
chmod +x docker-compose
#将docker-compose文件移动到了/usr/local/bin
mv docker-compose /usr/local/bin
#打开/etc/profile文件
vi /etc/profile
#添加内容到文件末尾即可,然后保存退出
export PATH=$JAVA_HOME:/usr/local/bin:$PATH
#重新加载配置文件,让其生效
source /etc/profile
#测试
docker-compose
Harbor详细教程---安装
默认你有harbor包
#解压
tar -zxvf harbor-offline-installer-v2.5.0-rc4.tgz
cp harbor.yml.tmpl harbor.yml
#注释掉htps的配置内容,配置htp相关的参数,主要是hostname,port,其他都可以不用动。
vi harbor.yml
#启动
./install.sh
您可以根据自己的需求变更下载的 Kubernetes 版本。安装 KubeSphere v3.1.1 的建议 Kubernetes 版本:v1.17.9,v1.18.8,v1.19.8 以及 v1.20.4。如果不指定 Kubernetes 版本,KubeKey 将默认安装 Kubernetes v1.19.8。有关受支持的 Kubernetes 版本的更多信息,请参见支持矩阵。 运行脚本后,会自动创建一个文件夹
kubekey
。请注意,您稍后创建集群时,该文件和kk
必须放在同一个目录下。
#文件可执行。
chmod +x offline-installation-tool.sh
# IP:8088->私服仓库的ip+端口
./offline-installation-tool.sh -l images-list.txt -d ./kubesphere-images -r IP:8088/library
#解压进入目录:
chmod +x kk
#指定对应版本(kk文件中有yaml就无需操作此步)
./kk create config --with-kubernetes v1.20.4 --with-kubesphere v3.1.1 -f config-sample.yaml
#编辑(文件需要调整以下配置)
vi config-sample.yaml
#执行
./kk create cluster -f config-sample.yaml
温馨提示⚠️ config-sample.yaml文件需要调整以下配置
spec.host
s 指定要纳入集群的机器spec.roleGroups.etcd spec.roleGroups.maste
r 指定主节点spec.roleGroups.worke
r 指定工作节点registry.insecureRegistrie
s 添加私服地址
出现访问地址和用户名密码及安装成功
步骤:
建议mysql外部安装
查看集群管理
开启日志组件启动可插拔组件
配置镜像仓库
本文采用的是yml的方式进行自制应用的构建,不同的项目只需要
修改企业空间
、镜像仓库
以及数据库配置
即可。温馨提示:本文是基于已有数据库以及项目上传到镜像仓库的前提下进行操作~
...
注意:在构建制品时项目pom文件中镜像源地址仓库名是否在harbor中有创建,没有的话,运行流水线会报错,需要自己创建仓库
成功,等启动完,查看服务下所有容器的的日志,是否启动完成无报错
略
注意
1、主节点上修改kk配置文件,加入新节点node2配置
2、执行添加命令
./kk add nodes -f 配置文件
./kk add nodes -f sample.yaml
3、查看节点
kubectl get node
./kk delete node <nodeName> -f config-sample.yaml
标签选择器
1、打标签
#查看当前node
$ kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-elasticsearch Ready <none> 16h v1.21.0 172.16.66.167 <none> CentOS Linux 8 4.18.0-305.19.1.el8_4.x86_64 docker://20.10.9
k8s-master Ready control-plane,master 43h v1.21.0 172.16.66.169 <none> CentOS Linux 8 4.18.0-305.19.1.el8_4.x86_64 docker://20.10.9
k8s-node1 Ready <none> 43h v1.21.0 172.16.66.168 <none> CentOS Linux 8 4.18.0-305.19.1.el8_4.x86_64 docker://20.10.9
k8s-node2 Ready <none> 43h v1.21.0 172.16.66.170 <none> CentOS Linux 8 4.18.0-305.19.1.el8_4.x86_64 docker://20.10.9
#给k8s-node1 k8s-node2打上标签sign
$ kubectl label nodes k8s-node1 k8s-node2 type=sign
#查看type=websvr标签的node
$ kubectl get node -l type=websvr
NAME STATUS ROLES AGE VERSION
k8s-node1 Ready <none> 43h v1.21.0
k8s-node2 Ready <none> 43h v1.21.0
#以下附带标签的其他操作:
#修改标签
$ kubectl label nodes k8s-node1 k8s-node2 type=webtest --overwrite
#查看node标签
$ kubectl get nodes k8s-node1 k8s-node2 --show-labels
#删除标签
$ kubectl label nodes k8s-node1 k8s-node2 type-
2、插入选择器
apiVersion: apps/v1
kind: Deployment
metadata:
name: websvr1-deployment
spec:
selector:
matchLabels:
app: websvr1
replicas: 3
template:
metadata:
labels:
app: websvr1
spec:
nodeSelector: #选择标签为type:lables的node部署
type: lables
containers:
- name: websvr1
image: websvr:v1
ports:
- containerPort: 3000
kubesphere的存储卷是属于分布式存储,不用关心存储底层做了啥。但我们需要将数据存在当前服务器的文件目录下,可使用volume配置形式挂载
#直白的说, 就是挂载在磁盘上.
apiVersion: v1
kind: Pod
spec:
containers:
- name: test
image: nginx
# 定义进行挂载的数据卷
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
hostPath:
path: /usr/etc/nginx/config
上面将(服务器宿主机)本地的目录 /usr/etc/nginx/config
挂在到的容器的 /etc/config
上.
移动网络
dnsConfig:
nameservers:
- 114.114.114.114
options:
- name: ndots
value: '2'
- name: edns0
原因分析:顾名思义,一看就知道,https参数未设置,而本就不需要用https 解决方案:配置文件中的https注释掉,注释掉即可
原因分析:之前关闭防火墙之后docker没有重启, 解决方案:执行以下命令重启docker即可 service docker restart
原因分析:Docker Registry 交互默认使用的是 HTTPS,但是搭建私有镜像默认使用的是 HTTP 服务,所以与私有镜像交互时出现以下错误。 解决方案:docker系统服务添加安全进程
vi /etc/docker/daemon.json填入insecure-registries
:
{
"registry-mirrors": [
"https://sq9p56f6.mirror.aliyuncs.com"
],
"insecure-registries": ["192.168.239.24:8088"],
"exec-opts":["native.cgroupdriver=systemd"]
}
原因分析:通过
du -h --max-depth=1 /
逐级目录排查,发现/var/lib/docker目录文件过大 解决方案:转移数据修改docker默认存储位置 或者 搞一个外部存储
(1)转移数据修改docker默认存储位置
#停止docker服务
systemctl stop docker
#创建新的docker目录,执行命令df -h,找一个大的磁盘
mkdir -p /app/docker/lib
#迁移/var/lib/docker目录下面的文件到/app/docker/lib
rsync -avz /var/lib/docker/ /app/docker/lib/
#配置 /usr/lib/systemd/system/docker.service
vi /usr/lib/systemd/system/docker.service
#重启docker
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
(2)确认Docker Root Dir修改是否已经生效
[root@node24 docker]# docker info
...
Docker Root Dir: /app/docker/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
...
(3)确认之前的镜像是否还在
[root@master24 kk]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
perl latest f9596eddf06f 5 months ago 890MB
hello-world latest feb5d9fea6a5 8 months ago 13.3kB
192.168.239.24:8088/library/nginxdemos/hello plain-text 21dd11c8fb7a 8 months ago 22.9MB
nginxdemos/hello plain-text 21dd11c8fb7a 8 months ago 22.9MB
192.168.239.24:8088/library/kubesphere/edge-watcher v0.1.0 f3c1c017ccd5 8 months ago 47.8MB
kubesphere/edge-watcher v0.1.0 f3c1c017ccd5 8 months ago 47.8MB
(4) 确定容器没问题后删除/var/lib/docker/目录中的文件
rm -rf /var/lib/docker
docker里没有该镜像,pull一个然后给个标记推送至私服即可
(1)情况一
docker默认的源为国外官方源,下载速度较慢,改成国内镜像源
vi /etc/docker/daemon.json填入registry-mirrors
:
{
"registry-mirrors": [
"https://sq9p56f6.mirror.aliyuncs.com" #这是我自个阿里云的镜像加速器,你可去阿里弄个自己的
],
"insecure-registries": ["192.168.239.24:8088"],
"exec-opts":["native.cgroupdriver=systemd"]
}
(2)情况二
harbor出问题了,导致私服连接不上
(3)情况三
config-sample.yaml中配置的私服有问题,导致找不到对应的镜像
查看日志
journalctl -xeu kubelet
查看进度
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items0.metadata.name}') -f