Kubernetes环境中的Spring Boot WEB服务反向代理配置

Spring Boot WEB服务在Kubernetes环境中的反向代理配置

请汉语母语化为以下内容提供一种表达方式:

目标

在 Kubernetes 环境中启动的 Spring Boot WEB 服务中配置反向代理以加深理解。

实现

在本地(Ubuntu)的 Kubernetes 环境(Docker Desktop)中部署和启动 Nginx 和 Spring Boot WEB 应用的 Docker 镜像。

研发环境

    • 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版本“11.0.17” 2022-10-18
OpenJDK运行时环境(build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64位服务器虚拟机(build 11.0.17+8-post-Ubuntu-1ubuntu222.04,混合模式,共享)

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 WEB服务

请参考最简单的Spring Boot WEB服务的Hello World!

转到项目文件夹
※ 我们将 ~/tmp/hello-spring-boot 视为项目文件夹。

$ cd ~/tmp/hello-spring-boot

创建Docker镜像

请参考在Docker环境下启动Spring Boot WEB服务。

准备 Kubernetes 环境

Docker桌面版

[设置] → [Kubernetes]

Enable Kubernetes

修改Kubernetes的配置文件。

$ vim kube-all-in-one.yaml

文件的内容

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    app: app
  ports:
  - name: http
    port: 8080
    targetPort: 8080
---
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: app-hello-spring-boot
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: NodePort
  selector:
    app: web
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: web-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
      - name: web-config
        configMap:
          name: web-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
data:
  nginx.conf: |
    events {
    }

    http {
      server {
        listen 80;
        location / {
          proxy_pass http://app-service:8080;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
        }
      }
    }

目录文件结构

$ tree -I test -I target
.
├── Dockerfile
├── HELP.md
├── docker-compose.yml
├── kube-all-in-one.yaml
├── mvnw
├── mvnw.cmd
├── nginx.conf
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── springboot
        │               ├── SpringbootApplication.java
        │               └── controller
        │                   └── DataController.java
        └── resources
            ├── application.properties
            ├── static
            └── templates

Kubernetes环境

确认连接目标

$ kubectl config current-context
docker-desktop

如果要将连接目标返回到本地(docker-desktop)的情况下。

$ kubectl config use-context docker-desktop

部署

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

确认状态

$ kubectl get pods,services,deployments
NAME                                  READY   STATUS    RESTARTS   AGE
pod/app-deployment-cdc7dcbf9-2f9wm    1/1     Running   0          19s
pod/web-deployment-86b95b4b97-4mdc5   1/1     Running   0          19s

NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/app-service   ClusterIP   10.96.102.36     <none>        8080/TCP       19s
service/kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP        3m16s
service/web-service   NodePort    10.102.128.228   <none>        80:30000/TCP   19s

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

当检查Docker Desktop时,它以容器的形式启动。

在一个网络浏览器中确认。

http://localhost:30000/api/data

在网页浏览器中显示 {“message”:”你好世界!”} ,并成功获取到 JSON 数据。

可以通过另一个终端命令确认

$ curl -X GET http://localhost:30000/api/data
{"message":"Hello World!"}

确认反向代理的运作。

确认Nginx POD的日志。

$ kubectl get pods | grep web
$ kubectl logs web-deployment-86b95b4b97-rghsn
192.168.65.4 - - [16/Feb/2023:09:10:06 +0000] "GET /api/data HTTP/1.1" 200 37 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"

我正在通过Nginx访问Spring Boot WEB应用程序。

总结

    Ubuntu の 最小構成の Java 開発環境で Spring Boot WEBサービスとリバースプロキシを Kubernetes 環境にてコンテナとして起動させることが出来ました。
bannerAds