在node节点命令行执行kubectl exec,在对应pod容器内执行命令
# kubectl exec -it <podName> -c <containerName> -n <namespace> -- shell comand
例如,我们在pod容器内创建一个testfile文件:
# kubectl exec -it <podname> -c <container name> -n <namespace> -- touch /usr/local/testfile
shell命令前,需要加 - - 号,否则不能识别其中的shell参数。
修改后的脚本
#!/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
执行脚本
# ./1.sh -e preview