惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
aimingoo的专栏
aimingoo的专栏
腾讯CDC
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园_首页
B
Blog RSS Feed
D
Docker
M
MIT News - Artificial intelligence
爱范儿
爱范儿
I
InfoQ

Nik Ogura

Writing for Generation Ships | Nik Ogura Load-Bearing Humans | Nik Ogura Your Hiring Pipeline Has the Same Bug as Your Deploy Pipeline | Nik Ogura Nice People Who Give Us Money | Nik Ogura Gambling on Failure | Nik Ogura DDCRI: Declarative, Deterministic, Continuously Reconciling Infrastructure | Nik Ogura Stop Holding Out for a Hero | Nik Ogura Don't Paint Yourself Into a Corner | Nik Ogura Most Infrastructure as Code Is Broken — and Reconciliation Is Only Half the Reason | Nik Ogura Continuous Acceptance Tests | Nik Ogura There's More Than One Way to Get Observability Right | Nik Ogura Put Dex In Front of Google OAuth | Nik Ogura Incident Management | Nik Ogura C-Style Thinking vs Go-Style Thinking | Nik Ogura 'Can' vs 'Does' | Nik Ogura Control Repositories | Nik Ogura Trunk-Based Development | Nik Ogura Web3 Is Just Infrastructure With a Hoodie | Nik Ogura "Design Me a Highly Resilient Database" | Nik Ogura Security Is Infrastructure | Nik Ogura Metrics, Logs, Traces, and Events: What's Actually Different | Nik Ogura Distributed Tracing: A Practical Guide | Nik Ogura Prometheus and OpenTelemetry: How They Fit Together | Nik Ogura Puppets and Octopi: Why Top-Down Orchestration Hits a Wall | Nik Ogura The Best Dog Trainer in the World - Or Why Getting Better Isn't Helping | Nik Ogura FluxCD vs ArgoCD: Architectural Comparison | Nik Ogura GitOps | Nik Ogura GitHub Actions Reference Implementation | Nik Ogura Engineering Standards | Nik Ogura Cross-Cloud Kubernetes Clusters with AWS IRSA and Talos Linux | Nik Ogura
Shell Functions | Nik Ogura
2025-12-29 · via Nik Ogura

The following shell functions are often quite useful for debugging. You can add them to your ~/.bashrc or ~/.zshrc at your pleasure.

You don’t have to use them, but it’s helpful to know what they are, since folks tend to use them as abbreviations in messaging.

Just as typing kubectl get pod is kind of tedious to type on the cli, (k get pod or even just pods is much faster), typing that as instructions to someone looking for help is likewise faster and less error prone.

Plus, once you start using the funcs, you start having trouble remembering the full command syntax. Don’t even get me started on kubectl config get-contexts. Install the functions and just type context, get the info you’re after, and get on with your day.

Nik’s Shell Functions

Contents:

    # called without arguments, displays context and current context
    # called with an argument, changes into the context given
    function context {
      if [ -n "$1" ]; then
        kubectl config use-context $1
      else
        kubectl config get-contexts
      fi
    }

    # Do you want to type 'kubectl' all the time?
    alias k=kubectl

    # Typing 'kubectl get pod' gets old, too
    alias pods="kubectl get pod"

    # get logs from a pod matching a pattern in the current namespace, or another
    function podlogs {
      PATTERN=$1
      NS=$2
      if [ -n "$NS" ]; then
        clear && k logs -n $NS -f $(pods -n $NS | grep ${PATTERN} | cut -d " " -f 1)
      else
        clear && k logs -f $(pods | grep ${PATTERN} | cut -d " " -f 1)
      fi
    }

    # delete a pod matching a pattern in the current namespace or another
    function delpod {
      PATTERN=$1
      NS=$2
      if [ -n "$NS" ]; then
        PODS=$(pods -n $NS | grep ${PATTERN} | cut -d " " -f 1)
        echo "${PODS}" | xargs kubectl delete pod -n $NS
      else
        PODS=$(pods | grep ${PATTERN} | cut -d " " -f 1)
        echo "${PODS}" | xargs kubectl delete pod
      fi
    }

    # describe a pod matching a pattern in the current namespace or another
    function descpod {
      PATTERN=$1
      NS=$2
      if [ -n "$NS" ]; then
        PODS=$(pods -n $NS | grep ${PATTERN} | cut -d " " -f 1)
        echo "${PODS}" | xargs kubectl describe pod -n $NS
      else
        PODS=$(pods | grep ${PATTERN} | cut -d " " -f 1)
        echo "${PODS}" | xargs kubectl describe pod
      fi
    }

    # show images in use in a namespace, or globally
    function images {
      NS=$1

      if [[ -z "$NS" ]]; then
        kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c

      else
        kubectl get pods -n "${NS}" -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c

      fi
    }
        
    # Delete Failed Jobs in a Namespace
    function delfailedjobs {
      NS=$1
      if [ -z "$NS" ]; then
        echo "Usage: delfailedjobs <namespace>"
      else
        echo "Deleting failed jobs in $NS"
      fi

      kubectl get pods -n ${NS} --field-selector 'status.phase=Failed' -o name | xargs kubectl delete -n ${NS}
    }

    # Decode JWT's
    function decjwt {
        jq -R 'split(".") |.[0:2] | map(@base64d) | map(fromjson)'
    }

    # Get a JWT out of a K8S Cluster
    function gettoken {
      POD_PATTERN=$1
      TOKEN_DOMAIN=$2
      kubectl exec $(kubectl get pod | grep $POD_PATTERN | awk '{print $1}') -- cat /var/run/secrets/${TOKEN_DOMAIN}/serviceaccount/token

    }

    # Pull the generated Prometheus Config out of a Prometheus instance created by Prometheus Operator.
    alias promconfig="kubectl get secret -n monitoring prometheus-k8s -o json | jq -r .data.\\\"prometheus.yaml.gz\\\" | base64 -d | gunzip | vim -R -"