19

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.

1 Answer 1

25

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

6
  • Hi @Nicolas, Thanks a lot for your answer. I tried implementing this and at the end of the Jenkins Job, I am getting this: + kubectl --kubeconfig **** --namespace=dev-a apply -f k8s/services/ service "tomcat-service" created [Pipeline] sh + kubectl --kubeconfig **** --namespace=dev-a apply -f k8s/dev/ ingress "tomcat-ingress" created However, I don't see any new pod created when I run this: kubectl --kubeconfig=dev.us-east-1 --namespace=dev-a get pods Jan 24, 2019 at 10:57
  • Something is getting wrong probably. Type kube get events --sort-by lastTimestamp --namespace=dev-a to check the events relative to the new components created Jan 24, 2019 at 11:27
  • should the ingress content and the deployment file be placed in different yaml? Jan 24, 2019 at 11:38
  • no you can put all in one yaml file. Check this example Jan 24, 2019 at 11:43
  • The build is getting executed successfully but I am getting ImagePullBackOff error in pod. On describing the pod, I found this error: pull access denied for xyz/myrepo, repository does not exist or may require 'docker login'. However, I can see the image getting pushed with the same name. I also tried docker login but it didn't work. Jan 25, 2019 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.