kubectl version
kubectl api-versions
kubectl cluster-info
&kubectl cluster-info dump
kubectl get namespace
kubectl get all
kubectl get cs
orkubectl get nodes
kubectl describe nodes
- Get the name of the Pod and store it in the POD_NAME environment variable:
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
- Label a POD
kubectl label pod $POD_NAME app=v1
- Query PODs by the label
kubectl get pods -l app=v1
kubectl get pods
(in default Namespace)- get pod details in another namespace
kubectl get pods --namespace=kube-system
- get pod in selector
dev
environmentkubectl get pods --selector env=dev
- get pod details in another namespace
- All objects in
prod
environmentkubectl get all --selector env=prod
- Get POD which is 'prod', part of 'finance' BU and is a 'frontend' tier
kubectl get all --selector env=prod,bu=finance,tier=frontend
kubectl get pods -o wide
kubectl describe pods
- Get the container variable
kubectl exec POD_NAME env
- Enter into a container of a POD
kubectl exec -it POD_NAME bash
- To know details about one particular pod -
kubectl describe pod POD_NAME
kubectl get replicasets
orkubectl get rs
kubectl describe replicasets
orkubectl describe rs
kubectl get deployments
kubectl describe deployments
kubectl get services
kubectl get sc
kubectl get events
kubectl create -f pod-definition.yml
orkubectl create -f pod-definition.yml --namespace=dev
(in dev namespace)- Create a Pod with NGINX Image -
kubectl run nginx-pod --image=nginx --generator=run-pod/v1
kubectl run nginx-pod --image=nginx
- Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
kubectl run --generator=run-pod/v1 nginx-pod --image=nginx --dry-run -o yaml
- Delete a pod
kubectl delete pod POD_NAME
- Edit the service named 'docker-registry':
kubectl edit svc/docker-registry
- Use an alternative editor -
KUBE_EDITOR="nano" kubectl edit svc/docker-registry
- Edit the service 'docker-registry' in JSON using the v1 API format:
kubectl edit svc/docker-registry --output-version=v1 -o json
kubectl create deployment --image=nginx nginx-deployment
- Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
kubectl create deployment --image=nginx nginx-deployment --dry-run -o yaml
- Generate Deployment YAML file (-o yaml). Don't create it(--dry-run) with 4 Replicas (--replicas=4)
kubectl create deployment --image=nginx nginx-deployment --dry-run -o yaml > nginx-deployment.yaml
- kubectl create deployment does not have a
--replicas
option. You could first create it and then scale it using thekubectl scale
command.
kubectl logs pod pod_name --namespace=kube-system
- If there are multiple containers in the POD definition file then use the name of the container to view the log of a particular container.
kubectl logs pod_name container-1_name
- Get Nodes ->
kubectl get nodes
- Check Taint on Node ->
kubectl get nodes node01 | grep -i taint
- Apply Taint on Node ->
kubectl taint nodes node01 spray=mortein:NoSchedule
- Untained Master Node ->
kubectl taint nodes master node-role.kubernetes.io/master:NoSchedule-
kubectl top node
kubectl top pod