How to assign a DNS name to an application in a local Kubernetes cluster?

11/11/2019

I have a local Kubernetes cluster based on MicroK8s running on an Ubuntu 18.04 machine.

What I want to achieve: Generally I want to expose my applications to DNS names and test them locally.

My setup:

I created the following test deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-app
  labels:
    app: hello-app
    tier: backend
    version: v1
spec:
  selector:
    matchLabels:
      app: hello-app
  replicas: 2
  template:
    metadata:
      labels:
        app: hello-app
    spec:
      containers:
      - name: hello-app
        image: localhost:5000/a-local-hello-image
        ports:
        - containerPort: 3000

I added the following service descriptor:

apiVersion: v1
kind: Service
metadata:
  name: hello-app
spec:
  selector:
    app: hello-app
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000

Now I want to see my app available, let's say, at http://hello.someurl.com:3000.

Question: What do I need to setup in addition to my current configuration to map my application to a DNS name locally?

Note: I have read the documentation which unfortunately didn't help. I also enabled DNS addon on my cluster.

I would appreciate any help, any directions on how to move forward.

-- Sasha Shpota
dns
kubernetes
microk8s

2 Answers

11/12/2019

The easiset way to achieve what you want would be using

kubectl port-forward service/hello-app 3000:3000

and appending following entry to /etc/hosts file

127.0.0.1 hello.someurl.com

Then you can just open your browser and go to http://hello.someurl.com:3000

-- HelloWorld
Source: StackOverflow

11/12/2019

You can create an Ingress resource, like the one below:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  tls:
  - secretName: tls 
  rules:
  - host: site1.domain.com
    http:
      paths:
      - path: /path1/
        backend:
          serviceName: service1
          servicePort: 80
      - path: /path2/
        backend:
          serviceName: service2
          servicePort: 8080
      - path: /
        backend:
          serviceName: service3
          servicePort: 80
  - host: site2.domain.com
    http:
      paths:
      - backend:
          serviceName: service1
          servicePort: 80

To make this work, you would need an Ingress Controller, like Nginx, Traefik, etc.

Once you have got Ingress Controller (if you haven't already), and have created the Ingress resource (the yaml above), you can forward the requests depending on the host and path to the desired service. As an example, if I would want to send traffic to service2, I would do:

curl -H "site1.domain.com/path2/" INGRESS_IP:INGRESS_PORT. The request would go through the Ingress Controller, and it would deliver the packet to service2. If you modify the service name and port by yours, it should work fine.

You can also put it in /etc/hosts, of course.

And you could set up as many domains and paths as you wish in 1 singe ingress resource.

-- suren
Source: StackOverflow