从零开始搭建Kubernetes - 即刻开始

如何帮助水土不服的Kubeadm?

从零开始搭建Kubernetes - 即刻开始

本文为“从零开始搭建Kubernetes”系列的第四篇。在理清全部物理机状况,完成大量虚拟机的迁移、以及数台物理机的虚拟化之后,终于可以开始部署Kubernetes了!

本次部署Kubernetes将使用四个物理机:1台作为控制面板(control panel),另外4台作为任务执行者(worker)。5台机器均安装了Ubuntu Server 20.04。部署工具将使用Kubeadm,这是Kubernetes官方推荐部署工具中的一种。确保用来部署的5台机器均可以访问互联网,然后正式开始部署工作。

准备工作

首先分别在5台物理机上设定好各自的主机名(hostname)。以control panel节点为例:

sudo hostnamectl set-hostname "k8smaster"

之后将各个节点的主机名与对应IP地址添加到所有主机的 /etc/hosts 文件中。

192.168.1.64 dz-1
192.168.1.66 dz-2
192.168.1.68 dz-3
192.168.1.70 dz-4
192.168.1.62 k8smaster

安装Kubeadm

Kubeadm官方安装文档在这里:

Installing kubeadm
This page shows how to install the kubeadm toolbox. For information on how to create a cluster with kubeadm once you have performed this installation process, see the Creating a cluster with kubeadm page. Before you begin A compatible Linux host. The Kubernetes project provides generic instructions…

正常情况下遵照官方指南一步一步操作即可完成安装。但是,如果你访问Google存在问题的话,需要格外注意一点——使用正确的sandbox镜像。

在安装文档的 Overriding the sandbox (pause) image 章节中,要求修改containerd的配置文件如下:

[plugins."io.containerd.grpc.v1.cri"]
  sandbox_image = "registry.k8s.io/pause:3.2"

官方文档中的沙箱镜像

其中 registry.k8s.io/pause:3.2 为沙箱镜像。注意由于k8s.io这个网址服务器在国外访问不便,可以将其替换为国内的阿里云镜像:

[plugins."io.containerd.grpc.v1.cri"]
  sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.8"

适配国内环境的沙箱镜像

containerd正常运行是Kubeadm的前提。如果这里的沙箱镜像无法获取可能导致kubectl报错 "error getting node"。

创建集群

一旦Kubeadm安装完成,即可使用它来构建集群。

创建集群的官方文档地址:

Creating a cluster with kubeadm
Using kubeadm, you can create a minimum viable Kubernetes cluster that conforms to best practices. In fact, you can use kubeadm to set up a cluster that will pass the Kubernetes Conformance tests. kubeadm also supports other cluster lifecycle functions, such as bootstrap tokens and cluster upgrades.…

正常情况下按照文档一步一步操作即可。同样,如果你访问Google存在问题的话,需要格外注意一点——使用正确的镜像仓库。

具体操作为:在执行 kubeadm init 指令时,使用 --image-repository=registry.aliyuncs.com/google_containers 参数指定国内的阿里云仓库。例如,在控制面板节点执行初始化操作:

sudo kubeadm init --v=5 --image-repository=registry.aliyuncs.com/google_containers --control-plane-endpoint=k8smaster

这条指令执行完毕kubernetes会打印加入到当前集群的指令。将其复制并在其它worker节点执行即可加入集群。

获取集群信息

完成集群创建以后,任意节点均可获取集群信息:

$ kubectl cluster-info
Kubernetes control plane is running at https://k8smaster:6443
CoreDNS is running at https://k8smaster:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

获取集群节点:

$ kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
dz25-310-170-035-1   Ready    <none>          30d   v1.25.4
dz25-310-170-035-2   Ready    <none>          37d   v1.25.4
dz25-310-170-035-3   Ready    <none>          30d   v1.25.4
dz25-310-170-035-4   Ready    <none>          30d   v1.25.4
k8smaster            Ready    control-plane   37d   v1.25.4

至此,Kubernetes集群搭建完成。

转发至

微信扫一扫分享

WeChat QR Code

有AI相关的想法,但不知道怎么落地?

我是国冰,独立AI开发者。十多年技术一线——半导体显示、智慧城市到自动驾驶;现在一个人+AI,接AI落地项目,从梳理需求到系统落地,陪你走通。

微信聊聊 微信二维码 看我能做什么