Rikard Olsson
Rikard Olsson

Reputation: 869

Can I use multiple names for a Kubernetes service?

In Kubernetes, one communicates with a specific service X by doing http requests to http://X:9999. X here is the application name. I wonder, can one add multiple names, or alias, to which it will point to http://X:9999? I.e can I forward/point http://Y:9999 to http://X:9999?

Upvotes: 5

Views: 4007

Answers (1)

Serge
Serge

Reputation: 674

Answer

Yes you can have multiple hostnames point to the same Pod(s).

You can achieve this by creating multiple Services with the same label selectors.

Background

A service creates endpoints to Pod IPs based on label selectors.

Services will match their selectors against Pod labels.

If multiple Services (with different names) have the same label selectors, they will create multiple endpoints to the same Pods.

Example

First service:

apiVersion: v1
kind: Service
metadata:
  name: nginx1
  namespace: nginx
spec:
  selector:
    app: nginx
...

Second service:

apiVersion: v1
kind: Service
metadata:
  name: nginx2
  namespace: nginx
spec:
  selector:
    app: nginx
...

Each Service will create an endpoint pointing to any Pods with the label app: nginx.

So you could hit the same Pods using nginx2:<port> and nginx1:<port>.

Upvotes: 9

Related Questions