本教程向您展示如何使用 Kubernetes 和 Docker 构建并部署一个简单的(非生产就绪)、多层 Web 应用。该示例由以下组件组成:
你需要有一个 Kubernetes 集群,并且必须配置 kubectl 命令行工具以与你的集群通信。建议在至少有两个节点的集群上运行本教程,且这些节点不能作为控制平面主机。如果你还没有集群,可以通过 minikube 创建一个,或者使用以下 Kubernetes 演练场之一。
您的 Kubernetes 服务器版本必须在 v1.14 或更高。要检查版本,请输入 kubectl version。
留言板应用使用 Redis 来存储数据。
下方的清单文件指定了一个 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: "registry.k8s.io/redis@sha256:cb111d1bd870a6a471385a4a69ad17469d326e9dd91e0e455350cacf36e1b3ee"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379在您下载清单文件的目录中打开终端窗口。
应用 redis-leader-deployment.yaml 文件中的 Redis 部署
kubectl 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 主服务器 Pod 的日志
kubectl logs -f deployment/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 Service
kubectl 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 主服务器是一个单一的 Pod,但您可以通过添加几个 Redis 从服务器(或称副本)来使其具有高可用性并满足流量需求。
# 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 部署
kubectl apply -f https://k8s.io/examples/application/guestbook/redis-follower-deployment.yaml
通过查询 Pod 列表来验证两个 Redis 从服务器副本是否正在运行
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 从服务器通信以读取数据。为了使 Redis 从服务器可被发现,您必须设置另一个 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 Service
kubectl 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 从服务器一样,前端使用 Kubernetes Deployment 进行部署。
留言板应用使用 PHP 前端。它被配置为与 Redis 从服务器或主服务器 Service 通信,具体取决于请求是读还是写。前端提供 JSON 接口,并提供基于 jQuery-Ajax 的用户体验。
# 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 文件中的前端部署
kubectl 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
您所应用的 Redis Service 仅在 Kubernetes 集群内部可访问,因为 Service 的默认类型是 ClusterIP。ClusterIP 为 Service 指向的一组 Pod 提供单个 IP 地址。该 IP 地址仅在集群内部可访问。
如果您希望访客能够访问您的留言板,则必须配置前端 Service 使其在外部可见,以便客户端可以从 Kubernetes 集群外部请求该 Service。不过,Kubernetes 用户可以使用 kubectl port-forward 来访问该 Service,即使它使用的是 ClusterIP。
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 文件中的前端 Service
kubectl 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 查看前端 Service运行以下命令将本地机器的 8080 端口转发到 Service 的 80 端口。
kubectl port-forward svc/frontend 8080:80
响应应类似于以下内容:
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
在浏览器中加载 https://:8080 以查看您的留言板。
LoadBalancer 查看前端 Service如果您部署了 type: LoadBalancer 的 frontend-service.yaml 清单,则需要找到 IP 地址来查看您的留言板。
运行以下命令以获取前端 Service 的 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 地址,并在浏览器中加载该页面以查看您的留言板。
由于您的服务器被定义为使用 Deployment 控制器的 Service,因此您可以根据需要进行伸缩。
运行以下命令以增加前端 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
删除 Deployment 和 Service 也会删除所有正在运行的 Pod。使用标签可以通过一条命令删除多个资源。
运行以下命令以删除所有 Pod、Deployment 和 Service。
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.