Skip to content

Instantly share code, notes, and snippets.

@brettmilford
Forked from hemanthnakkina-zz/query-kubeapiserver.sh
Last active November 27, 2020 05:13
Show Gist options
  • Save brettmilford/17fb21bfc3e81204822c445e83dcab19 to your computer and use it in GitHub Desktop.
Save brettmilford/17fb21bfc3e81204822c445e83dcab19 to your computer and use it in GitHub Desktop.
Send parallel API requests to kube-api server
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/metrics
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups:
- extensions
- networking.k8s.io
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics", "/metrics/cadvisor"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: default
namespace: default
#!/bin/bash -eux
# brettmilford: added kubeapiserver /metrics gathering
# This file is based on https://code.launchpad.net/~freyes/+git/experiment
# Modified to use xargs instead of parallel
# source: https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/
PARALLEL_REQS=20
SLEEP_SECS=0.1
TEMPLOG=$(mktemp).log
TEMPLOGMETRICS=$(mktemp)-metrics.log
K8S_ENDPOINT='/api'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="juju-cluster"
# Point to the API server referring the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 --decode)
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
local DEST=query-kubeapiserver.$(date '+%Y%m%d_%H%M').log
local DESTMETRICS=query-kubeapiserver-metrics.$(date '+%Y%m%d_%H%M').log
mv $TEMPLOG $DEST
mv $TEMPLOGMETRICS $DESTMETRICS
echo -e "\noutput at $DEST \nmetrics at $DESTMETRICS"
exit 0
}
#set +x
(
ts_and_log() {
while read line; do
# filter comments
[[ "$line" =~ ^\# ]] || echo "$(date +%d-%m-%YT%H:%m:%S) $line" 2>&1 >> $TEMPLOGMETRICS
done
}
while true; do
(curl -s -X GET "${APISERVER}/metrics" --header "Authorization: Bearer $TOKEN" --insecure) | ts_and_log
/bin/sleep 5
done
) &
while true; do
seq $PARALLEL_REQS | xargs -I {} -n1 -P$PARALLEL_REQS curl -s -X GET $APISERVER$K8S_ENDPOINT --header "Authorization: Bearer $TOKEN" --insecure 2>&1 >> $TEMPLOG
/bin/sleep $SLEEP_SECS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment