Reprint please indicate , If you like, please click three times
List of articles
- List of articles
- One 、 Sync Kubernetes and Consul service
- Two 、Consul Sync install
- 3、 ... and 、 summary
- Four 、 Reference documents
To improve the availability of production , Reduce manual O & M costs , Now let's use k8s Choreography and deployment has become a trend . As we gradually K8s When migrating to , How to solve the current transition Architecture Service discovery The question of ？
（ Getting through service discovery may just be the first step , In the future, we may do cross platform service governance based on service discovery , We will analyze this situation later ）
This article is mainly based on Consul Solutions for , To achieve Not K8s Service（ Running on the K8s Outside ） And Native K8s Service Between the Service discovery and Interoperability .
Simply speaking , Is to achieve K8s Outside the cluster The application can find calling K8s Service , meanwhile K8s Inside The application can call in the cluster native service Call services outside the cluster as well .
Consul Based on this scenario , Yes Consul-Sync Components , To achieve Consul and Kubernetes A solution for services to synchronize with each other .
Consul Sync Components can be synchronized automatically Kubernetes Service Sign up to Consul Catalog in .
synchronous K8s services, Make it possible for users to Consul Native service discovery is used to discover and connect services K8s In the service , Such as DNS or HTTP.
（ Span K8s Service discovery of cluster , It can also be done like this ）
Synchronized services will be registered to
k8s-sync Node , It's not a real Consul node , Unlike Consul Client Node to register and monitor services . Relative ,
k8s-sync What the node monitors is K8s, And synchronize services to Consul.
synchronous Service type 、 Synchronous namespace 、 synchronous Service Can be configured 、 Comments to set , In the future, I will write a separate article to explain the configuration .
PS: It is worth noting that , By default K8s Virtual... Used within IP, So not all Services All types can be accessed outside the cluster .
At present, there are four kinds of support K8s services Sync ： NodePort、LoadBalancer、External IPs、Cluster IP, If the service type does not belong to these four , Will not be synchronized . The synchronization of each type is as follows , I made a table .
|Service type||NodePort||LoadBalancer||External IPs||Cluster IP|
|The service name||service name + namespace||service name + namespace||service name + namespace||service name + namespace|
|Number of service instances||Pod Count||External IP Count||External IP Count||Pod Count|
|Service instance address （Address）||Node External IP||LB External IP||External IP||Pod IP|
|Service instance port （Port）||Node port||first port||first port||first target port|
PS: About the service name 、 Service port 、 Service Tag 、 Service Metadata All of them can actually pass through Consul Annotation or configuration to customize , In the next section, we will introduce .
I've prepared three examples , One of me Nginx Deployment , Three copies of , Colleagues create three types of Service , Respectively ClusterIP、NodePort、LoadBalancer, Let's take a look at their synchronization effects .
Take a look Consul In the cluster , Service after synchronization .
Generate three service instances .
Generate three service instances , Each service instance IP Corresponding Node Public network IP
- LoadBalancer（External IP）
LoadBalancer type , The official website said that a service instance was generated , But after I measured , When I am LB There are two Ip when （ Like an intranet , A public network ）, This situation produces two service instances , This is the sum of External Ip It's a bit similar .
External IP Not at all K8s Service type , It's a kind of Specified external Ip The situation of , If a service specifies External Ip, So this service can also be synchronized .
In this case , External Ip It might have been set up by other systems , however External Ips It must be resolved by other service discovery system , Don't be empty IP.
But notice this type , Under normal circumstances , Pod Ip It's empty Ip, Is not accessible outside the cluster . If you don't want to synchronize this ClusterIP type , Can pass Helm Turn off the configuration in
In fact, in the case of public cloud , commonly K8s Internal IP Can use directly VPC IP, So in the cluster 、 The network outside the cluster is flat , Can communicate with each other .
about Consul Every one of them Service Will be in K8s Create a
external name by Consul DNS name . for example Consul There is a name in foo Of Service , So created K8s Service Will be as follows
apiVersion: v1 kind: Service metadata: name: foo annotations: consul.hashicorp.com/service-sync: "false" labels: consul: "true" ... spec: externalName: foo.service.consul type: ExternalName
Be careful :
foo.service.consul Need to use Consul DNS Can be resolved to Oh .
// 127.0.0.1 Can be replaced with Consul Service, If Consul It's deployed in K8s Medium . dig @127.0.0.1 -p 8600 redis.service.dc1.consul. ANY
Add ： All synchronized services will bring
consul: "true" , So it's easy to screen out .
Let me show you an example , stay Consul In the cluster , There will be a
Consul Service , Let's take it as an example to see what happens after synchronization .
Take a look Kubernetes After synchronization Consul service .
Actually from Consul towards Kubernetes Synchronization is not necessary , It can be based on Kubernetes Of DNS programme , To configure Consul DNS. For example, if you're using KubeDNS see stub-domain configuration, If in use CoreDNS, May refer to proxy configuration.
Once configured Consul DNS, When we use
<consul-service-name>.service.consul When the format initiates a request ,Consul DNS Will be parsed into Consul Inside Service, All of the K8s All spaces can work .
Of course , If you want to pass
service-name Format to access , It still has to be synchronized .
About Consul DNS stay K8s It can be used for reference ： Consul DNS on Kubernetes
Limited to the length of the article , Let's put it in the next article Consul Sync Configuration and related installation of .
take Kubernetes Service Synchronize to Consul Catalog, You can make Consul All nodes in the cluster can access these services . Moreover, this scheme can be used as a variety of K8s Cluster service discovery scheme . For non K8s node （ Outside the cluster ） Can pass Consul DNS perhaps HTTP API Get service information .
After synchronization ,K8s Services within can be called like K8s It's the same as in-house service , Call external services . It's also similar to the automation of external changes like some databases .