1. 通过KubeKey去安装K8S和KubeSphere
- 安装流程参考KubeSphere官网提供的教程:https://www.kubesphere.io/zh/docs/v4.1/03-installation-and-upgrade/02-install-kubesphere/02-install-kubernetes-and-kubesphere/
- K8S文档:https://kubernetes.io/zh-cn/docs/home/。
- KubeKey的文档Github地址:https://github.com/kubesphere/kubekey/blob/master/README_zh-CN.md。
- KubeKey的Github地址:https://github.com:kubesphere/kubekey.git
1.1 所有K8S节点安装提前执行操作
首先,在安装KubeKey之前,我们需要在K8S的所有节点上安装一些必须的依赖,比如K8S集群有三台节点node1
、node2
、node3
,需要在这三台机器上同时执行。
sudo apt install socat conntrack ebtables ipset -y
下面是安装K8S过程中,需要开放的一些防火墙端口,对于云服务器,则需要在云厂商的控制台当中放行下面这些端口。
|services|protocol|action|start port|end port|comment
|—|—|—|—|—|—|
ssh|TCP|allow|22|
etcd|TCP|allow|2379|2380|
apiserver|TCP|allow|6443|
calico|TCP|allow|9099|9100|
bgp|TCP|allow|179||
nodeport|TCP|allow|30000|32767|
master|TCP|allow|10250|10258|
dns|TCP|allow|53|
dns|UDP|allow|53|
local-registry|TCP|allow|5000||offline environment|
local-apt|TCP|allow|5080||offline environment|
rpcbind|TCP|allow|111|| use NFS
ipip|IPENCAP / IPIP|allow| | |calico needs to allow the ipip protocol
上面的操作都需要在所有的节点上进行操作,除了nodeport的30000-32767的端口,需要允许所有的IP访问之外,其他的端口只需要内网访问(同一个安全组)即可。
对于下面提到的操作,都只需要在K8S集群的Master节点上执行就行。
1.2 安装KubeKey(简称KK)
我们如下的命令去下载KubeKey,并给KK加上执行权限。
curl -sfL https://get-kk.kubesphere.io | sh -
sudo chmod +x kk
在给KK加上执行权限之后,我们可以通过如下的命令,去生成一个KK的config-sample配置文件,其中v1.31.2是要安装的K8S版本,可以把它换成你想要安装的K8S版本。
./kk create config --with-kubernetes v1.31.2
1.3 使用KK生成模板文件并完成自定义配置
使用kk create config
命令之后,会生成下面这样的config-sample.yml
的配置文件
kind: Cluster
metadata:
name: sample
spec:
hosts:
- {name: node1, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: "Qcloud@123"}
- {name: node2, address: 172.16.0.3, internalAddress: 172.16.0.3, user: ubuntu, password: "Qcloud@123"}
roleGroups:
etcd:
- node1
control-plane:
- node1
worker:
- node1
- node2
controlPlaneEndpoint:
## Internal loadbalancer for apiservers
# internalLoadbalancer: haproxy
domain: lb.kubesphere.local
address: ""
port: 6443
kubernetes:
version: v1.31.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
multusCNI:
enabled: false
registry:
privateRegistry: ""
namespaceOverride: ""
registryMirrors: []
insecureRegistries: []
addons: []
需要注意的是:默认情况,我们使用containerd作为容器运行时,不再使用docker作为容器运行时,在K8S的高版本1.24+,就已经默认使用containerd作为默认的容器运行时。如果需要使用docker,可以参照KubeKey官网的文档进行自定义配置
其中,我们一定需要修改的地方是下面这些:
spec:
hosts:
- {name: node1, address: 172.16.0.2, internalAddress: 172.16.0.2, user: ubuntu, password: "Qcloud@123"}
- {name: node2, address: 172.16.0.3, internalAddress: 172.16.0.3, user: ubuntu, password: "Qcloud@123"}
roleGroups:
etcd:
- node1
control-plane:
- node1
worker:
- node1
- node2
对于spec.hosts
配置,主要是配置K8S集群的各个节点,name
用于指定节点的名称,address和internalAdress用于指定节点的IP地址,内网的情况,配置成为一样就行,user
和password
(如果是使用SSH私钥的方式,需要指定user
和privateKeyPath
参数,privateKeyPath
指定私钥存放的位置)是登录该目标服务器的密码的情况,因为KK需要登录目标服务器去完成K8S集群的自动配置。
spec:
hosts:
- {name: node1, address: 10.0.0.95, internalAddress: 10.0.0.95, user: root, password: "..."}
- {name: node2, address: 10.0.0.97, internalAddress: 10.0.0.97, user: root, password: "..."}
- {name: node3, address: 10.0.0.152, internalAddress: 10.0.0.152, user: root, password: "..."}
roleGroups当中的相关配置说明:
roleGroups.control-plane
用于指定K8S集群的控制平面需要安装在哪个节点,也就是指定K8S集群的Master节点。roleGroups.ectd
用于指定K8S集群的控制平面中的Etcd需要安装在哪个节点(通常是Master节点,也可以独立部署到其他节点)。roleGroups.worker
则用于配置K8S集群的Worker节点(Node节点)。Worker节点其实对应的也就是用于K8S进行Pod的调度的机器,比如我希望K8S管理的Pod,可以从node1和node2两台机器上去启动容器,那么我就在这里指定worker
为node1和node2。
roleGroups:
etcd:
- node1
control-plane:
- node1
worker:
- node1
- node2
- node3
关于KK的更多配置参考
https://www.kubesphere.io/zh/docs/v4.1/03-installation-and-upgrade/02-install-kubesphere/02-install-kubernetes-and-kubesphere/
1.4 使用配置的KubeKey配置文件创建K8S集群
新创建K8S集群
接着,我们使用刚刚的KK的配置文件,基于KK去创建K8S集群。
我们首先添加KKZONE
环境变量指定区域为中国,不然可能因为Github和Google的限制导致网速慢,还会下载失败。
export KKZONE=cn
指定好区域之后,我们就可以使用下面的命令,基于KubeKey
去创建K8S集群。
# 创建K8S集群
./kk create cluster -f config-sample.yaml
在这个过程中,KK会自动下载K8S需要的各种依赖。当看到下面的提示语之后,说明我们的K8S集群已经创建好。
19:13:36 CST Pipeline[CreateClusterPipeline] execute successfully
Installation is complete.
Please check the result using the command:
kubectl get pod -A
在主节点执行如下命令,可以查看我们创建的K8S集群运行的POD情况。
kubectl get pod -A
命令执行结果如下,我们可以看到K8S集群的核心组件都已经跑起来。
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-6d7dfcb56d-5hd47 1/1 Running 0 58s
kube-system calico-node-fjnn6 1/1 Running 0 58s
kube-system calico-node-lzmh5 1/1 Running 0 58s
kube-system calico-node-tbpgg 1/1 Running 0 58s
kube-system coredns-76b9f7d479-2x5jt 1/1 Running 0 67s
kube-system coredns-76b9f7d479-jcrhv 1/1 Running 0 67s
kube-system kube-apiserver-node1 1/1 Running 0 83s
kube-system kube-controller-manager-node1 1/1 Running 0 85s
kube-system kube-proxy-dd4nc 1/1 Running 0 67s
kube-system kube-proxy-fwnm2 1/1 Running 0 59s
kube-system kube-proxy-pscwm 1/1 Running 0 61s
kube-system kube-scheduler-node1 1/1 Running 0 84s
kube-system nodelocaldns-4dw6n 1/1 Running 0 67s
kube-system nodelocaldns-4mngt 1/1 Running 0 59s
kube-system nodelocaldns-9cmkj 1/1 Running 0 61s
已经有K8S集群新增Node节点
在K8S集群新增K8S节点,直接在KubeKey的配置配置文件当中新增node,并配置在worker当中。
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
name: sample
spec:
hosts:
- {name: node1, address: 10.0.0.95, internalAddress: 10.0.0.95, user: root, password: "..."}
- {name: node2, address: 10.0.0.97, internalAddress: 10.0.0.97, user: root, password: "..."}
- {name: node3, address: 10.0.0.152, internalAddress: 10.0.0.152, user: root, password: "..."}
- {name: node4, address: 10.0.0.166, internalAddress: 10.0.0.166, user: root, password: "..."}
roleGroups:
etcd:
- node1
control-plane:
- node1
worker:
- node1
- node2
- node3
- node4
接着使用kubekey执行一下如下命令,就可以添加Node进来。
./kk add nodes -f config-sample.yaml
问题点1:命令执行成功之后,新节点可能因为dns和kubeproxy拉取镜像时有问题,导致出现有可能遇到如下的问题:
Failed to create pod sandbox: rpc error: code = Unknown desc = failed to get sandbox image "kubesphere/pause:3.9": failed to pull image "kubesphere/pause:3.9": failed to pull and unpack image "docker.io/kubesphere/pause:3.9": failed to resolve reference "docker.io/kubesphere/pause:3.9": failed to do request: Head "https://registry-1.docker.io/v2/kubesphere/pause/manifests/3.9": dial tcp 130.211.15.150:443: connect: connection refused
说明,新加的这台机器,无法拉取到镜像docker.io/kubesphere/pause:3.9
,此时可以找一台可以拉取外部镜像的机器,并打包再上传到这台Node机器上。
# 拉取镜像
docker pull kubesphere/pause:3.9
# 打包镜像成为压缩包
docker save -o pause.tar kubesphere/pause:3.9
接着,将pause.tar上传到新增节点的机器。
# 将镜像解压缩并上传到containerd的本地仓库
ctr --namespace=k8s.io images import pause.tar
# 验证pause镜像是否已经被我们上传成功
ctr images list | grep pause
上传成功,之后这个Node节点上的dns和kubeproxy相关的Pod,将会自动完成重建。
问题点2,提示crictl没安装。
pull image failed: Failed to exec command: sudo -E /bin/bash -c "env PATH=$PATH crictl pull registry.cn-beijing.aliyuncs.com/kubesphereio/pause:3.9 --platform amd64"
env: 'crictl': No such file or directory: Process exited with status 127
依次执行如下的命令,去安装crictl
,再重新执行./kk add nodes -f config-sample.yaml
即可。
curl -LO https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.31.1/crictl-v1.31.1-linux-amd64.tar.gz
tar -zxvf crictl-*.tar.gz
sudo mv crictl /usr/local/bin/
crictl --version
已经有K8S集群删除节点
指定nodeName后,通过kk delete
命令即可快速删除节点。
./kk delete node {node-name} -f config-sample.yaml
# example
./kk delete node node02 -f config-sample.yaml
1.5 安装KubeSphere管理页面
可以使用下面的命令,基于Helm的方式去安装KubeSphere。
helm upgrade --install -n kubesphere-system --create-namespace ks-core https://charts.kubesphere.io/main/ks-core-1.1.3.tgz --debug --wait
如果访问DockerHub出现问题,下载镜像失败,可以在上面的命令的基础上加上如下的参数信息去配置镜像仓库:
--set global.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks
--set extension.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks
完整命令如下:
helm upgrade --install -n kubesphere-system --create-namespace ks-core https://charts.kubesphere.io/main/ks-core-1.1.3.tgz --debug --wait --set global.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks --set extension.imageRegistry=swr.cn-southwest-2.myhuaweicloud.com/ks
如果遇到https://charts.kubesphere.io/main/ks-core-1.1.3.tgz
无法访问的情况,可以将文件下载到本地并上传到服务器上进行执行。
如果出现如下的提示信息,则说明KubeSphere安装成功。
NOTES:
Thank you for choosing KubeSphere Helm Chart.
Please be patient and wait for several seconds for the KubeSphere deployment to complete.
1. Wait for Deployment Completion
Confirm that all KubeSphere components are running by executing the following command:
kubectl get pods -n kubesphere-system
2. Access the KubeSphere Console
Once the deployment is complete, you can access the KubeSphere console using the following URL:
http://10.0.0.95:30880
3. Login to KubeSphere Console
Use the following credentials to log in:
Account: admin
Password: P@88w0rd
NOTE: It is highly recommended to change the default password immediately after the first login.
For additional information and details, please visit https://kubesphere.io.
提示你可以在30880端口访问页面,通过访问Node的30880端口,可以进入KubeSphere管理页面,通过账号admin
和密码P@88w0rd
可以登录KubeSphere,登录后需要及时修改KubeSphere的管理员密码。
登录KubeSphere之后是如下的页面,选择集群管理,去管理我们现有的K8S集群。
下面是KubeSphere的后台管理页面,可以管理整个K8S集群的工作负载、存储PV/PVC、配置ConfigMap/Secret等,可以基于页面的方式去简单创建K8S相关的资源。
可以使用如下的命令,卸载KubeSphere。
helm -n kubesphere-system uninstall ks-core
评论