kubectl, oc Openshift, Other K8s related command Snippets, Other K8s related command Snippets
Kubectl
Section titled “Kubectl”# See nodes, pods, services, deploymentskubectl get node## See more pod informationkubectl get pod -o widekubectl get pod
## Get resource information as yamlkubectl get pod/mypod2 -o yamlkubectl get service/myservice -o yaml > service-myservice.yaml
# See serviceskubectl get svckubectl get deployment## See replicas of podskubectl get replicaset## See all deployments, serviceskubectl get all## See all deployments, services in all namespaceskubectl get all --all-namespaces
## Get all pods in namespacekubectl get pods -n <namespace>
# See pod detailskubectl describe pod <pod name>
# Create components with deployments## Helpkubectl create -h
## View emply YAML manifests for K8s components### --dry-run=client : only print out the object### -oyaml : output in yaml formatkubectl create <kind> <component-name> --dry-run=client -oyaml### Examplekubectl create deployment test --image redis --dry-run=client -oyaml
## Deploymentkubectl create deployment NAME --image=image COMMANDS### Example: Create an nginx deploymentkubectl edit deployment nginx-depl### Editor deployment, open yaml in EDITORkubectl edit deployment nginx-depl## Get deployments on all namespaceskubectl get deployment --all-namespaces=true## Get deployment in yaml format, check statuskubectl get deployment <deployment-name> -o yamlkubectl get deployment <deployment-name> -o yaml > result.yaml### nushellkubectl get deployment <deployment-name> -o yaml | save result.yaml
# Serviceskubectl get svckubectl describe service <service-name>kubectl delete svc <service-name>
# Apply a configuration, repeat to update a deployment## -f filekubectl apply -f config-file.yaml
# Debugging, Logskubectl logs <pod name>
# Command execution in container with -it interactive terminalkubectl exec -it <pod name> -- bin/bash
# Check versionkubectl version
# Remove a deploymentkubectl delete deployment <deployment name>kubectl delete deployment <deployment name> --namespace <namespace name>
# Ingress## See all ingresskubectl get ingress -A
# Switch cluster or use kubectxkubectl config use-context <cluster># Switch namespace or use kubenskubectl config set-context --current --namespace=<namespace>
# Namespaceskubectl create namespace my-namespacekubectl get namespace## See namespace and non-namespace resourceskubectl api-resources --namespace=falsekubectl api-resources --namespace=true# Set namespace on applykubectl apply -f deployment.yaml --namespace=my-namespacekubectl get all --namespace=my-namespace# Change active namespacekubectl config set-context --current --namespace=my-namespace## List namespaceskubens## Change namespacekubens my-namspace
# Create pod and run image with parameters## cpu-test : name of pod## --image= : name of image, in this case from Docker Hub## -- <text> : parameters for imagekubectl run cpu-test --image=containerstack/cpustress -- --cpu 4 --timeout 30s --metrics-brief# Delete previously created podkubectl delete pod cpu-test
# Scale replicas## Remove mongodb examplekubectl scale --replicas=0 statefulset/mongodb## Restart mongodb example and see pods will re-establish connection with persistent storeskubectl scale --replicas=3 statefulset/mongodb
# Role Based Access Control, Roles# Set rolekubectl apply -f developer-role.yaml
# Get roleskubectl get roleskubectl describe role developer# Check current user API access## Check if user can do a commandkubectl auth can-i create deployments -n dev## As admin, can also check permissions of other users
Kubectl Shell Settings
Section titled “Kubectl Shell Settings”# Set default kubectl editorexport KUBE_EDITOR="nvim"## as VS Codeexport KUBE_EDITOR="code --wait"
# Short handalias k=kubectl
Prometheus Operator
Section titled “Prometheus Operator”# View rules in specific namespacekubectl get PrometheusRule -n monitoring
OC - Openshift
Section titled “OC - Openshift”Source: Chapter 2. OpenShift CLI (oc) | Red Hat Product Documentation
# Login (requires version >= 4.14)oc login# Login with web APIoc login --web --server=openshift_api_server# Login example with weboc login --web --server=https://api.myserver.com:6443
# Status, Endpoint informationoc status
# List namespacesoc get projects
# Switch projectsoc project <project_name>
# Get all pods in namespaceoc get pods -n <namespace># in current selected namespaceoc get pods
# Connect to a podoc rsh --shell=/bin/bash pod_name
# Tail Container Logs# Replace <pod_name> with the name of the pod where the container is running and <container_name> with the name of the container you want to view logs from.# The -f option streams the logs so they will be continuously displayed in the terminal until you stop it by pressing CTRL + C.oc logs -f <pod_name> -c <container_name>
# Execute a command in a container# Replace <pod_name> with the name of the pod where the container is running and <container_name> with the name of the container you want to execute the command in.oc exec -it <pod_name> -c <container_name> -- <command># Exampleoc exec -it prod-connect-cluster-dbz-connect-0 -- curl -v telnet://10.33.33.33:1433
Kubectx, Kubens - Switch k8s clusters, namespaces
Section titled “Kubectx, Kubens - Switch k8s clusters, namespaces”# Switch cluster/context namekubectx <context_name>
# Switch namespacekubens <namespace>
Stern - tail pods
Section titled “Stern - tail pods”stern <pod-name>
# Look at specific containerstern --container <container-name> <pod-name>
k9s - terminal user interface for Kubernetes clusters
Section titled “k9s - terminal user interface for Kubernetes clusters”Source: k9s Commands
# Open to current cluster/contextk9s
# List all available CLI optionsk9s help# Get info about K9s runtime (logs, configs, etc..)k9s info# Run K9s in a given namespace.k9s -n mycoolns# Run K9s and launch in pod view via the pod command.k9s -c pod# Start K9s in a non default KubeConfig contextk9s --context coolCtx# Start K9s in readonly mode - with all modification commands disabledk9s --readonly