Last active
February 11, 2019 20:37
-
-
Save kelseyhightower/5e43e93d3633edaf52de0046be5edbd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with: | |
// GOOS=linux go build -a --ldflags '-extldflags "-static"' -tags netgo -installsuffix netgo -o dns-example main.go | |
// | |
// Run on Kubernetes. Example resolv.conf | |
// | |
// # /etc/reslov.conf | |
// search default.svc.cluster.local svc.cluster.local cluster.local google.internal c.hightowerlabs.internal | |
// nameserver 10.179.240.10 | |
// options ndots:5 | |
// | |
// Results in the following error: | |
// $ curl -O https://storage.googleapis.com/hightowerlabs/dns-example | |
// $ chmod +x dns-example | |
// $ ./dns-example | |
// 2016/10/25 16:11:46 lookup kubernetes.default.svc.cluster.local: unrecognized address | |
// | |
// But this works: | |
// $ dig +short kubernetes.default.svc.cluster.local | |
// 10.179.240.1 | |
// | |
// While dig works it does not respect resolv.conf or the search domain list. So this does not work | |
// $ dig kubernetes | |
package main | |
import ( | |
"log" | |
"fmt" | |
"net" | |
) | |
func main() { | |
ips, err := net.LookupAddr("kubernetes.default.svc.cluster.local") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, ip := range ips { | |
fmt.Println(ip) | |
} | |
} |
We followed the lead of skydns and others. The fact that only the Go resolver does this says that, while it may be technically right, it is wrong.
Also: dig +search
is your friend
Looks like this is working as desired with the following code update:
package main
import (
"log"
"fmt"
"net"
)
func main() {
ips, err := net.LookupHost("kubernetes")
if err != nil {
log.Fatal(err)
}
for _, ip := range ips {
fmt.Println(ip)
}
}
Need to use LookupHost
not LookupAddr
This may have been fixed in 1.7.x
Fixed in 1.7.1 with this: https://go-review.googlesource.com/#/c/27250/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The issue on the Go side is well documented: https://golang.org/pkg/net
The big issue here is that Kubernetes leverages
.local
for cluster services.