Kubectl
lastmod 2026-03-13created 2022-10-0120 edits
Kubectl is a CLI (Command Line Interface) for issuing commands to a Kubernetes cluster.
Command examples
Check nodes
Check namespaces:
Check pods (POD):
Print everything:
Installation
Simply install it from snap:
Cluster access
Use the KUBECONFIG environment variable:
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl get pods --all-namespaces
helm ls --all-namespaces
Or specify the location of the kubeconfig file in the command:
kubectl --kubeconfig /etc/rancher/k3s/k3s.yaml get pods --all-namespaces
helm --kubeconfig /etc/rancher/k3s/k3s.yaml ls --all-namespaces
Running per file
To apply:
To remove it as is:
How to remove
Remove a Deployment:
To delete all resources in a specific namespace:
Accessing the cluster from outside with kubectl
Set the KUBECONFIG environment variable to the path of the kube config yaml file and run kubectl, or copy it to ~/.kube/config.
If you use k3s, see k3s#Accessing the cluster from outside with kubectl.
Commands
Basic commands (beginner)
- Kubectl:create - Create a resource from a file or stdin
- Kubectl:expose - Take a replication controller, service, deployment, or pod (Pod) and expose it as a new Kubernetes service
- Kubectl:run - Run a particular image on the cluster
- Kubectl:set - Set specific features on objects
Basic commands (intermediate)
- Kubectl:explain - Get documentation for a resource
- Kubectl:get - Display one or many resources
- Kubectl:edit - Edit a resource on the server
- Kubectl:delete - Delete resources by file names, stdin, resources and names, or by resources and label selector.
Deploy Commands
- Kubectl:rollout - Manage the rollout of a resource
- Kubectl:scale - Set a new size for a deployment, replica set, or replication controller
- Kubectl:autoscale - Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster management commands
- Kubectl:certificate - Modify certificate resources.
- Kubectl:cluster-info - Display cluster information
- Kubectl:top - Display resource (CPU/memory) usage
- Kubectl:cordon - Mark node as unschedulable
- Kubectl:uncordon - Mark node as schedulable
- Kubectl:drain - Drain node in preparation for maintenance
- Kubectl:taint - Update the taints on one or more nodes.
Troubleshooting and debugging commands
- Kubectl:describe - Show details of a specific resource or group of resources
- Kubectl:logs - Print the logs for a container in a pod.
- Kubectl:attach - Attach to a running container
- Kubectl:exec - Execute a command in a container
- Kubectl:port-forward - Forward one or more local ports to a pod
- Kubectl:proxy - Run a proxy to the Kubernetes API server
- Kubectl:cp - Copy files and directories to and from containers
- Kubectl:auth - Inspect authorization
- Kubectl:debug - Create debugging sessions for troubleshooting workloads and nodes
Advanced commands
- Kubectl:diff - Diff the live version against the would-be applied version.
- Kubectl:apply - Apply a configuration to a resource by file name or stdin.
- Kubectl:patch - Update fields of a resource
- Kubectl:replace - Replace a resource by file name or stdin.
- Kubectl:wait - Experimental: Wait for a specific condition on one or many resources.
- Kubectl:kustomize - Build a kustomization target from a directory or URL.
Settings commands
- Kubectl:label - Update the labels on a resource.
- Kubectl:annotate - Update the annotations on a resource.
- Kubectl:completion - Output shell completion code for the specified shell (bash, zsh, or fish)
Other commands
- Kubectl:alpha - Commands for features in alpha
- Kubectl:api-resources - Print the supported API resources on the server.
- Kubectl:api-versions - Print the supported API versions on the server, in the form of "group/version".
- Kubectl:config - Modify kubeconfig files
- Kubectl:plugin - Provides utilities for interacting with plugins.
- Kubectl:version - Print the client and server version information.
How to check labels
There are several ways:
kubectl describe namespace {네임스페이스명}
kubectl get namespace {네임스페이스명} --show-labels
kubectl get namespace {네임스페이스명} -o yaml
kubectl get namespace {네임스페이스명} -o jsonpath='{.metadata.labels}' | jq
Troubleshooting
tls: failed to verify certificate
- Remote kubectl x509: certificate is valid for 127.0.0.1 · Issue #1381 · k3s-io/k3s
- Configuration Options | K3s
Running a kubectl command produces the following error:
E0523 10:59:59.467327 2585371 memcache.go:265] couldn't get current server API group list: Get "https://api.yourid.run:6443/api?timeout=32s": tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
E0523 10:59:59.537153 2585371 memcache.go:265] couldn't get current server API group list: Get "https://api.yourid.run:6443/api?timeout=32s": tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
E0523 10:59:59.567092 2585371 memcache.go:265] couldn't get current server API group list: Get "https://api.yourid.run:6443/api?timeout=32s": tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
E0523 10:59:59.596498 2585371 memcache.go:265] couldn't get current server API group list: Get "https://api.yourid.run:6443/api?timeout=32s": tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
E0523 10:59:59.636441 2585371 memcache.go:265] couldn't get current server API group list: Get "https://api.yourid.run:6443/api?timeout=32s": tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
Unable to connect to the server: tls: failed to verify certificate: x509: certificate is valid for api-yourid-run, kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, localhost, not api.yourid.run
If you use k3s, add the domain to ExeStart in the server's service file (/etc/systemd/system/k3s.service), like --tls-san api.yourid.run:
Or add the insecure-skip-tls-verify: true option to the yaml file:
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://api.yourid.run:6443
name: default
...
See also
- Kubernetes
- Kubernetes:Basic
- klaw.sh - kubectl for AI agents
Favorite site
- [Recommended] Kubernetes guide