Skip to content

Instantly share code, notes, and snippets.

@andrewloux
Created May 2, 2024 11:56
Show Gist options
  • Save andrewloux/6f236c9e503d610688cf5a4a8bcf2e4a to your computer and use it in GitHub Desktop.
Save andrewloux/6f236c9e503d610688cf5a4a8bcf2e4a to your computer and use it in GitHub Desktop.
Restart all pods in K8s cluster
#!/bin/bash
## Usage: ./restart.sh --env staging
# Check if the --env argument is provided
if [ "$#" -ne 2 ] || [ "$1" != "--env" ]; then
echo "Usage: $0 --env <environment>"
exit 1
fi
ENV=$2
# Set the kubectl context based on the environment
case $ENV in
staging)
kubectl config use-context staging
;;
production)
kubectl config use-context production
;;
*)
echo "Invalid environment: $ENV. Available options are staging or production."
exit 1
;;
esac
# Get all namespaces
namespaces=$(kubectl get namespaces -o jsonpath="{.items[*].metadata.name}")
# Loop through each namespace and restart all deployments
for namespace in $namespaces; do
echo "Restarting deployments in namespace: $namespace"
# Get all deployments in the namespace
deployments=$(kubectl get deployments -n $namespace -o jsonpath="{.items[*].metadata.name}")
# Loop through each deployment and perform a rollout restart
for deployment in $deployments; do
echo "Restarting deployment: $deployment"
kubectl rollout restart deployment/$deployment -n $namespace
done
done
echo "All deployments have been restarted in all namespaces."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment