Skip to content

k6 HTTP Request DNS Resolution Failure Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about k6 http request dns resolution failure fix. We cover key concepts, practical examples, and best practices.

Your k6 HTTP request fails with dial tcp: lookup api.example.com: no such host — the virtual user cannot resolve the target domain, the DNS resolver is misconfigured, or the network cannot reach the endpoint.

The Problem

import http from 'k6/http';

export default function () {
  const res = http.get('https://api.example.com/users');
  console.log(`Status: ${res.status}`);
}
ERRO[0001] dial tcp: lookup api.example.com: no such host
WARN[0001] Request Failed

The script uses a domain that k6's DNS resolver cannot find, or the test environment lacks DNS resolution for internal hostnames.

Step-by-Step Fix

1. Verify DNS resolution outside k6

nslookup api.example.com
dig +short api.example.com

If DNS fails outside k6, the issue is with the network or DNS server, not k6.

2. Use an IP address or override DNS

import http from 'k6/http';

export const options = {
  hosts: {
    'api.example.com': '203.0.113.50',
  },
};

export default function () {
  const res = http.get('https://api.example.com/users');
  console.log(`Status: ${res.status}`);
}

3. Configure custom DNS resolver

import http from 'k6/http';
import { DNS } from 'k6/net';

export const options = {
  dns: {
    servers: ['8.8.8.8', '1.1.1.1'],
    ttl: '30s',
    timeout: '5s',
  },
};

export default function () {
  const res = http.get('https://api.example.com/users');
}

4. Set a DNS resolution fallback

# Run k6 with --dns flag
k6 run --dns 8.8.8.8 script.js

# Use a local hosts file
k6 run --hosts /etc/hosts script.js

5. Add connection timeout and retry

import http from 'k6/http';
import { sleep } from 'k6';

const BASE_URL = 'https://api.example.com';
const MAX_RETRIES = 3;

export default function () {
  for (let i = 0; i < MAX_RETRIES; i++) {
    const res = http.get(BASE_URL + '/users', {
      timeout: '10s',
    });
    if (res.status === 200) {
      console.log(`Attempt ${i + 1}: OK`);
      break;
    }
    console.log(`Attempt ${i + 1}: failed, retrying...`);
    sleep(1);
  }
}

Expected output:

     ✓ status is 200
     ✓ response time < 500ms

     http_req_duration........: avg=145ms   min=132ms   med=140ms   max=210ms
     http_req_failed..........: 0.00%   ✓ 0     ✗ 100

Prevention Tips

  • Verify DNS resolution independently before running k6 tests
  • Use hosts option in k6 to override DNS for internal services
  • Set dns.ttl and dns.timeout for more reliable resolution
  • Use IP addresses for stable internal endpoints
  • Add retry logic for transient DNS failures

Common Mistakes with http request

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world K6 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Can I use /etc/hosts entries with k6?

Yes, k6 reads /etc/hosts by default. You can also use the --hosts flag to point to a custom hosts file. Entries in k6's hosts option in the script take precedence over system hosts.

Why does DNS resolution work in curl but fail in k6?

k6 does not use the system DNS resolver — it uses its own Go-based DNS resolver. If your network uses custom DNS servers (e.g., Kubernetes CoreDNS), you may need to specify them explicitly with the dns option or run k6 within the same network namespace.

How do I test internal Kubernetes services with k6?

Run k6 inside the Kubernetes cluster, use the Kubernetes service DNS name (e.g., service.namespace.svc.cluster.local), or use k6-operator to distribute load test jobs within the cluster.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro