Home > Blockchain >  How to access an application/container from dns/hostname in k8s?
How to access an application/container from dns/hostname in k8s?

Time:01-15

I have a k8s cluster where I deploy some containers.

The cluster is accessible at microk8s.hostname.internal.

At this moment I have an application/container deployed that is accessible here: microk8s.hostname.internal/myapplication with the help of a service and an ingress.

And this works great.

Now I would like to deploy another application/container but I would like it accessible like this: otherapplication.microk8s.hostname.internal.

How do I do this?

Currently installed addons in microk8s:

aasa@bolsrv0891:/snap/bin$ microk8s status
microk8s is running
high-availability: no
addons:
  enabled:
    dashboard            # (core) The Kubernetes dashboard
    dns                  # (core) CoreDNS
    helm                 # (core) Helm - the package manager for Kubernetes
    helm3                # (core) Helm 3 - the package manager for Kubernetes
    ingress              # (core) Ingress controller for external access
    metrics-server       # (core) K8s Metrics Server for API access to service metrics

Update 1: If I portforward to my service it works. I have tried this ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  namespace: jupyter-notebook
  annotations:
    kubernetes.io/ingress.class: public
spec:
  rules:
  - host: jupyter.microk8s.hostname.internal
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: jupyter-service
            port:
              number: 7070

But I cant access it nor ping it. Chrome says: jupyter.microk8s.hostname.internal’s server IP address could not be found.

My service looks like this:

apiVersion: v1
kind: Service
metadata:
  name: jupyter-service
  namespace: jupyter-notebook
spec:
  ports:
  - name: 7070-8888
    port: 7070
    protocol: TCP
    targetPort: 8888
  selector:
    app: jupyternotebook
  type: ClusterIP
status:
  loadBalancer: {}

I can of course ping microk8s.hostname.internal.

Update 2:

The ingress that is working today that has a context path: microk8s.boliden.internal/myapplication looks like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: jupyter-ingress
  namespace: jupyter-notebook
spec:
  rules:
    - http:
        paths:
          - path: "/jupyter-notebook/?(.*)"
            pathType: Prefix
            backend:
              service:
                name: jupyter-service
                port:
                  number: 7070

This is accessible externally by accessing microk8s.hostname.internal/jupyter-notebook.

CodePudding user response:

To do this you would have to configure a kube service, kube ingress and the configure your DNS.

Adding an entry into the hosts file would allow DNS resolution to otherapplication.microk8s.hostname.internal

You could use dnsmasq to allow for wildcard resolution e.g. *.microk8s.hostname.internal

You can test the dns reoslution using nslookup or dig

CodePudding user response:

You can copy the same ingress and update name of it and Host inside it, that's all change you need.

For ref:

kind: Ingress
metadata:
  name: second-ingress <<- make sure to update name else it will overwrite  if the same
spec:
  rules:
  - host: otherapplication.microk8s.hostname.internal
    http:
      paths:
      - path: /
        backend:
          serviceName: service-name
          servicePort: service-port

You can create the subdomain with ingress just update the Host in ingress and add the necessary serviceName and servicePort to route traffic to specific service.

Feel free to append the necessary fields, and annotation if any to the above ingress from the existing ingress which is working for you.

If you are running it locally you might have to map the IP to the subdomain locally in /etc/hosts file

/etc/hosts

otherapplication.microk8s.hostname.internal <IP address>
  • Related