问题概述
本文主要为了解决,在使用Docker部署Spring Boot应用,Spring Boot在向Eureka注册时,如何配置正确IP的问题。
解决方案配置
先把最终解决方案的配置贴出来:
- Gitlab Runners,该机器的网卡和IP信息如下。可以看到本机eth0网卡的IP为
10.16.180.7
,docker0网卡的IP为172.17.0.1
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:50:e1:74:da txqueuelen 0 (Ethernet)
RX packets 114151657 bytes 17800379440 (16.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 217089263 bytes 288568000281 (268.7 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker_gwbridge: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:99:31:8f:ad txqueuelen 0 (Ethernet)
RX packets 182761 bytes 11494613 (10.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 139451 bytes 9795778 (9.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.16.180.7 netmask 255.255.240.0 broadcast 10.16.191.255
ether fa:16:3e:0e:3d:fd txqueuelen 1000 (Ethernet)
RX packets 442851873 bytes 307687290719 (286.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 356404292 bytes 51936607569 (48.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 0 (Local Loopback)
RX packets 649114 bytes 47094870 (44.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 649114 bytes 47094870 (44.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth0133ad0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 96:6f:6d:89:29:7b txqueuelen 0 (Ethernet)
RX packets 945694 bytes 2541883907 (2.3 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1978551 bytes 2484265627 (2.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...
在注册Gitlab Runner时,Description填上该机器的IP地址:10.16.180.7
- 采用Gitlab CI发布Spring Boot应用,.gitlab-ci.yml文件如下:
image: docker-registry.qiyi.virtual/ads-bi/docker:dind
stages:
- build
- deploy
cache:
paths:
- .m2
job-build:
image: docker-registry.qiyi.virtual/ads-bi/maven:3.5-jdk-8-slim
tags:
- test-env
stage: build
script:
- source ~/.bashrc && mvn -Dmaven.repo.local=.m2 -Duser.timezone=GMT+08 clean install test sonar:sonar
artifacts:
name: "$CI_PROJECT_NAME"
paths:
- target/*.jar
job-deploy:
image: docker-registry.qiyi.virtual/ads-bi/docker:dind
environment: production
tags:
- test-env
stage: deploy
before_script:
- SERVER_PORT=`awk -v min=10000 -v max=20000 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'`
script:
- docker build -t docker-registry.qiyi.virtual/ads-bi/${CI_PROJECT_NAME}:prod
--build-arg spring_profiles_active=prod
--build-arg server_port=${SERVER_PORT}
--build-arg spring_application_name=${CI_PROJECT_NAME}
--build-arg ip_address=${CI_RUNNER_DESCRIPTION}
.
- docker push docker-registry.qiyi.virtual/ads-bi/${CI_PROJECT_NAME}:prod
- sh ./scripts/start_job.sh ${CI_PROJECT_NAME} prod ${SERVER_PORT}
- docker rmi --force $(docker images | grep " <none>" | awk '{print $3}') || true
- Spring Boot的启动shell脚本
#! /bin/bash
CI_PROJECT_NAME=$1
ENV=$2
SERVER_PORT=$3
TIMESTAMP=`date +%s`
CID=`docker ps | grep ${CI_PROJECT_NAME} | awk '{print $1}'`
if [ ! -z ${CID} ]; then
docker stop ${CID} | xargs docker rm
fi
docker run -d -p ${SERVER_PORT}:${SERVER_PORT} -v /var/log:/var/log --name ${CI_PROJECT_NAME}-${TIMESTAMP} docker-registry.qiyi.virtual/ads-bi/${CI_PROJECT_NAME}:${ENV}
- Dockerfile
FROM docker-registry.qiyi.virtual/ads-bi/maven:3.5-jdk-8-slim
VOLUME /tmp
ADD target/*.jar /app.jar
ARG spring_profiles_active
ARG server_port
ARG spring_application_name
ARG ip_address
ENV SPRING_PROFILES_ACTIVE=${spring_profiles_active}
ENV SERVER_PORT=${server_port}
ENV SPRING_APPLICATION_NAME=${spring_application_name}
ENV EUREKA_INSTANCE_IP-ADDRESS=${ip_address}
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar", "/app.jar"]
详细解读
这里需要先介绍一下我们面临的环境。首先,我们配置了一个Gitlab的Specific Runner,作为Spring Boot构建的实际机器。在这台机器上,需要安装Docker环境,那么这台机器就有了至少两个IP地址,分别为:
- 本机网卡eth0,ip地址为10.16.180.7
- Docker网卡docker0,ip地址为172.17.0.1
这里我们最终想在Eureka中注册的地址,是本机网卡的地址。
因为Spring Cloud Admin是通过Eureka中注册的地址,来判断Spring Boot应用是否健康的,如果注册使用的是docker0的地址,这个地址并不能通过http访问,Spring Cloud Admin会认为该应用是Down的,虽然它实际的状态是UP的。
在网上,我们能找到不少介绍Spring Cloud Eureka的多网卡选择的文章,例如这篇文章介绍的就很好:
https://blog.csdn.net/xichenguan/article/details/76632033
这里提到了几个重要的Spring Cloud配置:
- eureka.instance.prefer-ip-address: true/false
- spring.cloud.inetutils.preferred-networks: list<ip>
- spring.cloud.inetutils.ignored-interfaces: list<ip>
关于这几个配置项的含义,可以参考Spring Cloud的官方文档,介绍的更加准确:
https://cloud.spring.io/spring-cloud-static/spring-cloud.html#ignore-network-interfaces
这几个配置的目的,就是Spring Boot在启动时,会在多网卡的环境,根据配置项的信息,进行网卡的选择。找到这里,我们认为可能通过这项配置就可以选择到想要的ip地址了。
但实际并不是这样的,在经过无数次的尝试之后,我们发现在Eureka中每次注册的ip地址始终都是:
这个问题我们使用百思不得其解,在翻看这部分的源码时,我们发现Spring Boot使用的是InetUtils类进行ip选择的。这部分的源码在遍历每个可用的网络之后,结合我们配置的preferred和ignored信息,选择index最小的这个ip作为最终的返回ip。
public InetAddress findFirstNonLoopbackAddress() {
InetAddress result = null;
try {
int lowest = Integer.MAX_VALUE;
for (Enumeration < NetworkInterface > nics = NetworkInterface
.getNetworkInterfaces(); nics.hasMoreElements();) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {
log.trace("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
} else if (result != null) {
continue;
}
// @formatter:off
if (!ignoreInterface(ifc.getDisplayName())) {
for (Enumeration < InetAddress > addrs = ifc
.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address &&
!address.isLoopbackAddress() &&
!ignoreAddress(address)) {
log.trace("Found non-loopback interface: " +
ifc.getDisplayName());
result = address;
}
}
}
// @formatter:on
}
}
} catch (IOException ex) {
log.error("Cannot get first non-loopback address", ex);
}
if (result != null) {
return result;
}
try {
return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
log.warn("Unable to retrieve localhost");
}
return null;
}
这里我们发现,可以通过调整log的打印等级,将选择过程中trace等级的日志打印出来。在调整之后,查看服务器端这部分的日志如下:
2018-05-04 06:43:00.610 [main] DEBUG org.elasticsearch.common.network -configuration:
lo
inet 127.0.0.1 netmask:255.0.0.0 scope:host
inet6 ::1 prefixlen:128 scope:host
UP LOOPBACK mtu:65536 index:1
eth0
inet 172.17.0.5 netmask:255.255.0.0 broadcast:0.0.0.0 scope:site
inet6 fe80::42:acff:fe11:3 prefixlen:64 scope:link
hardware 02:42:AC:11:00:03
UP MULTICAST mtu:1500 index:4899
...
2018-05-04 06:43:02.332 [main] TRACE org.springframework.cloud.commons.util.InetUtils -Testing interface: eth0
2018-05-04 06:43:02.332 [main] TRACE org.springframework.cloud.commons.util.InetUtils -Found non-loopback interface: eth0
2018-05-04 06:43:02.332 [main] TRACE org.springframework.cloud.commons.util.InetUtils -Testing interface: lo
这时我们才明白,原来我们采用的是dind的这个image,进行Spring Boot Application Docker镜像的生成,发布和启动。那么在构建时,会生成一个dind的container,这个container的网卡信息如上所示。所以我们配置时,所有可选的网卡并不属于Gitlab Runner这台机器,而是Docker container的,那么之前关于多网卡的配置无论如何都是无用的了。
在发现问题的原因之后,我们发现这里需要做的,是把Docker所在的主机的网卡ip信息,传递到container中。由于这两个环境是隔离的,所以并不现实。
那么最终如何才能把Docker所在主机的ip,顺利的传递到container中呢,我们利用了Gitlab Runner注册时填写的信息。我们在Gitlab Runner注册时,将该机器的ip,填写到Description中。在.gitlab-ci.yml中,可以利用内置参数${CI_RUNNER_DESCRIPTION},将此ip传递到Dockerfile中。在Dockerfile中,通过设置系统环境变量的方式,使Spring Boot获取到这个环境变量。
2018-05-04 06:37:34.611 [main] TRACE o.s.core.env.PropertySourcesPropertyResolver -Searching for key 'eureka.instance.ip-address' in PropertySource 'servletConfigInitParams'
2018-05-04 06:37:34.611 [main] TRACE o.s.core.env.PropertySourcesPropertyResolver -Searching for key 'eureka.instance.ip-address' in PropertySource 'servletContextInitParams'
2018-05-04 06:37:34.611 [main] TRACE o.s.core.env.PropertySourcesPropertyResolver -Searching for key 'eureka.instance.ip-address' in PropertySource 'systemProperties'
2018-05-04 06:37:34.611 [main] TRACE o.s.core.env.PropertySourcesPropertyResolver -Searching for key 'eureka.instance.ip-address' in PropertySource 'systemEnvironment'
2018-05-04 06:37:34.611 [main] DEBUG o.s.core.env.SystemEnvironmentPropertySource -PropertySource 'systemEnvironment' does not contain property 'eureka.instance.ip-address', but found equivalent 'EUREKA_INSTANCE_IP-ADDRESS'
2018-05-04 06:37:34.611 [main] DEBUG o.s.core.env.PropertySourcesPropertyResolver -Found key 'eureka.instance.ip-address' in PropertySource 'systemEnvironment' with value of type String
这样就实现了Spring Boot的ip指定,在Eureka中注册的我们想要的Gitlab Runner主机ip,最终结果图如下:
Spring Cloud Eureka:
Spring Cloud Admin:
启示
- 使用Docker部署应用时,需要理清楚各个环境之间的关系
- 在遇到难解的问题,通过查看源码,并且分析打印日志的方式,定位问题原因