<p>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:</p>

<ol>
<li>A way to install Tomcat on Kubernetes platform. </li>
<li>Deploy my war file on this newly installed Tomcat.</li>
</ol>

<p>I need to make use of <code>Dockerfile</code> to make this happen.</p>

<p>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. </p>

<p>Any help will be highly highly appreciated. </p>

<h2>Docker part</h2>

<p>You can use the <a href="https://hub.docker.com/_/tomcat" rel="noreferrer">tomcat docker official image</a></p>

<p>In your <code>Dockerfile</code> just copy your war file in <code>/usr/local/tomcat/webapps/</code> directory :</p>

<pre><code>FROM tomcat

COPY app.war /usr/local/tomcat/webapps/
</code></pre>

<p>Build it :</p>

<p><code>docker build --no-cache -t &lt;REGISTRY&gt;/&lt;IMAGE&gt;:&lt;TAG&gt; .</code></p>

<p>Once your image is built, push it into a Docker registry of your choice.</p>

<p><code>docker push &lt;REGISTRY&gt;/&lt;IMAGE&gt;:&lt;TAG&gt;</code></p>

<h2>Kubernetes part</h2>

<p>1) Here is a simple kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="noreferrer">Deployment</a> for your tomcat image </p>

<pre><code>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: &lt;REGISTRY&gt;/&lt;IMAGE&gt;:&lt;TAG&gt;
        ports:
        - containerPort: 8080
</code></pre>

<p>This Deployment definition will create a pod based on your tomcat image. </p>

<p>Put it in a yml file and execute <code>kubectl create -f yourfile.yml</code> to create it.</p>

<p>2) Create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="noreferrer">Service</a> :</p>

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

<p>You can now access your pod inside the cluster with <a href="http://tomcat-service.your-namespace/app" rel="noreferrer">http://tomcat-service.your-namespace/app</a> (because your war is called <code>app.war</code>)</p>

<p>3) If you have <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#ingress-controllers" rel="noreferrer">Ingress controller</a>, you can create an <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#the-ingress-resource" rel="noreferrer">Ingress ressource</a> to expose the application outside the cluster :</p>

<pre><code>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
</code></pre>

<p>Now access the application using <a href="http://ingress-controller-ip/app" rel="noreferrer">http://ingress-controller-ip/app</a></p>
