Joldnine / joldnine.github.io

My github.io blog repo. https://joldnine.github.io
2 stars 1 forks source link

Frequently Used K8S Commands #39

Open Joldnine opened 4 years ago

Joldnine commented 4 years ago

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 correctness xxx --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 NAMESPACE

Resource

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