將容器映像部署至 Azure Kubernetes Service

已完成

在本單元中,您會將容器映像部署至 Azure Kubernetes Service。

使用 Azure Kubernetes Service,您會透過部署將 Kubernetes 叢集設定為以所需狀態執行,這是將宣告式更新提供給 Pod 和 ReplicaSet 的流程。 此狀態宣告會在資訊清單 (YAML) 檔案中加以管理,而且 Kubernetes 控制器會在得到指示時,將目前狀態變更為宣告的狀態。 您將會建立此 deployment.yml 資訊清單檔案,並指示 Azure Kubernetes Service 以所需狀態執行,其中 Pod 已設定為提取/執行駐留在 Azure Container Registry 的 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 Container Registry 提取單一映像 <AZ_CONTAINER_REGISTRY>.azurecr.io/flightbookingsystemsample:latest。 您也會注意到服務已設定為允許連接埠 8080 的傳入 HTTP Pod 流量,類似於您在本機使用 -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 Service 將 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 Service 內執行中的應用程式。

注意

將下列 (20.81.13.151) 中的 IP 位址取代為您先前所執行命令中的 EXTERNAL-IP 位址。

開啟瀏覽器並造訪航班預訂系統範例登陸頁面,網址為 http://20.81.13.151:8080/FlightBookingSystemSample

您將會得到如下的頁面:

Screenshot showing the running app.

您可以選擇性地使用來自 tomcat-users.xml 的任何使用者登入,例如 someuser@azure.com: password