Skip to content

Instantly share code, notes, and snippets.

View zulhfreelancer's full-sized avatar

Zulhilmi Zainudin zulhfreelancer

View GitHub Profile
@zulhfreelancer
zulhfreelancer / ipv4-brief-summary-linux.md
Created June 3, 2024 02:05
How to displays a brief summary of all IPv4 addresses assigned to the network interfaces Linux system?

How to displays a brief summary of all IPv4 addresses assigned to the network interfaces Linux system?

Command:

ip -4 -br a

Sample output:

@zulhfreelancer
zulhfreelancer / check-network-interface.md
Created June 3, 2024 01:55
How to check which network interface will be used to route/send packets for specific IP address?

How to check which network interface will be used to route/send packets for specific IP address?

# Install `ip` tool
apt update -y
apt install iproute2 -y

# Check
ip r get 8.8.8.8 fibmatch
@zulhfreelancer
zulhfreelancer / get-all-prometheus-metrics.md
Last active May 16, 2024 06:41
Get all Prometheus metrics and descriptions/help texts

Get all Prometheus metrics and descriptions/help texts

Requirement:

As JSON

curl -sg 'http://prom-server:prom-port/api/v1/metadata' | jq -r '.data'
@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'