Skip to content

Instantly share code, notes, and snippets.

View zulhfreelancer's full-sized avatar

Zulhilmi Zainudin zulhfreelancer

View GitHub Profile
@zulhfreelancer
zulhfreelancer / how-to-install-promtool.md
Created April 4, 2024 04:07
How to install promtool by Prometheus?

How to install promtool by Prometheus?

# Fetch latest version
TAG=$(curl -s https://api.github.com/repos/prometheus/prometheus/releases | jq -r '.[] | .tag_name' | head -n 1)
echo $TAG

# Remove the `v` prefix
VERSION=$(echo $TAG | sed 's/v//')
echo $VERSION
@zulhfreelancer
zulhfreelancer / query-prometheus-from-cli.md
Last active April 4, 2024 04:13
How to query Prometheus from local CLI / Terminal

How to query Prometheus from local CLI / Terminal

Requirements

promtool - see installation steps here

Command

promtool query instant '' '' | jq
@zulhfreelancer
zulhfreelancer / kubectl-jq-filter-nodes-by-label.md
Created March 20, 2024 01:46
How to filter Kubernetes nodes by label using kubectl and jq

How to filter Kubernetes nodes by label using kubectl and jq

# Get all control-plane nodes
kubectl get nodes -o json | jq -r '.items[] | select(.metadata.labels."node-role.kubernetes.io/control-plane"=="true").metadata.name'

# Get all worker nodes
kubectl get nodes -o json | jq -r '.items[] | select(.metadata.labels."node-role.kubernetes.io/control-plane"!="true").metadata.name'
@zulhfreelancer
zulhfreelancer / get-pods-restart-count-zero.md
Last active March 2, 2024 15:38
Get all pods with restart count more than zero / 0

Get all pods with restart count more than zero / 0, with headers, node name and restart count

k get pods -A -o json | jq -r '["NAMESPACE", "POD_NAME", "NODE_NAME", "RESTART_COUNT"], (.items[] | select(.status.containerStatuses[].restartCount > 0) | [.metadata.namespace, .metadata.name, .spec.nodeName, .status.containerStatuses[].restartCount]) | @tsv' | sort -k1,1 | column -t -s $'\t'

Get all pods with restart count more than zero / 0, without headers, just namespace and pod name

k get pods -A -o json | jq -r '(.items[] | select(.status.containerStatuses[].restartCount > 0) | [.metadata.namespace, .metadata.name]) | @tsv' | sort -k1,1 | column -t -s $'\t'
@zulhfreelancer
zulhfreelancer / kubectl-jq-select-not-null.sh
Last active April 17, 2024 02:50
kubectl/jq - How to select/filter not `null` objects?
# List all deleted pods
kubectl get pods -o json | jq -r '.items[] | select(.metadata.deletionTimestamp != null)'
@zulhfreelancer
zulhfreelancer / kubectl-list-nodes-by-taints.sh
Created November 9, 2023 03:43
List all Kubernetes nodes by a specific taint(s)
# Search by key
kubectl get nodes -o json | jq -r '.items[] | select(.spec.taints[]? | select(.key == "foo/bar")) | .metadata.name'
# Search by key and value
kubectl get nodes -o json | jq -r '.items[] | select(.spec.taints[]? | select(.key=="foo/bar" and .value=="baz")) | .metadata.name'
@zulhfreelancer
zulhfreelancer / kubectl-get-nodes-sort-by-type.sh
Last active November 1, 2023 04:34
Kubectl get nodes, sort by types (control-planes & workers)
kubectl get nodes -o json | jq -r '.items | map({name: (.metadata.name + ""), type: (if (.metadata.labels | has("node-role.kubernetes.io/control-plane")) then "control-plane" else "worker" end), status: (.status.conditions[] | select(.type=="Ready") | if .status == "True" then "Ready" else "NotReady" end)}) | group_by(.type) | (["NODE_NAME", "NODE_TYPE", "NODE_STATUS"] | (., map(length*"-"))), (.[][] | [.name,.type,.status]) | @tsv' | column -ts $'\t'
@zulhfreelancer
zulhfreelancer / netshoot.yaml
Created October 4, 2023 03:49
Netshoot Deployment YAML Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: netshoot
labels:
app: netshoot
spec:
replicas: 1
selector:
matchLabels:
@zulhfreelancer
zulhfreelancer / yaml-to-json-yq.md
Last active April 26, 2024 21:23
How to convert YAML to JSON using yq?

How to convert YAML to JSON using yq?

FYI, I'm using yq version 4 by mikefarah

Command:

cat <YAML-FILE-NAME> | yq e -j
@zulhfreelancer
zulhfreelancer / nginx-controller-certificate-signed-by-unknown-authority-error.md
Created June 27, 2023 04:42
How to fix Nginx ingress controller "certificate signed by unknown authority" error?

Problem

How to fix Nginx ingress controller "certificate signed by unknown authority" error?

Error:

"Internal error occurred: failed calling webhook \"validate.nginx.ingress.kubernetes.io\": failed to call webhook: Post \"https://nginx-ingress-ingress-nginx-controller-admission.default.svc:443/networking/v1/ingresses?timeout=10s\": x509: certificate signed by unknown authority"