Coder Social home page Coder Social logo

kubernetes-scripts's Introduction

Kubernetes Scripts

A collection of scripts and commands for various tasks in Kubernetes.
These were all written during my work with Kubernetes on various project. Enjoy and share. Contributions are more than welcome!

Usage

Each script has a usage function. See usage with

<script> --help

Scripts

  • countPodsAndContainerPerNodeCSV.sh: Count number of pods and containers per node. Print in CSV format.
  • findEmptyNamespaces.sh: Loop over all namespaces in a cluster and find empty ones.
  • getPodsLoad.sh: Get formatted results of pods in a namespace underlying node's load average (using cat /proc/loadavg).
  • getPodsTopCSV.sh: Get a pod's cpu and memory usage (optionally per container) written as CSV formatted file.
  • getResourcesCSV.sh: Get all pods resources requests, limits and actual usage per container in a CSV format with values normalized.
  • getRestartingPods.sh: Get all pods (all or single namespace) that have restarts detected in one or more containers. Formatted in CSV.
  • podReady.sh: Simple script to check if pod is really ready. Check status is 'Running' and that all containers are ready. Returns 0 if ready. Returns 1 if not ready.
  • getNodesLoadCSV.sh: Traverse over the kube-proxy pods to get the nodes load average and number of CPUs in a CSV format. Will also mark high load node with big YES in the output.
  • runCommandOnPods.sh: Run a command on a list of pods.
  • canIdo.sh: Check all or some permissions current user has in a namespace on all or some resources using the kubectl auth can-i command.

YAML

Commands

Kubectl

See all cluster nodes load (top)

kubectl top nodes

Get cluster events

# All cluster
kubectl get events

# Specific namespace events
kubectl get events --namespace=kube-system

Get all cluster nodes IPs and names

# Single call to K8s API
kubectl get nodes -o json | grep -A 12 addresses

# A loop for more flexibility
for n in $(kubectl get nodes -o name); do \
  echo -e "\nNode ${n}"; \
  kubectl get ${n} -o jsonpath='{.status.addresses}'; \
done

See all cluster nodes CPU and Memory requests and limits

# With node names
kubectl describe nodes | grep -A 3 "Name:\|Resource .*Requests .*Limits" | grep -v "Roles:"

# Just the resources
kubectl describe nodes | grep -A 3 "Resource .*Requests .*Limits"
Using kube-capacity

There is a great CLI for getting a cluster capacity and utilization - kube-capacity.
Install as described in the installation section.

# Get cluster current capacity
kube-capacity

# Get cluster current capacity with pods breakdown
kube-capacity --pods

# Get cluster current capacity and utilization
kube-capacity --util

# Displaying available resources
kube-capacity --available

# Roll over all clusters in your kubectl contexts
for a in $(kubectl ctx); do echo -e "\n---$a"; kubectl ctx $a; kube-capacity; done

# Roll over all clusters in your kubectl contexts and get just summary of each cluster
for a in $(kubectl ctx); do echo -e "\n---$a"; kubectl ctx $a; kube-capacity| grep -B 1 "\*"; done

Get all labels attached to all pods in a namespace

for a in $(kubectl get pods -n namespace1 -o name); do \
  echo -e "\nPod ${a}"; \
  kubectl -n namespace1 describe ${a} | awk '/Labels:/,/Annotations/' | sed '/Annotations/d'; \
done

Forward local port to a pod or service

# Forward localhost port 8080 to a specific pod exposing port 8080
kubectl port-forward -n namespace1 web 8080:8080

# Forward localhost port 8080 to a specific web service exposing port 80
kubectl port-forward -n namespace1 svc/web 8080:80

Port forwarding

  • A great tool for port forwarding all services in a namespace + adding aliases to /etc/hosts is kubefwd. Note that this requires root or sudo to allow temporary editing of /etc/host.
# Port forward all service in namespace1
kubefwd svc -n namespace1

Extract and decode a secret's value

