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