How to Discover non-Kubernetes applications using Kubernetes Service Discovery

How to use Ingress Resources to discover Non-Kubernetes services using NGINX and BIND DNS

Photo by Matteo Di Iorio on Unsplash

Pre Requisites

git clone https://github.com/bharatmicrosystems/kubernetes-nginx-service-discovery.git
kubectl apply -f kubernetes-nginx-service-discovery/ingress/mandatory.yaml
kubectl apply -f kubernetes-nginx-service-discovery/ingress/service-nodeport.yaml

Setting up the Bind DNS Server

On-Premise setup

git clone https://github.com/bharatmicrosystems/kubernetes-nginx-service-discovery.git
sed -i "s/example.com/<YOUR DOMAIN>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server.yaml
sed -i "s/dns_loadbalancer_ip_value/<YOUR DNS LOAD BALANCER IP>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server.yaml
sed -i "s/ingress_loadbalancer_ip_value/<YOUR INGRESS LOAD BALANCER IP>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server.yaml
kubectl apply -f kubernetes-nginx-service-discovery/bind-dns-server/dns-server.yaml

Cloud setup

git clone https://github.com/bharatmicrosystems/kubernetes-nginx-service-discovery.git
$ vim kubernetes-nginx-service-discovery/bind-dns-server/dns-server-lb-service.yaml
---
apiVersion: v1
kind: Service
metadata:
name: dns-server
annotations:
cloud.google.com/load-balancer-type: "Internal"
labels:
app: dns-server
spec:
type: LoadBalancer
ports:
- port: 53
targetPort: 53
protocol: UDP
selector:
app: dns-server
kubectl apply -f kubernetes-nginx-service-discovery/bind-dns-server/dns-server-lb-service.yaml
sed -i "s/example.com/<YOUR DOMAIN>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server-deployment.yaml
sed -i "s/dns_loadbalancer_ip_value/<YOUR DNS LOAD BALANCER IP>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server-deployment.yaml
sed -i "s/ingress_loadbalancer_ip_value/<YOUR INGRESS LOAD BALANCER IP>/g" kubernetes-nginx-service-discovery/bind-dns-server/dns-server-deployment.yaml
kubectl apply -f kubernetes-nginx-service-discovery/bind-dns-server/dns-server-deployment.yaml
kubectl edit -n kube-system cm/corednsapiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . 172.16.0.1
cache 30
loop
reload
loadbalance
}
+ <YOUR DOMAIN HERE>:53 {
+ errors
+ cache 30
+ forward . <YOUR DNS LOAD BALANCER IP>
+ }
sed -i "1 i\nameserver <YOUR DNS LOAD BALANCER IP>\noptions timeout:1" /etc/resolv.conf

Setting up the NGINX Load Balancer

sed -i "s/example.com/<YOUR DOMAIN>/g" kubernetes-nginx-service-discovery/nginx-load-balancer/nginx.yaml
kubectl apply -f kubernetes-nginx-service-discovery/nginx-load-balancer/nginx.yaml
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-backend-ingress
annotations:
nginx.ingress.kubernetes.io/add-base-url: "true"
nginx.ingress.kubernetes.io/proxy-body-size: "0"
labels:
backend-host: "nginx-backend-host1,nginx-backend-host2"
backend-port: "80"
spec:
rules:
- host: nginx.example.com
http:
paths:
- path: /
backend:
serviceName: nginx-load-balancer
servicePort: 80

Installing the Agent

mkdir -p <configuration_dir>
./kubernetes-nginx-service-discovery/agent/setup.sh <configuration_dir> <domain>
$ vim <configuration_dir>/example.json
[
{
"name": "nginx-backend-ingress-example",
"ingress_host": "nginx.example.com",
"server_host": "nginx-backend",
"port": "80"
}
]

Testing the setup

$ vim <configuration_dir>/example.json
[
{
"name": "nginx-backend-ingress-example",
"ingress_host": "nginx.<YOUR DOMAIN>",
"server_host": "<HOST NAME OF THE SERVER>",
"port": "80"
}
]
$ curl http://nginx.<YOUR DOMAIN>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Further Reading

Certified Kubernetes Administrator | Cloud Architect | DevOps Enthusiast | Connect @ https://gauravdevops.com | https://freedevtools.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store