📗
Everyday Cheat Sheets
  • README
  • Build
    • Architecture
      • API Management
        • Kong
      • Authentication
        • Keycloak
        • Okta
      • Cloud Native
      • Design Patterns
      • Design Principles
      • IaC
      • IoT
      • Message Broker
      • Methods
      • Networking
      • Payment
        • Stripe
      • Testing
    • Code lifecycle (ALM)
      • Automation Pipelines
        • Argo CD
        • CircleCI
        • Flux
        • Keptn
        • Travis
      • Azure DevOps
        • Azure Pipelines
      • Chef
      • GitHub
        • GitHub Actions
      • Nexus
      • Promyze
      • RunDeck
      • SaltStack
      • Sonar
      • Tuleap
    • Containers & Cloud Native
      • Argo Workflows
      • Containerization
        • Docker
          • Docker CLI
        • containerd
        • cri-o
      • CNAB
      • Dapr
      • Envoy
      • Fluentd
      • Knative
      • Kubernetes
        • Cluster API
        • etcd
        • Helm
        • k3d
        • kind
        • Kubectl
        • MetalLB
        • Minikube
      • Open Application Model (OAM)
      • Unleash
    • Data storage
      • MySQL
      • MongoDB
        • Atlas
        • Compass
        • Evergreen
        • MongoDB 4.2
        • MongoDB 5.0
        • MongoDB design
        • MongoDB events
        • MongoDB driver for .NET
        • Ops Manager
        • Realm
      • Oracle
      • Redis
      • SQL Server
      • PostgreSQL
    • Frameworks & libraries
      • Angular
        • Angular CLI
        • Angular events
      • .NET
        • ASP.NET Core
        • Blazor
        • .NET 5.0
        • .NET 6.0
        • .NET CLI
        • .NET Core
        • .NET Events
        • .NET Logging
        • .NET Testing
        • NuGet
        • WPF
        • Xamarin
      • gRPC
      • Ionic
      • Jekyll
      • Node.js
        • Express
        • NPM
      • React
        • React Native
      • Redux
    • IDE
      • Visual Studio 2022
    • Languages
      • C#
        • C# 8.0
      • ECMAScript
      • GraphQL
      • JavaScript
        • webpack
        • Yarn
      • MS-DOS
      • PHP
      • PowerShell
      • Python
      • Swagger
      • TypeScript
    • Messaging
      • Azure Service Bus
      • RabbitMQ
    • Testing
    • Workstation
      • QGIS
      • Visual Studio 2019
      • Windows 10
      • Windows Subsystem for Linux
  • Collaborate
    • Marp
    • Microsoft 365
      • Microsoft Graph
      • SharePoint Framework
        • Fluent UI
        • SharePoint Framework UI components
  • Run
    • Cloud computing
      • Alibaba
      • AWS
      • Azure
        • Azure AD
        • Azure CLI
        • Azure Container Registry
        • Azure Portal
        • Azure Service Bus
      • Firebase
      • OVH
    • Hardware
      • Single-board computers
        • Odroid
        • Raspberry Pi
    • Infrastructure automation
      • Azure Resource Manager
      • Packer
      • Pulumi
      • Puppet
      • Terraform
        • HCL
        • Terraform CLI
        • Terraform Providers
    • Networking
      • HAProxy
      • nginx
    • Observability
      • Grafana Labs
        • Grafana
        • Loki
        • Tempo
      • OpenTelemetry
      • Prometheus
      • Splunk
    • Security
      • Falco
    • Systems
      • Linux
        • CentOS
        • eBPF
        • Linux Kernel
        • Rocky
        • Ubuntu
      • Windows Server
    • Virtualization
      • Hyper-V
      • Vagrant
  • Optimize
    • DevOps
  • Join
    • Companies
      • HashiCorp
Powered by GitBook
On this page
  • Documentation
  • Local configuration
  • Cluster information
  • Management
  • Objects
  • Namespaces (ns)
  • Pods
  • ServiceAccounts
  • Definitions
  • RBAC
  • MutatingWebhookConfiguration
  • ValidatingWebhookConfiguration
  • Secrets
  • CronJobs (cj)
  • ConfigMap
  • Deployments
  • Services
  • Events
  • Ingress
  • Actions
  • Scale
  • Port forwarding
  • Proxy
  • Service provider
  • Azure Kubernetes Service (AKS)
  • Google Kubernetes Engine (GKE)
  • Recipes
  • Quick fixes
  • Known issues

