一開始項目使用的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下來,然后才能更新模塊,這個要咋處理?
這三大遺留問題等待后續進一步處理