sandervanvugt / cka

394 stars 510 forks source link

lab 7: nslookup (resolver) in busybox:latest fails to lookup service (mydb, myservice) #2

Open ekc opened 4 years ago

ekc commented 4 years ago

initContainers of init-pod pod (lab7-1.yaml) may not run at all because it depends on the latest busybox image. I found the behavior change is in busybox:1.29 to busybox:1.32 (latest) see Nslookup does not work in latest busybox image #48.

[root@master01 learn]# kubectl run --attach busybox --rm --image=busybox:latest --restart=Never -- \
> sh -c "sleep 4 && nslookup kubernetes"
If you don't see a command prompt, try pressing enter.
Server:         10.96.0.10
Address:        10.96.0.10:53

** server can't find kubernetes.default.svc.cluster.local: NXDOMAIN

*** Can't find kubernetes.svc.cluster.local: No answer
*** Can't find kubernetes.cluster.local: No answer
*** Can't find kubernetes.default.svc.cluster.local: No answer
*** Can't find kubernetes.svc.cluster.local: No answer
*** Can't find kubernetes.cluster.local: No answer

pod "busybox" deleted
pod default/busybox terminated (Error)

busybox:1.28 is working fine.

[root@master01 learn]# kubectl run --attach busybox --rm --image=busybox:1.28 --restart=Never -- \
> sh -c "sleep 4 && nslookup kubernetes"
If you don't see a command prompt, try pressing enter.
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
pod "busybox" deleted
[root@master01 learn]#

So, a simple solution to solve this problem is to use busybox:1.28 as in this snippet.

[root@master01 learn]# cat lab7-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: init-pod
  labels:
    app: initapp
spec:
  containers:
  - name: main-container
    image: busybox
    command: ['sh', '-c', 'echo main app running && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done']
[root@master01 learn]#

Another useful tool to troubleshoot DNS problem in kubernetes in dnsutils.

Dichtrich commented 11 months ago

thank you @ekc i almost go mad wondering why the Initpod wouldn't start. @sandervanvugt you should probably pin the busybox version in your repo files.