Was this helpful?

Export as PDF
  1. Build
  2. Containers & Cloud Native
  3. Kubernetes

Kubectl

PreviouskindNextMetalLB

Last updated 4 years ago

Was this helpful?

Documentation

Local configuration

~/.kube/config is the local configuration file (contains all the contexts, information about the clusters and user credentials)

# get current context
kubectl config current-context

# display context configuration
kubectl config get-contexts

# change context
kubectl config use-context <cluster-name>

Cluster information

# display version
kubectl version

# display cluster information
kubectl cluster-info

# display cluster configuration
kubectl config get-clusters

# get health information for the control plane components (the scheduler, the controller manager and etcd)
kubectl get componentstatuses

# list all the nodes in the cluster and report their status and Kubernetes version
kubectl get nodes

# show the CPU and memory capacity of each node, and how much of each is currently in use
kubectl top pods

# view sereval resources at once
kubectl get deploy,rs,po,svc,ep

Management

# create resources from a manifest file
kubectl create -f <filename>

# create or update resources from a manifest file
kubectl update -f <filename>

# delete resources from a manifest file
kubectl delete -f <filename>

Objects

Namespaces (ns)

# list all namespaces
kubectl get namespaces

# create a new namespace
kubectl create ns hello-there

Pods

# list pods of a specific namespace
kubectl get pods --namespace kube-system

# list pods of all namespaces
kubectl get pods -A

# get more information about a pod
kubectl describe pod

# get log information of a specific pod
kubectl logs

# get pod yaml definition
kubectl get pod -o yaml

# watch pods
watch kubectl get pod --all-namespaces

# desribe a pod
kubectl describe pod <pod-name> --namespace <namespace>

# get pod logs
kubectl logs [--tail=20] [--since=1h] <pod-name>

# display metrics about a pod and its containers
kubectl top pod <pod-name> --containers

# execute commands inside a pod (for investigation purpose)
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash

# download or upload files from a container
kubectl cp my-file.txt <namespace>/<pod-name>:my-file.txt
kubectl cp <namespace>/<pod-name>:my-file.txt my-file.txt

ServiceAccounts

# see all service accounts in all namespaces
kubectl get ServiceAccount -A

Definitions

CustomResourceDefinition

RBAC

ClusterRole

ClusterRoleBinding

Role

RoleBinding

MutatingWebhookConfiguration

ValidatingWebhookConfiguration

Secrets

# see all secrets in all namespaces
kubectl get secrets -A

CronJobs (cj)

# create a CronJob
kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hello

# update a CronJob
kubectl edit cronjob/my-cron

# update a CronJob with a specific IDE
KUBE_EDITOR="nano" kubectl edit cronjob/my-cron

# delete a CronJob
kubectl delete cronjob my-cron

ConfigMap

Deployments

kubectl get deployment

Services

# see all services in all namespaces
kubectl get services -A

Events

kubectl get events --sort-by=.metadata.creationTimestamp

Ingress

# see all ingresses in all namespaces
kubectl get ingress -A

# see a resource definition
kubectl get ingress mymicroservice -o yaml

Actions

Scale

kubectl scale

Port forwarding

kubectl port-forward xxx 8080:80

Proxy

# runs a proxy to the Kubernetes API Server
kubectl proxy

Service provider

Azure Kubernetes Service (AKS)

# review agent pool specification
az aks show --resource-group myrgname --name myaksname --query agentPoolProfiles

# scale agent pool (2 nodes here)
az aks scale --resource-group myrgname --name myaksname --node-count 2 --query properties.provisioningState

Google Kubernetes Engine (GKE)

gcloud container clusters create mycluster

gcloud container clusters list

kubectl get nodes

gcloud container clusters delete linuxfoundation

Recipes

Quick fixes

# find and delete pods
kubectl delete pods $(kubectl get pods -o=name | grep mypodname | sed "s/^.\{4\}//")

Known issues

Issue

Advice

Pod with status CreateContainerConfigError

Look at the pod logs (kubectl logs podxxx), the issue should be detailed there

Overview
Installation
Cheatsheet
Source code