Deploy WAR in Tomcat on Kubernetes
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
Akhil Prajapati
Updated on March 06, 2020Comments
-
Akhil Prajapati over 4 years
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:
- A way to install Tomcat on Kubernetes platform.
- 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.
-
Akhil Prajapati over 5 yearsHi @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
-
Nicolas Pepinster over 5 yearsSomething is getting wrong probably. Type
kube get events --sort-by lastTimestamp --namespace=dev-a
to check the events relative to the new components created -
Akhil Prajapati over 5 yearsshould the ingress content and the deployment file be placed in different yaml?
-
Nicolas Pepinster over 5 yearsno you can put all in one yaml file. Check this example
-
Akhil Prajapati over 5 yearsThe 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.
-
Nicolas Pepinster over 5 years