fix: add k8s and hpc modules to main repo

This commit is contained in:
Jonas Juselius
2025-06-30 12:21:05 +02:00
parent 4aa9fa677a
commit bc3a034654
46 changed files with 4393 additions and 0 deletions

View File

@@ -0,0 +1 @@
for i in (seq 2 5); ssh k0- docker system prune -a;end

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env bash
ETCDCTL_API=3 etcdctl --endpoints https://etcd.local:2379 \
--cacert=/var/lib/kubernetes/secrets/ca.pem \
--cert=/var/lib/kubernetes/secrets/kube-apiserver-etcd-client.pem \
--key=/var/lib/kubernetes/secrets/kube-apiserver-etcd-client-key.pem \
snapshot save snapshot.db

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
token=$(kubectl get secret -n kube-system | grep cluster-admin-token | cut -d' ' -f1)
kubectl get secret -n kube-system $token -o yaml | \
grep ' token:' | cut -d' ' -f4 | base64 -d

View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bash
token=UTjgSspYQcX-BVUd1UsC
api=https://gitlab.com/api/v4
prune () {
id=$1
reg=$(curl -s --header "PRIVATE-TOKEN: $token" \
"$api/projects/$id/registry/repositories" \
| json_pp | sed -n 's/^ *"id" *: *\([0-9]\+\).*/\1/p')
for i in $reg; do
curl -s --request DELETE --data 'keep_n=10' \
--data 'name_regex=.*[0-9].*' \
--header "PRIVATE-TOKEN: $token" \
"$api/projects/$id/registry/repositories/$i/tags"
done
}
gc () {
pod=$(kubectl get pod -n gitlab -lapp=registry | tail -1 | cut -d' ' -f1)
kubectl exec -n gitlab $pod -- \
registry garbage-collect /etc/docker/registry/config.yml -m
}
all () {
groups=$(curl -s --header "PRIVATE-TOKEN: $token" "$api/groups" \
| json_pp | sed -n 's/^ *"id" *: *\([0-9]\+\).*/\1/p')
for g in $groups; do
proj=$(curl -s --header "PRIVATE-TOKEN: $token" \
"$api/groups/$g/projects?simple=true&include_subgroups=true" \
| json_pp | sed -n 's/^ \{6\}"id" *: *\([0-9]\+\).*/\1/p')
for p in $proj; do
prune $p
done
done
}
projects () {
for i in $@; do
prune $(echo $i | sed 's,/,%2F,g')
done
}
case $1 in
--all) all ;;
*) projects $@
esac
gc

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
TOP="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
linkerd=$(which kubectl 2> /dev/null)
if [ -z "$linkerd" ]; then
echo "linkerd cli is not available"
exit 1
fi
inject () {
for i in $@; do
kubectl get ns $i -o yaml | linkerd inject - | kubectl apply -f-
kubectl rollout restart daemonsets -n $i
kubectl rollout restart statefulsets -n $i
kubectl rollout restart deployments -n $i
done
}
if [ $# > 0 ]; then
inject $@
else
inject $(kubectl get ns | sed "1d; /kube-system/d; s/ .*//")
fi

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
TOP="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
if [ $# != 2 ]; then
echo "usage: inject-sa-pull-secrets.sh {namespace} {all|serviceaccount}"
exit 1
fi
namespace=$1
sa=$2
inject () {
kubectl patch serviceaccount $1 \
-n $namespace \
-p "{\"imagePullSecrets\": [ \
{\"name\": \"docker-pull-secret\"}, \
{\"name\": \"gitlab-pull-secret\"} \
]}"
}
if [ $sa = all ]; then
for i in $(kubectl get sa -n $namespace | sed '1d;s/\([^ ]\+\).*/\1/'); do
inject $i
done
else
inject $sa
fi

View File

@@ -0,0 +1,76 @@
#!/usr/bin/env bash
set +e
TOP="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
if [ x$1 = x ]; then
ehco "usage: install-namespace.sh {namespace|all}"
exit 1
fi
namespace=$1
setup_namespace () {
local namespace
namespace=$1
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
annotations:
linkerd.io/inject: enabled
labels:
name: $namespace
name: $namespace
EOF
}
create_docker_secret () {
local namespace
namespace=$1
kubectl get secret docker-pull-secret -n $namespace >/dev/null 2>&1
[ $? = 0 ] && kubectl delete secret docker-pull-secret -n $namespace
kubectl create secret docker-registry docker-pull-secret \
-n $namespace \
--docker-username=juselius \
--docker-password=ed584a31-c7ff-47ba-8469-3f0f4db6402c \
--docker-email=jonas.juselius@gmail.com
}
create_gitlab_secret () {
local namespace
namespace=$1
cat << EOF | kubectl apply -f -
apiVersion: v1
metadata:
name: gitlab-pull-secret
namespace: $namespace
kind: Secret
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSJyZWdpc3RyeS5naXRsYWIuY29tIjogewoJCQkiYXV0aCI6ICJaMmwwYkdGaUsyUmxjR3h2ZVMxMGIydGxiaTB4T1Rnd01qQTZPRmxqU0VoMFZIaENSVUZUTFZKUWRsSnJXbGM9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy4xMiAobGludXgpIgoJfQp9Cg==
EOF
}
inject_pull_secrets () {
local namespace
namespace=$1
$TOP/inject-sa-pull-secrets.sh $namespace all
}
configure_namespace () {
setup_namespace $1
create_docker_secret $1
create_gitlab_secret $1
inject_pull_secrets $1
}
if [ "x$namespace" = "xall" ]; then
for i in $(kubectl get ns | sed '1d;/^kube-system/d;s/\([^ ]\+\).*/\1/'); do
configure_namespace $i
done
else
configure_namespace $namespace
fi

13
modules/k8s/scripts/k8s-all Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# Simple script for fetching all resources from a namespace, might include some
# clutter
[ $# -ne 1 ] && echo "Usage: k8s-all [namespace]" && exit 1
for r in $(kubectl api-resources --verbs=list --namespaced -o name)
do
echo "=== Resource: $r ==="; echo \
&& kubectl get $r -n $1 --ignore-not-found \
&& echo
done

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
pods=$(kubectl get po -A -l linkerd.io/control-plane-ns -ojsonpath="{range .items[*]}{.metadata.name} {.metadata.namespace}{'\n'}{end}")
IFS=" "
while read name namespace; do
tcp=$(kubectl exec -n $namespace $name -c linkerd-proxy -- cat /proc/net/tcp)
close_wait=$(echo $tcp | awk 'BEGIN {cnt=0} $4==08 {cnt++} END {print cnt}')
fin_wait_2=$(echo $tcp | awk 'BEGIN {cnt=0} $4==05 {cnt++} END {print cnt}')
if [ "$close_wait" -gt "0" -o "$fin_wait_2" -gt "0" ]; then
echo "$name.$namespace has $close_wait sockets in CLOSE_WAIT and $fin_wait_2 sockets in FIN_WAIT_2"
else
echo "$name.$namespace is okay"
fi
done <<< "$pods"

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
kubectl delete secrets --all-namespaces --field-selector='type=kubernetes.io/service-account-token'

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# master="etcd.service"
master=""
node="flannel.service"
nodes="@nodes@"
master_node="@master@"
# nodes=$(kubectl get nodes --no-headers | cut -d' ' -f1)
# master_node=$(echo $nodes | cut -d' ' -f1)
echo "$master_node: systemctl restart $master"
sudo systemctl restart $master
for n in $nodes; do
echo "$n: systemctl restart $node"
ssh root@$n systemctl restart $node &
done
echo "Waiting..."
wait

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
master="kube-apiserver kube-scheduler kube-controller-manager"
node="kube-proxy kubelet kube-certmgr-apitoken-bootstrap"
nodes="@nodes@"
master_node="@master@"
# nodes=$(kubectl get nodes --no-headers | cut -d' ' -f1)
# master_node=$(echo $nodes | cut -d' ' -f1)
echo "$master_node: systemctl restart $master"
sudo systemctl restart $master
for n in $nodes; do
echo "$n: systemctl restart $node"
ssh root@$n systemctl restart $node &
done
echo "Waiting..."
wait

View File

@@ -0,0 +1,3 @@
#!/bin/sh
kubectl taint node $1 ClusterService="true":NoSchedule

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
repos=(
"stable=https://charts.helm.sh/stable"
"ingress-nginx=https://kubernetes.github.io/ingress-nginx"
"prometheus-community=https://prometheus-community.github.io/helm-charts"
"hashicorp=https://helm.releases.hashicorp.com"
"bitnami=https://charts.bitnami.com/bitnami"
"minio=https://helm.min.io/"
"anchore=https://charts.anchore.io"
"linkerd=https://helm.linkerd.io/stable"
)
for i in ${repos[@]}; do
IFS="="
set $i
helm repo add $1 $2
done
helm repo update

View File

@@ -0,0 +1,5 @@
for pem in /var/lib/cfssl/*.pem /var/lib/kubernetes/secrets/*.pem; do
printf 'exp: %s: %s\n' \
"$(date --date="$(openssl x509 -enddate -noout -in "$pem"|cut -d= -f 2)" --iso-8601)" \
"$pem"
done | sort

12
modules/k8s/scripts/ws-curl.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/sh
host=$1; shift
curl -i -N \
-H "Connection: upgrade"\
-H "Upgrade: websocket"\
-H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ=="\
-H "Sec-WebSocket-Version: 13"\
-H "Origin: http://foo.com/"\
-H "Host: $host" $@

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
for i in (kubectl get pods -A |grep CrashLoop | sed 's/^\([^ ]\+\) \+\([^ ]\+\) .*/kubectl delete pod -n \1 \2 --force=true/'); eval $i; end

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
kubectl delete pods --field-selector 'status.phase==Failed' -A

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
for i in $(kubectl get nodes | sed -nr 's/^(k[^ ]+) .*/\1/p'); do
ssh root@$i pkill node_exporter
done