使用Redis部署PHP留言板应用程序

1. 使用Redis部署PHP留言板应用程序

这个实例是Kubernetes官网的实例,地址:

https://kubernetes.io/zh/docs/tutorials/stateless-application/guestbook/

本教程向您展示如何使用 Kubernetes 和 Docker 构建和部署 一个简单的_(非面向生产)的_多层 web 应用程序。本例由以下组件组成:

  • 单实例 Redis 以保存留言板条目
  • 多个 web 前端实例

1.1. 教程目标

  • 启动 Redis 领导者(Leader)
  • 启动两个 Redis 跟随者(Follower)
  • 公开并查看前端服务
  • 清理

1.2. 准备开始

你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 Minikube 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

您的 Kubernetes 服务器版本必须不低于版本 v1.14. 要获知版本信息,请输入 kubectl version.

1.3. 启动 Redis 数据库

留言板应用程序使用 Redis 存储数据。

1.3.1. 创建 Redis Deployment

下面包含的清单文件指定了一个 Deployment 控制器,该控制器运行一个 Redis Pod 副本。

application/guestbook/redis-leader-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: leader
tier: backend
spec:
containers:
- name: leader
image: "docker.io/redis:6.0.5"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
  1. 在下载清单文件的目录中启动终端窗口。

  2. redis-leader-deployment.yaml 文件中应用 Redis Deployment:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/redis-leader-deployment.yaml
  3. 查询 Pod 列表以验证 Redis Pod 是否正在运行:

    1
    kubectl get pods

    响应应该与此类似:

    1
    2
    NAME                           READY   STATUS    RESTARTS   AGE
    redis-leader-fb76b4755-xjr2n 1/1 Running 0 13s
  4. 运行以下命令查看 Redis Deployment 中的日志:

    1
    kubectl logs -f deployment/redis-leader

1.3.2. 创建 Redis 领导者服务

留言板应用程序需要往 Redis 中写数据。因此,需要创建 Service 来转发 Redis Pod 的流量。Service 定义了访问 Pod 的策略。

application/guestbook/redis-leader-service.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: leader
tier: backend
  1. 使用下面的 redis-leader-service.yaml 文件创建 Redis的服务:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/redis-leader-service.yaml
  2. 查询服务列表验证 Redis 服务是否正在运行:

    1
    kubectl get service

    响应应该与此类似:

    1
    2
    3
    NAME           TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
    kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 1m
    redis-leader ClusterIP 10.103.78.24 <none> 6379/TCP 16s

说明: 这个清单文件创建了一个名为 redis-leader 的 Service,其中包含一组 与前面定义的标签匹配的标签,因此服务将网络流量路由到 Redis Pod 上。

1.3.3. 设置 Redis 跟随者

尽管 Redis 领导者只有一个 Pod,你可以通过添加若干 Redis 跟随者来将其配置为高可用状态, 以满足流量需求。

application/guestbook/redis-follower-deployment.yaml Copy application/guestbook/redis-follower-deployment.yaml to clipboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: follower
tier: backend
spec:
containers:
- name: follower
image: heyuze/gb-redis-follower:v2
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

注意:这里image官方的镜像拉取不到,我将官方镜像放到我的docker镜像仓库中,如果这里想使用官方的路径,可以将我的镜像拉取到本地后使用如下命令修改:

docker tag heyuze/gb-redis-follower:v2 gcr.io/google_samples/gb-redis-follower:v2

  1. 应用下面的 redis-follower-deployment.yaml 文件创建 Redis Deployment:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/redis-follower-deployment.yaml
  2. 通过查询 Pods 列表,验证两个 Redis 跟随者副本在运行:

    1
    kubectl get pods

    响应应该类似于这样:

    1
    2
    3
    4
    NAME                             READY   STATUS    RESTARTS   AGE
    redis-follower-dddfbdcc9-82sfr 1/1 Running 0 37s
    redis-follower-dddfbdcc9-qrt5k 1/1 Running 0 38s
    redis-leader-fb76b4755-xjr2n 1/1 Running 0 11m

1.3.4. 创建 Redis 跟随者服务

Guestbook 应用需要与 Redis 跟随者通信以读取数据。 为了让 Redis 跟随者可被发现,你必须创建另一个 Service

application/guestbook/redis-follower-service.yaml Copy application/guestbook/redis-follower-service.yaml to clipboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
ports:
# the port that this service should serve on
- port: 6379
selector:
app: redis
role: follower
tier: backend
  1. 应用如下所示 redis-follower-service.yaml 文件中的 Redis Service:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/redis-follower-service.yaml
  2. 查询 Service 列表,验证 Redis 服务在运行:

    1
    kubectl get service

    响应应该类似于这样:

    1
    2
    3
    4
    NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d19h
    redis-follower ClusterIP 10.110.162.42 <none> 6379/TCP 9s
    redis-leader ClusterIP 10.103.78.24 <none> 6379/TCP 6m10s

说明:

清单文件创建了一个名为 redis-follower 的 Service,该 Service 具有一些与之前所定义的标签相匹配的标签,因此该 Service 能够将网络流量 路由到 Redis Pod 之上。

1.4. 设置并公开留言板前端

现在你有了一个为 Guestbook 应用配置的 Redis 存储处于运行状态, 接下来可以启动 Guestbook 的 Web 服务器了。 与 Redis 跟随者类似,前端也是使用 Kubernetes Deployment 来部署的。

Guestbook 应用使用 PHP 前端。该前端被配置成与后端的 Redis 跟随者或者 领导者服务通信,具体选择哪个服务取决于请求是读操作还是写操作。 前端对外暴露一个 JSON 接口,并提供基于 jQuery-Ajax 的用户体验。

1.4.1. 创建 Guestbook 前端 Deployment

application/guestbook/frontend-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v5
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80

注意:这里也是image官方的镜像拉取不到,我将官方镜像放到我的docker镜像仓库中,如果这里想使用官方的路径,可以将我的镜像拉取到本地后使用如下命令修改:

docker tag heyuze/gb-frontend:v5 gcr.io/google_samples/gb-frontend:v5

  1. 应用来自 frontend-deployment.yaml 文件的前端 Deployment:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml
  2. 查询 Pod 列表,验证三个前端副本正在运行:

    1
    kubectl get pods -l app=guestbook -l tier=frontend

    响应应该与此类似:

    1
    2
    3
    4
    NAME                        READY   STATUS    RESTARTS   AGE
    frontend-85595f5bf9-5tqhb 1/1 Running 0 47s
    frontend-85595f5bf9-qbzwm 1/1 Running 0 47s
    frontend-85595f5bf9-zchwc 1/1 Running 0 47s

1.4.2. 创建前端服务

应用的 Redis 服务只能在 Kubernetes 集群中访问,因为服务的默认类型是 ClusterIPClusterIP 为服务指向的 Pod 集提供一个 IP 地址。这个 IP 地址只能在集群中访问。

如果你希望访客能够访问你的 Guestbook,你必须将前端服务配置为外部可见的, 以便客户端可以从 Kubernetes 集群之外请求服务。 然而即便使用了 ClusterIP,Kubernetes 用户仍可以通过 kubectl port-forward 访问服务。

说明: 一些云提供商,如 Google Compute Engine 或 Google Kubernetes Engine, 支持外部负载均衡器。如果你的云提供商支持负载均衡器,并且你希望使用它, 只需取消注释 type: LoadBalancer

application/guestbook/frontend-service.yaml Copy application/guestbook/frontend-service.yaml to clipboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
#type: LoadBalancer
ports:
# the port that this service should serve on
- port: 80
selector:
app: guestbook
tier: frontend
  1. 应用来自 frontend-service.yaml 文件中的前端服务:

    1
    kubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml
  2. 查询 Service 列表以验证前端服务正在运行:

    1
    kubectl get services

    响应应该与此类似:

    1
    2
    3
    4
    5
    NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    frontend ClusterIP 10.97.28.230 <none> 80/TCP 19s
    kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d19h
    redis-follower ClusterIP 10.110.162.42 <none> 6379/TCP 5m48s
    redis-leader ClusterIP 10.103.78.24 <none> 6379/TCP 11m

