示例:使用 Redis 部署 PHP Guestbook 应用程序
本教程展示了如何使用 Kubernetes 和 Docker 构建和部署一个简单的(非生产环境可用)多层 Web 应用程序。此示例包含以下组件:
- 一个单实例 Redis 用于存储留言条目。
- 多个 Web 前端实例。
目标
- 启动一个 Redis leader。
- 启动两个 Redis follower。
- 启动留言簿前端。
- 公开并查看前端服务。
- 清理。
准备工作
你需要一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与你的集群通信。建议在至少有两个不是控制平面主机的节点的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用这些 Kubernetes 游乐场之一:
你的 Kubernetes 服务器版本必须是 v1.14 或更高。要检查版本,请输入 kubectl version
。
启动 Redis 数据库
留言簿应用程序使用 Redis 来存储其数据。
创建 Redis Deployment
下面包含的清单文件指定了一个 Deployment 控制器,它运行一个单副本 Redis Pod。
# 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
在你下载清单文件的目录中启动一个终端窗口。
从
redis-leader-deployment.yaml
文件应用 Redis Deploymentkubectl apply -f https://k8s.io/examples/application/guestbook/redis-leader-deployment.yaml
查询 Pod 列表以验证 Redis Pod 是否正在运行
kubectl get pods
响应应类似于以下内容:
NAME READY STATUS RESTARTS AGE redis-leader-fb76b4755-xjr2n 1/1 Running 0 13s
运行以下命令查看 Redis leader Pod 的日志
kubectl logs -f deployment/redis-leader
创建 Redis leader 服务
留言簿应用程序需要与 Redis 通信以写入数据。你需要应用一个 Service 来代理到 Redis Pod 的流量。Service 定义了访问 Pod 的策略。
# 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
从以下
redis-leader-service.yaml
文件应用 Redis Servicekubectl apply -f https://k8s.io/examples/application/guestbook/redis-leader-service.yaml
查询 Service 列表以验证 Redis Service 是否正在运行
kubectl get service
响应应类似于以下内容:
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,其标签集与之前定义的标签匹配,因此 Service 将网络流量路由到 Redis Pod。设置 Redis follower
尽管 Redis leader 是一个单一的 Pod,但你可以通过添加一些 Redis follower(或副本)来使其高可用并满足流量需求。
# 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: us-docker.pkg.dev/google-samples/containers/gke/gb-redis-follower:v2
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
从以下
redis-follower-deployment.yaml
文件应用 Redis Deploymentkubectl apply -f https://k8s.io/examples/application/guestbook/redis-follower-deployment.yaml
通过查询 Pod 列表来验证两个 Redis follower 副本是否正在运行
kubectl get pods
响应应类似于以下内容:
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
创建 Redis follower 服务
留言簿应用程序需要与 Redis follower 通信以读取数据。为了使 Redis follower 可被发现,你必须设置另一个 Service。
# 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
从以下
redis-follower-service.yaml
文件应用 Redis Servicekubectl apply -f https://k8s.io/examples/application/guestbook/redis-follower-service.yaml
查询 Service 列表以验证 Redis Service 是否正在运行
kubectl get service
响应应类似于以下内容:
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 将网络流量路由到 Redis Pod。设置并公开留言簿前端
现在你已经启动并运行了留言簿的 Redis 存储,接下来启动留言簿 Web 服务器。与 Redis follower 一样,前端也使用 Kubernetes Deployment 进行部署。
留言簿应用程序使用 PHP 前端。它配置为与 Redis follower 或 leader 服务通信,具体取决于请求是读取还是写入。前端公开了一个 JSON 接口,并提供了一个基于 jQuery-Ajax 的用户体验。
创建留言簿前端 Deployment
# 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: us-docker.pkg.dev/google-samples/containers/gke/gb-frontend:v5
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80
从
frontend-deployment.yaml
文件应用前端 Deploymentkubectl apply -f https://k8s.io/examples/application/guestbook/frontend-deployment.yaml
查询 Pod 列表以验证三个前端副本是否正在运行
kubectl get pods -l app=guestbook -l tier=frontend
响应应类似于以下内容:
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
创建前端 Service
你应用的 Redis
服务只能在 Kubernetes 集群内部访问,因为 Service 的默认类型是 ClusterIP。ClusterIP
为 Service 指向的 Pods 集合提供一个单独的 IP 地址。此 IP 地址只能在集群内部访问。
如果你希望客人能够访问你的留言簿,你必须将前端服务配置为外部可见,以便客户端可以从 Kubernetes 集群外部请求该服务。然而,即使服务使用 ClusterIP
,Kubernetes 用户也可以使用 kubectl port-forward
访问该服务。
注意
一些云提供商,如 Google Compute Engine 或 Google Kubernetes Engine,支持外部负载均衡器。如果你的云提供商支持负载均衡器并且你想使用它,请取消注释type: LoadBalancer
。# 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
从
frontend-service.yaml
文件应用前端 Servicekubectl apply -f https://k8s.io/examples/application/guestbook/frontend-service.yaml
查询 Service 列表以验证前端 Service 是否正在运行
kubectl get services
响应应类似于以下内容:
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
通过 kubectl port-forward
查看前端服务
运行以下命令将本地机器上的端口
8080
转发到服务上的端口80
。kubectl port-forward svc/frontend 8080:80
响应应类似于以下内容:
Forwarding from 127.0.0.1:8080 -> 80 Forwarding from [::1]:8080 -> 80
在浏览器中加载页面 https://:8080 以查看你的留言簿。
通过 LoadBalancer
查看前端服务
如果你使用 type: LoadBalancer
部署了 frontend-service.yaml
清单,你需要找到 IP 地址才能查看你的留言簿。
运行以下命令获取前端服务的 IP 地址。
kubectl get service frontend
响应应类似于以下内容:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE frontend LoadBalancer 10.51.242.136 109.197.92.229 80:32372/TCP 1m
复制外部 IP 地址,并在浏览器中加载页面以查看你的留言簿。
注意
尝试通过输入消息并点击“提交”来添加一些留言条目。你输入的消息将显示在前端。此消息表明数据已通过你之前创建的服务成功添加到 Redis。扩展 Web 前端
你可以根据需要进行扩容或缩容,因为你的服务器被定义为使用 Deployment 控制器的服务。
运行以下命令来增加前端 Pod 的数量
kubectl scale deployment frontend --replicas=5
查询 Pod 列表以验证正在运行的前端 Pod 数量
kubectl get pods
响应应类似于以下内容:
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
运行以下命令来减少前端 Pod 的数量
kubectl scale deployment frontend --replicas=2
查询 Pod 列表以验证正在运行的前端 Pod 数量
kubectl get pods
响应应类似于以下内容:
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
清理
删除 Deployments 和 Services 也会删除任何正在运行的 Pods。使用标签可以通过一个命令删除多个资源。
运行以下命令删除所有 Pods、Deployments 和 Services。
kubectl delete deployment -l app=redis kubectl delete service -l app=redis kubectl delete deployment frontend kubectl delete service frontend
响应应类似于以下内容:
deployment.apps "redis-follower" deleted deployment.apps "redis-leader" deleted deployment.apps "frontend" deleted service "frontend" deleted
查询 Pod 列表以验证没有 Pod 正在运行
kubectl get pods
响应应类似于以下内容:
No resources found in default namespace.
下一步
- 完成 Kubernetes 基础 交互式教程
- 使用 Kubernetes 通过 MySQL 和 Wordpress 的持久卷 创建博客
- 阅读更多关于 使用服务连接应用程序 的信息
- 阅读更多关于 有效使用标签 的信息