Last active
March 6, 2019 22:19
-
-
Save leoh0/813204837b77550530a65345e06392d2 to your computer and use it in GitHub Desktop.
assign pod spec when scheduling k8s pods https://asciinema.org/a/230498
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| set -o pipefail | |
| SERVER='localhost:8001' | |
| while true; | |
| do | |
| for POD in $(kubectl --server $SERVER get pods -o json --all-namespaces | \ | |
| jq -r '.items[] | | |
| select(.spec.schedulerName == "my-scheduler") | | |
| select(.spec.nodeName == null) | | |
| .metadata.namespace+","+.metadata.name') | |
| do | |
| NAMESPACE=$(echo "$POD" | cut -d',' -f1) | |
| PODNAME=$(echo "$POD" | cut -d',' -f2) | |
| NODES=($(kubectl --server $SERVER get nodes -o json | \ | |
| jq '.items[].metadata.name' | tr -d '"')) | |
| NUMNODES=${#NODES[@]} | |
| ### scheduling algorithm here | |
| CHOSEN=${NODES[$[$RANDOM % $NUMNODES]]} | |
| ### | |
| ### check limit | |
| LIMIT=$(kubectl --server $SERVER get -n $NAMESPACE pods $PODNAME \ | |
| -o jsonpath='{$.spec.containers[0].resources.limits}') | |
| ### recreate pod for updating pod spec | |
| if [[ "$LIMIT" == "" ]]; then | |
| NEW=$(kubectl --server $SERVER get -n $NAMESPACE pods $PODNAME \ | |
| --export -o json | \ | |
| jq ".metadata.namespace = \"$NAMESPACE\"" | \ | |
| jq ".spec.containers[0].resources.limits.cpu = \"1000m\"") | |
| kubectl --server $SERVER delete -n $NAMESPACE pods $PODNAME | |
| echo "$NEW" | kubectl --server $SERVER create -f - | |
| continue | |
| fi | |
| ### binding pod | |
| curl --header "Content-Type:application/json" --request POST \ | |
| --data '''{ | |
| "apiVersion":"v1", | |
| "kind": "Binding", | |
| "metadata": {"name": "'$PODNAME'"}, | |
| "target": {"apiVersion": "v1", "kind": "Node", "name": "'$CHOSEN'"} | |
| }''' http://$SERVER/api/v1/namespaces/$NAMESPACE/pods/$PODNAME/binding/ | |
| echo "Assigned $PODNAME to $CHOSEN" | |
| done | |
| sleep 1 | |
| done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apiVersion: v1 | |
| kind: Pod | |
| metadata: | |
| name: nginx | |
| labels: | |
| app: nginx | |
| spec: | |
| schedulerName: my-scheduler | |
| containers: | |
| - name: nginx | |
| image: nginx:1.10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment