Skip to content

Instantly share code, notes, and snippets.

@lukaszbudnik
Created June 18, 2020 07:48
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 lukaszbudnik/9b36bea508f1f071e4deef55f54cf267 to your computer and use it in GitHub Desktop.
Save lukaszbudnik/9b36bea508f1f071e4deef55f54cf267 to your computer and use it in GitHub Desktop.
Shows how to scale in Kubernetes apps in a deterministic and controlled way by specifying which pods to delete.
# minikube version
minikube version
minikube version: v1.11.0
commit: 57e2f55f47effe9ce396cea42a1e0eb4f611ebbd
# Kubernetes version
kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-21T14:51:23Z", GoVersion:"go1.14.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
# helm version
helm version
version.BuildInfo{Version:"v3.1.2", GitCommit:"d878d4d45863e42fd5cff6743294a11d28a9abce", GitTreeState:"clean", GoVersion:"go1.13.8"}
# start minikube
minikube start
# install kruise (give it a moment to initialise)
helm install kruise https://github.com/openkruise/kruise/releases/download/v0.5.0/kruise-chart.tgz
# deploy 6 replicas of nginx container using kruise CloneSet
cat <<EOF > test-app.yaml
apiVersion: apps.kruise.io/v1alpha1
kind: CloneSet
metadata:
name: webapp
labels:
app: autoscaler-test
spec:
replicas: 6
selector:
matchLabels:
app: autoscaler-test
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: autoscaler-test
tier: frontend
spec:
containers:
- image: nginx
name: nginx
EOF
kubectl apply -f test-app.yaml
# check that CloneSet and pods are running fine
kubectl get clone
kubectl get pods
# select 3 pods to terminate, using awk to format pod names as yaml list, shuf to select random 3
pods=$(kubectl get pods -l "app=autoscaler-test" --no-headers | awk -F ' ' '{printf(" - %s\n",$1)}' | shuf -n 3)
echo "$pods"
# update the CloneSet - change replicas to 3 and add scaleStrategy/podsToDelete with a list of pods to delete (using $pods generated above)
cat <<EOF > test-app.yaml
apiVersion: apps.kruise.io/v1alpha1
kind: CloneSet
metadata:
name: webapp
labels:
app: autoscaler-test
spec:
replicas: 3
scaleStrategy:
podsToDelete:
$pods
selector:
matchLabels:
app: autoscaler-test
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: autoscaler-test
tier: frontend
spec:
containers:
- image: nginx
name: nginx
EOF
kubectl apply -f test-app.yaml
# you can verify that indeed only the selected 3 pods were deleted
kubectl get clone
kubectl get pods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment