为了使部署简便,镜像多次使用,而不是每修改一次配置或者参数就要重新打一次的问题(项目较老都是替换.xml)

问题:替换项目中文件的web.xml或者jdbc.properties

刚开始想着直接war包打镜像,然后直接替换就好了
哪曾想替换之后会把所有的文件覆盖,只留下configMap替换的文件

然后想着直接把war包解压后的文件放到镜像中,这样替换就不会覆盖了

之前的Dockerfile

FROM tomcat8-jdk8:latest
RUN rm -rf  /usr/local/tomcat/webapps/*
COPY platform4j-web.war /usr/local/tomcat/
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone #设置时区

改变思路之后的Dockerfile

FROM tomcat8-jdk8:latest
RUN rm -rf  /usr/local/tomcat/webapps/*
COPY platform4j-web /usr/local/tomcat/webapps/platform4j-web/
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone #设置时区

先创建configMap

---
apiVersion: v1
data:
  jdbc.properties: >-
    jdbc.driverClassName=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql://mysqlip:3306/kplatform4j_yk_intel?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull

    jdbc.username=root

    jdbc.password=toor

    jdbc.initialSize=3

    jdbc.minIdle=2

    jdbc.maxActive=60

    jdbc.maxWait=60000

    jdbc.timeBetweenEvictionRunsMillis=60000

    jdbc.minEvictableIdleTimeMillis=30000

    jdbc.validationQuery=SELECT 'x'

    jdbc.testWhileIdle=true

    jdbc.testOnBorrow=false

    jdbc.testOnReturn=false

    jdbc.poolPreparedStatements=true

    jdbc.maxPoolPreparedStatementPerConnectionSize=20

    jdbc.removeAbandoned=true

    jdbc.removeAbandonedTimeout=120

    jdbc.logAbandoned=false

    jdbc.filters=stat

    redis_hostname=redisip

    redis_port=6379

    redis_pwd=
kind: ConfigMap
metadata:
  name: jdbcproperties
  namespace: kube-public


在项目中配置挂载

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '1'
    k8s.kuboard.cn/displayName: 智能双重预防信息化平台
    k8s.kuboard.cn/ingress: 'false'
    k8s.kuboard.cn/service: NodePort
    k8s.kuboard.cn/workload: platform
  creationTimestamp: '2021-05-14T00:41:32Z'
  generation: 1
  labels:
    k8s.kuboard.cn/layer: svc
    k8s.kuboard.cn/name: platform
  name: platform
  namespace: kube-public
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.kuboard.cn/layer: svc
      k8s.kuboard.cn/name: platform
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: '2021-05-13T15:12:18+08:00'
      creationTimestamp: null
      labels:
        k8s.kuboard.cn/layer: svc
        k8s.kuboard.cn/name: platform
    spec:
      containers:
        - image: '192.168.30.85:5000/kingc/platform:latest'
          imagePullPolicy: Always
          name: platform
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: >-
                /usr/local/tomcat/webapps/platform4j-web/WEB-INF/classes/spring-dubbo.xml
              name: dubbo
              subPath: spring-dubbo.xml
            - mountPath: >-
                /usr/local/tomcat/webapps/platform4j-web/WEB-INF/classes/jdbc.properties
              name: jdbc
              subPath: jdbc.properties
      dnsConfig: {}
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: ipharbor
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        seLinuxOptions: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - configMap:
            defaultMode: 420
            name: dubbo
          name: dubbo
        - configMap:
            defaultMode: 420
            name: jdbcproperties
          name: jdbc

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.kuboard.cn/displayName: platform
    k8s.kuboard.cn/workload: platform
  labels:
    k8s.kuboard.cn/layer: svc
    k8s.kuboard.cn/name: platform
  name: platform
  namespace: kube-public
spec:
  clusterIP: 10.99.203.193
  externalTrafficPolicy: Cluster
  ports:
    - name: jy3ffk
      nodePort: 32622
      port: 80
      protocol: TCP
      targetPort: 8080
  selector:
    k8s.kuboard.cn/layer: svc
    k8s.kuboard.cn/name: platform
  sessionAffinity: None
  type: NodePort


这样就可以替换完成了 每次更换环境不用重新打包 完美

更完美的解决方案是
保证pod启动前,要把配置文件覆盖过去,新增了一个tomcat的运行脚本负责解压war包,覆盖文件及跑起tomat
https://www.cnblogs.com/windysai/p/14305441.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