Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Created September 20, 2023 18:12
Show Gist options
  • Save keyan1603/c8d74c9ca8c71d7b06d0e7726b076a75 to your computer and use it in GitHub Desktop.
Save keyan1603/c8d74c9ca8c71d7b06d0e7726b076a75 to your computer and use it in GitHub Desktop.
Some useful Kubectl commands - Quick reference
# Display list of all contexts in the current machine
kubectl config get-contexts
# Display the current-context
kubectl config current-context
# Set the default context to my-cluster-name
kubectl config use-context <my-cluster-name>
# Get all the nodes
kubectl get nodes
# Get all Pods
kubectl get pods
# Get all pods with additional details
kubectl get pods -o wide
# Get all services
kubectl get svc
# Get all jobs (if you have configured any)
kubectl get jobs
# Get all secrets
kubectl get secrets
# Get all events
kubectl get events
# Get all Persistent volumes
kubectl get pv
# Get into Windows Pod and access Powershell inside the pod
kubectl exec -it <podname> -- powershell
# Get logs of a Pod
kubectl logs <podname>
# Get last or most recent 100 lines of logs from a pod
kubectl logs <podname> --tail 100
# Get logs from pod written in the last hour
kubectl logs --since=1h <podname>
# Get the details of the Pod with all the details related to it
kubectl describe pod <podname>
# Get applied pod config
kubectl get pod <podname> -o yaml
# Get the details of the node
kubectl describe nodes <nodename>
# Add Metrics server to Kubernetes
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# Get ingress details
kubectl get ingress
# Add helm charts
helm repo add stable https://charts.helm.sh/stable
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
# Update helm repo
helm repo update
# Restart a demonset
kubectl rollout restart -n <namespace> daemonset <daemonset name>
# Restart a pod deployment (example - cm)
kubectl rollout restart deployment cm
# Copy files from local to Pod
kubectl cp Rules.config <pod namespace>/<podname>:c:\inetpub\wwwroot\rules.config
# Copy files from Pod to local
kubectl cp <pod namespace>/<podname>:\inetpub\wwwroot\web.config web.config
# Scale deployment - replica sets - Example: scaling CD to 4 servers
kubectl scale deployment cd --replicas=4
# Edit a deployment
kubectl edit deployment <deployment name>
eg: kubectl edit deployment cm
# Add a new user to your kubeconf that supports basic auth
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
# Get secrets from 'sitecore-database' - to list all the secrets
kubectl get secret sitecore-database -o json | ConvertFrom-Json | select -ExpandProperty data | % { $_.PSObject.Properties | % { $_.Name + [System.Environment]::NewLine + [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($_.Value)) + [System.Environment]::NewLine + [System.Environment]::NewLine } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment