将容器映像部署到 Azure Kubernetes 服务

已完成

在本单元中,你要将容器映像部署到 Azure Kubernetes 服务。

使用 Azure Kubernetes 服务,你可以配置 Kubernetes 群集,使其在所需的状态下通过部署运行,这是向 Pod 和 ReplicaSet 提供声明性更新的过程。 此状态声明在清单 (YAML) 文件中进行管理,而 Kubernetes 控制器会在收到指示时将当前状态更改为声明的状态。 你将创建这个 deployment.yml 清单文件,并指示 Azure Kubernetes 服务在所需的状态下运行,并将 Pod 配置为拉取/运行驻留在 Azure 容器注册表中的 flightbookingsystemsample 容器映像(在上一单元中进行了推送)。 如果没有此清单文件,你需要手动创建、更新和删除 Pod,而不是让 Kubernetes 来协调操作。

注意

如果会话已经空闲,或者你是在另一个时间点和/或从其他 CLI 执行此步骤时,可能需要重新初始化环境变量,并使用以下 CLI 命令重新进行身份验证。

AZ_RESOURCE_GROUP=javacontainerizationdemorg

AZ_CONTAINER_REGISTRY=<YOUR_CONTAINER_REGISTRY>

AZ_KUBERNETES_CLUSTER=javacontainerizationdemoaks

AZ_LOCATION=<YOUR_AZURE_REGION>

AZ_KUBERNETES_CLUSTER_DNS_PREFIX=<YOUR_UNIQUE_DNS_PREFIX_TO_ACCESS_YOUR_AKS_CLUSTER>

az login

az acr login -n $AZ_CONTAINER_REGISTRY

部署容器映像

在此,你需要将 flightbookingsystemsample 容器映像部署到 Azure Kubernetes 群集。

在项目的根目录中 (Flight-Booking-System-JavaServlets_App/Project/Airlines),创建一个名为 deployment.yml 的文件。 在 CLI 中运行以下命令:

vi deployment.yml

将以下内容添加到 deployment.yml 中,然后保存并退出:

注意

使用先前设置的 AZ_CONTAINER_REGISTRY 环境变量值进行更新,例如 javacontainerizationdemoacr

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flightbookingsystemsample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: flightbookingsystemsample
  template:
    metadata:
      labels:
        app: flightbookingsystemsample
    spec:
      containers:
      - name: flightbookingsystemsample
        image: <AZ_CONTAINER_REGISTRY>.azurecr.io/flightbookingsystemsample:latest
        resources:
          requests:
            cpu: "1"
            memory: "1Gi"
          limits:
            cpu: "2"
            memory: "2Gi"
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: flightbookingsystemsample
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    app: flightbookingsystemsample

注意

或者,你的项目的根目录中的 deployment_solution.yml 文件包含所需的内容;你可能会发现,重命名/更新该文件的内容会更容易。

在前面的 deployment.yml,你会注意到该文件包含一个部署和一项服务。 部署用于管理一组 Pod,服务用于允许网络访问这些 Pod。 你会注意到,Pod 已配置为从 Azure 容器注册表中拉取一个映像 <AZ_CONTAINER_REGISTRY>.azurecr.io/flightbookingsystemsample:latest。 你还会注意到,该服务已配置为允许 HTTP Pod 流量传入端口 8080,这与使用 -p 端口参数在本地运行容器映像的方式类似。

现在,Azure Kubernetes 群集创建应已成功完成。

你需要将 Azure CLI 配置为通过 kubectl 命令访问 Azure Kubernetes 群集。 使用 az aks install-cli 命令在本地安装 kubectl。 在 CLI 中运行以下命令:

az aks install-cli

使用 az aks get-credentials 命令将 kubectl 配置为连接到 Kubernetes 群集。 在 CLI 中运行以下命令:

az aks get-credentials --resource-group $AZ_RESOURCE_GROUP --name $AZ_KUBERNETES_CLUSTER

你会看到类似于以下内容的输出:

Merged AZ_KUBERNETES_CLUSTER as current context in ~/.kube/config

现在指示 Azure Kubernetes 服务将 deployment.yml 更改应用到群集。 在 CLI 中运行以下命令:

kubectl apply -f deployment.yml

你会看到类似于以下内容的输出:

deployment.apps/flightbookingsystemsample created
service/flightbookingsystemsample created

现在可以使用 kubectl 来监视部署状态。 在 CLI 中运行以下命令:

kubectl get all

你会看到类似于以下内容的输出:

NAME                                   READY   STATUS    RESTARTS   AGE
pod/flightbookingsystemsample-75647c4c98-v4v4r   1/1     Running   2          13d

NAME                      TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)          AGE
service/kubernetes        ClusterIP      10.0.0.1      <none>         443/TCP          66d
service/flightbookingsystemsample   LoadBalancer   10.0.34.128   20.81.13.151   8080:30265/TCP   66d

NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/flightbookingsystemsample   1/1     1            1           66d

NAME                                         DESIRED   CURRENT   READY   AGE
replicaset.apps/flightbookingsystemsample-75647c4c98   1         1         1       66d
replicaset.apps/flightbookingsystemsample-7564c58f55   0         0         0       13d

如果 POD 状态为 Running,表示应用应可供访问。

此外,你还可以查看每个 Pod 中的应用日志。 在 CLI 中运行以下命令:

 kubectl logs pod/flightbookingsystemsample-<POD_IDENTIFIER_FROM_YOUR_RUNNING_POD>

你会看到类似于以下内容的输出:

NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
07-Oct-2021 18:31:14.073 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.71
07-Oct-2021 18:31:14.164 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Sep 9 2021 18:43:14 UTC
07-Oct-2021 18:31:14.164 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.71.0
07-Oct-2021 18:31:14.165 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
07-Oct-2021 18:31:14.166 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            5.4.0-1051-azure
07-Oct-2021 18:31:14.166 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
07-Oct-2021 18:31:14.166 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
07-Oct-2021 18:31:14.167 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.12+7
07-Oct-2021 18:31:14.167 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
07-Oct-2021 18:31:14.167 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
07-Oct-2021 18:31:14.168 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
07-Oct-2021 18:31:14.261 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
07-Oct-2021 18:31:14.261 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
07-Oct-2021 18:31:14.261 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
07-Oct-2021 18:31:14.262 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
07-Oct-2021 18:31:14.262 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
07-Oct-2021 18:31:14.262 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
07-Oct-2021 18:31:14.263 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
07-Oct-2021 18:31:14.263 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
07-Oct-2021 18:31:14.263 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
07-Oct-2021 18:31:14.263 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
07-Oct-2021 18:31:14.264 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
07-Oct-2021 18:31:14.264 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
07-Oct-2021 18:31:14.264 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
07-Oct-2021 18:31:14.265 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
07-Oct-2021 18:31:14.265 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
07-Oct-2021 18:31:14.265 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [{4}].
07-Oct-2021 18:31:14.266 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
07-Oct-2021 18:31:14.361 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k  25 Mar 2021]
07-Oct-2021 18:31:14.763 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
07-Oct-2021 18:31:14.962 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
07-Oct-2021 18:31:15.071 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 6497 ms
07-Oct-2021 18:31:15.771 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
07-Oct-2021 18:31:15.772 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.71]
07-Oct-2021 18:31:16.261 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web app archive [/usr/local/tomcat/webapps/FlightBookingSystemSample.war]
07-Oct-2021 18:31:30.782 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.ws.policy.privateutil.MethodUtil (file:/usr/local/tomcat/webapps/FlightBookingSystemSample/WEB-INF/lib/webservices-rt-2.3.1.jar) to method sun.reflect.misc.MethodUtil.invoke(java.lang.reflect.Method,java.lang.Object,java.lang.Object[])
WARNING: Please consider reporting this to the maintainers of com.sun.xml.ws.policy.privateutil.MethodUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
07-Oct-2021 18:31:53.370 INFO [localhost-startStop-1] com.sun.xml.ws.server.MonitorBase.createRoot Metro monitoring rootname successfully set to: com.sun.metro:pp=/,type=WSEndpoint,name=/FlightBookingSystemSample-PriceAndSeats-PriceAndSeatsPort
07-Oct-2021 18:31:54.864 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.<init> WSSERVLET14: JAX-WS servlet initializing
07-Oct-2021 18:32:02.869 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized WSSERVLET12: JAX-WS context listener initializing
07-Oct-2021 18:32:02.870 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized WSSERVLET12: JAX-WS context listener initializing
07-Oct-2021 18:32:03.069 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web app archive [/usr/local/tomcat/webapps/FlightBookingSystemSample.war] has finished in [46,808] ms
07-Oct-2021 18:32:03.165 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
07-Oct-2021 18:32:03.267 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 48195 ms

现在可以使用 kubectl get services flightbookingsystemsample 输出中的 EXTERNAL-IP 来访问 Azure Kubernetes 服务中正在运行的应用了。

注意

将下面的 IP 地址 (20.81.13.151) 替换为先前执行的命令中的 EXTERNAL-IP。

打开浏览器并访问航班预订系统示例登陆页面,网址为 http://20.81.13.151:8080/FlightBookingSystemSample

你将获得一个如下所示的页面:

Screenshot showing the running app.

可以选择使用 tomcat-users.xml 中的任何用户登录,例如 someuser@azure.com: password