I would like to setup my microservices eco system in kubernetes. I have one environment let's say TEST in which I have deployed app1, app2 ,app3 and DB. Database is shared among the service. Assume all my services are deployed in a namespace called "stable". Now I have another namespace called "dev" in which I deployed only app1.
Now, I want to access the app2 and DB in "stable" namespace from app1 in "dev" namespace, if both are not deployed in "dev". i.e when I try to access any service from dev namespace, it should try to connect in the same namespace and if not exists it should go to 'stable'.
I want to achieve cross namespace communication with condition that if not exists in current namespace redirect to the next namespace. Is it possible?
Kubernetes supports 2 primary modes of finding a Service - "environment variables" and "DNS". While you are trying to conect services in different namespaces you should use full qualified svc name (the sv name with namespace) like your_svc1.namespace2.svc.cluster.local
As per documentation:
When you create a Service, it creates a corresponding DNS entry. This entry is of the form ..svc.cluster.local, which means that if a container just uses , it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN). More information about "Namespaces and DNS".
You can use also "Services without selectors" where endpoints can by defined by the user. Another example is "Service Type ExternalName" with an example.
According to the second question please refer to the "Pod’s DNS Policy"
It's helpful in when you need to adjust the upstream nameservers or search domain suffixes configured in resolv.conf. You can do this with the dnsConfig option during pod creation like in the example below. You can find more information here.
apiVersion: v1
kind: Pod
metadata:
namespace: default
name: dns-example
spec:
containers:
- name: test
image: nginx
dnsPolicy: "None"
dnsConfig:
nameservers:
- 1.2.3.4
searches:
- ns1.svc.cluster.local
- ns2.svc.cluster.local
options:
- name: ndots
value: "2"
- name: edns0
You'd need a loadbalancer service with all the upstreams defined for such a failover. Can be achieved with Istio if you want this centrally configured.