コンテナー イメージを Azure Kubernetes Service にデプロイする
このレッスンでは、Azure Kubernetes Service にコンテナー イメージをデプロイします。
Azure Kubernetes Service では、デプロイを通じて Kubernetes クラスターを必要な状態で実行するように構成します。これは、ポッドとレプリカセットに対する宣言型の更新を行うプロセスです。 この状態の宣言はマニフェスト (YAML) ファイルで管理し、Kubernetes コントローラーが、指示に従って現在の状態を宣言された状態に変更します。 この deployment.yml
マニフェスト ファイルを作成し、Azure Container Registry 内に存在する flightbookingsystemsample
コンテナー イメージ (前のユニットでプッシュしたもの) をプルして実行するように構成されたポッドを使用して、必要な状態で実行するように Azure Kubernetes Service に命令します。 このマニフェスト ファイルを使用しない場合は、Kubernetes にプロセスの調整を委ねるのではなく、ポッドの作成、更新、削除を手動で行う必要があります。
Note
セッションがアイドル状態になった場合、またはこの手順を別の時点や別の 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 に追加し、保存して終了します。
Note
前に設定した 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
Note
プロジェクトのルート内にある deployment_solution.yml
に必要な内容が含まれているので、必要に応じて、そのファイルの内容の名前を変更したり更新したりするほうが簡単な場合があります。
前の deployment.yml
では、ファイルに Deployment と Service が含まれていることがわかります。 デプロイはポッドのセットを管理するために使用され、サービスはポッドへのネットワーク アクセスを許可するために使用されます。 ポッドは、Azure Container Registry から 1つのイメージ <AZ_CONTAINER_REGISTRY>.azurecr.io/flightbookingsystemsample:latest
をプルするように構成されています。 また、サービスがポート 8080 への着信 HTTP ポッド トラフィックを許可するように構成されていることもわかります。これは、-p
ポート引数を使ってローカル環境でコンテナー イメージを実行した方法と似ています。
ここまでで、Azure Kubernetes クラスターの作成は正常に完了しているはずです。
kubectl
コマンドを使用して Azure Kubernetes クラスターにアクセスするように Azure CLI を構成します。 az aks install-cli
コマンドを使用して kubectl をローカルにインストールします。 CLI で次のコマンドを実行します。
az aks install-cli
az aks get-credentials
コマンドを使って Kubernetes クラスターに接続するように、kubectl を構成します。 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
であれば、アプリにアクセスできます。
各ポッド内のアプリ ログを表示することもできます。 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 内で実行中のアプリにアクセスできます。
Note
次の IP アドレス (20.81.13.151) を、前に実行したコマンドの EXTERNAL-IP の IP アドレスに置き換えます。
ブラウザーを開き、Flight Booking System のサンプル ランディング ページ (http://20.81.13.151:8080/FlightBookingSystemSample) にアクセスします
次のようなページが表示されます。
必要に応じて、tomcat-users.xml
から任意のユーザー (たとえば、someuser@azure.com: password
) を使用してサインインすることができます。