kubectl格式化输出

事件查看


  1.  查看事件并输出资源类型名称
    kubectl get events -A  -o=jsonpath='{range .items[*]}{.involvedObject.namespace} {.involvedObject.name} {.involvedObject.kind} {.source.component} {.source.name} {.message}{"\n"}{end}' 
  2.  按时间降序查看日志

    kubectl get events --sort-by='.metadata.creationTimestamp'

     

Pod查看


  1.  查看Pod,按重启次数排序
    kubectl get pods -A  --sort-by='.status.containerStatuses[0].restartCount'
  2. 统计集群中所有Pods的状态
    方法一
    kubectl get pods  --all-namespaces  | awk -F " " '{print $4}' | sort | uniq -c
          1 Completed
         36 ContainerCreating
          5 CrashLoopBackOff
          5 CreateContainerError
          1 Error
        966 Evicted
         69 Pending
       3504 Running
          1 STATUS
        320 Terminating

    方法二

    [root@sh-bs-b1-303-d10-uatccemaster-0-2 ~]# kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name} {.status.phase}{"\n"}{end}' | awk -F " " '{print $3}' | sort | uniq -c
        966 Failed
        116 Pending
       3824 Running
          1 Succeeded
  3. 查看指定的Pods的运行状态
    kubectl get pod -n ad-effective ad-brand-web-bfd894b8c-sznlc -o=jsonpath='{.status}' | jq -r
  4. 获取集群中Evicted的Pods
    kubectl get pods --all-namespaces | grep Evicted
    或者使用
    kubectl get pods --all-namespaces --field-selector=status.phase=Failed -o=jsonpath='{range .items[?(@.status.reason=="Evicted")]}{.metadata.namespace} {.metadata.name}{"\n"}{end}'
  5. 清理Evicted的Pods
    kubectl get pods --all-namespaces --field-selector=status.phase=Failed -o jsonpath='{range .items[?(@.status.reason=="Evicted")]}{.metadata.name}{" "}{.metadata.namespace}{"\n"}{end}' |
    while read -r pod namespace; do
        echo -e "\e[1;34mDeleting Pod: $pod in Namespace: $namespace\e[0m"
        echo -e "\e[1;33m-----------------------------\e[0m"
        kubectl delete pod -n "$namespace" "$pod" --grace-period=0 --force
    done
  6. 清理集群中Terminating的Pods
    kubectl get pods --all-namespaces -o json |
    jq -r '.items[] | select(.metadata.deletionTimestamp != null) | .metadata.namespace + " " + .metadata.name' |
    while read -r namespace pod; do
        echo -e "\e[1;34mDeleting Pod: $pod in Namespace: $namespace\e[0m"
        echo -e "\e[1;33m-----------------------------\e[0m"
        kubectl delete pod -n "$namespace" "$pod" --grace-period=0 --force
    done
    kubectl get pods --all-namespaces | awk '/Terminating/{print $1, $2}' | while read -r namespace pod; do echo "Deleting Pod: $namespace/$pod"; kubectl delete pod -n "$namespace" "$pod" --grace-period=0 --force; done
  7. 清理集群中NotReady的Pods
    先查看集群中NotReady的Pods
    kubectl get pods --all-namespaces -o json | jq -r '.items[] | select(.status.conditions != null and any(.status.conditions[]; .type == "Ready" and .status == "False")) | .metadata.namespace + " " + .metadata.name'

    方法一 清理集群中NotReady的Pods

    kubectl get pods --all-namespaces -o json |
    jq -r '.items[] | select(.status.conditions[].type=="Ready" and .status.conditions[].status=="False") | .metadata.namespace + " " + .metadata.name' |
    while read -r namespace pod; do
        echo -e "\033[1;31mDeleting NotReady Pod: $namespace/$pod\033[0m"
        kubectl delete pod -n "$namespace" "$pod" --grace-period=0 --force
        echo "--------------------------------------------------"
    done

    方法二 

    kubectl get pods --all-namespaces -o jsonpath='{range .items[?(@.status.conditions)]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' |
    while read -r namespace pod; do
      ready=$(kubectl get pod -n "$namespace" "$pod" -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}')
      if [[ $ready != "True" ]]; then
        echo -e "\033[1;31mDeleting NotReady Pod: $namespace/$pod\033[0m"
        kubectl delete pod -n "$namespace" "$pod" --grace-period=0 --force
        echo "--------------------------------------------------"
      fi
    done

     

posted @ 2023-05-20 17:48  MacoPlus  阅读(121)  评论(0编辑  收藏  举报