“开箱即用” 的 Kubernetes 集群
Kubernetes K8S
![](/data/attachment/album/201804/28/065439yadbvnowe5wdxrew.png)


这是我以前的 [10 分钟内配置 Kubernetes](https://www.youtube.com/watch?v=6xJwQgDnMFE) 教程的精简版和更新版。我删除了一些我认为可以去掉的内容，所以，这个指南仍然是通顺的。当你想在云上创建一个集群或者尽可能快地构建基础设施时，你可能会用到它。


### 1.0 挑选一个主机


我们在本指南中将使用 Ubuntu 16.04，这样你就可以直接拷贝/粘贴所有的指令。下面是我用本指南测试过的几种环境。根据你运行的主机，你可以从中挑选一个。


* [DigitalOcean](https://www.digitalocean.com/) - 开发者云
* [Civo](https://www.civo.com/) - UK 开发者云
* [Packet](https://packet.net/) - 裸机云
* 2x Dell Intel i7 服务器 —— 它在我家中



> 
> Civo 是一个相对较新的开发者云，我比较喜欢的一点是，它开机时间只有 25 秒，我就在英国，因此，它的延迟很低。
> 
> 
> 


### 1.1 准备机器


你可以使用一个单台主机进行测试，但是，我建议你至少使用三台机器，这样你就有一个主节点和两个工作节点。


下面是一些其他的指导原则：


* 最好选至少有 2 GB 内存的双核主机
* 在准备主机的时候，如果你可以自定义用户名，那么就不要使用 root。例如，Civo 通常让你在 `ubuntu`、`civo` 或者 `root` 中选一个。


现在，在每台机器上都运行以下的步骤。它将需要 5-10 钟时间。如果你觉得太慢了，你可以使用我[放在 Gist](https://gist.github.com/alexellis/e8bbec45c75ea38da5547746c0ca4b0c) 的脚本 ：



```
$ curl -sL https://gist.githubusercontent.com/alexellis/e8bbec45c75ea38da5547746c0ca4b0c/raw/23fc4cd13910eac646b13c4f8812bab3eeebab4c/configure.sh | sh

```

### 1.2 登入和安装 Docker


从 Ubuntu 的 apt 仓库中安装 Docker。它的版本可能有点老，但是，Kubernetes 在老版本的 Docker 中是测试过的，工作的很好。



```
$ sudo apt-get update \
  && sudo apt-get install -qy docker.io

```

### 1.3 禁用 swap 文件


这是 Kubernetes 的强制步骤。实现它很简单，编辑 `/etc/fstab` 文件，然后注释掉引用 swap 的行即可。


保存它，重启后输入 `sudo swapoff -a`。



> 
> 一开始就禁用 swap 内存，你可能觉得这个要求很奇怪，如果你对这个做法感到好奇，你可以去 [这里阅读它的相关内容](https://github.com/kubernetes/kubernetes/issues/53533)。
> 
> 
> 


### 1.4 安装 Kubernetes 包



```
$ sudo apt-get update \
  && sudo apt-get install -y apt-transport-https \
  && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" \
  | sudo tee -a /etc/apt/sources.list.d/kubernetes.list \
  && sudo apt-get update 

$ sudo apt-get update \
  && sudo apt-get install -y \
  kubelet \
  kubeadm \
  kubernetes-cni

```

### 1.5 创建集群


这时候，我们使用 `kubeadm` 初始化主节点并创建集群。这一步仅在主节点上操作。



> 
> 虽然有警告，但是 [Weaveworks](https://weave.works/) 和 Lucas（他们是维护者）向我保证，`kubeadm` 是可用于生产系统的。
> 
> 
> 



```
$ sudo kubeadm init

```

如果你错过一个步骤或者有问题，`kubeadm` 将会及时告诉你。


我们复制一份 Kube 配置：



```
mkdir -p $HOME/.kube  
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config  
sudo chown $(id -u):$(id -g) $HOME/.kube/config

```

确保你一定要记下如下的加入 `token` 的命令。



```
$ sudo kubeadm join --token c30633.d178035db2b4bb9a 10.0.0.5:6443 --discovery-token-ca-cert-hash sha256:<hash>

```

### 2.0 安装网络


许多网络提供商提供了 Kubernetes 支持，但是，默认情况下 Kubernetes 都没有包括。这里我们使用来自 [Weaveworks](https://weave.works/) 的 Weave Net，它是 Kebernetes 社区中非常流行的选择之一。它近乎不需要额外配置的 “开箱即用”。



```
$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

```

如果在你的主机上启用了私有网络，那么，你可能需要去修改 Weavenet 使用的私有子网络，以便于为 Pod（容器）分配 IP 地址。下面是命令示例：



```
$ curl -SL "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=172.16.6.64/27" \
| kubectl apply -f -

```


> 
> Weave 也有很酷的称为 Weave Cloud 的可视化工具。它是免费的，你可以在它上面看到你的 Pod 之间的路径流量。[这里有一个使用 OpenFaaS 项目的示例](https://www.weave.works/blog/openfaas-gke)。
> 
> 
> 


### 2.2 在集群中加入工作节点


现在，你可以切换到你的每一台工作节点，然后使用 1.5 节中的 `kubeadm join` 命令。运行完成后，登出那个工作节点。


### 3.0 收获


到此为止 —— 我们全部配置完成了。你现在有一个正在运行着的集群，你可以在它上面部署应用程序。如果你需要设置仪表板 UI，你可以去参考 [Kubernetes 文档](https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/)。



```
$ kubectl get nodes
NAME        STATUS    ROLES     AGE       VERSION  
openfaas1   Ready     master    20m       v1.9.2  
openfaas2   Ready     <none>    19m       v1.9.2  
openfaas3   Ready     <none>    19m       v1.9.2  

```

如果你想看到我一步一步创建集群并且展示 `kubectl` 如何工作的视频，你可以看下面我的视频，你可以订阅它。


你也可以在你的 Mac 电脑上，使用 Minikube 或者 Docker 的 Mac Edge 版本，安装一个 “开箱即用” 的 Kubernetes 集群。[阅读在这里的我的评估和第一印象](https://blog.alexellis.io/docker-for-mac-with-kubernetes/)。




---


via: <https://blog.alexellis.io/your-instant-kubernetes-cluster/>


作者：[Alex Ellis](https://blog.alexellis.io/author/alex/) 译者：[qhwdw](https://github.com/qhwdw) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
This is a condensed and updated version of my previous tutorial [Kubernetes in 10 minutes](https://www.youtube.com/watch?v=6xJwQgDnMFE). I've removed just about everything I can so this guide still makes sense. Use it when you want to create a cluster on the cloud or on-premises as fast as possible.

## 1.0 Pick a host

We will be using Ubuntu 16.04 for this guide so that you can copy/paste all the instructions. Here are several environments where I've tested this guide. Just pick where you want to run your hosts.

[DigitalOcean](https://www.digitalocean.com)- developer cloud[Civo](https://www.civo.com)- UK developer cloud[Equinix Metal](https://metal.equinix.com/)- bare metal cloud- 2x Dell Intel i7 boxes - at home

Civo is a relatively new developer cloud and one thing that I really liked was how quickly they can bring up hosts - in about 25 seconds. I'm based in the UK so I also get very low latency.


## 1.1 Provision the machines

You can get away with a single host for testing but I'd recommend at least three so we have a single master and two worker nodes.

Here are some other guidelines:

- Pick dual-core hosts with ideally at least 2GB RAM
- If you can pick a custom username when provisioning the host then do that rather than root. For example Civo offers an option of
`ubuntu`

,`civo`

or`root`

.

Now run through the following steps on each machine. It should take you less than 5-10 minutes. If that's too slow for you then you can use my utility script [kept in a Gist](https://gist.github.com/alexellis/e8bbec45c75ea38da5547746c0ca4b0c):

```
$ curl -sL https://gist.githubusercontent.com/alexellis/e8bbec45c75ea38da5547746c0ca4b0c/raw/23fc4cd13910eac646b13c4f8812bab3eeebab4c/configure.sh | sh
```


## 1.2 Login and install Docker

Install Docker from the Ubuntu apt repository. This will be an older version of Docker but as Kubernetes is tested with old versions of Docker it will work in our favour.

```
$ sudo apt-get update \
&& sudo apt-get install -qy docker.io
```


## 1.3 Disable the swap file

This is now a mandatory step for Kubernetes. The easiest way to do this is to edit `/etc/fstab`

and to comment out the line referring to swap.

To save a reboot then type in `sudo swapoff -a`

.

Disabling swap memory may appear like a strange requirement at first. If you are curious about this step then

[read more here].

## 1.4 Install Kubernetes packages

```
$ sudo apt-get update \
&& sudo apt-get install -y apt-transport-https \
&& curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" \
| sudo tee -a /etc/apt/sources.list.d/kubernetes.list \
&& sudo apt-get update
$ sudo apt-get update \
&& sudo apt-get install -y \
kubelet \
kubeadm \
kubernetes-cni
```


## 1.5 Create the cluster

At this point we create the cluster by initiating the master with `kubeadm`

. Only do this on the master node.

Despite any warnings I have been assured by

[Weaveworks]and Lucas (the maintainer) that`kubeadm`

is suitable for production use.

```
$ sudo kubeadm init
```


If you missed a step or there's a problem then `kubeadm`

will let you know at this point.

Take a copy of the Kube config:

```
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
```


Make sure you note down the join token command i.e.

```
$ sudo kubeadm join --token c30633.d178035db2b4bb9a 10.0.0.5:6443 --discovery-token-ca-cert-hash sha256:<hash>
```


## 2.0 Install networking

Many networking providers are available for Kubernetes, but none are included by default, so let's use Weave Net from [Weaveworks](https://weave.works) which is one of the most popular options in the Kubernetes community. It tends to work out of the box without additional configuration.

```
$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
```


If you have private networking enabled on your host then you may need to alter the private subnet that Weavenet uses for allocating IP addresses to Pods (containers). Here's an example of how to do that:

```
$ curl -SL "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')&env.IPALLOC_RANGE=172.16.6.64/27" \
| kubectl apply -f -
```


Weave also have a very cool visualisation tool called Weave Cloud. It's free and will show you the path traffic is taking between your Pods.

[See here for an example with the OpenFaaS project].

## 2.2 Join the worker nodes to the cluster

Now you can switch to each of your workers and use the `kubeadm join`

command from 1.5. Once you run that log out of the workers.

## 3.0 Profit

That's it - we're done. You have a cluster up and running and can deploy your applications. If you need to setup a dashboard UI then consult the [Kubernetes documentation](https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/).

```
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
openfaas1 Ready master 20m v1.9.2
openfaas2 Ready <none> 19m v1.9.2
openfaas3 Ready <none> 19m v1.9.2
```


If you want to see my running through creating a cluster step-by-step and showing you how `kubectl`

works then checkout my video below and make sure you subscribe

You can also get an "instant" Kubernetes cluster on your Mac for development using Minikube or Docker for Mac Edge edition. [Read my review and first impressions here](https://blog.alexellis.io/docker-for-mac-with-kubernetes/).

## 4.0 Keep learning

You can keep learning and get head with weekly emails from me as part of a GitHub Sponsorship. Sign up below to learn more about Kubernetes, Raspberry Pi and Docker from me.