Merge pull request #6060 from StdioA/master

20170822 Running WordPress in a Kubernetes Cluster
This commit is contained in:
Chang Liu 2017-09-14 21:26:21 +08:00 committed by GitHub
commit 990b9f76d3
2 changed files with 187 additions and 190 deletions

View File

@ -1,190 +0,0 @@
translating by StdioA
Running WordPress in a Kubernetes Cluster
============================================================
![](https://cdn.deliciousbrains.com/content/uploads/2017/08/10131149/db-WPKubernetesCluster-1440x699.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][8], used for hosting web apps at scale. Im not technically a devops person but as I build web apps on a daily basis its 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)][9], [Docker Swarm][10] and [Apache Mesos][11]. If you want a good intro to each of these technologies and their differences I recommend giving [this article][12] a read.
In this article, were 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][13] have a great interactive tutorial that covers a lot of this stuff but for the purpose of this article Im just going to cover installation and usage on macOS.
The first thing we need to do is install Kubernetes on your local machine. Were going to use a tool called [Minikube][14] 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][15] (known as `kubectl`). If you use Homebrew this is as simple as running:
```
$ brew install kubectl
```
Now we can actually [install Minikube][16]:
```
$ 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][17] which will create a virtual machine which will act as our single-node Kubernetes cluster. At this point I should state that, although were running things locally in this article, most of the following concepts will apply when running a full Kubernetes cluster on [real servers][18]. On a multi-node cluster a “master” node would be responsible for managing the other worker nodes (VMs 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][19] to be a good introduction to Kubernetes concepts and terms if you want to start playing around.
While we could set things up manually, were actually going to use a separate tool to install our WordPress application to our Kubernetes cluster. [Helm][20] 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 lets 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][21] 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][22] 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 dont have a load balancer for our cluster, we wouldnt 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. Lets check the status of our deployments:
```
$ kubectl get deployments --namespace=wordpress
```
[![kubectl get deployments](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image4.png)][23]
We should see that we have 2 deployments, one for the Mariadb database and one for WordPress itself. Now lets 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:
[![kubectl get deployments](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120710/image2.png)][24]
Youve 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][25].
### 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
```
[![kubectl get pods](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image3.png)][26]
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.
[![kubectl get pods](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120709/image1.png)][27]
### Going Further
Weve 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:
* [Horizontal scaling][2]
* [Self healing][3]
* [Automated rollouts and rollbacks][4]
* [Secret management][5]
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.
--------------------------------------------------------------------------------
作者简介:
Gilbert loves to build software. From jQuery scripts to WordPress plugins to full blown SaaS apps, Gilbert has been creating elegant software his whole career. Probably most famous for creating the Nivo Slider.
--------
via: https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
作者:[ Gilbert Pellegrom][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://deliciousbrains.com/author/gilbert-pellegrom/
[1]:https://deliciousbrains.com/author/gilbert-pellegrom/
[2]:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
[3]:https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
[4]:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#what-is-a-deployment
[5]:https://kubernetes.io/docs/concepts/configuration/secret/
[6]:https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
[7]:https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
[8]:http://www.zdnet.com/article/what-is-docker-and-why-is-it-so-darn-popular/
[9]:https://kubernetes.io/
[10]:https://docs.docker.com/engine/swarm/
[11]:http://mesos.apache.org/
[12]:https://mesosphere.com/blog/docker-vs-kubernetes-vs-apache-mesos/
[13]:https://kubernetes.io/docs/tutorials/kubernetes-basics/
[14]:https://kubernetes.io/docs/getting-started-guides/minikube/
[15]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
[16]:https://github.com/kubernetes/minikube/releases
[17]:https://kubernetes.io/docs/getting-started-guides/minikube/#quickstart
[18]:https://kubernetes.io/docs/tutorials/kubernetes-basics/
[19]:http://kubernetesbyexample.com/
[20]:https://docs.helm.sh/
[21]:https://kubeapps.com/charts/stable/wordpress
[22]:https://kubernetes.io/docs/tutorials/kubernetes-basics/explore-intro/
[23]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image4.png
[24]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120710/image2.png
[25]:https://kubernetes.io/docs/tutorials/kubernetes-basics/scale-intro/
[26]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image3.png
[27]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120709/image1.png

View File

@ -0,0 +1,187 @@
在 Kubernetes 集群中运行 WordPress
============================================================
![](https://cdn.deliciousbrains.com/content/uploads/2017/08/10131149/db-WPKubernetesCluster-1440x699.jpg)
作为一名开发者,我会尝试留意那些我可能不会每天使用的技术进步。了解这些技术至关重要,因为它们可能会间接影响到我的工作。比如[由 Docker 推动][8]的,近期正在兴起的容器化技术,可用于托管 Web 应用,并方便地调整其规模。从技术层面来讲,我并不是一个 DevOps但当我每天构建 Web 应用时,多去留意这些技术如何去发展,会对我有所裨益。
这种进步的一个绝佳的例子,是近一段时间高速发展的容器编排平台。它允许你轻松地部署、管理容器化应用,并对它们的规模进行调整。目前看来,容器编排的流行工具有 [Kubernetes (来自 Google][9][Docker Swarm][10] 和 [Apache Mesos][11]。如果你想较好的了解上面那些技术以及它们的区别,我推荐你看一下[这篇文章][12]。
在这片文章中,我们将会从一些简单的操作开始,看一看 Kubernetes 平台,将一个 WordPress 网站部署在本地机器上的一个单节点集群中。
### 安装 Kubernetes
在 [Kubernetes 文档][13]中有一个很好的互动教程,涵盖了很多东西。但出于本文的目的,我只会介绍在 MacOS 中Kuberentes 的安装和使用。
我们要做的第一件事是在你的本地主机中安装 Kubernetes. 我们将使用一个叫做 [MiniKube][14] 的工具,它专门用于在你的机器上方便地设置一个 Kubernetes 集群用于测试。
根据 Minikube 文档,在我们开始之前,有一些先决条件。首先要保证你已经安装了一个 Hypervisor (我将会使用 Virtualbox。接下来我们需要[安装 Kubernetes 命令行工具][15](也就是 `kubectl`)。如果你在用 Homebrew这一步非常简单只需要运行命令
```
$ brew install kubectl
```
现在我们可以真正 [安装 Minikube][16] 了:
```
$ 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][17],创建一个虚拟机,来作为我们的单节点 Kubernetes 集群。现在我要说一点:尽管我们在本文中只在本地运行它,但是在[真正的服务器][18]上运行 Kubernetes 集群时,后面提到的大多数概念都会适用。在多节点集群上,“主节点”将负责管理其它工作节点(虚拟机或物理服务器),并且 Kubernetes 将会在集群中自动进行容器的分发和调度。
```
$ minikube start --vm-driver=virtualbox
```
### 安装 Helm
现在本机中应该有一个正在运行的单节点Kubernetes 集群了。我们现在可以用任何方式来与 Kubernetes 交互。如果你想现在可以玩它,我觉得 [kubernetesbyexample.com][19] 会很好地想你介绍 Kubernetes 的概念和术语。
虽然我们可以手动配置这些东西,但实际上我们将会使用另外的工具,来将我们的 WordPress 应用部署到 Kubernetes 集群中。[Helm][20] 被称为“Kubernetes 的包管理工具”,它可以让你轻松地在你的集群中部署预构建的软件包,也就是“图表”。你可以把图表看做一组专为特定应用(如 WordPress而设计的容器定义和配置。首先我们在本地主机上安装 Helm
```
$ brew install kubernetes-helm
```
然后我们需要在集群中安装 Helm. 幸运的是,只需要运行下面的命令就好:
```
$ helm init
```
### 安装 WordPress
现在 Helm 已经在我们的集群中运行了,我们可以安装 [WordPress 图表][21]。运行:
```
$ helm install --namespace wordpress --name wordpress --set serviceType=NodePort stable/wordpress
```
这条命令将会在容器中运行 WordPress并在容器中运行 MariaDB 作为数据库。它在 Kubernetes 中被称为“Pod”。一个 [Pod][22] 基本上可视为一个或多个应用程序容器和一些这些容器的共享资源(例如存储卷,网络等)的组合的抽象。
我们需要给这个部署一个名字和一个命名空间,以保证他们有序,而且方便查找。我们同样会给 `NodePort` 设置 `serviceType`。这一步非常重要,因为在默认设置中,服务类型会被设置为 `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](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image4.png)][23]
可以看到,我们有两个部署,一个是 Mariadb 数据库,一个是 WordPress 本身。现在,我们假设你的 WordPress 开始承载大量的流量,所以我们想将这些负载分摊在多个实例上。我们可以通过一个简单的命令来扩展 `wordpress-wordpress` 部署:
```
$ kubectl scale --replicas 2 deployments wordpress-wordpress --namespace=wordpress
```
再次运行 `kubectl get deployments`,我们现在应该会看到下面的场景:
[![kubectl get deployments](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120710/image2.png)][24]
你刚刚扩大了你的 WordPress 站点规模!超级简单,对不对?现在我们有多个 WordPress 容器,可以在他们之中对流量进行负载均衡。想了解 Kubernetes 扩展的更多信息,参见[这篇指南][25]。
### 高可用
Kubernetes 等平台的的另一大特色在于,它不单单能进行方便的扩展,还可以通过自愈组建来提供高可用性。假设我们的 WordPress 部署因为某些原因失效了,那 Kubernetes 会立刻自动替换掉这个部署。我们可以通过删除我们 WordPress 部署的一个 pod 来模拟这个过程。
首先运行命令,获取 pod 列表:
```
$ kubectl get pods --namespace=wordpress
```
[![kubectl get pods](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image3.png)][26]
然后删除其中一个 pod
```
$ kubectl delete pod {POD-ID} --namespace=wordpress
```
如果你再次运行 `kubectl get pods` 命令,应该会看到 Kubernetes 立刻换上了新的 pod。
[![kubectl get pods](https://cdn.deliciousbrains.com/content/uploads/2017/08/07120709/image1.png)][27]
### 更进一步
我们只是简单了解了 Kubernetes 能完成工作的表面。如果你想深入研究,我建议你查看以下功能:
* [平行扩展][2]
* [自动恢复][3]
* [自动更新及回滚][4]
* [密钥管理][5]
你在容器平台上运行过 WordPress 吗?有没有使用过 Kubernetes或其它容器编排平台有没有什么好的提示你通常会怎么扩展你的 WordPress 站点?请在评论中告诉我们。
--------------------------------------------------------------------------------
作者简介:
Gilbert 喜欢构建软件。从 jQuery 脚本到 WordPress 插件,再到完整的 SaaS 应用程序Gilbert 一直在创造优雅的软件。 他粗昂做的最有名的的产品,应该是 Nivo Slider.
--------
via: https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
作者:[ Gilbert Pellegrom][a]
译者:[StdioA](https://github.com/StdioA)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://deliciousbrains.com/author/gilbert-pellegrom/
[1]:https://deliciousbrains.com/author/gilbert-pellegrom/
[2]:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
[3]:https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
[4]:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#what-is-a-deployment
[5]:https://kubernetes.io/docs/concepts/configuration/secret/
[6]:https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
[7]:https://deliciousbrains.com/running-wordpress-kubernetes-cluster/
[8]:http://www.zdnet.com/article/what-is-docker-and-why-is-it-so-darn-popular/
[9]:https://kubernetes.io/
[10]:https://docs.docker.com/engine/swarm/
[11]:http://mesos.apache.org/
[12]:https://mesosphere.com/blog/docker-vs-kubernetes-vs-apache-mesos/
[13]:https://kubernetes.io/docs/tutorials/kubernetes-basics/
[14]:https://kubernetes.io/docs/getting-started-guides/minikube/
[15]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
[16]:https://github.com/kubernetes/minikube/releases
[17]:https://kubernetes.io/docs/getting-started-guides/minikube/#quickstart
[18]:https://kubernetes.io/docs/tutorials/kubernetes-basics/
[19]:http://kubernetesbyexample.com/
[20]:https://docs.helm.sh/
[21]:https://kubeapps.com/charts/stable/wordpress
[22]:https://kubernetes.io/docs/tutorials/kubernetes-basics/explore-intro/
[23]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image4.png
[24]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120710/image2.png
[25]:https://kubernetes.io/docs/tutorials/kubernetes-basics/scale-intro/
[26]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120711/image3.png
[27]:https://cdn.deliciousbrains.com/content/uploads/2017/08/07120709/image1.png