Open Joldnine opened 4 years ago
Some frequently used kubectl commands.
Create objects: kubectl run --image nginx nginx Example: Create a pod with exposed clusterip service: kubectl run webapp --image=webapp --port=80 --expose
kubectl run --image nginx nginx
kubectl run webapp --image=webapp --port=80 --expose
kubectl create deployment --image nginx nginx kubectl expose deployment nginx --port=80 Update objects: kubectl edit deployment nginx kubectl scale deployment nginx --replicas=5 kubectl set image deployment nginx nginx=nginx:1.18
kubectl create deployment --image nginx nginx
kubectl expose deployment nginx --port=80
kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment nginx nginx=nginx:1.18
xxx --dry-run -o yaml Dry run and check the correctness xxx --dry-run=client -o yaml
xxx --dry-run -o yaml
xxx --dry-run=client -o yaml
Use YAML files: kubectl create -f nginx.yaml kubectl replace -f nginx.yaml kubectl delete -f nginx.yaml
kubectl create -f nginx.yaml
kubectl replace -f nginx.yaml
kubectl delete -f nginx.yaml
kubectl apply -f nginx.yaml
List pods of a namespace kubectl get pods -n=NAMESPACE
kubectl get pods -n=NAMESPACE
List pods kubectl get pods --all-namespaces kubectl get pods -n NAMESPACE --sort-by .metadata.creationTimestamp | grep Pending
kubectl get pods --all-namespaces
kubectl get pods -n NAMESPACE --sort-by .metadata.creationTimestamp | grep Pending
Describe a pod kubectl describe POD_NAME -n=NAMESPACE
kubectl describe POD_NAME -n=NAMESPACE
Get namespaces kubectl get namespaces
kubectl get namespaces
Get logs of the init container of a pod kubectl logs POD_NAME -c INIT_CONTAINER_NAME
Go into bash shell of a pod kubectl exec -it POD_NAME -n NAMESPACE -- /bin/bash
kubectl exec -it POD_NAME -n NAMESPACE -- /bin/bash
List service accounts of a namespace kubectl get serviceaccounts -n NAMESPACE
kubectl get serviceaccounts -n NAMESPACE
Describe serect of a service account kubectl describe secret SERVICE_ACCOUNT -n NAMESPACE
kubectl describe secret SERVICE_ACCOUNT -n NAMESPACE
kubectl get nodes --show-labels
kubectl describe node NODE_NAME
kubectl drain NODE_NAME --delete-local-data --ignore-daemonsets --force
kubectl get pvc -n NAMESPACE
Patch a hanging pvc kubectl patch pvc PVC_NAME -p '{"metadata":{"finalizers":null}}' -n NAMESPACE
kubectl patch pvc PVC_NAME -p '{"metadata":{"finalizers":null}}' -n NAMESPACE
kubectl describe configmap CONFIGMAP_NAME -n NAMESPACE
kubectl describe configmap CONFIGMAP_NAME
List the resources utilized by pods or nodes kubectl top node/pod -n NAMESPACE
kubectl top node/pod -n NAMESPACE
Launch K8S proxy server in local to access k8s APIs kubectl proxy --port=XXXX
kubectl proxy --port=XXXX
Some frequently used kubectl commands.
Imperative and Declarative Commands
Imperative Commands
Create objects:
kubectl run --image nginx nginx
Example: Create a pod with exposed clusterip service:kubectl run webapp --image=webapp --port=80 --expose
kubectl create deployment --image nginx nginx
kubectl expose deployment nginx --port=80
Update objects:kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment nginx nginx=nginx:1.18
xxx --dry-run -o yaml
Dry run and check the correctnessxxx --dry-run=client -o yaml
Use YAML files:
kubectl create -f nginx.yaml
kubectl replace -f nginx.yaml
kubectl delete -f nginx.yaml
Declarative Command
kubectl apply -f nginx.yaml
Object related commands
Pod
List pods of a namespace
kubectl get pods -n=NAMESPACE
List pods
kubectl get pods --all-namespaces
kubectl get pods -n NAMESPACE --sort-by .metadata.creationTimestamp | grep Pending
Describe a pod
kubectl describe POD_NAME -n=NAMESPACE
Get namespaces
kubectl get namespaces
Get logs of the init container of a pod kubectl logs POD_NAME -c INIT_CONTAINER_NAME
Go into bash shell of a pod
kubectl exec -it POD_NAME -n NAMESPACE -- /bin/bash
Service Account
List service accounts of a namespace
kubectl get serviceaccounts -n NAMESPACE
Describe serect of a service account
kubectl describe secret SERVICE_ACCOUNT -n NAMESPACE
Nodes
kubectl get nodes --show-labels
kubectl describe node NODE_NAME
kubectl drain NODE_NAME --delete-local-data --ignore-daemonsets --force
PVC
kubectl get pvc -n NAMESPACE
Patch a hanging pvc
kubectl patch pvc PVC_NAME -p '{"metadata":{"finalizers":null}}' -n NAMESPACE
Configmap
kubectl describe configmap CONFIGMAP_NAME
-n NAMESPACEResource
List the resources utilized by pods or nodes
kubectl top node/pod -n NAMESPACE
Proxy
Launch K8S proxy server in local to access k8s APIs
kubectl proxy --port=XXXX