14、Kubernetes学习总结-Kubernetes 实用命令总结

一、Pods

1、Get all pods in the current namespace

kubectl get pods

2、Get pods in all namespaces

kubectl get pods --all-namespaces

3、Get pods with more details

kubectl get pods -o wide

4、Get the yaml for a pod

kubectl get pod <pod> -o yaml

5、Inspect a pod

kubectl describe pods <pod>

6、Get pods sorted by a metric

kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

7、Get pods with their labels

kubectl get pods --show-labels

8、Get pods that match a label

kubectl get pods -l <label>=<value>

9、Forward traffic from a localhost port to a pod port

kubectl port-forward <pod> <localhost-port>:<pod-port>

10、Run a command on a pod

kubectl exec <pod> -- <command>

11、Run a command on a container in a pod

kubectl exec <pod> -c <container> -- <command>

二、Secrets

1、Get all secrets in the current namespace

kubectl get secrets

2、Get secrets in all namespaces

kubectl get secrets --all-namespaces

3、Get secrets with more details

kubectl get secrets -o wide

4、Get the contents of a secret

kubectl get secrets <secret> -o yaml 

三、Services

1、Get all services in the current namespace

kubectl get services

2、Get services in all namespaces

kubectl get service --all-namespaces

3、Get services with more details

kubectl get service -o wide

4、Get the yaml for a services

kubectl get service <service> -o yaml

5、Inspect a service

kubectl describe service <service>

6、Get service's labels

kubectl get service --show-labels

7、Get services that match a label

kubectl get service -l <label>=<value>

四、Updating Resources

1、Roll a new version of a deployment

kubectl set image deployment/<deployment> <containername>=image:<version>

2、Check the deployment history

kubectl rollout history deployment/<deployment>

3、Rollback a deployment

kubectl rollout undo deployment/<deployment>

4、Rollback to a specific version

kubectl rollout undo deployment/<deployment> --to-revision=2

5、Watch a rolling update

kubectl rollout status -w deployment/<deployment>

6、Restart the rolling deploy

kubectl rollout restart deployment/<deployment>

7、Edit a resource’s yaml

kubectl edit deployment/<deployment>

8、Scale a deployment to 3 pods

kubectl scale --replicas=3 deployment/<deployment>

9、Delete a pod

kubectl delete pod <pod>

五、Context

1、Show contexts

kubectl config get-contexts

2、Show current context

kubectl config current-context

3、Switch context to another cluster

kubectl config use-context <my-cluster-name>

4、Change Namespace

kubectl config set-context --current --namespace=<namespace>

六、Logs

1、Show logs (stdout) of a pod

kubectl logs <pod>

2、Show logs (stdout) of pods that match a label

kubectl logs -l <label>=<value>

3、Show logs of a previous instantiation of a container

kubectl logs <pod> --previous

4、Show logs for a specific container in a pod (i.e. init container)

kubectl logs <pod> -c <container>

5、Following logs from a pod

kubectl logs -f <pod>

6、Follow all logs from a pod that match a label

kubectl logs -f -l <label>=<value> --all-containers

7、Show logs with verbosity level of logs from 0 - 9

kubectl logs <pod> --v=<0:9>

七、Deployments

1、Get all deployments in the current namespace

kubectl get deployment

2、Get deployments in all namespaces

kubectl get deployment --all-namespaces

3、Get deployments with more details

kubectl get deployment -o wide

4、Get the yaml for a deployment

kubectl get deployment <deployment> -o yaml

5、Inspect a deployment

kubectl describe deployment <deployment>

6、Get deployment's labels

kubectl get deployment --show-labels

7、Get deployments that match a label

kubectl get deployment -l <label>=<value>

八、Ingress

1、Get all ingress in the current namespace

kubectl get ingress

2、Get ingress in all namespaces

kubectl get ingress --all-namespaces

3、Get ingress with more details

kubectl get ingress -o wide

4、Get the yaml for a ingress

kubectl get ingress <ingress> -o yaml

5、Inspect a ingress

kubectl describe ingress <ingress>

6、Get ingress labels

kubectl get ingress --show-labels

7、Get ingress that match a label

kubectl get ingress -l <label>=<value>

九、Creating Resources

1、Create a kubernetes resource from a file

kubectl apply -f ./<manifest>.yaml

2、Create kubernetes resources from multiple files

kubectl apply -f ./<manifest>.yaml -f ./<manifest>.yaml

3、Create resources from all manifest files in a directory

kubectl apply -f ./<directory>

4、Create resource from a url

kubectl apply -f <url_to_manifest>

5、Start a single instance of an image

kubectl create deployment <deployment_name> --image=<image>

十、Nodes

1、Mark node as unschedulable

kubectl cordon <node>

2、Drain a node for maintenance

kubectl drain <node>

3、Mark node as schedulable

kubectl uncordon <node>

4、Show ‘top’ metrics for a node

kubectl top node <node>

5、Display addresses of the master and services

kubectl cluster-info

6、ump current cluster state to stdout

kubectl cluster-info dump

7、Show a list of eligible kube resource (i.e. pods, service, pv, etc)

kubectl api-resources

8、Show a list of eligible kube resources in your namespace

kubectl api-resources --namespaced=true