Home Best Practices Add a Custom Host to Kubernetes

Add a Custom Host to Kubernetes

Author

Date

Category

it's dns it can't be dns

I’ve been working with different MicroK8s instances a lot in the last months. Microk8s, from my point of view, it’s the absolutely perfect abstraction for deploying Kubernetes applications on a “non-cluster” environment.

Although I’m a happy user, there’s a constant pain point that I keep hitting: custom hosts. MicroK8s resolves the domains using the Google DNS (8.8.8.8, 8.8.4.4) and this means that if you need to reach some custom host from inside a pod, you can’t! Well, actually you can! How? Let’s do it!

First, edit the ConfigMap of the coredns using the following command:

kubectl -n kube-system edit configmap/coredns

Add a section called “hosts custom.hosts” where you define the address you want to use (mycustom.host in the example) and point it to the IP address you need (1.2.3.4 in the example). Your ConfigMap should look like this:

data: 
  Corefile: |
      .:53 {
          errors
          health
          ready
          kubernetes cluster.local in-addr.arpa ip6.arpa {
            pods insecure
            fallthrough in-addr.arpa ip6.arpa
          }
          prometheus :9153
          forward . 8.8.8.8 8.8.4.4
          cache 30
          loop
          reload
          loadbalance
          hosts custom.hosts mycustom.host {
            1.2.3.4 mycustom.host
            fallthrough
          }
       }

At this moment, you’ll just need to delete the current core-dns pod and wait for the new one to spawn:

kubectl get pod -A

It’s done! You can now reach that custom host from inside any pod on the cluster and stop googling weird ways of injecting lines in each pod hosts file.

Image for post

Over two years ago, the Codacy team decided to switch their cloud infrastructure to Kubernetes.
If you’d like to learn more about our experience switching to k8s check out blog posts such as DNS Problems Scaling To Kubernetes by our DevOps team. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

7 drawbacks of linting tools

Linting tools (also known as linters or static analyzers) help automate the code review process. They perform basic static code analysis by flagging programming...

Using the API to add Codacy Grade details to the Readme

Some context Codacy has a badge mechanism that can be included in your Readme file. It gives you an idea of the grade of your repository, from...

March Product Update: Support for Cloud Infrastructure-as-code, Custom Reports with API endpoints & more 🚀

Here are some fresh updates from March! This month we bring you a new product offering, new features, and product updates, interesting reads, and...

Top 10 ways to perform fast code review

We always want to be fast at code review.. How frequent is it for you to be reviewing code at 3am? When code reviewing, do you...

Interview with Gary McKay, Somos’ Director of Agile Service Delivery

Somos is a proven leader in registry management and data solutions. Somos fosters meaningful connections by delivering value, innovation and confidence to consumers. We...