在 Kubernetes 上的 Tomcat 中部署 WAR [英] Deploy WAR in Tomcat on Kubernetes
问题描述
我需要创建一个 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:
- 一种在 Kubernetes 平台上安装 Tomcat 的方法.
- 在这个新安装的 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
镜像构建完成后,将其推送到您选择的 Docker 注册表中.
Once your image is built, push it into a Docker registry of your choice.
docker push
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/访问集群内的 podapp(因为你的战争叫做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) 如果您有 入口控制器,您可以创建一个 Ingress ressource 将应用程序暴露在外面集群:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!