本文介绍了在Kubernetes上的Tomcat中部署WAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  1. 在Kubernetes平台上安装Tomcat的方法.
  2. 在这个新安装的Tomcat上部署我的war文件.

我需要利用Dockerfile来做到这一点.

I need to make use of Dockerfile to make this happen.

PS:我对Kubernetes和Docker知识还很陌生,也需要基本的细节.我尝试查找教程,但未获得满意的文章.

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.

任何帮助都会受到高度赞赏.

Any help will be highly highly appreciated.

推荐答案

Docker部分

您可以使用 tomcat docker官方映像

在您的Dockerfile中,只需将您的war文件复制到/usr/local/tomcat/webapps/目录中:

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

FROM tomcat

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

构建它:

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

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

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

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

1)这是一个简单的kubernetes 部署用于您的tomcat图像

1) Here is a simple kubernetes Deployment for your tomcat image

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

此部署定义将基于您的tomcat映像创建一个pod.

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

将其放入yml文件,然后执行kubectl create -f yourfile.yml进行创建.

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

2)创建服务:

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

您现在可以使用 http://tomcat-service.your-namespace/访问集群中的Pod.应用(因为您的战争称为app.war)

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

3)如果您有入口控制器,则您可以创建入口资源以便在外部公开应用程序集群:

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

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

现在使用 http://ingress-controller-ip/app

这篇关于在Kubernetes上的Tomcat中部署WAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 13:33