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

推荐订阅源

Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
IT之家
IT之家
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
博客园 - 【当耐特】
The Cloudflare Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
小众软件
小众软件
博客园_首页
Last Week in AI
Last Week in AI
J
Java Code Geeks
V
V2EX
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
N
Netflix TechBlog - Medium
F
Full Disclosure
B
Blog
H
Help Net Security
C
Check Point Blog
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
P
Proofpoint News Feed
D
Docker
Microsoft Security Blog
Microsoft Security Blog

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 FITFO - Figure It The (Fun?) Out | Nik Ogura Golang Design Tips | Nik Ogura Auto Updating AMI's on a Rolling Window with Terraform | Nik Ogura The Documentation Problem | Nik Ogura Vault Operator Notes | Nik Ogura Coding Standards (especially in Golang) | Nik Ogura TDD (Test-Driven Development) | Nik Ogura Managed Secrets | Nik Ogura Using CircleCI as if it was a Maven Repo | Nik Ogura Dynamic Binary Toolkit: Tools that automatically keep themselves up to date! | Nik Ogura Access and Identity that Just Works | Nik Ogura LocalEnv | Nik Ogura One Shot OpenStack Liberty Installer | Nik Ogura Python Development on MacOS | Nik Ogura IAM Beyond AWS or Hacking Hacks, and the Hackers who Hack Them | 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 -"