Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
franznemeth committed Dec 20, 2023
1 parent 9c90cb1 commit 87f7823
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
44 changes: 44 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# kubectl oneliners
These scripts can be used as subcommands for kubectl to serve a specific purpose
## Installation
```bash
sudo cp ./kubectl-* /usr/local/bin/
```

## Desciption
### kubectl-resources
This oneliner shows you the resource limits/requests for pods in a namespace or all namespaces

Usage example:
```shell
# Supports --all-namespaces
kubectl resources
```
### kubectl-containerids
This oneliner shows you the associated containerids for kubernetes pods.
It can be used to debug a container directly on the node. The containerId matches the ID on the node itsself

Usage example:
```shell
# Supports --all-namespaces
kubectl containerids
```
### kubectl-geteverything
This oneliner shows all (and by that it means every resource/cr) in a namespace.
It requires an argument which is the name of the namespace in question.

It may be used to debug a namespace stuck in terminating state to find out which resources remain.

Usage exmaple:
```shell
kubectl geteverything my-namespace
```
### kubectl-finalize-namespace
This is a dangerous script but sometimes necessary to get rid of a namespace which is stuck in Terminating state

**Run this script with extrem caution!**

Usage example:
```shell
kubectl finalize namespace some-namespace --yes-i-really-mean-it
```
5 changes: 5 additions & 0 deletions commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kubectl get pods --all-namespaces --field-selector spec.nodeName=
git config --global push.autoSetupRemote true
select pg_size_pretty(pg_database_size('db_name'));
stern
kubectx/kubens
2 changes: 2 additions & 0 deletions kubectl-containerids
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
kubectl get pods -o custom-columns="NAMESPACE:.metadata.namespace,POD:.metadata.name,CONTAINER:.spec.containers[*].name,containerIds:.status.containerStatuses[*].containerID" "$@"
12 changes: 12 additions & 0 deletions kubectl-finalize-namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "No argument supplied. You have to supply a namespace you wish to terminate as argument"
exit 1
fi
if [ "x--yes-i-really-mean-itx" != "x$2x" ]; then
echo "Doing nothing... use --yes-i-really-mean-it if you want to run the script"
exit 1
fi
kubectl get namespace ${1} -o json \
| tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \
| kubectl replace --raw /api/v1/namespaces/${1}/finalize -f -
3 changes: 3 additions & 0 deletions kubectl-geteverything
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n $@
4 changes: 4 additions & 0 deletions kubectl-remove-finalizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
# Removes finalizers from resources
patch=$(echo -e 'metadata:\n finalizers: null')
kubectl patch --patch "${patch}" --type=merge $@
2 changes: 2 additions & 0 deletions kubectl-resources
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
kubectl get pods -o custom-columns="NAMESPACE:.metadata.namespace,POD:.metadata.name,CONTAINER:.spec.containers[*].name,REQ_CPU:.spec.containers[*].resources.requests.cpu,LIM_CPU:.spec.containers[*].resources.limits.cpu,REQ_MEM:.spec.containers[*].resources.requests.memory,LIM_CPU:.spec.containers[*].resources.limits.memory" "$@"
31 changes: 31 additions & 0 deletions node-shell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
NODENAME=$1
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: node-shell-${NODENAME}
namespace: kube-system
spec:
containers:
- name: shell
image: "docker.io/alpine:3.9"
securityContext:
privileged: true
command: ["nsenter"]
args: ["-t", "1", "-m", "-u", "-i", "-n", "sleep", "14000"]
hostIPC: true
hostNetwork: true
hostPID: true
terminationGracePeriodSeconds: 0
restartPolicy: Never
tolerations:
- operator: Exists
nodeName: ${NODENAME}
EOF

sleep 10

kubectl exec -it -n kube-system node-shell-${NODENAME} -- /bin/bash

#kubectl delete pod -n kube-system node-shell-${NODENAME}

0 comments on commit 87f7823

Please sign in to comment.