Skip to content

Instantly share code, notes, and snippets.

@thetillhoff
Last active November 5, 2023 23:40
Show Gist options
  • Save thetillhoff/e19ffed34f4b441ed8b598087b74a7da to your computer and use it in GitHub Desktop.
Save thetillhoff/e19ffed34f4b441ed8b598087b74a7da to your computer and use it in GitHub Desktop.
devops cheat sheet

Get ec2 instance information from within (when connected via ssh)

curl http://169.254.169.254/latest/dynamic/instance-identity/document

Make GET request

curl -X GET https://thetillhoff.de

Make POST request

curl -X POST -F 'key=value' https://thetillhoff.de

Get header

curl -I https://thetillhoff.de

Update ubuntu/debian packages without clutter

RUN apt-get update && \
  apt-get upgrade -y && \
  apt-get autoremove -y && \
  apt-get clean -y && \
  rm -rf /tmp/* /var/tmp/* /var/cache/apt/* /var/cache/distfiles/*

Manually trigger reconcilation

flux reconcile source git flux-system

Create tag

git tag <tag>

Push tag

git push origin <tag> # single tag
git push --tags # all tags

Delete tag

# local tag only
git tag -d <tag>

# remote tag only
git push --delete origin <tag>

Update local clone - either branches, tags or both

git fetch --prune # delete branches locally if they don't exist on remote
git fetch --prune-tags # delete tags locally if they don't exist on remote
git fetch --prune --prune-tags # delete branches and tags locally if they don't exist on remote

Show kubeconfig

kubectl config view

List all resource names, including their abbreviations

kubectl api-resources

Show all resources of all namespaces

kubectl get all -A
kubectl get all,cm,secret,ing -A

Get details

kubectl get <resource> [<name>] -o wide # also shows ip of pods
kubectl describe <resource> [<name>] # also shows latest events like scaling
kubectl logs <podname>
kubectl exec <podname> -- <command> # non-interactive!
kubectl exec -ti <podname> -- <command> # interactive, -ti == --stdin --tty
kubectl top node <nodename> # Show metrics for a given node

Labels

kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp
kubectl label pods <podname> version=v1

Expose deployment

kubectl expose deployment/<name> --type="NodePort" --port 8080
kubectl delete service -l app=kubernetes-bootcamp

Scale

kubectl scale deployments/kubernetes-bootcamp --replicas=4

Delete failed pods

kubectl delete pods --field-selector status.phase=Failed -A

Get nodeports

kubectl get service <service-name> --output='jsonpath="{.spec.ports[0].nodePort}"'

Port forwarding

kubectl port-forward <container-id> <local-port>:<container-port>

Run command in pod

Run command in existing pod (1 container case)

kubectl exec <pod-name> -- ls /

Run command in existing pod (multi-container case)

kubectl exec <pod-name> -c <container-name> -- ls /

Interactive shell access to a running pod (1 container case)

kubectl exec -it <pod-name> -- /bin/sh

Show metrics for a given pod and sort it by 'cpu' or 'memory'

kubectl top pod <pod-name> --sort-by=cpu

Get token for kubernetes dashboard

kubectl -n kube-system get secret
kubectl -n kube-system describe secret deployment-controller-token-*

Restart deployments/daemonsets

kubectl rollout restart deployment/<deployment-name>

Get master nodes

kubectl get node --selector='!node-role.kubernetes.io/master'

Namespace stuck in terminating state

First get the resource that is blocking the deletetion

kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found -n <terminating-namespace>

Get list of container in pod

kubectl get pod <pod-name> -o jsonpath="{.spec.containers[*].name}"

Get all resources in a namespace

kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found

Access service in another namespace

(via k8s internal DNS, FQDN for that is kube-dns.kube-system.svc.cluster.local)

servicename.namespace.svc.cluster.local

Ping

ping -c 4 <destination>

Get foldersize at specific path

tags: storage, size, space

du -shx <path>/* 2>/dev/null
# sorted, top 5:
du -shx <path>/* 2>/dev/null | sort -rh | head -n 5

Clear memory cache (without impact on programs)

sync; echo 3 > /proc/sys/vm/drop_caches

Get disk size per device

lsblk
lsblk -a # all devices (rarely a difference)
lsblk -f # show filesystem for each device

Get disk usage

(In human readable format)

df -h

Get overall memory usage

(In human readable format)

free -h

Get top 5 memory consumers

The first column is the memory in percent

ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

Flush dns cache

Choose command depending on your linux system

sudo systemd-resolve --flush-caches # ubuntu, debian

Get open ports and which process is using it

netstat -tulpn

Find file/s

find . -name abc.txt -maxdepth 5 # implicitly recursive

Add opengpg apt-get repository

curl -fsSL <key-url> | gpg --dearmor --yes -o /usr/share/keyrings/<filename>.gpg
OR
wget -O- | gpg --dearmor > /usr/share/keyrings/<filename>.gpg

echo "<repo-url>" > /etc/apt/sources.list.d/<filename>.list

Remove host from known_hosts

ssh-keygen -R <hostname>

Delete /tmp contents that weren't accessed in the last 10 days

(/tmp is deleted on reboots, so this is most useful for servers that are rarely rebooted)

sudo find /tmp -type f -atime +10 -delete

Get filename only of an path in a environment variable

${SOMEVARIABLE##*/}

Get disks / blockdevices

lsblk
lsblk -f # this also prints the filesystems on the devices

Clear syslog

truncate -s 0 /var/log/syslog

Get processes with most open files

lsof | awk '{ print "- name: " $1 " pid: " $2; }' | uniq -c | sort -rn | head

Get DNS records

nslookup <domain>
or
dig <domain> +short
or
dig <domain> <type> +short
or
dig <domain <type> +short
or
dig @<DNS server> <domain> +short

Get nameservers of a domain

dig NS <domain>

Get uid or gid of a user

cat /etc/passwd
  <user>:x:<uid>:<gid>::<homedir>:<console>

tar gz compress a folder

tar -czf <filename>.tar.gz <path/to/folder>

tar gz uncompress a folder

tar -xf <filename>.tar.gz

Install base tooling for debugging (debian/ubuntu)

apt install -y \
  curl \
  wget \
  nano \
# for nslookup,dig:
  dnsutils \
# for ping:
  iputils-ping \
# for ifconfig:
  net-tools \
# for ps,top:
  procps

Copy with progress bar

rsync -ah --info=progress2 <source> <destination>
rsync -ah --info=progress2 --delete <source> <destination>
# -a stands for archive and will preserve permissions, timestamps, ...
# -h stands for human readable values
# --info=progress2 will display progress over all files, instead of per file

Copy multiple files from different folders in one command

cp ./{some/where.a,some/where/else.b,$variablelocation/c.d} ./destination/
# or simply
cp ./some/where.a ./some/where/else.b $variablelocation/c.d ./destination/

Flush dns cache

dscacheutil -flushcache

Flush dns cache

ipconfig /flushdns

List all installed programs

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |Format-Table -AutoSize

List all installed app-programs

Get-AppxPackage | Select-Object Name, PackageFullName, Version |Format-Table
-AutoSize

Shutdown after specific time

shutdown -s -t <seconds>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment