改造项目使用的Dockerfile文件

一开始项目使用的Dockerfile文件内容如下:

FROM idocker.io/jre:1.8.251
MAINTAINER sandu <1103324414@qq.com>
VOLUME ["/tmp","/opt/hkd-cloud/hkd-eureka/logs"]
RUN mkdir -p /opt/hkd-cloud/hkd-eureka/logs/
WORKDIR /opt/hkd-cloud/hkd-eureka/
ADD hkd-eureka.sh .
ADD target/hkd-eureka-1.0.jar .
CMD ["/bin/sh","hkd-eureka.sh"]
EXPOSE 8761

hkd-eureka.sh文件内容

#!/bin/sh
java -jar -Xms512m -Xmx512m -XX:+UseParNewGC -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+UseConcMarkSweepGC 
 -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintGCDateStamps -XX:+CMSClassUnloadingEnabled -XX:SoftRefLRUPolicyMSPerMB=0 
 -XX:+PrintGCDetails  -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses -XX:+PrintHeapAtGC -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow 
 -Xloggc:/opt/hkd-cloud/hkd-eureka/logs/heap_trace.txt -XX:HeapDumpPath=/opt/hkd-cloud/hkd-eureka/logs/HeapDumpOnOutOfMemoryError/ /opt/hkd-cloud/hkd-eureka/hkd-eureka-1.0.jar 
 --spring.profiles.active=dev >> /opt/hkd-cloud/hkd-eureka/logs/hkd-eureka.out 2>&1

这种写法的话,单独使用是没啥问题的,在Portainer中通过stack部署也可以,能够正常使用
1,服务可以正常启动
2,日志文件能自动生成到指定目录中,里面也有数据
3,eureka服务启动后,其他服务也能够注册进来
4,前端访问测试可以

不过,这种形式的部署到k8s上会有各种问题
1.eureka可以正常启动,浏览器访问查看
2.config可以正常启动,同时也能注册到eureka上
3.gateway启动不了,就算再通过command添加启动命令后启动,但是无法从config中获取到配置信息,也没法注册的eureka上

# 无法从config中获取到配置信息
2020-06-29 10:13:10,534 INFO  ConfigServicePropertySourceLocator:205 -Fetching config from server at : http://cloud-config-577b47c9d9-jvvfs:8888/
2020-06-29 10:13:10,540 INFO  ConfigServicePropertySourceLocator:227 -Connect Timeout Exception on Url - http://cloud-config-577b47c9d9-jvvfs:8888/. Will be trying the next url if available
2020-06-29 10:13:10,544 ERROR SpringApplication:837 -Application run failed
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing

# 没法注册的eureka
2020-06-29 09:30:36.972  WARN 6 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.UnknownHostException: cloud-eureka-2.cloud-eureka.hkd.svc.cluster.local

综上所述,需要修改Dockerfile文件
参考这个网址的内容:https://gitee.com/owenwangwen/open-capacity-platform/tree/2.0.1/register-center/eureka-server
具体Dockerfile文件路径不用管,只看Dockerfile文件内容,根据自己的实际情况修改如下:

# tag:0.2
#FROM idocker.io/jre:1.8.251
#MAINTAINER sandu <1103324414@qq.com>
#VOLUME ["/tmp","/opt/hkd-cloud/hkd-eureka/logs"]
#RUN mkdir -p /opt/hkd-cloud/hkd-eureka/logs/
#WORKDIR /opt/hkd-cloud/hkd-eureka/
#ADD hkd-eureka.sh .
#ADD target/hkd-eureka-1.0.jar .
#CMD ["/bin/sh","hkd-eureka.sh"]
#EXPOSE 8761

# tag:0.3
FROM idocker.io/jre:1.8.251
VOLUME /tmp
ADD target/hkd-eureka-1.0.jar hkd-eureka-1.0.jar
RUN sh -c 'touch /hkd-eureka-1.0.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /hkd-eureka-1.0.jar" ]

然后就是一套流程,修改文件后提交到gitlab,然后使用jenkins拉取,打包,制作docker镜像上传到nexus中

然后接下来在kuboard界面上操作。
这样改造后,gateway模块能从config中获取到配置信息,也能注册到eureka中


这样的dockerfile文件没有使用shell脚本,但是shell脚本中有个重要的参数:--spring.profiles.active=dev,表示的是启动时使用哪个配置文件,这样修改的话,没有特殊指定,使用的是默认的,这个默认要看模块中application.yml文件指定的是哪一个,
比如eureka指定的这个

这样一来,就有三大遗留问题需要处理:
1.正式环境使用的话启动时需要指定使用正式的配置文件,这个要咋处理?
2.日志现在是直接查看的,没有存储到文件中,以后查看日志要咋处理?
3.发布更新的话需要先把服务从注册中心给down下来,然后才能更新模块,这个要咋处理?
这三大遗留问题等待后续进一步处理

原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13209058.html