dgkanatsios / CKAD-exercises

A set of exercises to prepare for Certified Kubernetes Application Developer exam by Cloud Native Computing Foundation
MIT License
8.77k stars 5.64k forks source link

Misleading solution in 'hitting IP with wget'? #231

Open LukaszKosc opened 3 years ago

LukaszKosc commented 3 years ago

Hi,

I was looking today on https://github.com/dgkanatsios/CKAD-exercises/blob/master/f.services.md - and I think I noticed an issue there:

in question: Get service's ClusterIP, create a temp busybox pod and 'hit' that IP with wget

Second solution is given as:

IP=$(kubectl get svc nginx --template={{.spec.clusterIP}}) # get the IP (something like 10.108.93.130)
kubectl run busybox --rm --image=busybox -it --restart=Never --env="IP=$IP" -- wget -O- $IP:80 --timeout 2

It works only by coincident as we use the same environment variable 'IP' in host system and inside of pod, because if we change name of variable containing ip address - it fails:

IP=$(kubectl get svc nginx --template={{.spec.clusterIP}})
kubectl run busybox --rm --image=busybox -it --restart=Never --env="HA=$IP" -- wget -O- $HA:80 --timeout 2
If you don't see a command prompt, try pressing enter.
Error attaching, falling back to logs: unable to upgrade connection: container busybox not found in pod busybox_default
wget: bad address ':80'
pod "busybox" deleted
pod default/busybox terminated (Error)

so, either we skip --env part:

IP=$(kubectl get svc nginx --template={{.spec.clusterIP}}) # get the IP (something like 10.108.93.130)
kubectl run busybox --rm --image=busybox -it --restart=Never -- wget -O- $IP:80 --timeout 2

or we use again first solution but with --env variable:

IP=$(kubectl get svc nginx --template={{.spec.clusterIP}})
kubectl run busybox --rm --image=busybox -it --restart=Never --env=HA=$IP -- sh
If you don't see a command prompt, try pressing enter.
/ # wget -O- $HA:80
Connecting to 10.152.183.190:80 (10.152.183.190:80)
writing to stdout
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-                    100% |***********************************************|   615  0:00:00 ETA
written to stdout
/ # exit
pod "busybox" deleted

Thank you for verification.

Regards, Lukasz

tharpta commented 2 years ago

I also was having a problem with this question. The answer specifies using sh after the --. I was unable to get it to work with the sh command . I'd get this error. sh: can't open 'wget': No such file or directory

Running this worked for me: k run bboxtemp2 --image=busybox -it --restart=Never -- wget -O- epIP where epIP is the IP address of the endpoint.

Also, since this is a temporary pod, the command should probably look like:

k run bboxtemp2 --image=busybox -it --rm --restart=Never -- wget -O- epIP

zhongdai commented 2 years ago

I had a simple fix, just don't run the wget directly, but swap a new sh to run it, and you can use single quote for the command with $.

❯ kubectl run nginx --image=nginx --restart=Never --port=80 --expose
service/nginx created
pod/nginx created

❯ IP=$(kubectl get svc nginx --template={{.spec.clusterIP}})

❯ kubectl run busybox --rm --image=busybox -it --restart=Never --env="xxIP=$IP" -- sh -c 'wget -O- $xxIP:80 --timeout 2'
Connecting to 10.71.252.172:80 (10.71.252.172:80)
writing to stdout
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
-                    100% |********************************|   615  0:00:00 ETA
written to stdout
pod "busybox" deleted