Java 在 Kubernetes 上的 Tomcat 中部署 WAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54341432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:01:44  来源:igfitidea点击:

Deploy WAR in Tomcat on Kubernetes

javadockertomcatkubernetes

提问by Akhil Prajapati

I need to create a Multibranch Jenkins job to deploy a .war file in Tomcat that should run on Kubernetes. Basically, I need the following:

我需要创建一个 Multibranch Jenkins 作业来在 Tomcat 中部署一个应该在 Kubernetes 上运行的 .war 文件。基本上,我需要以下内容:

  1. A way to install Tomcat on Kubernetes platform.
  2. Deploy my war file on this newly installed Tomcat.
  1. 一种在Kubernetes平台上安装Tomcat的方法。
  2. 在这个新安装的 Tomcat 上部署我的 war 文件。

I need to make use of Dockerfileto make this happen.

我需要利用Dockerfile来实现这一点。

PS: I am very new to Kubernetes and Docker stuff and need basic details as well. I tried finding tutorials but couldn't get any satisfactory article.

PS:我对 Kubernetes 和 Docker 的东西很陌生,也需要基本的细节。我尝试寻找教程,但找不到任何令人满意的文章。

Any help will be highly highly appreciated.

任何帮助都将受到高度赞赏。

采纳答案by Nicolas Pepinster

Docker part

Docker 部分

You can use the tomcat docker official image

可以使用tomcat docker 官方镜像

In your Dockerfilejust copy your war file in /usr/local/tomcat/webapps/directory :

在您Dockerfile刚刚将您的战争文件复制到/usr/local/tomcat/webapps/目录中:

FROM tomcat

COPY app.war /usr/local/tomcat/webapps/

Build it :

构建它:

docker build --no-cache -t <REGISTRY>/<IMAGE>:<TAG> .

docker build --no-cache -t <REGISTRY>/<IMAGE>:<TAG> .

Once your image is built, push it into a Docker registry of your choice.

构建镜像后,将其推送到您选择的 Docker 注册表中。

docker push <REGISTRY>/<IMAGE>:<TAG>

docker push <REGISTRY>/<IMAGE>:<TAG>

Kubernetes part

Kubernetes 部分

1) Here is a simple kubernetes Deploymentfor your tomcat image

1) 这是您的 tomcat 映像的简单 kubernetes部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: <REGISTRY>/<IMAGE>:<TAG>
        ports:
        - containerPort: 8080

This Deployment definition will create a pod based on your tomcat image.

此部署定义将根据您的 tomcat 映像创建一个 pod。

Put it in a yml file and execute kubectl create -f yourfile.ymlto create it.

将其放入一个 yml 文件并执行kubectl create -f yourfile.yml以创建它。

2) Create a Service:

2)创建服务

kind: Service
apiVersion: v1
metadata:
  name: tomcat-service
spec:
  selector:
    app: tomcat
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

You can now access your pod inside the cluster with http://tomcat-service.your-namespace/app(because your war is called app.war)

您现在可以使用http://tomcat-service.your-namespace/app访问集群内的 pod (因为您的 war 被称为app.war

3) If you have Ingress controller, you can create an Ingress ressourceto expose the application outside the cluster :

3) 如果你有Ingress 控制器,你可以创建一个Ingress 资源来暴露集群外的应用程序:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /app
        backend:
          serviceName: tomcat-service
          servicePort: 80

Now access the application using http://ingress-controller-ip/app

现在使用http://ingress-controller-ip/app 访问应用程序