# Get the value of the postgresql password
kubectl get secret -n namespace1 my-postgresql -o jsonpath="{.data.postgres-password}" | base64 --decode

Copy secret from namespace1 to namespace2

kubectl get secret my-secret --namespace namespace1 -o yaml | sed "/namespace:/d" | kubectl apply --namespace=namespace2 -f -

Create an Ubuntu pod

A one liner to create an Ubuntu pod that will just wait forever.

# Create the pod
cat <<ZZZ | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: my-ubuntu-pod
spec:
  containers:
  - name: my-ubuntu-container
    image: eldada.jfrog.io/docker/ubuntu:22.04
    command:
    - 'bash'
    - '-c'
    - 'while true; do date; sleep 60; done'
ZZZ

# Shell into the pod
kubectl exec -it my-ubuntu-pod bash

# Delete the pod once done
kubectl delete pod my-ubuntu-pod

Start a shell in a temporary pod

Note - Pod will terminate once exited

# Ubuntu
kubectl run my-ubuntu --rm -i -t --restart=Never --image ubuntu -- bash

# CentOS
kubectl run my-centos --rm -i -t --restart=Never --image centos:8 -- bash

# Alpine
kubectl run my-alpine --rm -i -t --restart=Never --image alpine:3.10 -- sh

# Busybox
kubectl run my-busybox --rm -i -t --restart=Never --image busybox -- sh

Get formatted list of containers and container images

Useful for listing all running containers in your cluster

# Example 1 - just the container names
kubectl get pods -A -o jsonpath='{..containers[*].name}' | tr -s ' ' '\n'
# With sorting and unique names
kubectl get pods -A -o jsonpath='{..containers[*].name}' | tr -s ' ' '\n' | sort | uniq

# Example 2 - container images and tags
kubectl get pods -A -o=jsonpath='{..containers[*].image}' | tr -s ' ' '\n'
# With sorting and unique names
kubectl get pods -A -o=jsonpath='{..containers[*].image}' | tr -s ' ' '\n' | sort | uniq

# Example 3 - pod and its container images
kubectl get pods -A -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\n"}{range .spec.containers[*]}{.name},{.image}{"\n"}{end}{end}'

# Example 4 - pod and its container images with their resources requests (cpu and memory)
kubectl get pods -A -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\n"}{range .spec.containers[*]}{.name},{.image}{.resources.requests.cpu},{.resources.requests.memory}{"\n"}{end}{end}'

Look into a few more examples of listing containers

Get list of pods sorted by restart count

kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'
  • Option 2 with a filter, and a CSV friendly output
kubectl get pods -A | grep my-app | awk '{print $5 ", " $1 ", " $6}'  | sort -n -r

Get current replica count on all HPAs (Horizontal Pod Autoscaling)

kubectl get hpa -A -o=custom-columns=NAME:.metadata.name,REPLICAS:.status.currentReplicas | sort -k2 -n -r

List non-running pods

kubectl get pods -A --no-headers | grep -v Running | grep -v Completed

Top Pods by CPU or memory usage

# Top 20 pods by highest CPU usage
kubectl top pods -A --sort-by=cpu | head -20

# Top 20 pods by highest memory usage
kubectl top pods -A --sort-by=memory | head -20

# Roll over all kubectl contexts and get top 20 CPU users
for a in $(kubectl ctx); do echo -e "\n---$a"; kubectl ctx $a; kubectl top pods -A --sort-by=cpu | head -20; done

Helm

Helm template

View the templates generated by helm install. Useful for seeing the actual templates generated by helm before deploying.
Can also be used for deploying the templates generated when cannot use Tiller

helm template <chart>

Debug helm install

  • Debug a helm install. Useful for seeing the actual values resolved by helm before deploying
helm install --debug --dry-run <chart>

Rolling restarts

Roll a restart across all resources managed by a Deployment, DaemonSet or StatefulSet with zero downtime
IMPORTANT: For a Deployment or StatefulSet, a zero downtime is possible only if initial replica count is higher than 1!

