Aakash revised this gist 4 hours ago. Go to revision
1 file changed, 82 insertions
.md(file created)
| @@ -0,0 +1,82 @@ | |||
| 1 | + | # K8s Index | |
| 2 | + | ## Collection of useful commands and code snippets related to K8s | |
| 3 | + | ||
| 4 | + | ### Find and delete all pods with regex match | |
| 5 | + | ||
| 6 | + | **Note:** Change `^name` with pod name | |
| 7 | + | ||
| 8 | + | Without force | |
| 9 | + | ```bash | |
| 10 | + | kubectl delete pods $(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep ^name) | |
| 11 | + | ``` | |
| 12 | + | With force | |
| 13 | + | ```bash | |
| 14 | + | kubectl delete pods $(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep ^name) --force --grace-period=0 | |
| 15 | + | ``` | |
| 16 | + | ||
| 17 | + | --- | |
| 18 | + | ||
| 19 | + | ### Create k8s user super with cluster-admin role and fetch its api key | |
| 20 | + | ||
| 21 | + | ```bash | |
| 22 | + | kubectl create sa super | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | ```bash | |
| 26 | + | kubectl create clusterrolebinding super-admin-binding --clusterrole=cluster-admin --serviceaccount=default:super | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | ```bash | |
| 30 | + | kubectl apply -f - <<EOF | |
| 31 | + | apiVersion: v1 | |
| 32 | + | kind: Secret | |
| 33 | + | metadata: | |
| 34 | + | name: super-api | |
| 35 | + | namespace: default | |
| 36 | + | annotations: | |
| 37 | + | kubernetes.io/service-account.name: super | |
| 38 | + | type: kubernetes.io/service-account-token | |
| 39 | + | EOF | |
| 40 | + | ``` | |
| 41 | + | ||
| 42 | + | ```bash | |
| 43 | + | kubectl get secret super-api -o jsonpath='{.data.token}' | base64 --decode | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | ### SSH access of any node | |
| 47 | + | ||
| 48 | + | ```bash | |
| 49 | + | kubectl debug node/<node-name> -it --image=ubuntu | |
| 50 | + | .... | |
| 51 | + | .... | |
| 52 | + | chroot /host | |
| 53 | + | ... | |
| 54 | + | ... | |
| 55 | + | bash # now you have ssh connection of the node | |
| 56 | + | ``` | |
| 57 | + | ||
| 58 | + | ||
| 59 | + | ### Run a K8s benchmark | |
| 60 | + | ||
| 61 | + | ```bash | |
| 62 | + | kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yaml | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | to see result | |
| 66 | + | ||
| 67 | + | ```bash | |
| 68 | + | kubectl get pods | |
| 69 | + | ||
| 70 | + | kubectl logs <pod-name> # pods name will be kube-bench-***** | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ||
| 74 | + | ### add docker Registry secret | |
| 75 | + | ||
| 76 | + | ```bash | |
| 77 | + | kubectl create secret docker-registry <secret-name> -n <namespace> \ | |
| 78 | + | --docker-server=<your-registry-server> \ | |
| 79 | + | --docker-username=<your-username> \ | |
| 80 | + | --docker-password=<your-password> # can provide tokens too\ | |
| 81 | + | --docker-email=<your-email> | |
| 82 | + | ``` | |
Newer
Older