spring-boot - 使用自定义上下文 url 作为环境变量在 minikube 中启动 spring boot

标签 spring-boot kubernetes environment-variables minikube contextpath

我有一个 springboot 应用程序,我想将其部署在 Kubernetes(我使用的是 minikube)上,并使用从环境变量中获取的自定义上下文路径。

我编译了一个 app.war 文件。在 Linux 中导出一个环境变量如下:

export SERVER_SERVLET_CONTEXT_PATH=/app

然后按如下方式在我的机器上启动我的应用程序:

java -jar app.war --server.servlet.context-path=$(printenv CONTEXT_PATH)

它按预期工作,我可以使用 url localhost:8080/app/

访问我的应用程序抛出浏览器

我想在 minikube 上实现同样的事情,所以我准备了这些配置文件:


  • Docker 文件:

    FROM openjdk:8
    ADD app.war app.war
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "app.war", "--server.servlet.context-path=$(printenv CONTEXT_PATH)"]
    

  • 部署配置文件:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: esse-deployment-1
      labels:
        app: esse-1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: esse-1
      template:
        metadata:
          labels:
            app: esse-1
        spec:
          containers:
          - image: mysql:5.7
            name: esse-datasource
            ports:
            - containerPort: 3306
            env: 
            - name: MYSQL_ROOT_PASSWORD
              value: root 
          - image: esse-application
            name: esse-app
            imagePullPolicy: Never
            ports:
            - containerPort: 8080
            env:
            - name: server.servlet.context-path
              value: /esse-1
          volumes:
            - name: esse-1-mysql-persistent-storage
              persistentVolumeClaim:
                claimName: mysql-persistent-storage-claim
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: esse-service-1
      labels:
        app: esse-1
    spec:
      selector:
        app: esse-1
      ports:
        - protocol: TCP
          port: 8080
          targetPort: 8080
      type: NodePort
    

但是pod里面的java容器启动失败,这里是spring抛出的异常:

Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'

最佳答案

利用configmaps .

configmap 将保存您的 springboot 应用程序的 application.properties。

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: esse-config
data:
  application-dev.properties: |
    spring.application.name=my-esse-service
    server.port=8080
    server.servlet.context-path=/esse-1

注意: server.servlet.context-path=/esse-1 将覆盖您的 springboot 应用程序的上下文路径.

现在在您的部署 yaml 中引用此配置映射。

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: esse-deployment-1
  labels:
    app: esse-1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: esse-1
  template:
    metadata:
      labels:
        app: esse-1
    spec:
      containers:
      - image: mysql:5.7
        name: esse-datasource
        ports:
        - containerPort: 3306
        env: 
        - name: MYSQL_ROOT_PASSWORD
          value: root 
      - image: esse-application
        name: esse-app
        imagePullPolicy: Never
        command: [ "java", "-jar", "app.war", "--spring.config.additional-location=/config/application-dev.properties" ]
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: esse-application-config
          mountPath: "/config"
          readOnly: true
      volumes:
      - name: esse-application-config
        configMap:
          name: esse-config
          items:
          - key: application-dev.properties
            path: application-dev.properties
      - name: esse-1-mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-persistent-storage-claim

注意:这里我们将 configmap 安装在您的 springboot 应用程序容器中的 /config 文件夹中。此外,--spring.config.additional-location=/config/application-dev.properties 指向 application.properties 配置文件。

将来如果你想添加任何新配置或更新现有配置的值,只需在 configmap 中进行更改,然后 kubectl apply 即可。然后,为了反射(reflect)这些新的配置更改,只需缩小和扩大部署。

希望这对您有所帮助。

关于spring-boot - 使用自定义上下文 url 作为环境变量在 minikube 中启动 spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333619/

相关文章:

Laravel 5.6 env ('APP_BASE_URL')返回null

windows - 适用于所有 Windows 版本的所有用户桌面路径的环境变量是什么?

postgresql - 错误 : operator does not exist: timestamp without time zone >= boolean Hint: No operator matches the given name and argument type(s)

kubernetes - Google Kubernetes引擎上的Traefik错误

spring-boot - 实体类名转换为带下划线的SQL表名

kubernetes - 有时,将在没有网络的情况下创建 pods ,这会导致 pods 通过CrashLoopBackOff反复失败

docker - Docker镜像的新文件在哪里保存到GCP中?

environment-variables - 将环境变量传递给 autoconf 的 `./configure`

spring - 为什么Spring Boot会生成jar?

java - Spring - GET URL 中的斜杠字符