在 Kubernetes 集群中运行 WordPress
Kubernetes WordPress 容器
![](/data/attachment/album/201709/19/092435z39irasw73b99rqb.jpg)


作为一名开发者，我会尝试留意那些我可能不会每天使用的技术的进步。了解这些技术至关重要，因为它们可能会间接影响到我的工作。比如[由 Docker 推动](http://www.zdnet.com/article/what-is-docker-and-why-is-it-so-darn-popular/)的、近期正在兴起的容器化技术，可用于上规模地托管 Web 应用。从技术层面来讲，我并不是一个 DevOps，但当我每天构建 Web 应用时，多去留意这些技术如何去发展，会对我有所裨益。


这种进步的一个绝佳的例子，是近一段时间高速发展的容器编排平台。它允许你轻松地部署、管理容器化应用，并对它们的规模进行调整。目前看来，容器编排的流行工具有 [Kubernetes （来自 Google）](https://kubernetes.io/)，[Docker Swarm](https://docs.docker.com/engine/swarm/) 和 [Apache Mesos](http://mesos.apache.org/)。如果你想较好的了解上面那些技术以及它们的区别，我推荐你看一下[这篇文章](https://mesosphere.com/blog/docker-vs-kubernetes-vs-apache-mesos/)。


在这篇文章中，我们将会从一些简单的操作开始，了解一下 Kubernetes 平台，看看如何将一个 WordPress 网站部署在本地机器上的一个单节点集群中。


### 安装 Kubernetes


在 [Kubernetes 文档](https://kubernetes.io/docs/tutorials/kubernetes-basics/)中有一个很好的互动教程，涵盖了很多东西。但出于本文的目的，我只会介绍在 MacOS 中 Kuberentes 的安装和使用。


我们要做的第一件事是在你的本地主机中安装 Kubernetes。我们将使用一个叫做 [MiniKube](https://kubernetes.io/docs/getting-started-guides/minikube/) 的工具，它专门用于在你的机器上方便地设置一个用于测试的 Kubernetes 集群。


根据 Minikube 文档，在我们开始之前，有一些先决条件。首先要保证你已经安装了一个 Hypervisor （我将会使用 Virtualbox）。接下来，我们需要[安装 Kubernetes 命令行工具](https://kubernetes.io/docs/tasks/tools/install-kubectl/)（也就是 `kubectl`）。如果你在用 Homebrew，这一步非常简单，只需要运行命令：



```
$ brew install kubectl

```

现在我们可以真正 [安装 Minikube](https://github.com/kubernetes/minikube/releases) 了：



```
$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.21.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

```

最后，我们要[启动 Minicube](https://kubernetes.io/docs/getting-started-guides/minikube/#quickstart) 创建一个虚拟机，来作为我们的单节点 Kubernetes 集群。现在我要说一点：尽管我们在本文中只在本地运行它，但是在[真正的服务器](https://kubernetes.io/docs/tutorials/kubernetes-basics/)上运行 Kubernetes 集群时，后面提到的大多数概念都会适用。在多节点集群上，“主节点”将负责管理其它工作节点（虚拟机或物理服务器），并且 Kubernetes 将会在集群中自动进行容器的分发和调度。



```
$ minikube start --vm-driver=virtualbox

```

### 安装 Helm


现在，本机中应该有一个正在运行的（单节点）Kubernetes 集群了。我们现在可以用任何方式来与 Kubernetes 交互。如果你想现在可以体验一下，我觉得 [kubernetesbyexample.com](http://kubernetesbyexample.com/) 可以很好地向你介绍 Kubernetes 的概念和术语。


虽然我们可以手动配置这些东西，但实际上我们将会使用另外的工具，来将我们的 WordPress 应用部署到 Kubernetes 集群中。[Helm](https://docs.helm.sh/) 被称为“Kubernetes 的包管理工具”，它可以让你轻松地在你的集群中部署预构建的软件包，也就是“<ruby> 图表 <rt>  chart </rt></ruby>”。你可以把图表看做一组专为特定应用（如 WordPress）而设计的容器定义和配置。首先我们在本地主机上安装 Helm：



```
$ brew install kubernetes-helm

```

然后我们需要在集群中安装 Helm。 幸运的是，只需要运行下面的命令就好：



```
$ helm init

```

### 安装 WordPress


现在 Helm 已经在我们的集群中运行了，我们可以安装 [WordPress 图表](https://kubeapps.com/charts/stable/wordpress)。运行：



```
$ helm install --namespace wordpress --name wordpress --set serviceType=NodePort stable/wordpress  

```

这条命令将会在容器中安装并运行 WordPress，并在容器中运行 MariaDB 作为数据库。它在 Kubernetes 中被称为“Pod”。一个 [Pod](https://kubernetes.io/docs/tutorials/kubernetes-basics/explore-intro/) 基本上可视为一个或多个应用程序容器和这些容器的一些共享资源（例如存储卷，网络等）的组合的抽象。


我们需要给这个部署一个名字和一个命名空间，以将它们组织起来并便于查找。我们同样会将 `serviceType` 设置为 `NodePort` 。这一步非常重要，因为在默认设置中，服务类型会被设置为 `LoadBalancer`。由于我们的集群现在没有负载均衡器，所以我们将无法在集群外访问我们的 WordPress 站点。


在输出数据的最后一部分，你会注意到一些关于访问你的 WordPress 站点的有用的命令。运行那些命令，你可以获取到我们的 WordPress 站点的外部 IP 地址和端口：



```
$ export NODE_PORT=$(kubectl get --namespace wordpress -o jsonpath="{.spec.ports[0].nodePort}" services wordpress-wordpress)
$ export NODE_IP=$(kubectl get nodes --namespace wordpress -o jsonpath="{.items[0].status.addresses[0].address}")
$ echo http://$NODE_IP:$NODE_PORT/admin

```

你现在访问刚刚生成的 URL（忽略 `/admin` 部分），就可以看到 WordPress 已经在你的 Kubernetes 集群中运行了！


### 扩展 WordPress


Kubernetes 等服务编排平台的一个伟大之处，在于它将应用的扩展和管理变得易如反掌。我们看一下应用的部署状态：



```
$ kubectl get deployments --namespace=wordpress

```

[![kubectl get deployments](/data/attachment/album/201709/19/092437fu4o5jx5u7oj0f0f.png)](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image4.png)


可以看到，我们有两个部署，一个是 Mariadb 数据库，一个是 WordPress 本身。现在，我们假设你的 WordPress 开始承载大量的流量，所以我们想将这些负载分摊在多个实例上。我们可以通过一个简单的命令来扩展 `wordpress-wordpress` 部署：



```
$ kubectl scale --replicas 2 deployments wordpress-wordpress --namespace=wordpress

```

再次运行 `kubectl get deployments`，我们现在应该会看到下面的场景：


[![kubectl get deployments](/data/attachment/album/201709/19/092438t1cz2uppzo3uc51b.png)](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120710/image2.png)


你刚刚扩大了你的 WordPress 站点规模！超级简单，对不对？现在我们有了多个 WordPress 容器，可以在它们之中对流量进行负载均衡。想了解 Kubernetes 扩展的更多信息，参见[这篇指南](https://kubernetes.io/docs/tutorials/kubernetes-basics/scale-intro/)。


### 高可用


Kubernetes 等平台的的另一大特色在于，它不单单能进行方便的扩展，还可以通过自愈组件来提供高可用性。假设我们的一个 WordPress 部署因为某些原因失效了，那 Kubernetes 会立刻自动替换掉这个部署。我们可以通过删除我们 WordPress 部署的一个 pod 来模拟这个过程。


首先运行命令，获取 pod 列表：



```
$ kubectl get pods --namespace=wordpress

```

[![kubectl get pods](/data/attachment/album/201709/19/092440iosh45bz5swv33wb.png)](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image3.png)


然后删除其中一个 pod：



```
$ kubectl delete pod wordpress-wordpress-876183909-jqc8s --namespace=wordpress

```

如果你再次运行 `kubectl get pods` 命令，应该会看到 Kubernetes 立刻换上了新的 pod （`3l167`）。


[![kubectl get pods](/data/attachment/album/201709/19/092442eg1ncufxyfuxcckk.png)](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120709/image1.png)


### 更进一步


我们只是简单了解了 Kubernetes 能完成工作的表面。如果你想深入研究，我建议你查看以下功能：


* [平行扩展](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)
* [自愈](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller)
* [自动更新及回滚](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#what-is-a-deployment)
* [密钥管理](https://kubernetes.io/docs/concepts/configuration/secret/)


你在容器平台上运行过 WordPress 吗？有没有使用过 Kubernetes（或其它容器编排平台），有没有什么好的技巧？你通常会怎么扩展你的 WordPress 站点？请在评论中告诉我们。




---


作者简介：


Gilbert 喜欢构建软件。从 jQuery 脚本到 WordPress 插件，再到完整的 SaaS 应用程序，Gilbert 一直在创造优雅的软件。 他粗昂做的最有名的的产品，应该是 Nivo Slider.




---


via: <https://deliciousbrains.com/running-wordpress-kubernetes-cluster/>


作者：[Gilbert Pellegrom](https://deliciousbrains.com/author/gilbert-pellegrom/) 译者：[StdioA](https://github.com/StdioA) 校对：[wxy](https://github.com/wxy)


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


OK
![](https://cdn.deliciousbrains.com/content/uploads/2017/08/10131149/db-WPKubernetesCluster-1540x748.jpg)

As a developer I try to keep my eye on the progression of technologies that I might not use every day, but are important to understand as they might indirectly affect my work. For example the recent rise of containerization, [popularized by Docker](http://www.zdnet.com/article/what-is-docker-and-why-is-it-so-darn-popular/), used for hosting web apps at scale. I’m not technically a devops person but as I build web apps on a daily basis it’s good for me to keep my eye on how these technologies are progressing.

A good example of this progression is the rapid development of container orchestration platforms that allow you to easily deploy, scale and manage containerized applications. The main players at the moment seem to be [Kubernetes (by Google)](https://kubernetes.io/), [Docker Swarm](https://docs.docker.com/engine/swarm/) and [Apache Mesos](http://mesos.apache.org/). If you want a good intro to each of these technologies and their differences I recommend giving [this article](https://mesosphere.com/blog/docker-vs-kubernetes-vs-apache-mesos/) a read.

In this article, we’re going to start simple and take a look at the Kubernetes platform and how you can set up a WordPress site on a single node cluster on your local machine.

## Installing Kubernetes

The [Kubernetes docs](https://kubernetes.io/docs/tutorials/kubernetes-basics/) have a great interactive tutorial that covers a lot of this stuff but for the purpose of this article I’m just going to cover installation and usage on macOS.

The first thing we need to do is install Kubernetes on your local machine. We’re going to use a tool called [Minikube](https://kubernetes.io/docs/getting-started-guides/minikube/) which is specifically designed to make it easy to set up a Kubernetes cluster on your local machine for testing.

As per the Minikube docs, there are a few prerequisites before we get going. Make sure you have a Hypervisor installed (‘m going to use Virtualbox). Next we need to [install the Kubernetes command-line tool](https://kubernetes.io/docs/tasks/tools/install-kubectl/) (known as `kubectl`

). If you use Homebrew this is as simple as running:

```
$ brew install kubectl
```


Now we can actually [install Minikube](https://github.com/kubernetes/minikube/releases):

```
$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.21.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
```


Finally we want to [start Minikube](https://kubernetes.io/docs/getting-started-guides/minikube/#quickstart) which will create a virtual machine which will act as our single-node Kubernetes cluster. At this point I should state that, although we’re running things locally in this article, most of the following concepts will apply when running a full Kubernetes cluster on [real servers](https://kubernetes.io/docs/tutorials/kubernetes-basics/). On a multi-node cluster a “master” node would be responsible for managing the other worker nodes (VM’s or physical servers) and Kubernetes would automate the distribution and scheduling of application containers across the cluster.

```
$ minikube start --vm-driver=virtualbox
```


## Installing Helm

At this point we should now have a (single node) Kubernetes cluster running on our local machine. We can now interact with Kubernetes in any way we want. I found [kubernetesbyexample.com](http://kubernetesbyexample.com/) to be a good introduction to Kubernetes concepts and terms if you want to start playing around.

While we could set things up manually, we’re actually going to use a separate tool to install our WordPress application to our Kubernetes cluster. [Helm](https://docs.helm.sh/) is labelled as a “package manager for Kubernetes” and works by allowing you to easily deploy pre-built software packages to your cluster, known as “Charts”. You can think of a Chart as a group of container definitions and configs that are designed for a specific application (such as WordPress). First let’s install Helm on our local machine:

```
$ brew install kubernetes-helm
```


Next we need to install Helm on our cluster. Thankfully this is as simple as running:

```
$ helm init
```


## Installing WordPress

Now that Helm is running on our cluster we can install the [WordPress chart](https://kubeapps.com/charts/stable/wordpress) by running:

```
$ helm install --namespace wordpress --name wordpress --set serviceType=NodePort stable/wordpress
```


The will install and run WordPress in a container and MariaDB in a container for the database. This is known as a “Pod” in Kubernetes. A [Pod](https://kubernetes.io/docs/tutorials/kubernetes-basics/explore-intro/) is basically an abstraction that represents a group of one or more application containers and some shared resources for those containers (e.g. storage volumes, networking etc.).

We give the release a namespace and a name to keep things organized and make them easy to find. We also set the `serviceType`

to `NodePort`

. This is important because, by default, the service type will be set to `LoadBalancer`

and, as we currently don’t have a load balancer for our cluster, we wouldn’t be able to access our WordPress site from outside the cluster.

In the last part of the output from this command you will notice some helpful instructions on how to access your WordPress site. Run these commands to get the external IP address and port for our WordPress site:

```
$ export NODE_PORT=$(kubectl get --namespace wordpress -o jsonpath="{.spec.ports[0].nodePort}" services wordpress-wordpress)
$ export NODE_IP=$(kubectl get nodes --namespace wordpress -o jsonpath="{.items[0].status.addresses[0].address}")
$ echo http://$NODE_IP:$NODE_PORT/admin
```


You should now be able to visit the resulting URL (ignoring the `/admin`

bit) and see WordPress running on your very own Kubernetes cluster!

## Scaling WordPress

One of the great things about container orchestration platforms such as Kubernetes is that it makes scaling and managing your application really simple. Let’s check the status of our deployments:

```
$ kubectl get deployments --namespace=wordpress
```


We should see that we have 2 deployments, one for the Mariadb database and one for WordPress itself. Now let’s say your WordPress site is starting to see a lot of traffic and we want to split the load over multiple instances. We can scale our `wordpress-wordpress`

deployment by running a simple command:

```
$ kubectl scale --replicas 2 deployments wordpress-wordpress --namespace=wordpress
```


If we run the `kubectl get deployments`

command again we should now see something like this:

You’ve just scaled up your WordPress site! Easy peasy, right? There are now multiple WordPress containers that traffic can be load-balanced across. For more info on Kubernetes scaling check out [this tutorial](https://kubernetes.io/docs/tutorials/kubernetes-basics/scale-intro/).

## High Availability

Another great feature of platforms such as Kubernetes is the ability to not only scale easily, but to provide high availability by implementing self-healing components. Say one of your WordPress deployments fails for some reason. Kubernetes will automatically replace the deployment instantly. We can simulate this by deleting one of the pods running in our WordPress deployment.

First get a list of pods by running:

```
$ kubectl get pods --namespace=wordpress
```


Then delete one of the pods:

```
$ kubectl delete pod {POD-ID} --namespace=wordpress
```


If you run the `kubectl get pods`

command again you should see Kubernetes spinning up the replacement pod straight away.

## Going Further

We’ve only really scratched the surface of what Kubernetes can do. If you want to delve a bit deeper, I would recommend having a look at some of the following features:

Have you ever run WordPress on a container platform? Have you ever used Kubernetes (or another container orchestration platform) and got any good tips? How do you normally scale your WordPress sites? Let us know in the comments.