Execute kubectl exec on the node node command line and execute the command in the corresponding pod container

Execute kubectl exec on the node node command line and execute the command in the corresponding pod container

# kubectl exec -it <podName> -c <containerName> -n <namespace> -- shell comand

For example, we create a testfile file in the pod container:

# kubectl exec -it <podname> -c <container name> -n <namespace> -- touch /usr/local/testfile

Before the shell command, you need to add the--sign, otherwise the shell parameters cannot be recognized.

Modified script

#!/bin/bash
 
print_help() {
    
    
    echo "USAGE: $0 -e <ENV>"
    echo "OPTIONS: "
    echo "-e : which env cache you want to clear(required): dev, stg, preview, prd"
    echo "-h : help"
    exit -1
}
 
while getopts ":e:h" opt; do
    case $opt in
        e)  ENV="$OPTARG"
            ;;
        h)  print_help
            ;;
        \?) echo "Option -$OPTARG is not recognized." >&2
            print_help
            ;;
        :)  echo "Option -$OPTARG requires an argument." >&2
            print_help
            ;;
    esac
done
 
if [ -z ${ENV} ]; then
    echo "ENV is required."
    print_help
fi
 
result=`kubectl get pods -o=name -n j7v126xg-vip-${
     
     ENV} | sed "s/^.\{5\}//"`
 
pods=($result)
for pod in ${pods[@]}; do
  if [[ $pod == vip-nginx-* ]]; then
     echo the pod name is $pod
     if kubectl exec -it $pod -c vip-nginx -n j7v126xg-vip-${ENV} -- rm -rf /tmp/nginx-cache; then
        echo cache clear successfully
     fi
  fi

Execute script

# ./1.sh -e preview

Guess you like

Origin blog.csdn.net/yjk13703623757/article/details/108452784