Skip to content

Instantly share code, notes, and snippets.

@dkeightley
dkeightley / rke2-userdata.sh
Last active October 19, 2021 08:34
Single node RKE2 cluster userdata
#!/bin/sh
PUBLIC_IP=$(curl ifconfig.io)
# export INSTALL_RKE2_VERSION="v1.20.5+rke2r1"
curl -sfL https://get.rke2.io | sh -
mkdir -p /etc/rancher/rke2
cat > /etc/rancher/rke2/config.yaml <<EOF
write-kubeconfig-mode: "0640"
@dkeightley
dkeightley / clean-monitoring-v1.sh
Created April 1, 2021 00:25
clean-monitoring-v1
for clusterId in $(kubectl get clusters.management.cattle.io -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}');
do
# empty recipients for cluster alert groups
for clusterAlertGroup in $(kubectl -n $clusterId get clusteralertgroups -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}');
do
kubectl -n $clusterId patch clusteralertgroup $clusterAlertGroup --type merge --patch '{"spec": {"recipients": []}}'
done
# disable cluster monitoring
kubectl patch clusters.management.cattle.io $clusterId --type merge --patch '{"spec": {"enableClusterMonitoring": false}}'
for projectId in $(kubectl -n $clusterId get projects.management.cattle.io -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
for i in $(kubectl get namespaces -o custom-columns=":metadata.name" --no-headers)
do
echo "------ $i"
kubectl get -n $i configmaps --no-headers > /dev/null | wc -l
done
export ETCDCTL_API=3
for key in `etcdctl get --prefix --keys-only /registry/configmaps`
do
size=`etcdctl get $key --print-value-only | wc -c`
versions=`etcdctl get $key --write-out=fields | grep \"Version\" | cut -f2 -d':'`
total=$(( $size * $versions))
echo $total $size $versions $key >> /tmp/etcdkeys-configmaps.txt
done
@dkeightley
dkeightley / ingress-to-pods.sh
Created February 5, 2021 00:23
ingress-to-pods.sh
SERVICE=my-nginx
NAMESPACE=default
PORT=80
for ingresspod in $(kubectl -n ingress-nginx get pods -l app=ingress-nginx --template '{{range.items}}{{.metadata.name}}{{"\n"}}{{end}}')
do
echo $ingresspod
for svcep in $(kubectl -n $NAMESPACE get ep $SERVICE -o json | jq -r '.subsets[].addresses[].ip')
do
echo "=> ${svcep}"
@dkeightley
dkeightley / rke2-lab-setup-cis-selinux.md
Last active November 11, 2022 02:24
rke2-lab-setup-cis-selinux

Server node 1

Install

RH-based

curl -sfL https://get.rke2.io | INSTALL_RKE2_METHOD=rpm sh -
sudo cp -f /usr/share/rke2/rke2-cis-sysctl.conf /etc/sysctl.d/60-rke2-cis.conf
mkdir -p /etc/rancher/rke2
@dkeightley
dkeightley / k3s-rancher-userdata.sh
Last active October 2, 2021 14:27
Quick rancher + k3s bootstrap
#!/bin/sh
echo "Installing K3S"
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.19.5+k3s2" sh -
PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
echo "Downlading cert-manager CRDs"
wget -q -P /var/lib/rancher/k3s/server/manifests/ https://github.com/jetstack/cert-manager/releases/download/v0.15.0/cert-manager.crds.yaml
cat > /var/lib/rancher/k3s/server/manifests/rancher.yaml << EOF
@dkeightley
dkeightley / k3s-db-compaction.sh
Last active October 29, 2020 00:09
k3s db compaction
#!/bin/bash
MYSQL_PASS=
MYSQL_USER=master
LIMIT=100000
SELECT_STMT="select max(id) m, count(*) c, name from kine group by name order by c desc limit 100;"
# start compaction
@dkeightley
dkeightley / box-request.sh
Created August 5, 2020 02:50
Function to create box folder and file requests
function box-request()
{
_BEARER_TOKEN="<box app token>"
_FOLDER_NAME=$1
echo -n "Creating ${_FOLDER_NAME}"
_FOLDER_ID=$(curl -sX POST https://api.box.com/2.0/folders \
-H "Authorization: Bearer ${_BEARER_TOKEN}" \
-H 'Content-Type: application/json' \
@dkeightley
dkeightley / etcd-objects.sh
Last active June 4, 2020 01:33
etcd-objects
## Run within a etcd container
# etcd object sizes
export ETCDCTL_API=3
for key in `etcdctl get --prefix --keys-only /`
do
size=`etcdctl get $key --print-value-only | wc -c`
versions=`etcdctl get $key --write-out=fields | grep \"Version\" | cut -f2 -d':'`
total=$(( $size * $versions))
echo $total $size $versions $key >> /tmp/etcdkeys.txt