Kubernetes - 获取tke日志,用于追溯

2024-09-07
#!/bin/bash
set -x
stime=$(date "+%s")
k8s_cluster="k8s-v6-prod"
namespaces="kube-system"
keyword="tke-log"
types="loglistener log-agent"
now_time=$(date "+%Y-%m-%d-%H-%M")
storage_path="/opt/tke-log-agent"
echo `date`
for namespace in ${namespaces}
do
    /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} -n ${namespace} get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\n"}{end}' | grep ${keyword} > /tmp/${k8s_cluster}-${namespace}-${keyword}
    cat /tmp/${k8s_cluster}-${namespace}-${keyword} | while read line
    do
        line=($line)
        pod_name=${line[0]}
        node_name=${line[1]}
	for type in ${types}
	do
            /usr/local/bin/kubectl --kubeconfig ~/.kube/${k8s_cluster} logs -n ${namespace} ${pod_name} -c ${type}  > ${storage_path}/${node_name}-${pod_name}-${type}-${now_time}.log
	    gzip ${storage_path}/${node_name}-${pod_name}-${type}-${now_time}.log
	done
    done
done

del_date=$(date "+%Y-%m-%d" -d '2 days ago')
ls ${storage_path} | grep ${del_date} | while read line
do
    rm -rf ${storage_path}/${line}
done

标题:Kubernetes - 获取tke日志,用于追溯
地址:https://blog.njqhome.com:8443/articles/2024/09/07/1725699355366.html