9

In Docker a client container can refer to a server container by different names by using link aliasing:

--link server-container:my_preferred_server

A second client can use its own choice of names

-- link server-container:server

Can I achieve this in K8s preferably by adding different records in K8s's DNS?

Note the solution we are using now is having the client containers not use a hardcoded name for the server container, but use the value of an env variable SERVER_HOSTNAME='server-container' and link without aliasing:

--link server_container

Edit: To answer to some of the questions got in the replies:
* there is more than one client container, otherwise aliasing would have no purpose. Each client knows the server by a different name
* the client and the server are not in the same pod

lcfd
  • 1,326
  • 10
  • 12
  • Do the containers that need to talk to each other reside on the same pod? – Yaron Idan Dec 08 '16 at 10:02
  • 1
    Have you tried to just add another service of a different name that selects the same pod? Also, if you're familiar with kubernetes services and have kube DNS setup, then none of these should be problems you need to solve. – iamnat Dec 08 '16 at 10:24
  • @Yaron different pods – lcfd Dec 09 '16 at 14:14
  • @iamnat adding another service will come with its own port definitions and at least another instance of the container, while all I want is name aliasing. – lcfd Dec 09 '16 at 14:18
  • 1
    Adding another service will not require another instance, the servicea merely register pods, they dont create any. – Yaron Idan Dec 09 '16 at 15:19

2 Answers2

9

I just had the same need and I was able to add an external Service, which points to the FQDN of the other service within the cluster:

apiVersion: v1
kind: Service
metadata:
  name: "my-alias"
spec:
  type: ExternalName
  externalName: "other-service.my-namespace.svc.cluster.local"

Now, I can access other-service as my-alias from within the cluster and namespace.

matlehmann
  • 392
  • 3
  • 8
  • Thanks, exactly what I was looking for: it works perfectly. It's weird there is not a native way to define a "service alias name", could be an useful option – Mauro Dorni Nov 25 '21 at 07:37
  • 2
    The `name` does not support adding dots `.`, which does not really make this a very reliable solution. Do you suggest any solution for this limitation? This is especially important in case I already have certificates for particular domain(s). – Mohammed Noureldin Nov 25 '22 at 00:23
2

There are a few ways to achieve that.

  • You can simply create a Service for the server container (in its own Pod), if the Pod is in the same Kubernetes cluster then a typical Service will do, if the server container is outside of Kubernetes, you can create a Service with ExternalIPs to define the IPs of the endpoints

  • If this is side-by-side deployment (i.e. 1 client <-> 1 server) you might consider keeping the client and server in the same Pod definition: in a single Pod, the client container can reach the server container via localhost on its specific port.

  • You can fiddle with kube-dns to inject arbitrary dns names (or even overwrite public ones), but you should really not have to do this.

MrE
  • 19,584
  • 12
  • 87
  • 105
Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48