Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on

Different way to test Kubernetes Ingress, DNS Records, Host file and Host Header.

What is Kubernetes Ingress?

Kubernetes Ingress is a way to route external traffic to services within a Kubernetes cluster. This can be useful if you have services that need to be accessible from the outside world, such as a web application. Ingress can provide load balancing, SSL termination, and name-based virtual hosting.

Kubernetes Ingress is implemented as a Kubernetes resource. This means it can be created, updated, and deleted using the kubectl command-line tool.

To use Kubernetes Ingress, you must have a Kubernetes cluster up and running. You can follow the instructions in the Kubernetes documentation to set up a cluster.

Once you have a Kubernetes cluster, you can deploy Ingress using the kubectl command-line tool.

Save the following content in a file name called sample-ingress-with-pods.yaml.

---
kind: Pod
apiVersion: v1
metadata:
  name: goglides-foo-app
  labels:
    app: goglides-foo
spec:
  containers:
  - name: goglides-foo-app
    image: hashicorp/http-echo:0.2.3
    args:
    - "-text=goglides-foo hoooo"
---
kind: Service
apiVersion: v1
metadata:
  name: goglides-foo-service
spec:
  selector:
    app: goglides-foo
  ports:
  # Default port used by the image
  - port: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: goglides-host-ingress
spec:
  rules:
  - host: "foo.goglides.local"
    http:
      paths:
      - pathType: Prefix
        path: "/foo"
        backend:
          service: 
            name: goglides-foo-service
            port:
              number: 5678
---
Enter fullscreen mode Exit fullscreen mode

To deploy this resource, you would run the following command:

kubectl create -f sample-ingress-with-pods.yaml
Enter fullscreen mode Exit fullscreen mode

Resources are created now to test ingress workflow; you can achieve this in various ways. Here is the list of what you need:

  • External DNS pointing to the Kubernetes cluster
  • Update /etc/host pointed to ingress service object
  • Pass the "Host" header in the curl command

We will not get into the details of every one of them but will touch base on what is required to test it.

Update DNS records:

The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network. It associates various information with domain names assigned to each participating entity. Most prominently, it translates more readily memorized domain names to the numerical IP addresses needed for locating and identifying computer services and devices with the underlying network protocols. By providing a worldwide, distributed directory service, the Domain Name System is an essential component of contemporary Internet usage.

You can update your DNS records if you want to make your changes permanent. You'll need to log in to your DNS provider and add a new A record. The A record should point to the IP address of the server you want to map the hostname too.

For example, if you're using Cloudflare, you would log in to your account and add a new A record like this:

Hostname: www.example.com
IP Address: 192.168.99.100
Enter fullscreen mode Exit fullscreen mode

Once you've added the new record, it can take up to 24 hours for the changes to propagate. After that, anyone who types www.example.com into their browser will be taken to your server.

Typically and in production, you would want to update the DNS record of your domain so that when users type in the URL, it resolves to the correct IP address. If you do not have a domain name that you can use, for testing you can use a tool like nip.io. This will give you a domain name that resolves your local IP address.

For example, if your IP address is 192.168.99.100, you can use a domain name like foo.192.168.99.100.nip.io. For example

host foo.192.168.99.100.nip.io && nslookup foo.192.168.99.100.nip.io
Enter fullscreen mode Exit fullscreen mode

It gives you the following output:

foo.192.168.99.100.nip.io has address 192.168.99.100

Server:     192.168.7.213
Address:    192.168.7.213#53

Non-authoritative answer:
Name:   foo.192.168.99.100.nip.io
Address: 192.168.99.100
Enter fullscreen mode Exit fullscreen mode

Updating /etc/hosts file:

The /etc/hosts file is a simple text file that maps hostnames to IP addresses. It is a part of the Domain Name System (DNS). When a computer wants to contact a hostname, such as www.example.com, it will check this file to see if there is an entry for that hostname. If there is, it will use the corresponding IP address. If not, it will ask a DNS server for the IP address.

To update your hosts file manually, open it in a text editor and add a new line for each hostname you want to map. The line should look like this:

IP_address hostname aliases
Enter fullscreen mode Exit fullscreen mode

For example, to map the hostname www.example.com to the IP address 192.168.99.100, you would add the following line to your hosts file:

192.168.99.100 www.example.com
Enter fullscreen mode Exit fullscreen mode

Some other examples,

127.0.0.1 localhost
127.0.1.1 my-laptop
Enter fullscreen mode Exit fullscreen mode

Pass "Host" header in curl

The "Host:" header is a standard way an HTTP client tells the HTTP server which site it wants to look at. By passing a custom "Host:" header, you can make the server respond with the content of that site, even if you didn't connect to that site's name. For example, if you request to www.example.com with a "Host:" header of www.foo.com, the server will think you're trying to look at www.foo.com and will respond with that site's content, even though you're technically still connected to www.example.com.

This can be useful for several reasons. For one, it can help you bypass restrictions that are in place on specific sites. If a site is only meant to be accessed from specific locations, you can use a proxy server to make it look like you're coming from the allowed location and then use a "Host:" header to access the site as if you were directly connected to it.

Another use for this technique is to bypass content filters. If a filter blocks access to specific sites, you can often get around it by connecting to a different site and then using a "Host:" header to request the blocked site.

Of course, this technique can also be used maliciously to steal sensitive information or to spread viruses and other malware. For this reason, it's essential to be careful when using "Host:" headers and only do so on sites you trust.

You can use the Host header in the curl command as follows,

curl -H 'Host: www.example.com' http://server-ip
Enter fullscreen mode Exit fullscreen mode

Conclusion:

This article shows how to map a hostname to an IP address using DNS records, host file, and host header in the curl command. DNS record is the permanent way to make changes if you want to expose your application to the outside world. But if you just want to test your ingress config, you can use other approaches like the hosts file and host header in the curl command. These methods are not permanent, and you must make changes every time your IP address changes. This can be useful if you want to access a site without going through a DNS server or if you want to test a site before making it live.

Top comments (1)

Collapse
 
jeewangautam profile image
Jeewan Gautam

well explained. Easy to understand dada😊