1.4.3. 通过 kubectl port-forward 查看前端服务

  1. 运行以下命令将本机的 8080 端口转发到服务的 80 端口。

    1
    kubectl port-forward svc/frontend 8080:80

    响应应该与此类似:

    1
    2
    Forwarding from 127.0.0.1:8080 -> 80
    Forwarding from [::1]:8080 -> 80
  2. 在浏览器中加载 http://localhost:8080 页面以查看 Guestbook。

1.4.4. 通过 LoadBalancer 查看前端服务

如果你部署了 frontend-service.yaml,需要找到用来查看 Guestbook 的 IP 地址。

  1. 运行以下命令以获取前端服务的 IP 地址。

    1
    kubectl get service frontend

    响应应该与此类似:

    1
    2
    NAME       TYPE           CLUSTER-IP      EXTERNAL-IP        PORT(S)        AGE
    frontend LoadBalancer 10.51.242.136 109.197.92.229 80:32372/TCP 1m
  2. 复制这里的外部 IP 地址,然后在浏览器中加载页面以查看留言板。

说明:

尝试通过输入消息并点击 Submit 来添加一些留言板条目。 你所输入的消息会在前端显示。这一消息表明数据被通过你 之前所创建的 Service 添加到 Redis 存储中。

1.5. 扩展 Web 前端

你可以根据需要执行伸缩操作,这是因为服务器本身被定义为使用一个 Deployment 控制器的 Service。

  1. 运行以下命令扩展前端 Pod 的数量:

    1
    kubectl scale deployment frontend --replicas=5
  2. 查询 Pod 列表验证正在运行的前端 Pod 的数量:

    1
    kubectl get pods

    响应应该类似于这样:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    NAME                             READY   STATUS    RESTARTS   AGE
    frontend-85595f5bf9-5df5m 1/1 Running 0 83s
    frontend-85595f5bf9-7zmg5 1/1 Running 0 83s
    frontend-85595f5bf9-cpskg 1/1 Running 0 15m
    frontend-85595f5bf9-l2l54 1/1 Running 0 14m
    frontend-85595f5bf9-l9c8z 1/1 Running 0 14m
    redis-follower-dddfbdcc9-82sfr 1/1 Running 0 97m
    redis-follower-dddfbdcc9-qrt5k 1/1 Running 0 97m
    redis-leader-fb76b4755-xjr2n 1/1 Running 0 108m
  3. 运行以下命令缩小前端 Pod 的数量:

    1
    kubectl scale deployment frontend --replicas=2
  4. 查询 Pod 列表验证正在运行的前端 Pod 的数量:

    1
    kubectl get pods

    响应应该类似于这样:

    1
    2
    3
    4
    5
    6
    NAME                             READY   STATUS    RESTARTS   AGE
    frontend-85595f5bf9-cpskg 1/1 Running 0 16m
    frontend-85595f5bf9-l9c8z 1/1 Running 0 15m
    redis-follower-dddfbdcc9-82sfr 1/1 Running 0 98m
    redis-follower-dddfbdcc9-qrt5k 1/1 Running 0 98m
    redis-leader-fb76b4755-xjr2n 1/1 Running 0 109m

1.6. 清理现场

删除 Deployments 和服务还会删除正在运行的 Pod。 使用标签用一个命令删除多个资源。

  1. 运行以下命令以删除所有 Pod,Deployments 和 Services。

    1
    2
    3
    4
    kubectl delete deployment -l app=redis
    kubectl delete service -l app=redis
    kubectl delete deployment frontend
    kubectl delete service frontend

    响应应该是:

    1
    2
    3
    4
    deployment.apps "redis-follower" deleted
    deployment.apps "redis-leader" deleted
    deployment.apps "frontend" deleted
    service "frontend" deleted
  2. 查询 Pod 列表,确认没有 Pod 在运行:

    1
    kubectl get pods

    响应应该是:

    1
    No resources found in default namespace.

1.7. 接下来

-------------本文结束感谢您的阅读-------------