Kubectl¶
Kubectl is a command line tool for communicating with a Kubernetes Cluster's control pane, using the Kubernetes API.
Documentation: Kubectl Reference
Installation¶
On Windows (PowerShell)¶
Install Kubectl with chocolatey:
On Linux¶
[!INFO] Installing on WSL2 On WSL2 it's recommended to install Docker Desktop [[docker-desktop]], which automatically comes with kubectl.
Download the latest release¶
Install kubectl¶
Config Management¶
Multiple Config Files¶
On Windows (PowerShell)¶
On Linux¶
Managing multiple config files manually can become extensive. Below you can find a handy script, which you can implement in your shell rc file (e.g. .bashrc or .zshrc). The script will automatically add all found kubeconfigs to the KUBECONFIG
environment variable.
Script was copied from here
Another helpful tool that makes you changing and selecting the cluster context easier is
kubectx
. You can download kubectx
here.
The above script conflicts with kubectx, cause kubectx can only work with one
kubeconfig file listed in the
KUBECONFIG
env var. If you want to use both, add the following
lines to your rc file.
Commands¶
Networking¶
Connect containers using Kubernetes internal DNS system:
<service-name>.<namespace>.svc.cluster.local
Troubleshoot Networking with a netshoot toolkit Container:
kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash
Containers¶
Restart Deployments (Stops and Restarts all Pods):
kubectl scale deploy <deployment> --replicas=0
kubectl scale deploy <deployment> --replicas=1
Executing Commands on Pods:
kubectl exec -it <PODNAME> -- <COMMAND>
kubectl exec -it generic-pod -- /bin/bash
Config and Cluster Management¶
COMMAND | DESCRIPTION |
---|---|
kubectl cluster-info |
Display endpoint information about the master and services in the cluster |
kubectl config view |
Get the configuration of the cluster |
### Resource Management | |
COMMAND | DESCRIPTION |
--- | --- |
kubectl get all --all-namespaces |
List all resources in the entire Cluster |
kubectl delete <RESOURCE> <RESOURCENAME> --grace-period=0 --force |
Try to force the deletion of the resource |
List of Kubernetes Resources "Short Names"¶
Short Name | Long Name |
---|---|
csr |
certificatesigningrequests |
cs |
componentstatuses |
cm |
configmaps |
ds |
daemonsets |
deploy |
deployments |
ep |
endpoints |
ev |
events |
hpa |
horizontalpodautoscalers |
ing |
ingresses |
limits |
limitranges |
ns |
namespaces |
no |
nodes |
pvc |
persistentvolumeclaims |
pv |
persistentvolumes |
po |
pods |
pdb |
poddisruptionbudgets |
psp |
podsecuritypolicies |
rs |
replicasets |
rc |
replicationcontrollers |
quota |
resourcequotas |
sa |
serviceaccounts |
svc |
services |
陼 Logs and Troubleshooting¶
...
Logs¶
...
MySQL¶
kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql -u USERNAME -h HOSTNAME -p
Networking¶
kubectl run -it --rm --image=nicolaka/netshoot netshoot -- /bin/bash
Resources stuck in Terminating state¶
...