如何快速编辑k8s集群中某个容器中的某个文件?

脚本如下

#!/bin/bash
set -e

useage(){
    echo "useage:"
    echo "  kubevi.sh NAMESPACE POD CONTAINER REMOTE"
}

if [ $# -ne 4 ];then
    useage
    exit
fi

NAMESPACE=$1
POD=$2
CONTAINER=$3
REMOTE=$4
LOCAL=$(basename "${REMOTE}")
TEMPDIR=$(mktemp -d)
echo "${TEMPDIR}"
cd "${TEMPDIR}"
kubectl -n "${NAMESPACE}" cp "${POD}":"${REMOTE}" "${LOCAL}" -c "${CONTAINER}"
LOCALMD51=$(md5sum "${LOCAL}" | awk '{print $1}')
vi "${LOCAL}"
LOCALMD52=$(md5sum "${LOCAL}" | awk '{print $1}')
if [ x"${LOCALMD51}" != x"${LOCALMD52}" ];then
kubectl -n "${NAMESPACE}" cp "${LOCAL}" "${POD}":"${REMOTE}" -c "${CONTAINER}"
fi
rm "${LOCAL}"

使用方法

kubevi testnamespace nginx-xxxx-xxxx nginx /etc/nginx/nginx.conf
posted @ 2019-12-06 22:24  futuretea  阅读(1656)  评论(0编辑  收藏  举报