使用Docker Hub自定义容器映像,在AKS(Azure Kubernetes Service)上启动Spring Boot WEB服务

使用Docker Hub自定义容器镜像,在 AKS (Azure Kubernetes Service) 上启动Spring Boot WEB服务。

目标

在 Azure Kubernetes Service 环境中启动 Spring Boot WEB 服务,以加深对其的理解。

能够实现

使用微软Azure Kubernetes Service (AKS) 部署Spring Boot WEB应用程序的自定义容器映像。

请注意,本文使用了在Docker Hub上推送的公共访问自定义容器映像来构建真实系统。

这里还有一个使用 Microsoft Azure 容器注册表(ACR) 的配置示例的文章。

 

科技背景

微软Azure Kubernetes服务(AKS)是什么?

这里可以展开并查看。Microsoft Azure Kubernetes Service(AKS)

Microsoft Azure Kubernetes Service(AKS)是Azure的全管理服务,用于管理Kubernetes集群。
AKS的特点和优势如下:

可扩展性
AKS可以根据需求的变化来扩展/缩减应用程序。利用Kubernetes的标准功能,通过调整Pod数量来优化整个集群的资源使用率。

自动化
AKS自动化了与集群部署、升级、监视、扩展和管理相关的任务。这样,用户就可以简化与Kubernetes相关的复杂任务。

安全性
AKS有助于用户安全地配置Kubernetes集群。AKS支持可配置的网络策略,预配置的登录和基于角色的访问控制(RBAC)等功能。

可移植性
AKS使用开源的Kubernetes,以实现在任何地方运行云原生应用程序。AKS可以部署到Azure的其他服务、本地环境和其他云服务提供商。

生产就绪
AKS专注于高可用性、监视、可扩展性和安全性,提供生产就绪的Kubernetes集群。AKS提供99.95%的可用性SLA。

开发环境

    • Windows 11 Home 22H2 を使用しています。

WSL の Ubuntu を操作していきますので macOS の方も参考にして頂けます。

WSL (Microsoft Store 应用版)
> wsl –version
WSL 版本:1.0.3.0
内核版本:5.15.79.1
WSLg 版本:1.0.47Ubuntu
$ lsb_release -a
没有可用的 LSB 模块。
发行商 ID:Ubuntu
描述:Ubuntu 22.04.1 LTS
发布:22.04

Java JDK ※ 安装 Java JDK 和 Hello World!
$ java -version
openjdk version “11.0.17” 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)

Maven ※ 安装 Maven 和 Hello World!
$ mvn -version
Apache Maven 3.6.3
Maven 主目录:/usr/share/maven
Java 版本:11.0.17,厂商:Ubuntu,运行时:/usr/lib/jvm/java-11-openjdk-amd64

Docker Desktop
版本 4.16.3 (96739)

$ docker –version
Docker 版本 20.10.22,构建 3a2c30b

$ docker-compose –version
Docker Compose 版本 v2.15.1

$ kubectl version –short
客户端版本:v1.25.4
Kustomize 版本:v4.5.7
服务器版本:v1.25.4

在这篇文章中,我们基本上会在Ubuntu的终端上进行操作。

展示”Hello World”的步骤。

创建Spring Boot网络服务

您可以参考这个链接。

 

请前往项目文件夹

※ 假设 ~/tmp/hello-spring-boot 是项目文件夹。

$ cd ~/tmp/hello-spring-boot

编译 Java 应用程序(※参考)。

※ 将创建 target/app.jar。
※ 在创建下方的容器映像操作中同时创建 target/app.jar,因此不是必需的。

$ mvn clean install

创建容器映像

构建容器镜像

在本地的Docker环境(Docker Desktop)中将创建app-hello-spring-boot容器映像。
容器映像的创建时间为Unix纪元是spring-boot:build-image规格的特性。

$ mvn spring-boot:build-image \
    -Dspring-boot.build-image.imageName=app-hello-spring-boot

确认容器镜像

$ docker images | grep app-hello-spring-boot
app-hello-spring-boot   latest   e37cc77f2b36   43 years ago   262MB

将容器镜像注册到Docker Hub。

※ 请将$USER 替换为您自己的容器注册表。

添加标签

$ docker tag app-hello-spring-boot $USER/app-hello-spring-boot:latest

噗嗮

$ docker push $USER/app-hello-spring-boot:latest

在Docker Hub上,我成功发布了一个自定义容器映像,名称为$USER/app-hello-spring-boot:latest。

获取 Azure 帐户

使用Azure的免费账户在云端进行构建。

使用 Azure CLI 登录

您可以参考这个。

 

登录

$ az login

Azure环境

资源组

创建资源组

$ az group create \
    --name rg-hello \
    --location japaneast

显示资源组列表

$ az group list

如果要删除资源组

$ az group delete -n <group>

AKS环境 -> Azure Kubernetes Service环境

