Skip to content

Instantly share code, notes, and snippets.

View moio's full-sized avatar

Silvio Moioli moio

View GitHub Profile
@moio
moio / continuous_profiling.sh
Last active December 21, 2023 09:30
Script to continuously collect Rancher profiles and upload them to Azure
#!/bin/bash
# Azure storage container SAS URL and token for uploading. Only creation permission is necessary
BLOB_URL=''
BLOB_TOKEN=''
cleanup() {
echo "Cleaning up..."
echo "Setting rancher logs back to error"
kubectl -n cattle-system get pods -l app=rancher --no-headers -o custom-columns=name:.metadata.name | while read rancherpod; do kubectl -n cattle-system exec $rancherpod -c rancher -- loglevel --set error; done
@moio
moio / steve_api_microbenchmark.js
Created September 1, 2023 10:29
k6 script to test Rancher's Steve API listing performance
import { check, fail } from 'k6';
import http from 'k6/http';
// Parameters
const vus = __ENV.VUS || 1
const perVuIterations = __ENV.PER_VU_ITERATIONS || 30
const baseUrl = __ENV.BASE_URL
const user = __ENV.USER
const password = __ENV.PASSWORD
const cluster = __ENV.CLUSTER || "local"
@moio
moio / rancher_pprof.md
Created June 16, 2023 10:18
Explore a Rancher pprof CPU profile

Explore a Rancher pprof CPU profile

Grab a kubeconfig file pointing to the Rancher upstream cluster, then copypaste:

export KUBECONFIG=/path/to/the/upstream_cluster.yaml

for pod in $(kubectl get pods -n cattle-system --no-headers -l app=rancher | cut -d ' ' -f1); do
  echo Collecting 30s worth of profile data for $pod...
 kubectl exec -n cattle-system $pod -- curl -s http://localhost:6060/debug/pprof/profile -o profile
@moio
moio / audit-to-csv-rancher.rb
Created June 6, 2023 15:52
Rancher audit log to CSV converter for Excel analysis
#!/usr/bin/env ruby
# encoding: UTF-8
# EXAMPLE
#
# {
# "kind": "Event",
# "apiVersion": "audit.k8s.io/v1",
# "level": "Metadata",
# "auditID": "f6618a15-8e4f-4f82-95b3-7592ed544eb9",
@moio
moio / trim-helm-history.rb
Last active June 2, 2023 16:45
Script to trim Helm's history
#!/usr/bin/env ruby
# encoding: UTF-8
# Prints kubectl commands to trim Helm history in excess of the parameter below
# Assumes the default Helm history storage (Secrets)
HISTORY_ENTRIES_TO_RETAIN = 2
namespaces = `kubectl get ns -o name`
.split
@moio
moio / audit-to-csv.rb
Created May 30, 2023 10:03
Script to convert Kubernetes API Audit jsonline files to CSV for Excel elaboration
#!/usr/bin/env ruby
# encoding: UTF-8
# EXAMPLE
#
# {
# "kind": "Event",
# "apiVersion": "audit.k8s.io/v1",
# "level": "Metadata",
# "auditID": "f6618a15-8e4f-4f82-95b3-7592ed544eb9",
@moio
moio / cattle-cluster-agent_heap_profile_collector.md
Created May 11, 2023 08:53
Collect Rancher's cattle-cluster-agent heap profile every 2 minutes

Run the following commands:

export KUBECONFIG=/path/to/the/downstream_cluster.yaml

while true; do
    for pod in $(kubectl get pods -n cattle-system --no-headers -l app=cattle-cluster-agent | cut -d ' ' -f1); do
      echo getting heap for $pod
      kubectl exec -n cattle-system $pod -- curl -s http://localhost:6060/debug/pprof/heap -o heap
 kubectl cp cattle-system/${pod}:heap ./${pod}-heap-$(date +'%Y-%m-%dT%H_%M_%S')
@moio
moio / fleet_agent_pprof.md
Last active April 28, 2023 15:18
Explore a fleet-agent pprof CPU profile

Explore a fleet-agent pprof CPU profile

Grab a kubeconfig file pointing to the fleet-agent downstream cluster, then copypaste:

export KUBECONFIG=/path/to/kubeconfig.yaml

# get pod name
if kubectl get ns | grep cattle-fleet-system; then
 export NAMESPACE=cattle-fleet-system
@moio
moio / kwok.md
Created April 3, 2023 12:48
kubectl: install and run KWOK

kubectl: install and run KWOK

Installation

# install KWOK
KWOK_WORK_DIR=$(mktemp -d)
KWOK_REPO=kubernetes-sigs/kwok
KWOK_LATEST_RELEASE=$(curl "https://api.github.com/repos/${KWOK_REPO}/releases/latest" | jq -r '.tag_name')
@moio
moio / kubectl_node_proc_access.md
Last active January 26, 2024 12:02
kubectl: access a host's /proc filesystem from a (privileged) Pod

kubectl: access a host's /proc filesystem from a (privileged) Pod

export NODE_NAME=35.93.96.175
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: proc-writer
 labels: