Vhtforums
AI Assistant
Configure custom DN...
 
Notifications
Clear all

Configure custom DNS server and host aliases to Kubernetes Pods

1 Posts
1 Users
0 Reactions
2,714 Views
Brandon Lee
Posts: 674
Admin
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@brandon-lee)
Member
Joined: 16 years ago
[#354]

You may have a need to configure a custom DNS server setting for your Kubernetes pods. Or, you may need to add a host alias in a specific pod just to do some custom name resolution. How do you do this? Well, as it turns out there is custom YAML code we can apply to add these values for our pods. 

For setting a specific DNS server for your pod, you can use the following:

kubectl patch deployment server -n myapp \
  --type=json \
  -p='[
    {
      "op": "add",
      "path": "/spec/template/spec/dnsPolicy",
      "value": "None"
    },
    {
      "op": "add",
      "path": "/spec/template/spec/dnsConfig",
      "value": {
        "nameservers": ["<custom DNS server IP"], 
        "searches": [
          "customsuffix1.com",
          "customsuffix2.com"
        ]
      }
    }
  ]'

For setting specific host aliases, you can use the following:

kubectl patch deployment server -n myapp \
  --type=json \
  -p='[
    {
      "op": "add",
      "path": "/spec/template/spec/hostAliases",
      "value": [
        {
          "ip": "<ip of server>",
          "hostnames": ["name.domain.com"]
        }
      ]
    }
  ]'

You can check and see if your deployment was updated correctly:

kubectl get deployment server -n myapp -o yaml | grep -A 5 dnsConfig
kubectl get deployment server -n myapp -o yaml | grep -A 5 hostAliases

If you need to roll back your DNS changes, you can use this:

kubectl patch deploymentserver -n myapp \
  --type=json \
  -p='[
    {
      "op": "remove",
      "path": "/spec/template/spec/dnsPolicy"
    },
    {
      "op": "remove",
      "path": "/spec/template/spec/dnsConfig"
    }
  ]'

 

Well, there you have it. A simple way to add a custom DNS server configuration or host aliases to specific pods. Pretty cool