Kubernetes#
Administration#
Setup ~/.bashrc#
Download: Kubernetes prompt for bash
curl -L https://raw.githubusercontent.com/jonmosco/kube-ps1/refs/heads/master/kube-ps1.sh -o ~/.bash_kube_ps1
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
source ~/.bash_kube_ps1
PS1='[\u@\h \W $(kube_ps1)]\$ '
Cluster and context#
Display addresses of the control plane and services#
Print the client and server version information#
Check cluster health#
Get the configuration of the cluster#
Display the current context#
Display the list of contexts#
Set the default context#
Merging kubeconfig files#
List the API resources#
List pods, services, daemonsets, deployments, etc...in all namespaces#
Nodes#
- Listing nodes
- Display resource usage (cpu/memory) for node
- Pods running on a node
- Get custom info about Nodes
k get nodes -o custom-columns="NAME:.metadata.name,INTERNAL_IP:.status.addresses[0].address,KERNEL:.status.nodeInfo.kernelVersion,MEMORY_PRESSURE:.status.conditions[0].status,DISK_PRESSURE:.status.conditions[1].status,PID_PRESSURE:.status.conditions[2].status,READY:.status.conditions[3].status"
Namespaces#
Listing namespaces#
Display details about namespace#
Deployments#
- List deployments
- Get details about a deployment
- Scale up/down a deployment
- Get deployment history
- Compare two revisions
diff <(k rollout history deployment <deployment_name>--revision=[revision_number]) <(k rollout history deployment <deployment_name>--revision=[revision_number])
Daemonsets#
- List daemonsets
- Display detailed state of daemonset
StatefulSet#
- List StatefulSet
- Scale Up/Down
Pods#
List pods#
Get information about a Pod#
Sort pods list using specified field. The field can be either 'cpu' or 'memory'
Get IP addr from Pod definition#
Logs#
Logs with label selector (10 lines if a selector is provided)
Exec command#
Get an interactive shell
- Attach to running process
Copy files#
Port Forward#
Example
ReplicaSet#
- Filter:
DESIRED!= 0
- Delete Pod
- Force Pod deletion
- List all Container images
k get pods -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
k get pods -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' | sort
- Get images IDs
k get pods -o jsonpath="{.items[*].status.containerStatuses[*].imageID}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
Get restartCount and state#
k get pods <pod_name> -o jsonpath='{.spec.containers[*].name} {.status.containerStatuses[*].restartCount} {.status.containerStatuses[*].state}'
Get Pods resources requests/limits#
k get pods -o custom-columns='NAME:.metadata.name,CPU_REQUEST:spec.containers[].resources.requests.cpu,CPU_LIMIT:spec.containers[].resources.limits.cpu,MEMORY_REQUEST:spec.containers[].resources.requests.memory,MEM_LIMIT:spec.containers[].resources.limits.memory'
Get pod states#
- Get Pods start time and ready time
k get pods -o custom-columns='NAME:.metadata.name,START_TIME:status.startTime,READY_TIME:.status.conditions[?(@.type=="Ready")].lastTransitionTime'
k get pods -o custom-columns='NAME:.metadata.name,START_TIME:status.startTime,READY:.status.conditions[?(@.type=="Ready")].status,READY_TIME:.status.conditions[?(@.type=="Ready")].lastTransitionTime' | (sed -u 1q; sort -k 3)
- Get
NAME,STARTED_ATandREADY_ATusingcustom-columns
k get pods -o custom-columns='NAME:.metadata.name,STARTED_AT:.status.containerStatuses[].state.running.startedAt,READY_AT:.status.conditions[?(@.type=="Ready")].lastTransitionTime'
- Get
Readytime(Headers ignored and sorted by date)
k get pods -o custom-columns='POD_NAME:.metadata.name,READY_AT:.status.conditions[?(@.type=="Ready")].lastTransitionTime' | (sed -u 1q; sort -k 2)
Running state(using jq)
Ready state(using jq)
Events#
- Get events with custom output
k get events --sort-by=.metadata.creationTimestamp -o custom-columns=LAST_SEEN:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.name,COMPONENT:.source.component,COUNT:.count,MESSAGE:.message
- Get events using
--field-selector
- List warnings events
- Sorting: Inverse order
- Get recently deleted pods
Persistent Volumes#
- Get pods with PVC
k get pods -o=json -A | jq -c '.items[] | {name: .metadata.name, namespace: .metadata.namespace, claimName: .spec | select( has ("volumes") ).volumes[] | select( has ("persistentVolumeClaim") ).persistentVolumeClaim.claimName }'
Secrets#
- Get secrets
- Decode secrets
- Decode Private Key
Maintenance#
Cordon the node(marked as unschedulable)#
Drain the workloads for the node#
Uncordon the node(marked as schedulable)#
Helm#
- List helm charts
- Update repo
- List all charts
- List all versions of all charts
- List the dependencies for the given chart
- Uninstall chart
- Locally render templates
Rancher, RKE2 and K3S#
crictl#
List images#
With k3s
Remove all unused images#
With k3s
Debugging Kubernetes nodes with crictl#
kubectl#
Systemd services#
RKE2 server
RKE2 agent
Uninstall#
containerd#
socket: /run/k3s/containerd/containerd.sock
Install Rancher using Docker#
Installing Rancher on a Single Node with default Rancher-generated Self-signed Certificate
K3S configuration#
Kind#
Quick Start(doc)#
- Create a cluster
- Lists existing kind clusters
- Get
kubeconfigof the cluster
- Delete a cluster
Known Issues#
- Pod errors due to "too many open files"(doc)
Netbox#
Installation#
- Install Netbox Helm Chart