4.9 KiB
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:
choco install kubernetes-cli
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
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Install kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
On mac OS
Install Kubectl with homebrew:
brew install kubernetes-cli
Config Management
Multiple Config Files
On Windows (PowerShell)
$env:KUBECONFIG = "$HOME/.kube/prod-k8s-clcreative-kubeconfig.yaml;$HOME/.kube/infra-home-kube-prod-1.yml;$HOME/.kube/infra-home-kube-demo-1.yml;$HOME/.kube/infra-cloud-kube-prod-1.yml"
On Linux
export KUBECONFIG=~/.kube/kube-config-1.yml:~/.kube/kube-config-2.yml
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
# If there's already a kubeconfig file in ~/.kube/config it will import that too and all the contexts
DEFAULT_KUBECONFIG_FILE="$HOME/.kube/config"
if test -f "${DEFAULT_KUBECONFIG_FILE}"
then
export KUBECONFIG="$DEFAULT_KUBECONFIG_FILE"
fi# Your additional kubeconfig files should be inside ~/.kube/config-files
ADD_KUBECONFIG_FILES="$HOME/.kube/config-files"
mkdir -p "${ADD_KUBECONFIG_FILES}"OIFS="$IFS"
IFS=$'\n'
for kubeconfigFile in `find "${ADD_KUBECONFIG_FILES}" -type f -name "*.yml" -o -name "*.yaml"`
do
export KUBECONFIG="$kubeconfigFile:$KUBECONFIG"
done
IFS="$OIFS"
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.
# now we merge all configs to one
kubectl config view --merge --flatten > $HOME/.kube/merged-config
export KUBECONFIG="$HOME/.kube/merged-config"
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
...
(
NAMESPACE=longhorn-demo-1
kubectl proxy &
kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' >temp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize
)