Brief introduction
Recent research k8s, transform the way the company springcloud bit architecture to better adapt to the use k8s to deploy. I encountered some problems during their own ways to solve. In this record.
1. Provide the problems and consumers to register with the eureka
As we all know, springcloud architecture is a registry, either the service provider or service consumer must be registered with the registry. In a previous post, we have been introduced to how to deploy eureka k8s. Next, we should be registered to other services in eureka, but when springcloud default registration to the registry, based on the host name: The instance name: Port of registration forms up, the registration results are cloud-eureka-server-6f59b94548-fc9gl: o2o-gateway: 9999, when calling between such services will be reported unknowHostException exception. To solve this problem, you can add configuration springcoud project, so that when the service registered with the registration center, registration forms to change the host IP: PORT way.
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} #springcloud版本不同,此处语法会稍有差异
eureka.instance.prefer-ip-address=true
Such services registered with the eureka result will look as shown below
Note: Deployment springcloud on k8s, registered address is up pod Address
2. Multi environmental issues
All companies will certainly be development, testing, production and other sets of different environments, and different environments using different profiles, we can not all environments are playing a mirror image of the corresponding environment, the ideal situation should be that all use the same environment a mirror, but when deployed in different environments, use the configuration file is different. So how to achieve it? We can consider all the places related to the configuration of multi-environment variable in the deployment of these variables were given different values to achieve so that the project to load a different environment profiles purposes.
Take springcloud gateway service to explain how I did it
spring.cloud.config.uri=http://${config.host:config-service}:${config.port:9999}
spring.cloud.config.name=o2o-gateway
spring.cloud.config.profile=${config.profile:default}
eureka.client.serviceUrl.defaultZone=http://${eureka.host:eureka-service}:9761/eureka/
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.prefer-ip-address=true
#client name
spring.application.name=cloud-o2o-gateway
In the top of the code, I place that involves multiple environmental variables are made of, whether it is the configuration center address, registered address or listening port center
Then I look at the configuration file dockerfile
FROM registry.11wlw.cn/common/java:8u111-jdk
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone
ADD /cloud-zuul-server-1.0.0.jar /
ADD ./run.sh /
ENV JAVA_OPTS=""
ENV EUREKA_HOST="eureka-service"
ENV CONFIG_HOST="config-service"
ENV CONFIG_PROFILE="default"
ENV SERVER_PORT="8105"
ENTRYPOINT [ "sh", "-c", "/run.sh" ]
I set up and bootstrap.properties corresponding environment variables dockerfile file and given a default value.
Look at the contents of run.sh
java $JAVA_OPTS -jar /cloud-zuul-server-1.0.0.jar \
--eureka.host=$EUREKA_HOST --config.host=$CONFIG_HOST --config.profile=$CONFIG_PROFILE --server.port=$SERVER_PORT
When the container starts, it will go to read the value of an environment variable to determine which service registry to configure the environment to the center, to which configuration center environments pull configuration files, which environmental profile pull and which port to listen.
So when we deploy springcloud with k8s, by deploying yaml file reassign these different environment variable values can achieve the purpose of solving this problem.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: cloud-zuul-server
namespace: local
spec:
replicas: 1
template:
metadata:
labels:
app: cloud-zuul-server
spec:
containers:
- name: cloud-module-auth
image: registry.11wlw.cn/o2o/cloud-zuul-server
env:
- name: EUREKA_HOST
value: eureka-service
- name: CONFIG_HOST
value: config-service
- name: CONFIG_PROFILE
value: newqa
tty: true
ports:
- containerPort: 8105
Yaml above us to re-file as the startup parameter bootstrap.properties file assignment by the env parameter, the container up, springcloud service will be registered to a different environment, load a different configuration files.
3 Configuration Center
springcloud architecture has its own configuration central component is springConfig. I do not know if other companies in each environment take a distribution center? But I think, for the distribution center, all environment can be shared only one set, when deployed, by way of a different value to CONFIG_HOST and CONFIG_PROFILE given.
Note: To go through the entire architecture is built on k8s installed kubedns, ingress on the basis of
- springcloud each item and configured eureka is a eureka config configurations and the two pod config service name of the service, to resolve for the post-deployment by ip kubedns
- springcloud service gateway service is exposed outwardly through the inlet ingress
- In addition to these three springcloud services, other services not necessary for the creation pod service, can be determined according to needs