# Deployment
kubectl -n <namespace> rollout restart deployment <deployment-name>

# DaemonSet
kubectl -n <namespace> rollout restart daemonset <daemonset-name>

# StatefulSet
kubectl -n <namespace> rollout restart statefulsets <statefulset-name>

Mark Nodes with some roles for visibility (ex. EKS nodes marked with the LifeCycle,NodeType)

  • Most use of it can be gained with some GUI client (Lens), still "k get nodes" shows ROLE fields as well
for n in $(kubectl get nodes -o 'jsonpath={.items[*].metadata.name}') ; do
  lb=""
  for a in $(kubectl label --list nodes $n | sort | grep -e NodeType -e lifecycle | cut -d= -f 2); do
    lb="${lb}$a"
  done
  kubectl label nodes $n node-role.kubernetes.io/$lb=
done

A Multi Node Kubernetes cluster in Mac with Kind

To run a multi node Kubernetes cluster in Mac with Kind, do the following (assuming Docker Desktop is already installed)

kind create cluster --config yaml/kind-config.yaml --name demo

Delete the cluster with

kind delete cluster --name demo

Metrics Server in Kubernetes on Docker Desktop or Kind for Mac

To get around issue with certificates in your local Docker Desktop or Kind Kubernetes

Install a metrics-server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Patch the metrics-server Deployment with the fix

kubectl patch deployment metrics-server -n kube-system --patch-file yaml/metrics-server-patch.yaml

OR Edit the metrics-server deployment directly and add --kubelet-insecure-tls to the args key:

spec:
  containers:
  - args:
    - --cert-dir=/tmp
    - --secure-port=443
    - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
    - --kubelet-use-node-status-port
    - --metric-resolution=15s
    - --kubelet-insecure-tls

Resources

Most of the code above is self experimenting and reading the docs. Some are copied and modified to my needs from other resources...

kubernetes-scripts's People

Contributors

eldada avatar furiatona avatar jijeesh avatar kappa8219 avatar yashbhutwala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kubernetes-scripts's Issues

namespace flag not working for multiple scripts

looks like string interpolation fails, here is the error after enabling set -xv. Error occurs for getPodsTopCSV.sh, getResourcesCSV.sh, and getRestartingPods.sh.

> ./getPodsTopCSV.sh --namespace dev01 --pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x
######### Main #########

main "$@"
+ main --namespace dev01 --pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x
+ processOptions --namespace dev01 --pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x
+ [[ 4 -gt 0 ]]
+ case "$1" in
+ NAMESPACE='--namespace dev01'
+ shift 2
+ [[ 2 -gt 0 ]]
+ case "$1" in
+ POD=dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x
+ shift 2
+ [[ 0 -gt 0 ]]
+ '[' -z dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x ']'
+ '[' false == true ']'
+ echo 'Getting Pod memory and cpu usage'
Getting Pod memory and cpu usage
+ testConnection
+ kubectl cluster-info
+ kubectl get pod '--namespace dev01' dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x
Error: unknown flag: --namespace dev01
See 'kubectl get --help' for usage.
+ errorExit 'Pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x not found in namespace --namespace dev01'
+ echo -e '\nERROR: Pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x not found in namespace --namespace dev01\n'

ERROR: Pod dev01-prometheus-postgres-exporter-7c5bf75df4-rbn7x not found in namespace --namespace dev01

+ exit 1

bash info

> bash --version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin18.7.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

system info

> uname -a
Darwin bhutwala-mac 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 18 20:50:10 PDT 2020; root:xnu-4903.278.43~1/RELEASE_X86_64 x86_64

Command for running a temporary container

Hi there

Fine list of scripts, thank you for these.

I would have one addition. The use of the generator flag throws a warning because it has been deprecated.
kubectl run --generator=run-pod/v1 my-ubuntu --rm -i -t --image ubuntu -- bash

I think we should use the command like this now:
kubectl run my-ubuntu --rm -i -t --image ubuntu -- bash

Not sure if it helps, just an input ;)

kindly, riscie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.