Last active
June 2, 2023 02:13
-
-
Save myclau/72324841281e18c9316688d227681c8b to your computer and use it in GitHub Desktop.
kubectl related script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create resource | |
kubectl create -f <yaml/json file> | |
# get info | |
kubectl get nodes | |
kubectl get pods -n namespace | |
kubectl get pods --all-namespaces | |
kubectl get ingress | |
kubectl config view | |
kubectl get namespaces | |
#you can always show more info by using -o wide | |
#get more information | |
kubectl describe <type> ... more opt | |
# cluster performance | |
kubectl top nodes | |
kubectl top pods | |
#remove stuck pods | |
kubectl get pods --all-namespaces | grep Terminating | |
kubectl -n namespacename delete pod podname --grace-period 0 --force | |
#check cluster name | |
kubectl config current-context | |
#get all node info | |
kubectl get node nodename -o yaml | |
#get pod on single node | |
kubectl get pods --all-namespaces --field-selector spec.nodeName=<node> -o wide | |
#get pod with nodename | |
kubectl get pods --all-namespaces --field-selector spec.nodeName!=null -o wide | |
#find pod info from uid | |
kubectl get pod --all-namespaces -o json | jq '.items[] | select(.metadata.uid == "<pod-uid>")' | |
#find docker id with overlay id | |
docker ps -a -q | xargs docker inspect | jq '.[] | select(.GraphDriver.Data.LowerDir | contains("overlayid")) | .Id' | |
#display only name | |
kubectl get pv -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | |
# delete all evicted pods from all namespaces | |
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod | |
# delete all containers in ImagePullBackOff state from all namespaces | |
kubectl get pods --all-namespaces | grep 'ImagePullBackOff' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod | |
# delete all containers in ImagePullBackOff or ErrImagePull or Evicted state from all namespaces | |
kubectl get pods --all-namespaces | grep -E 'ImagePullBackOff|ErrImagePull|Evicted' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod | |
# delete all container which suck on terminating | |
kubectl get pods --all-namespaces | grep -E 'Terminating' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod --grace-period 0 --force | |
# restart all pod | |
kubectl get deployment -n <namespace> | awk '{print $1}' | xargs kubectl -n <namespace> rollout restart deployment | |
#set all pod to zero | |
kubectl get deployment -n <namespace> | awk '{print $1}' | kubectl scale --replicas=0 deployment/ | |
#disable daemonsets | |
kubectl -n <namespace> patch daemonset <name-of-daemon-set> -p '{"spec": {"template": {"spec": {"nodeSelector": {"non-existing": "true"}}}}}' | |
#enable | |
kubectl -n <namespace> patch daemonset <name-of-daemon-set> --type json -p='[{"op": "remove", "path": "/spec/template/spec/nodeSelector/non-existing"}]' | |
#deployment get info | |
#image | |
kubectl get deployment -n "$NAMESPACE" --selector=release="$CHART_NAME" -o jsonpath="{.items[0].spec.template.spec.containers[0].image}" | |
kubectl get deployment -n $namespace --field-selector metadata.name="$deployment_name" -o json | |
#get pod info | |
kubectl get pods -n "$NAMESPACE" --selector=release="$CHART_NAME" --field-selector status.phase=Running -o jsonpath="{.items[*].spec.containers[*].image}" | |
# restart all deployment within namespace | |
kubectl rollout restart deployment -n <namespace> | |
#check k8s service status (componentstatus) | |
kubectl get --raw='/readyz?verbose' | |
kubectl get cs | |
# accessing pod | |
kubectl exec -it <pod> -n <namespace> -- /bin/sh | |
kubectl exec -it deployment/<deploymentname> -n <namespace> -- /bin/sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#need install jq first | |
kubectl get po -a --all-namespaces -o json | \ | |
jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | | |
"kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c | |
# one line | |
kubectl get pod --all-namespaces -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do it in docker nodes: | |
echo "ExecStartPost=/sbin/iptables -P FORWARD ACCEPT" >> /etc/systemd/system/docker.service.d/exec_start.conf | |
systemctl daemon-reload | |
systemctl restart docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i=$((0)) | |
for n in $(kubectl get -o=custom-columns=NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name pv,pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob --all-namespaces | grep -v 'secrets/default-token') | |
do | |
if (( $i < 1 )); then | |
namespace=$n | |
i=$(($i+1)) | |
if [[ "$namespace" == "PersistentVolume" ]]; then | |
kind=$n | |
i=$(($i+1)) | |
fi | |
elif (( $i < 2 )); then | |
kind=$n | |
i=$(($i+1)) | |
elif (( $i < 3 )); then | |
name=$n | |
i=$((0)) | |
echo "saving ${namespace} ${kind} ${name}" | |
if [[ "$namespace" != "NAMESPACE" ]]; then | |
mkdir -p $namespace | |
kubectl get $kind -o=yaml --export $name -n $namespace > $namespace/$kind.$name.yaml | |
fi | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
i=$((0)) | |
outstring="" | |
for n in $(kubectl get pods --all-namespaces -o go-template --template '{{range .items}}{{.metadata.namespace}} {{.metadata.name}}{{"\n"}}{{end}}') | |
do | |
if (( $i < 1 )); then | |
namespace=$n | |
i=$(($i+1)) | |
elif (( $i < 2 )); then | |
i=$((0)) | |
name=$n | |
tempjson=$(kubectl get pods -o json $name -n $namespace | jq '.') | |
count=$(echo "$tempjson"| jq '.spec.containers | length') | |
for ((j = 0; j < $count; j++)); do | |
extra="" | |
resource=$(echo "$tempjson" | jq ".spec.containers[$j].resources") | |
resource_limits_cpu=$(echo "$resource" | jq '.limits.cpu') | |
resource_limits_memory=$(echo "$resource" | jq '.limits.memory') | |
resource_requests_cpu=$(echo "$resource" | jq '.requests.cpu') | |
resource_requests_memory=$(echo "$resource" | jq '.requests.memory') | |
if (( $count > 1 )); then | |
extra="(sidercar$j)" | |
fi | |
#echo -e "$name$extra $namespace $resource_limits_cpu $resource_limits_memory $resource_requests_cpu $resource_requests_memory" | |
printf "%s\t%s\t%s\t%s\t%s\t%s\n" $name$extra $namespace $resource_limits_cpu $resource_limits_memory $resource_requests_cpu $resource_requests_memory | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment