Blog
Engineering
Kubernetes
3
minutes

Kubernetes Tips: How to find the Port of a Service with a DNS request

Last week I created a guide for our users to set up an NGINX service as an API Gateway with Qovery. The API gateway must redirect the incoming traffic to the appropriate service with the correct port. My problem is that the API Gateway does not know the ports exposed for every service. In this post, I will show you a quick tip on finding the port of a Kubernetes service with a single DNS request. Let's go!
Romaric Philogène
CEO & Co-founder
Summary
Twitter icon
linkedin icon

The tip

As you probably know, DNS service runs on Kubernetes to resolve the local service names. When your app A needs access to an app B from the same namespace, you will use it as a root domain "app-b.svc". Then your app will look at the nameserver to target (the one running on Kubernetes) and request to resolve "app-b.svc" into an IP address. For your application, it is transparent.

I don't want to make it too long, so here is the DNS request to get the port of a Kubernetes service:

dig +answer srv *._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local

You should get an output similar to this one:

; <<>> DiG 9.16.22 <<>> +answer srv *._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12263
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 9f7c41b86a20fb70 (echoed)
;; QUESTION SECTION:
;*._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local. IN SRV

;; ANSWER SECTION:
*._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local. 5 IN SRV 0 100 80 app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local.

;; ADDITIONAL SECTION:
app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local. 5 IN A 172.20.46.46

;; Query time: 4 msec
;; SERVER: 172.20.0.10#53(172.20.0.10)
;; WHEN: Mon Jan 24 12:13:15 UTC 2022
;; MSG SIZE rcvd: 295

Not clear enough? Here is a request to get only the port

dig +noall +answer srv \*._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local | awk '{print $7}'
80

Yes, the port is 80!

How does it work

A DNS service is a database of records. You can see it as a KV store (it is a bit more complex, of course). You can store and return much more than just the IP address. This is what happens here. By asking the DNS service to resolve the SRV record of "*._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local" I get the following answer.

;; ANSWER SECTION:
*._tcp.app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local. 5 IN SRV 0 100 80 app-z082e36c4.z489e9616-z209c3fd6.svc.cluster.local.

The SRV record is used as a Service Discovery record. The structure is the following: "IN SRV 0 100 <port> <service>.<ns>.svc.<zone>."

Conclusion

Thanks to Romain Gerard - Software Engineer @ Qovery, for this tip that saves my day 😍

This trick is directly used in my API Gateway run.sh script and is super helpful for our customers. You know how to get the port of one of Kubernetes services right from one of your apps now.

Share on :
Twitter icon
linkedin icon
Ready to rethink the way you do DevOps?
Qovery is a DevOps automation platform that enables organizations to deliver faster and focus on creating great products.
Book a demo

Suggested articles

DevOps
Kubernetes
Platform Engineering
15
 minutes
Top 10 Openshift Alternatives & Competitors

Because various organizations need cloud application and service management that offers different levels of simplicity, cost-effectiveness, or feature sets than OpenShift, this article will review its top alternatives to help readers make an informed decision aligned with their specific infrastructure needs.

Morgan Perry
Co-founder
AI
Infrastructure Management
Product
5
 minutes
GPU workloads on EKS just got way simpler with Qovery

Running GPU workloads on EKS has never been easy, until now. With Qovery’s latest update, you can enable GPU nodes, configure GPU access, and optimize costs automatically, all without writing a single line of YAML or touching Helm charts. Qovery now handles everything behind the scenes so you can focus entirely on your applications.

Alessandro Carrano
Lead Product Manager
Kubernetes
 minutes
Kubernetes Deployment Strategies: Pros, Cons & Use Cases

Master Kubernetes deployment strategies: Rolling Update, Recreate, Blue/Green, and Canary. Learn the pros, cons, and use cases to choose the right strategy based on your uptime, risk tolerance, and resources. Simplify complex rollouts with automation.

Mélanie Dallé
Senior Marketing Manager
DevOps
Developer Experience
 minutes
AWS ECS vs. EKS vs. Elastic Beanstalk: A Comprehensive Guide

Confused about which AWS container service to use? This comprehensive guide compares the trade-offs between simplicity, control, and complexity for ECS, EKS, and Elastic Beanstalk to help you choose the right platform for your application.

Mélanie Dallé
Senior Marketing Manager
DevOps
AWS
7
 minutes
Migrating from ECS to EKS: A Complete Guide

Planning your ECS to EKS migration? Learn the strategic business case (portability, ecosystem access), navigate the step-by-step roadmap, and avoid common pitfalls (networking, resource allocation). Discover how Qovery automates EKS complexity for a seamless transition.

Morgan Perry
Co-founder
DevOps
 minutes
Fargate Simplicity vs. Kubernetes Power: Where Does Your Scaling Company Land?

Is Fargate too simple or Kubernetes too complex for your scale-up? Compare AWS Fargate vs. EKS on cost, control, and complexity. Then, see how Qovery automates Kubernetes, giving you its power without the operational headache or steep learning curve.

Mélanie Dallé
Senior Marketing Manager
DevOps
Cloud Migration
 minutes
FluxCD vs. ArgoCD: Why Qovery is the Better Way to Do GitOps

Dive into the ultimate FluxCD vs. ArgoCD debate! Learn the differences between these top GitOps tools (CLI vs. UI, toolkit vs. platform) and discover a third path: Qovery, the DevOps automation platform that abstracts away Kubernetes complexity, handles infrastructure, and lets you ship code faster.

Mélanie Dallé
Senior Marketing Manager
Qovery
 minutes
Our rebrand: setting a new standard for DevOps automation

Qovery unveils its new brand identity, reinforcing its mission to make DevOps simple, intuitive, and powerful. Discover how our DevOps automation platform simplifies infrastructure, scaling, security, and innovation across the full DevOps lifecycle.

Romaric Philogène
CEO & Co-founder

It’s time to rethink
the way you do DevOps

Say goodbye to DevOps overhead. Qovery makes infrastructure effortless, giving you full control without the trouble.