Skip to content

Instantly share code, notes, and snippets.

@benc-uk
Last active February 3, 2024 15:19
Show Gist options
  • Save benc-uk/58ac45d05d00ea2db2bf86f6953fd225 to your computer and use it in GitHub Desktop.
Save benc-uk/58ac45d05d00ea2db2bf86f6953fd225 to your computer and use it in GitHub Desktop.
Kubernetes Stuff

General Kubernetes scripts and snippets

#!/bin/bash
HELP=false
# Display usage
usage(){
echo "\
aks-nodes.sh - Start or stop all nodes in your AKS cluster
Parameters:
-n, --name Name of AKS managed cluster (required)
-g, --group Resource group containing AKS managed cluster (required)
-a, --action VM action [start, stop]
[-h] Show this help text
" | column -t -s ";"
}
# Param handling stuff
OPTS=`getopt -o n:g:a:h --long name:,group:,action:,help -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; usage; exit 1 ; fi
eval set -- "$OPTS"
# Param handling stuff
while true; do
case "$1" in
-n | --name ) AKS="$2"; shift; shift;;
-g | --group ) GROUP="$2"; shift; shift;;
-a | --action ) ACTION="$2"; shift; shift;;
-h | --help ) HELP=true; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ ${HELP} = true ] ; then
usage
exit 0
fi
# Name and group are required params
if [ -z ${GROUP} ] || [ -z ${AKS} ] || [ -z ${ACTION} ] ; then
usage
exit 1
fi
# Action must be start or stop
if [[ ! ("$ACTION" == "stop" || "$ACTION" == "start") ]] ; then
usage
exit 1
fi
if [ ${ACTION} == "stop" ] ; then
ACTION="deallocate"
fi
loc=`az resource show -g $GROUP -n $AKS --resource-type Microsoft.ContainerService/managedClusters --query "location" -o tsv`
if [ -z ${loc} ] ; then
echo "Error. Unable to find AKS cluster '$AKS' in group '$GROUP'"
exit 0
fi
mcgroup="MC_${GROUP}_${AKS}_${loc}"
echo "Locating VMs in $mcgroup ..."
vmquery=`az vm list -g $mcgroup -o tsv --query "[].name"`
vmlist=$(echo $vmquery | tr " " "\n")
for vm in $vmlist
do
az vm $ACTION -g $mcgroup -n $vm --no-wait
echo "Running $ACTION on $vm ..."
done
#!/bin/bash
echo "### ❌ Deleting old service and waiting..."
kubectl delete svc dash-external -n kube-system
sleep 120
# CHANGE THIS TO YOUR STATIC IP IN AZURE
lbip="12.34.56.78"
ip=$(curl -s ipinfo.io/ip)
echo "### 🌐 Your public IP has been detected as $ip"
cat > dash.yaml <<- EOM
kind: Service
apiVersion: v1
namespace: kubernetes-dashboard
metadata:
name: dash-external
spec:
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 9090
selector:
k8s-app: kubernetes-dashboard
loadBalancerIP: $lbip
loadBalancerSourceRanges:
- $ip/32
EOM
echo "### πŸͺ„ Creating new service 'dash-external'"
kubectl apply -f dash.yaml -n kube-system
echo "### πŸ”Ž Get the new IP external, once it is assigned..."
kubectl get svc dash-external -n kube-system -w
#!/bin/bash
# One liner to add a new secret called CLUSTER_KUBECONFIG with access to the given AKS cluster
gh secret set CLUSTER_KUBECONFIG --body "$(az aks get-credentials -g resGrp -n clusterName --file -)"
helm ls --short | xargs -L1 helm delete
#!/bin/bash
image=${1:-"alpine/curl"}
cmd=${2:-"ps -ef"}
echo "πŸš€ Running command:"
echo " kubectl run cmdrunner -it --rm --image=$image --restart=Never -- $cmd"
echo
kubectl run cmdrunner -it --rm --image="$image" --privileged --restart=Never -- $cmd

Kubernetes Raspberry Pi Cluster 2024

This guide was written for Debian 12 aka Bookworm

Pre-prep:

https://computingforgeeks.com/install-kubernetes-cluster-on-debian-12-bookworm/

OS Preparation (all nodes)

Switch to root for all of this sudo su - it saves prefixing every command with sudo

Update the system and install common packages

apt update && apt -y full-upgrade
apt -y install iptables apt-transport-https gnupg2 software-properties-common apt-transport-https ca-certificates curl

Disable swap

swapoff -a
sudo dphys-swapfile swapoff && \
sudo dphys-swapfile uninstall && \
sudo systemctl disable dphys-swapfile

Enable iptables

update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

Enable some kernel modules

tee /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF

modprobe overlay
modprobe br_netfilter

Configure IP stack

tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl --system

Install Container Runtime (all nodes)

We will use containerd. Run these commands as root user

curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/trusted.gpg.d/debian.gpg
add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
apt update
apt install -y containerd.io
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
systemctl restart containerd
systemctl enable containerd

Appendix 1 - Static IP on Raspberry Pi OS Bookworm

sudo nmcli c show
sudo nmcli c mod 'preconfigured' ipv4.addresses 192.168.0.152/24 ipv4.method manual
sudo nmcli c mod 'preconfigured' ipv4.gateway 192.168.0.1
sudo nmcli c mod 'preconfigured' ipv4.dns 192.168.0.1

Then reboot

Other stuff

Install micro

  • mkdir -p ~/.local/bin && curl https://getmic.ro | bash && mv micro ~/.local/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment