Skip to content

Commit

Permalink
README.md: Update kubectl example
Browse files Browse the repository at this point in the history
Signed-off-by: Martynas Pumputis <m@lambda.lt>
  • Loading branch information
brb committed Jul 18, 2023
1 parent dca0529 commit 9956043
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,40 @@ docker run --privileged --rm -t --pid=host -v /sys/kernel/debug/:/sys/kernel/deb

The following example shows how to run `pwru` on a given node:
```
NODE=node-foobar
kubectl run pwru \
--image=cilium/pwru:latest \
--privileged=true \
--attach=true -i=true --tty=true --rm=true \
--overrides='{"apiVersion":"v1","spec":{"nodeSelector":{"kubernetes.io/hostname":"'$NODE'"}, "hostNetwork": true, "hostPID": true}}' \
-- --output-tuple 'dst host 1.1.1.1'
NODE=kind-control-plane
PWRU_ARGS="--output-tuple 'host 1.1.1.1'"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: pwru
spec:
nodeSelector:
kubernetes.io/hostname: ${NODE}
containers:
- image: docker.io/cilium/pwru:latest
name: pwru
volumeMounts:
- mountPath: /sys/kernel/debug
name: sys-kernel-debug
securityContext:
privileged: true
command: ["/bin/sh"]
args: ["-c", "pwru ${PWRU_ARGS}"]
volumes:
- name: sys-kernel-debug
hostPath:
path: /sys/kernel/debug
type: DirectoryOrCreate
hostNetwork: true
hostPID: true
EOF
kubectl logs -f pwru
kubectl delete pod pwru
```

Note: You may need to create a volume for `/sys/kernel/debug/` and mount it for the`pwru` pod.

### Running on Vagrant

See [docs/vagrant.md](docs/vagrant.md)
Expand Down

5 comments on commit 9956043

@azzid
Copy link
Contributor

@azzid azzid commented on 9956043 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add a wait and a trap for the script to behave properly:

NODE=kind-control-plane
PWRU_ARGS="--output-tuple 'host 1.1.1.1'"
trap " kubectl delete --wait=false pod pwru " EXIT

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: pwru
spec:
  nodeSelector:
    kubernetes.io/hostname: ${NODE}
  containers:
  - image: docker.io/cilium/pwru:latest
    name: pwru
    volumeMounts:
    - mountPath: /sys/kernel/debug
      name: sys-kernel-debug
    securityContext:
      privileged: true
    command: ["/bin/sh"]
    args: ["-c", "pwru ${PWRU_ARGS}"]
  volumes:
  - name: sys-kernel-debug
    hostPath:
      path: /sys/kernel/debug
      type: DirectoryOrCreate
  hostNetwork: true
  hostPID: true
EOF

kubectl wait pod pwru --for condition=Ready --timeout=90s
kubectl logs -f pwru

@brb
Copy link
Member Author

@brb brb commented on 9956043 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azzid What did the wait and trap solve?

@azzid
Copy link
Contributor

@azzid azzid commented on 9956043 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I guess you intended the commands to be run interactively - in which case they provide little of value.
I however copied the commands into a file and ran as a shell script - which first failed due to kubectl logs running before the pod was up:

$ ./pwru.sh                                                                                                                                                            
pod/pwru created                                                                                                                                                                               
Error from server (BadRequest): container "pwru" in pod "pwru" is waiting to start: ContainerCreating                                                                                          
pod "pwru" deleted

Adding the wait fixed that, but doing ctrl+c to exit logs then terminated the bash script rather than kubectl - which caused the delete to never run. Moving it to a trap sorts that.

@brb
Copy link
Member Author

@brb brb commented on 9956043 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azzid Cool! Mind sending a PR?

@azzid
Copy link
Contributor

@azzid azzid commented on 9956043 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! 🙂

Please sign in to comment.