Kubernetes service routing with wildcard DNS and ingress controller
Kubernetes wildcard DNS refers to a DNS configuration that allows for routing any subdomain of a domain to a particular service or set of services within a Kubernetes cluster. A wildcard DNS record is usually indicated by an asterisk (*), for example: *.yourdomain.com
.
Using wildcard DNS with Kubernetes has several advantages:
- Without wildcard DNS, each time you deploy a new service and want to expose it with a domain name, you would have to create a new DNS record. With wildcard DNS, any subdomain of
yourdomain.com
(likeservice1.yourdomain.com
,service2.yourdomain.com
, etc.) will automatically resolve to the IP address specified in the wildcard record. - Wildcard DNS is especially useful for development and staging environments where you might frequently spin up and tear down services. The wildcard DNS ensures that these services get valid DNS names without additional configuration.
- When used in conjunction with an ingress controller (like Nginx or Traefik), wildcard DNS can be powerful. The ingress controller can route traffic based on the hostname, meaning that while the wildcard DNS points all subdomains to the ingress controller, the controller itself determines which service should handle the request based on its configuration.
In short, Kubernetes wildcard DNS, combined with an ingress controller, provides a powerful way to dynamically route external traffic to different services in the cluster based on hostname patterns.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have created a Scaleway Kubernetes cluster
- You have
helm
installed on your local computer - You have a domain name
- The service you want to expose is a
TCP
orHTTP
one
Installing the ingress controller with helm
-
Add the Helm repository:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginxhelm repo update -
Install the Nginx ingress controller using the helm packet manager:
helm install nginx-ingress ingress-nginx/ingress-nginx \--namespace ingress-nginx --create-namespace -
Verify the installation:
kubectl get pods -n ingress-nginx
Configuring wildcard DNS
-
Retrieve the ingress IP:
kubectl get svc -n ingress-nginxNote:It may take a few minutes for the Load Balancer IP to be assigned.
-
Configure Wildcard DNS:
- Go to your DNS provider and add an A record for
*.yourdomain.com
pointing to the IP address of the ingress controller’s load balancer.
- Go to your DNS provider and add an A record for
Deploy a sample application
-
Deploy a sample application by creating a file
hello-world.yaml
. Below is a simple deployment and service example. Copy the content in the file and save it:apiVersion: apps/v1kind: Deploymentmetadata:name: hello-worldspec:replicas: 2selector:matchLabels:app: hello-worldtemplate:metadata:labels:app: hello-worldspec:containers:- name: hello-worldimage: nginxdemos/helloports:- containerPort: 80---apiVersion: v1kind: Servicemetadata:name: hello-worldspec:ports:- port: 80selector:app: hello-world -
Apply the configuration with
kubectl apply -f hello-world.yaml
Create an ingress resource
-
Copy the following sample resource and paste and save it into a YAML file called
wildcard-ingress.yaml
. Remember to replace the domain name with your own.apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: wildcard-ingressannotations:nginx.ingress.kubernetes.io/rewrite-target: /spec:rules:- host: "*.yourdomain.com"http:paths:- path: /pathType: Prefixbackend:service:name: hello-worldport:number: 80 -
Apply the configuration with
kubectl apply -f wildcard-ingress.yaml
Test your setup
-
Access your application by pointing your web brower to
http://anything.yourdomain.com
(replace this with your own domain name). It should load the hello-world application. -
Test with different subdomains, they should all lead to your
hello-world
application due to the wildcard DNS setup.
You have successfully set up an ingress controller with wildcard DNS on Scaleway Kubernetes Kapsule. This setup will allow you to easily manage multiple subdomains and route traffic to the various services in your Kubernetes cluster.