根据 原文 结合阿里云改编。
前言 Introduction
Kubernetes是一个开源的容器编制系统。它允许您创建、更新和扩展容器,而无需担心停机。
要运行一个PHP应用程序,Nginx充当PHP- FPM的代理。将此设置装入一个容器可能是一个很麻烦的过程,但是Kubernetes可以帮助在分开的容器中管理这两个服务。使用Kubernetes将允许您保持容器的可重用性和可切换性,并且您不必每次有新版本的Nginx或PHP时都重新构建容器映像。
Step 1 — 创建 PHP-FPM 和 Nginx 服务
在这个步骤,你将创建 PHP-FPM 和 Nginx 服务,在这个集群里,一个服务可以访问一组 pods,集群里的服务可以直接通过他们的 名字 来通讯,不需要 IP 地址。PHP-FPM 服务可以访问 PHP-FPM 的pods,Nginx 服务可以访问 Nginx pods。
由于Nginx pods将代理PHP-FPM pods,您需要告诉服务如何找到它们。您将利用Kubernetes的自动服务发现,使用人类可读的名称将请求路由到服务而不是使用IP地址。
要创建服务,您将创建一个对象定义文件。每个Kubernetes对象定义都是一个YAML文件,其中至少包含以下内容:
apiVersion
: The version of the Kubernetes API that the definition belongs to.kind
: Kubernetes 对象. 例如, apod
orservice
.metadata
: This contains thename
of the object along with anylabels
that you may wish to apply to it.spec
: This contains a specific configuration depending on the kind of object you are creating, such as the container image or the ports on which the container will be accessible from.
首先,您将创建一个目录来保存Kubernetes对象定义。这里我们使用 阿里云 托管版Kubernetes。只需创建Worker节点,Master节点由容器服务创建并托管。具备简单、低成本、高可用、无需运维管理Kubernetes集群Master节点的特点,您可以更多关注业务本身。
mkdir definitions && cd definitions
创建 php_service.yaml
文件来定义 PHP-FPM 服务:
nano php_service.yaml
设置 kind
为 Service
...
apiVersion: v1
kind: Service
给这个服务取名 php
, 因为它将提供对PHP-FPM的访问:
...
metadata:
name: php
您将使用标签对不同的对象进行逻辑分组。使用标签将对象分组到“层”中,如前端或后端。PHP pods将运行在这个服务的后面,因此您将把它标记为tier:backend
...
labels:
tier: backend
决定一个服务去访问哪些 pods 是通过 selector 标签。 一个 pod 匹配这些标签将可以通过服务访问,这与pod是在服务之前还是之后创建的无关。您将在后面为pod添加标签。
给这个 pod 打上标签 tier: backend ,归类到 后端层组。你还可以 加上 app: php 标签来标记这个 pod 执行 PHP,将这两个标签添加到 metadata。
...
spec:
selector:
app: php
tier: backend
接下来,给这个服务一个访问端口。这儿使用 9000 端口:添加下面的设置到 spec 下面:
...
ports:
- protocol: TCP
port: 9000
你完整的 php_service.yaml
文件应该像这样:
apiVersion: v1
kind: Service
metadata:
name: php
labels:
tier: backend
spec:
selector:
app: php
tier: backend
ports:
- protocol: TCP
port: 9000
现在你可以用 kubectl apply
创建你的服务,使用 -f 指定要执行的文件
kubectl apply -f php_service.yaml
服务创建成功会返回:
Outputservice/php created
确认你的服务正在执行中:
kubectl get svc
你可以看到 PHP-FPM 的服务在正执行中:
OutputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m
php ClusterIP 10.100.59.238 <none> 9000/TCP 5m
There are various service types that Kubernetes supports. Your php
service uses the default service type, ClusterIP
. This service type assigns an internal IP and makes the service reachable only from within the cluster.
Now that the PHP-FPM service is ready, you will create the Nginx service. Create and open a new file called nginx_service.yaml
with the editor:
nano nginx_service.yaml
This service will target Nginx pods, so you will name it nginx
. You will also add a tier: backend
label as it belongs in the backend tier:nginx_service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
tier: backend
Similar to the php
service, target the pods with the selector labels app: nginx
and tier: backend
. Make this service accessible on port 80, the default HTTP port.nginx_service.yaml
...
spec:
selector:
app: nginx
tier: backend
ports:
- protocol: TCP
port: 80
The Nginx service will be publicly accessible to the internet from your Droplet’s public IP address. your_public_ip
can be found from your DigitalOcean Cloud Panel. Under spec.externalIPs
, add:nginx_service.yaml
...
spec:
externalIPs:
- your_public_ip
Your nginx_service.yaml
file will look like this:nginx_service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
tier: backend
spec:
selector:
app: nginx
tier: backend
ports:
- protocol: TCP
port: 80
externalIPs:
- your_public_ip
Save and close the file. Create the Nginx service:
kubectl apply -f nginx_service.yaml
You will see the following output when the service is running:
Outputservice/nginx created
You can view all running services by executing:
kubectl get svc
You will see both the PHP-FPM and Nginx services listed in the output:
OutputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13m
nginx ClusterIP 10.102.160.47 your_public_ip 80/TCP 50s
php ClusterIP 10.100.59.238 <none> 9000/TCP 8m
Please note, if you want to delete a service you can run:
kubectl delete svc/service_name
Now that you’ve created your PHP-FPM and Nginx services, you will need to specify where to store your application code and configuration files.
Step 2 — 有状态服务-动态云盘使用最佳实践
具体参考阿里云官方文档
你的 code_volume.yaml
文件会像这样:
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
name: alicloud-disk-ssd-hangzhou-b
provisioner: alicloud/disk
reclaimPolicy: Retain
parameters:
type: cloud_ssd
regionid: cn-hangzhou
zoneid: cn-hangzhou-b
fstype: "ext4"
readonly: "false"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: code
spec:
accessModes:
- ReadWriteOnce
storageClassName: alicloud-disk-ssd-hangzhou-b
resources:
requests:
storage: 20Gi
Step 3 — 创建 PHP-FPM 无状态应用
In this step, you will learn how to use a Deployment to create your PHP-FPM pod. Deployments provide a uniform way to create, update, and manage pods by using ReplicaSets. If an update does not work as expected, a Deployment will automatically rollback its pods to a previous image.
The Deployment spec.selector
key will list the labels of the pods it will manage. It will also use the template
key to create the required pods.
This step will also introduce the use of Init Containers. Init Containers run one or more commands before the regular containers specified under the pod’s template
key. In this tutorial, your Init Container will fetch a sample index.php
file from GitHub Gist using wget
. These are the contents of the sample file:index.php
<?php
echo phpinfo();
To create your Deployment, open a new file called php_deployment.yaml
with your editor:
创建一个新的文件 `php_deployment.yaml` 来定义 PHP 无状态(Deployment)应用
nano php_deployment.yaml
This Deployment will manage your PHP-FPM pods, so you will name the Deployment object php
. The pods belong to the backend tier, so you will group the Deployment into this group by using the tier: backend
label:
这个无状态应用管理你的 PHP-FPM 的 Pods,所以它的名字是 php。 pods 属于后太层面,所以你可以使用 tier: backend 给无状态应用一个分组。
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
labels:
tier: backend
For the Deployment spec
, you will specify how many copies of this pod to create by using the replicas
parameter. The number of replicas
will vary depending on your needs and available resources. You will create one replica in this tutorial:
无状态应用的 spec,你可以通过 replicas 定义这个应用需要创建多少个 pods。根据资源的需求给出相应的定义。
...
spec:
replicas: 1
This Deployment will manage pods that match the app: php
and tier: backend
labels. Under selector
key add:
这个无状态应用会管理有 app:php 和 tier: backend 标签的 pods。定义在 selector 下面:
...
selector:
matchLabels:
app: php
tier: backend
Next, the Deployment spec
requires the template
for your pod’s object definition. This template will define specifications to create the pod from. First, you will add the labels that were specified for the php
service selectors
and the Deployment’s matchLabels
. Add app: php
and tier: backend
under template.metadata.labels
:
接下来,无状态应用 的 spec
需要用 template
来定义你的 pod 对象。首先,需要增加一些 php 服务 和 无状态应用 matchLabels 指定的标签。在 template.metadata.labels 下添加 app: php
和 tier: backend
...
template:
metadata:
labels:
app: php
tier: backend
A pod can have multiple containers and volumes, but each will need a name. You can selectively mount volumes to a container by specifying a mount path for each volume.
一个 pod 可以拥有多个容器和存储卷,但每个都需要一个名字。通过为每个卷指定挂载路径,可以有选择地将卷挂载到容器。
First, specify the volumes that your containers will access. You created a PVC named code
to hold your application code, so name this volume code
as well. Under spec.template.spec.volumes
, add the following:
首先,指定容器将访问的卷。你可以创建一个 名叫 code 的 PVC 来存储你程序的代码, 使用给这个卷也取名 code,放在 spec.template.spec.volumes 下面:
...
spec:
volumes:
- name: code
persistentVolumeClaim:
claimName: code
Next, specify the container you want to run in this pod. You can find various images on the Docker store, but in this tutorial, you will use the php:7-fpm
image.
接下来,指定要在这个 pod 中运行的容器。您可以在Docker商店中找到各种镜像,但是在这儿将使用 php:7-fpm 镜像。
在 spec.template.spec.containers
下增加:
...
containers:
- name: php
image: php:7-fpm
Next, you will mount the volumes that the container requires access to. This container will run your PHP code, so it will need access to the code
volume. You will also use mountPath
to specify /code
as the mount point.
再次,你需要挂载这个容器需要访问的卷, 这个容器将要执行 PHP 代码,使用需要访问 code 卷,你需要使用 mountPath 到 /code 来指定挂载点。
在 spec.template.spec.containers.volumeMounts
添加:
...
volumeMounts:
- name: code
mountPath: /code
Now that you have mounted your volume, you need to get your application code on the volume. You may have previously used FTP/SFTP or cloned the code over an SSH connection to accomplish this, but this step will show you how to copy the code using an Init Container.
现在你已经挂载了存储卷,你需要吧你的代码放到存储卷里,你可以提前用 FTP/SFTP 或者 SSH 实现。这儿为直接使用 kubectl cp 来复制代码。
# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
# 复制本地 ./src 目录到远程默认命名空间的 PHP pod 的 /code 目录
kubectl cp ./src <php-pod>:/code
最终,你的 php_deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
labels:
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: php
tier: backend
template:
metadata:
labels:
app: php
tier: backend
spec:
volumes:
- name: code
persistentVolumeClaim:
claimName: code
containers:
- name: php
image: php:7-fpm
volumeMounts:
- name: code
mountPath: /code
Save the file and exit the editor.
Create the PHP-FPM Deployment with kubectl
:
kubectl apply -f php_deployment.yaml
You will see the following output upon Deployment creation:
Outputdeployment.apps/php created
To summarize, this Deployment will start by downloading the specified images. It will then request the PersistentVolume
from your PersistentVolumeClaim
and serially run your initContainers
. Once complete, the containers will run and mount the volumes
to the specified mount point. Once all of these steps are complete, your pod will be up and running.
You can view your Deployment by running:
kubectl get deployments
You will see the output:
OutputNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
php 1 1 1 0 19s
This output can help you understand the current state of the Deployment. A Deployment
is one of the controllers that maintains a desired state. The template
you created specifies that the DESIRED
state will have 1 replicas
of the pod named php
. The CURRENT
field indicates how many replicas are running, so this should match the DESIRED
state. You can read about the remaining fields in the Kubernetes Deployments documentation.
You can view the pods that this Deployment started with the following command:
kubectl get pods
The output of this command varies depending on how much time has passed since creating the Deployment. If you run it shortly after creation, the output will likely look like this:
OutputNAME READY STATUS RESTARTS AGE
php-86d59fd666-bf8zd 0/1 Init:0/1 0 9s
The columns represent the following information:
Ready
: The number ofreplicas
running this pod.Status
: The status of the pod.Init
indicates that the Init Containers are running. In this output, 0 out of 1 Init Containers have finished running.Restarts
: How many times this process has restarted to start the pod. This number will increase if any of your Init Containers fail. The Deployment will restart it until it reaches a desired state.
Depending on the complexity of your startup scripts, it can take a couple of minutes for the status to change to podInitializing
:
OutputNAME READY STATUS RESTARTS AGE
php-86d59fd666-lkwgn 0/1 podInitializing 0 39s
This means the Init Containers have finished and the containers are initializing. If you run the command when all of the containers are running, you will see the pod status change to Running
.
OutputNAME READY STATUS RESTARTS AGE
php-86d59fd666-lkwgn 1/1 Running 0 1m
You now see that your pod is running successfully. If your pod doesn’t start, you can debug with the following commands:
- View detailed information of a pod:
kubectl describe pods pod-name
- View logs generated by a pod:
kubectl logs pod-name
- View logs for a specific container in a pod:
kubectl logs pod-name container-name
Your application code is mounted and the PHP-FPM service is now ready to handle connections. You can now create your Nginx Deployment.
Step 4 — 创建 Nginx 无状态应用
In this step, you will use a ConfigMap to configure Nginx. A ConfigMap holds your configuration in a key-value format that you can reference in other Kubernetes object definitions. This approach will grant you the flexibility to reuse or swap the image with a different Nginx version if needed. Updating the ConfigMap will automatically replicate the changes to any pod mounting it.
Create a nginx_configMap.yaml
file for your ConfigMap with your editor:
nano nginx_configMap.yaml
Name the ConfigMap nginx-config
and group it into the tier: backend
micro-service:nginx_configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
tier: backend
Next, you will add the data
for the ConfigMap. Name the key config
and add the contents of your Nginx configuration file as the value. You can use the example Nginx configuration from this tutorial.
Because Kubernetes can route requests to the appropriate host for a service, you can enter the name of your PHP-FPM service in the fastcgi_pass
parameter instead of its IP address. Add the following to your nginx_configMap.yaml
file:nginx_configMap.yaml
...
data:
config : |
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root ^/code^;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Your nginx_configMap.yaml
file will look like this:nginx_configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
labels:
tier: backend
data:
config : |
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Save the file and exit the editor.
Create the ConfigMap:
kubectl apply -f nginx_configMap.yaml
You will see the following output:
Outputconfigmap/nginx-config created
You’ve finished creating your ConfigMap and can now build your Nginx Deployment.
Start by opening a new nginx_deployment.yaml
file in the editor:
nano nginx_deployment.yaml
Name the Deployment nginx
and add the label tier: backend
:nginx_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
Specify that you want one replicas
in the Deployment spec
. This Deployment will manage pods with labels app: nginx
and tier: backend
. Add the following parameters and values:nginx_deployment.yaml
...
spec:
replicas: 1
selector:
matchLabels:
app: nginx
tier: backend
Next, add the pod template
. You need to use the same labels that you added for the Deployment selector.matchLabels
. Add the following:nginx_deployment.yaml
...
template:
metadata:
labels:
app: nginx
tier: backend
Give Nginx access to the code
PVC that you created earlier. Under spec.template.spec.volumes
, add:nginx_deployment.yaml
...
spec:
volumes:
- name: code
persistentVolumeClaim:
claimName: code
Pods can mount a ConfigMap as a volume. Specifying a file name and key will create a file with its value as the content. To use the ConfigMap, set path
to name of the file that will hold the contents of the key
. You want to create a file site.conf
from the key config
. Under spec.template.spec.volumes
, add the following:nginx_deployment.yaml
...
- name: config
configMap:
name: nginx-config
items:
- key: config
path: site.conf
Warning: If a file is not specified, the contents of the key
will replace the mountPath
of the volume. This means that if a path is not explicitly specified, you will lose all content in the destination folder.
Next, you will specify the image to create your pod from. This tutorial will use the nginx:1.7.9
image for stability, but you can find other Nginx images on the Docker store. Also, make Nginx available on the port 80. Under spec.template.spec
add:nginx_deployment.yaml
...
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Nginx and PHP-FPM need to access the file at the same path, so mount the code
volume at /code
:nginx_deployment.yaml
...
volumeMounts:
- name: code
mountPath: /code
The nginx:1.7.9
image will automatically load any configuration files under the /etc/nginx/conf.d
directory. Mounting the config
volume in this directory will create the file /etc/nginx/conf.d/site.conf
. Under volumeMounts
add the following:nginx_deployment.yaml
...
- name: config
mountPath: /etc/nginx/conf.d
Your nginx_deployment.yaml
file will look like this:nginx_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: nginx
tier: backend
template:
metadata:
labels:
app: nginx
tier: backend
spec:
volumes:
- name: code
persistentVolumeClaim:
claimName: code
- name: config
configMap:
name: nginx-config
items:
- key: config
path: site.conf
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- name: code
mountPath: /code
- name: config
mountPath: /etc/nginx/conf.d
Save the file and exit the editor.
Create the Nginx Deployment:
kubectl apply -f nginx_deployment.yaml
The following output indicates that your Deployment is now created:
Outputdeployment.apps/nginx created
List your Deployments with this command:
kubectl get deployments
You will see the Nginx and PHP-FPM Deployments:
OutputNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 1 1 1 0 16s
php 1 1 1 1 7m
List the pods managed by both of the Deployments:
kubectl get pods
You will see the pods that are running:
OutputNAME READY STATUS RESTARTS AGE
nginx-7bf5476b6f-zppml 1/1 Running 0 32s
php-86d59fd666-lkwgn 1/1 Running 0 7m
Now that all of the Kubernetes objects are active, you can visit the Nginx service on your browser.
List the running services:
kubectl get services -o wide
Get the External IP for your Nginx service:
OutputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 39m <none>
nginx ClusterIP 10.102.160.47 your_public_ip 80/TCP 27m app=nginx,tier=backend
php ClusterIP 10.100.59.238 <none> 9000/TCP 34m app=php,tier=backend
On your browser, visit your server by typing in http://your_public_ip
. You will see the output of php_info()
and have confirmed that your Kubernetes services are up and running.
总结
In this guide, you containerized the PHP-FPM and Nginx services so that you can manage them independently. This approach will not only improve the scalability of your project as you grow, but will also allow you to efficiently use resources as well. You also stored your application code on a volume so that you can easily update your services in the future.