Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy WAR in Tomcat on Kubernetes

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. A way to install Tomcat on Kubernetes platform.
  2. Deploy my war file on this newly installed Tomcat.

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

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.

like image 875
Akhil Prajapati Avatar asked Jan 24 '19 07:01

Akhil Prajapati


1 Answers

Docker part

You can use the tomcat docker official image

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

FROM tomcat

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

Build it :

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

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

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

Kubernetes part

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

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

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

2) Create a Service :

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)

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

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

like image 194
Nicolas Pepinster Avatar answered Oct 31 '22 05:10

Nicolas Pepinster