创建 AKS 集群
※ 创建 Kubernetes 基础设施需要一些时间。

$ az aks create \
    --resource-group rg-hello \
    --name aks-hello \
    --node-count 1 \
    --generate-ssh-keys

显示 AKS 集群列表

$ az aks list

※ 如果要删除 AKS 集群

$ az aks delete -n <aks-name> -g <group>

只需一个选项:

调整设置文件的权限※仅限第一次

$ chmod 600 /home/$USER/.kube/config

与AKS集群的连接
※ 覆盖现有配置:是的

$ az aks get-credentials \
    --resource-group rg-hello \
    --name aks-hello

连接设置已成功输出

Merged "aks-hello" as current context in /home/$USER/.kube/config

Kubernetes (AKS) 环境

請確認當前的連接目標。

$ kubectl config current-context
aks-hello

连接目标清单

$ kubectl config get-contexts
CURRENT   NAME             CLUSTER          AUTHINFO                         NAMESPACE
*         aks-hello        aks-hello        clusterUser_rg-hello_aks-hello
          docker-desktop   docker-desktop   docker-desktop

※ 如果需要将连接目标重新切换到本地(docker-desktop)

$ kubectl config use-context docker-desktop

※ 如果要删除连接目标

$ kubectl config delete-context <context>

请确认连接。

$ kubectl get nodes
NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-11584399-vmss000000   Ready    agent   4m43s   v1.24.9

修改Kubernetes的清单文件
※请将$USER部分根据您自己的容器注册表进行替换。

$ vim kube-all-in-one.yaml
apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: app
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: $USER/app-hello-spring-boot:latest
        ports:
        - containerPort: 8080

请务必确认连接目标。

$ kubectl apply -f kube-all-in-one.yaml
service/app-service created
deployment.apps/app-deployment created

确认状态

$ kubectl get pods,services,deployments
NAME                                 READY   STATUS    RESTARTS   AGE
pod/app-deployment-b876686f6-tzbwk   1/1     Running   0          16s

NAME                  TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)        AGE
service/app-service   LoadBalancer   10.0.125.15   20.210.31.184   80:32304/TCP   16s
service/kubernetes    ClusterIP      10.0.0.1      <none>          443/TCP        6m56s

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-deployment   1/1     1            1           16s

服务/应用服务的 EXTERNAL-IP 20.210.31.184 可从外部访问。

请使用网络浏览器进行确认。
※ 请根据环境更换URL。

http://20.210.31.184/api/data

在网页浏览器上显示了 {“message”:”Hello World!”} 并成功获取了JSON数据。

请使用curl命令从另一个终端进行确认。

$ curl -v http://20.210.31.184/api/data
*   Trying 20.210.31.184:80...
* Connected to 20.210.31.184 (20.210.31.184) port 80 (#0)
> GET /api/data HTTP/1.1
> Host: 20.210.31.184
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Mon, 27 Feb 2023 09:21:21 GMT
<
* Connection #0 to host 20.210.31.184 left intact
{"message":"Hello World!"}

在终端上显示 {“message”:”你好,世界!”},并成功获取了 JSON 数据。

据我所知,Azure 平台似乎没有进行 SSL/TLS 支持或应用 HTTP/2 协议。

连接到容器

$ kubectl exec -it app-deployment-b876686f6-tzbwk -- /bin/bash

连接到容器后

$ pwd
/workspace
$ ls -la
total 20
drwxr-xr-x 1 cnb  cnb  4096 Jan  1  1980 .
drwxr-xr-x 1 root root 4096 Feb 27 08:10 ..
drwxr-xr-x 1 cnb  cnb  4096 Jan  1  1980 BOOT-INF
drwxr-xr-x 3 cnb  cnb  4096 Jan  1  1980 META-INF
drwxr-xr-x 3 cnb  cnb  4096 Jan  1  1980 org

容器信息

$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
NAME="Ubuntu"
ID=ubuntu
PRETTY_NAME="Paketo Buildpacks Base Bionic"
VERSION_ID="18.04"
HOME_URL="https://github.com/paketo-buildpacks/bionic-base-stack"
UBUNTU_CODENAME=bionic
VERSION="18.04.6 LTS (Bionic Beaver)"
ID_LIKE=debian
SUPPORT_URL="https://github.com/paketo-buildpacks/bionic-base-stack/blob/main/README.md"
BUG_REPORT_URL="https://github.com/paketo-buildpacks/bionic-base-stack/issues/new"
VERSION_CODENAME=bionic

总结

    Ubuntu のシンプルな構成の Java 開発環境で Spring Boot WEBサービスのカスタムコンテナイメージを AKS (Azure Kubernetes Service) 環境で起動させることが出来ました。

请参考

[Microsoft Learn] Azure Kubernetes Service (AKS) 命令参考资料

 

广告
将在 10 秒后关闭
bannerAds