> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 从零开始搭建Kubernetes - 即刻开始
- URL: https://yinguobing.com/blog/setup-kubernetes-from-zero-jump-right-in/
- Published: 2022-12-28T09:00:56.000Z
- Updated: 2026-09-08T02:07:50.000Z
- Description: 如何帮助水土不服的Kubeadm？
- Author: 尹国冰
- Tags: Kubernetes, 新手入门

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

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

### 准备工作

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

```bash
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 kubeadmThis 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…![](https://kubernetes.io/images/kubernetes-192x192.png)Kubernetes![](https://kubernetes.io/images/kubernetes-horizontal-color.png)](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/?ref=yinguobing.com)

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

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

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

官方文档中的沙箱镜像

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

```yaml
[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 kubeadmUsing 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.…![](https://kubernetes.io/images/kubernetes-192x192.png)Kubernetes![](https://kubernetes.io/images/kubernetes-horizontal-color.png)](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/?ref=yinguobing.com)

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

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

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

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

### 获取集群信息

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

```bash
$ 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'.
```

获取集群节点：

```bash
$ 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集群搭建完成。