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

推荐订阅源

D
Docker
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
The GitHub Blog
The GitHub Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园_首页
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
The Cloudflare Blog

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 -"