Skip to content

Instantly share code, notes, and snippets.

@smitmartijn
Created January 2, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smitmartijn/96b78efc2d3efa4f98014ec67bda0df3 to your computer and use it in GitHub Desktop.
Save smitmartijn/96b78efc2d3efa4f98014ec67bda0df3 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# this script randomly scales 1 deployment per namespace per cluster (for demo purposes)
#
# 2019 Martijn Smit <msmit@vmware.com>
cluster_list=("k8s-cluster-1" "k8s-cluster-2")
for cluster in "${cluster_list[@]}"
do
# switch to a cluster
kubectl config use-context $cluster
# get all namespaces in that cluster, exclude the system ones
namespace_list=(`kubectl get namespaces | grep -v "kube-" | grep -v default | grep -v "pks-" | grep -v NAME | awk -F " " '{print $1}'`)
# loop through namespaces and get the deployments
for namespace in "${namespace_list[@]}"
do
# get a list of deployments
deployment_list=(`kubectl get deployments -n $namespace | grep -v NAME | awk -F " " '{print $1}'`)
# grab a number between 1 and 5 for the new scale
new_scale=`shuf -i 1-5 -n 1`
# pick a random deployment
deployment_list_size=${#deployment_list[@]}
deployment_list_index=$(($RANDOM % $deployment_list_size))
deployment="${deployment_list[$deployment_list_index]}"
# scale deployment!
kubectl scale --replicas=$new_scale deployment/$deployment -n $namespace
echo "Scaled $namespace/$deployment to $new_scale replicas"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment