Kuberntes kill process inside pod

4/14/2017

I was using the following code to randomly kill processes inside the pods

   : ${DELAY:=30}

if [ "$#" -ne 1 ]; then
    echo "One argument expected specifying the time in seconds to run"
else
 end=$((SECONDS+$1))
 while [ $SECONDS -lt $end ]; do
  kubectl \
    -o 'jsonpath={.items[*].metadata.name}' \
    get pods | \
      tr " " "\n" | \
      shuf | \
      head -n 1 |
      #xargs -t --no-run-if-empty \

      kubectl exec $(head -n 1) -- kill -9 9
  #if [ $((SECONDS+${DELAY})) -lt $end ];then            #break loop if (seconds completed + delay) is greater than time specified in argument
  # sleep "${DELAY}"                                     #sleep so that pod gets completely deleted and a terminating pod not selecte
  #else
  # break
  #fi
 done
fi

The script runs but no output is shown as in it goes into some infinite loop. Can someone help where I am going wrong?

-- Nagireddy Hanisha
bash
kubernetes

1 Answer

4/15/2017

Two things: -o 'jsonpath={.items[*].metadata.name}' should go after kubectl get pods, otherwise kubectl will return help message. Also, add -t to kubectl exec in order to see output from the container in your terminal.

-- Adam Otto
Source: StackOverflow