Skip to content

Commit 4c26ece

Browse files
committed
Update net tools
1 parent f76c8c0 commit 4c26ece

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

docs/17-tips-and-tricks/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tips and Tricks
22

3-
In this bonus section we will discuss some useful tips that can be used in the exam
3+
In this bonus section we will discuss some useful tips that can be used preparation for the exam
44

55
- [01-Server for testing network policies](docs/01-server-for-testing-network-policies.md)
66
- [02-Client-for-testing-network-things](docs/02-client--for-testing-network-things.md

docs/17-tips-and-tricks/docs/01-server-for-testing-network-policies.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# Server for testing network policies
1+
# Servers for testing network policies
22

33
Sometimes you may have a question that asks you to block ingress to a pod on all but some specific port. If a pod that meets the port requirement is not already present in the given namespace, then the issue here is "How do I create a pod onto which to attach the netpol that listens on the given port so I can test the policy?". You can't just run an nginx pod as that always listens on port 80. You could configure it otherwise, but that would require you to mount a configmap into the nginx pod containing an alternate config for nginx with the new port number. That's far too much hassle under exam conditions!
44

5+
## Simple server
6+
57
Fortunately, the default Python distribution contains a simple server that can have its port number configured from the command line, meaning you can run it imperatively. Let's say the network policy requires blocking all but port 9000. We can start a server test pod to listen on 9000 like so. If it's a different port, just put that port number instead of 9000.
68

79
```
@@ -26,5 +28,68 @@ You should get a response.
2628

2729
Now apply your network policy and test again.
2830

31+
## Slightly more advanced server
32+
33+
Perhaps you want to set up several pods and have each serve a specific message on a configurable port so you can tell them apart by their reponses. We can do that with a pod and a config map for each. The pod is the same each time - except for giving it a unique name and mounting the appropriate config map.
34+
35+
The following simulates a pod found in one of the Killer.sh network policy questions.
36+
37+
1. Create a config map which contains a shell script to run the server on a given port with a given message
38+
39+
```yaml
40+
apiVersion: v1
41+
kind: ConfigMap
42+
metadata:
43+
name: db1-configmap
44+
data:
45+
entrypoint.sh: |
46+
#!/bin/sh
47+
echo "database one" > index.htm #<- Message
48+
port=1111 #<- Server Port
49+
# Run server...
50+
while true
51+
do
52+
{ echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <index.htm)\r\n\r\n"; cat index.htm; } | nc -l -p $port
53+
done
54+
```
55+
1. Create a pod that mounts the configmap and runs the script it contains
56+
57+
```yaml
58+
apiVersion: v1
59+
kind: Pod
60+
metadata:
61+
name: db-1
62+
spec:
63+
containers:
64+
- name: server
65+
image: alpine:3.19
66+
command:
67+
- /opt/server/entrypoint.sh
68+
volumeMounts:
69+
- name: script
70+
mountPath: /opt/server
71+
volumes:
72+
- name: script
73+
configMap:
74+
name: db1-configmap
75+
defaultMode: 0755
76+
```
77+
1. Get the pod's IP address. Using the IP for curl test is quicker than typing out the DNS name.
78+
79+
```
80+
controlplane $ k get pod server -o wide
81+
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
82+
db-1 1/1 Running 0 16s 192.168.1.12 node01 <none> <none>
83+
```
84+
85+
1. Now run a pod with `curl` in and test connection to the server
86+
87+
```
88+
curl 192.168.1.12:1111
89+
```
90+
91+
92+
## See also
93+
2994
See also [client for testing](./02-client--for-testing-network-things.md)
3095

docs/17-tips-and-tricks/docs/02-client--for-testing-network-things.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ Often you will get questions that require you to test network polices, or look s
66
* nslookup
77
* netstat
88
* dig
9+
* telnet
10+
* nc
911

1012
and many more.
1113

12-
You run it like so. Commit the image name to memory - this image is a lifesaver!
14+
You run it like so. Commit the image name to memory - this image is a lifesaver! There is nothing to stop you using it in the exam.
1315

1416
```
1517
kubectl run tester --image wbitt/network-multitool

0 commit comments

Comments
 (0)