练习 - 在 Azure Kubernetes 服务群集上部署应用程序
在本练习中,将公司的网站作为测试应用部署到 Azure Kubernetes 服务 (AKS) 上。 该网站是一个静态网站,具有 HTML、CSS 和 JavaScript 的基本技术堆栈。 它不会收到像其他服务那样多的请求,并为我们提供了一种安全的部署选项测试方法。
注意
若要进一步浏览源代码,此 GitHub 存储库 中提供了 Web 应用的代码。 此外,此示例应用将只部署在 Linux 节点池上。
重要
需要自己的 Azure 订阅才能完成本练习,并且可能会产生费用。 如果还没有 Azure 订阅,请在开始之前创建 一个免费帐户 。
创建部署清单
创建部署清单文件以部署应用程序。 使用清单文件可以定义要部署的资源类型以及与工作负载相关的所有详细信息。
Kubernetes 将容器分组为逻辑结构(称为 Pod),这些逻辑结构没有智能。 部署会添加缺少的智能,以创建应用程序。 让我们创建一个部署文件。
登录到 Azure Cloud Shell。
在 Cloud Shell 中,使用集成编辑器为名为
deployment.yaml的 Kubernetes 部署创建一个清单文件。touch deployment.yaml输入
code .,在 Cloud Shell 中打开集成编辑器打开
deployment.yaml文件,并添加 YAML 的以下代码部分。# deployment.yaml apiVersion: apps/v1 # The API resource where this workload resides kind: Deployment # The kind of workload we're creating metadata: name: contoso-website # This will be the name of the deployment在该代码中,你添加了前两项,使 Kubernetes 了解你要创建的清单的
apiVersion和kind。name是部署的名称。 使用kubectl时,将用它来识别和查询部署信息。提示
要详细了解
apiVersion和要放入此密钥的值,请参阅 Kubernetes 官方文档。 找到本模块末尾的链接。部署会包装一个 Pod。 使用模板定义可定义清单文件中的 Pod 信息。 模板位于部署规范部分下方的清单文件中。
更新
deployment.yaml文件,使其匹配以下 YAML。# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: template: # This is the template of the pod inside the deployment metadata: # Metadata for the pod labels: app: contoso-websitePod 不使用与部署相同的名称。 Pod 的名称将由部署的名称和末尾添加的随机 ID 组合而成。
请注意
labels项的使用。 添加labels项,以允许部署查找 Pod 并对其进行分组。Pod 会包装一个或多个容器。 所有 Pod 都有一个规范部分,可用于定义该 Pod 中的容器。
更新
deployment.yaml文件,使其匹配以下 YAML。# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: template: # This is the template of the pod inside the deployment metadata: labels: app: contoso-website spec: containers: # Here we define all containers - name: contoso-websitecontainers项是容器规范数组,因为一个 Pod 可具有一个或多个容器。 该规范定义有关容器的image、name、resources、ports和其他重要信息。所有正在运行的 Pod 都遵循名称
contoso-website-<UUID>,其中 UUID 是生成的 ID,用于唯一标识所有资源。最好定义允许应用从群集使用的最小和最大的资源量。 使用
resources项来指定此信息。更新
deployment.yaml文件,使其匹配以下 YAML。# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: template: # This is the template of the pod inside the deployment metadata: labels: app: contoso-website spec: containers: - image: mcr.microsoft.com/mslearn/samples/contoso-website name: contoso-website resources: requests: # Minimum amount of resources requested cpu: 100m memory: 128Mi limits: # Maximum amount of resources requested cpu: 250m memory: 256Mi请注意资源部分如何让你可以将最小资源量作为请求进行指定,将最大资源量作为限制进行指定。
最后一步是定义此容器将通过
ports项对外公开的端口。ports项是对象数组,这意味着 Pod 中的容器可以公开具有多个名称的多个端口。更新
deployment.yaml文件,使其匹配以下 YAML。# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: template: # This is the template of the pod inside the deployment metadata: labels: app: contoso-website spec: nodeSelector: kubernetes.io/os: linux containers: - image: mcr.microsoft.com/mslearn/samples/contoso-website name: contoso-website resources: requests: cpu: 100m memory: 128Mi limits: cpu: 250m memory: 256Mi ports: - containerPort: 80 # This container exposes port 80 name: http # We named that port "http" so we can refer to it later请注意使用
name项命名端口的方式。 使用命名端口可以更改公开的端口,而无需更改引用该端口的文件。最后,添加选择器部分以定义部署管理的工作负载。
selector项位于清单文件的部署规范部分中。 使用matchLabels密钥列出部署管理的所有 Pod 的标签。更新
deployment.yaml文件,使其匹配以下 YAML。# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: selector: # Define the wrapping strategy matchLabels: # Match all pods with the defined labels app: contoso-website # Labels follow the `name: value` template template: # This is the template of the pod inside the deployment metadata: labels: app: contoso-website spec: nodeSelector: kubernetes.io/os: linux containers: - image: mcr.microsoft.com/mslearn/samples/contoso-website name: contoso-website resources: requests: cpu: 100m memory: 128Mi limits: cpu: 250m memory: 256Mi ports: - containerPort: 80 name: http注意
在具有多个节点池(Linux 和 Windows)的 AKS 群集中,上述部署清单文件还定义了
nodeSelector,以通知 AKS 群集在可运行 Linux 容器的节点上运行示例应用程序的 Pod。Linux 节点不能运行 Windows 容器,反之亦然。
保存清单文件并关闭编辑器。
应用清单
在 Cloud Shell 中运行
kubectl apply命令,将部署清单提交到群集。kubectl apply -f ./deployment.yaml此命令应输出类似于以下示例的结果。
deployment.apps/contoso-website created运行
kubectl get deploy命令,检查部署是否成功。kubectl get deploy contoso-website此命令应输出类似于以下示例的表。
NAME READY UP-TO-DATE AVAILABLE AGE contoso-website 0/1 1 0 16s运行
kubectl get pods命令,检查 Pod 是否正在运行。kubectl get pods此命令应输出类似于以下示例的表。
NAME READY STATUS RESTARTS AGE contoso-website-7c58c5f699-r79mv 1/1 Running 0 63s