kubernetes之部署war项目(二)
kubernetes之部署war项目(二)
k8s系列
源自我工作上的实际场景,记录于此。
现在老项目是war包形式,需要基于tomcat部署,因此在打包镜像时将tomcat带上的。
假设我的war项目是:mywar.war
,值得注意的是,我的war项目采用JNDI数据源形式。
用到的环境
centos 7.9 + docker + docker harbor + k8s
1、编写Dockerfile
harbor 是写在hosts中的域名
FROM harbor/tomcat:8.5.37
WORKDIR /usr/local
COPY context.xml /usr/local/tomcat/conf/context.xml
COPY mywar.war /usr/local/tomcat/webapps/mywar.war
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
此处需要注意,若你的war项目需要替换里面的某个配置,应该修改COPY配置将war的解压文件打进镜像,而不是上面的mywar.war整个打进去。参考解压war命令:unzip mywar.war -d mywar
将war与Dockerfile放在同一文件夹,将context.xml
也放进去,context.xml
可以随意找个tomcat服务器的copy进去。
执行构建镜像:
docker build -t harbor/mywar:2021102101 .
推送到私服
docker push harbor/mywar:2021102101
导出镜像
docker save harbor/mywar:2021102101 | gzip > mywar-2021102101.tar.gz
2、编写yaml
编写k8s的yaml,mywar.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: mywar
name: mywar-service
spec:
ports:
- port: 8080
protocol: TCP
# 指向dockerfile暴露的端口
targetPort: 8080
selector:
app: afa-console
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mywar
managedFields:
name: mywar-deployment
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: mywar
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: afa-console
spec:
containers:
# 需要注意修改对应的镜像
- image: harbor/mywar:2021102101
imagePullPolicy: IfNotPresent
name: afa-console
resources:
limits:
cpu: '2'
memory: 2048Mi
requests:
cpu: '1'
memory: 1024Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /usr/local/tomcat/conf/context.xml
name: context-xml
subPath: context.xml
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: mywar-configmap
name: context-xml
---
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: mywar-configmap
name: mywar-configmap
# 配置context.xml,注意根据实际情况修改数据DNS库域名、端口和数据库的名称
data:
context.xml: |
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Resource name="afa-main"
validationQuery="select 1"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://10.8.6.125:3306/temp?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"
username="root"
password="root" maxTotal="60" maxIdle="10"/>
</Context>
注意修改上面的数据库连接账号密码
部署k8s:
kubectl create -f mywar.yaml
查看项目部署日志
kubectl get pod
# 查看对应的pod
kubectl logs mywar-deployment-679bfdcbfc-sk4pc
到此部署完成,查看对外开放的端口
kubectl get svc
可以看到对外开放的端口是30947,访问: http://10.8.4.181:30947/ 可以看到tomcat页面。
访问我们的项目:http://10.8.4.181:30947/mywar
至此,部署完毕!
删除对应的pod、service等:
kubectl delete configmaps mywar-configmap -n
kubectl delete deployments.apps mywar-deployment
kubectl delete svc mywar-service
觉得对你有帮助,点个赞再走吧 ~