Detailed explanation of the kubectl command of common Kubernetes commands

Resource view command

These commands are used to view resource status and information in the Kubernetes cluster:

  • kubectl get nodes: View the status and information of all nodes. For example, the node name, IP address, status, version, etc.
  • kubectl get pods: View the status and information of all pods. For example, the name of the pod, the node where it is located, status, IP address, container status, etc.
  • kubectl get services: View the status and information of all services. For example, the name, type, IP address, port, etc. of the service.
  • kubectl get deployments: View the status and information of all deployments. For example, the name of the deployment, the namespace where it is located, the number of replicas, the number of available replicas, etc.
  • kubectl get replicasets: View the status and information of all replica sets. For example, the name of the replica set, the namespace where it is located, the number of replicas, the number of available replicas, etc.
  • kubectl get persistentvolumes: View the status and information of all persistent volumes. For example, the name, type, capacity, status, etc. of the persistent volume.
  • kubectl get persistentvolumeclaims: View the status and information of all persistent volume claims. For example, the name of the persistent volume declaration, the namespace where it is located, the status, the name of the bound persistent volume, etc.
  • kubectl get namespaces: View the status and information of all namespaces. For example, the name, status, creation time, etc. of the namespace.
  • kubectl get configmaps: View the status and information of all configured maps. For example, configure the map name, namespace, data, etc.
  • kubectl get secrets: View the status and information of all keys. For example, the key's name, namespace, type, data, etc.
  • kubectl get events: View the status and information of all events. For example, the type, object, cause, message, etc. of the event.
  • kubectl top nodes: Check the resource usage of the node. For example, CPU and memory usage, usage, etc.
  • kubectl top pods: Check the resource usage of the pod. For example, CPU and memory usage, usage, etc.

Resource details command

These commands are used to view details of specific resources in a Kubernetes cluster:

  • kubectl describe pod <pod-name>: View detailed information for a specific pod. For example, pod status, container status, events, etc.
  • kubectl describe service <service-name>: View details for a specific service. For example, service type, IP address, port, associated pod, etc.
  • kubectl describe node <node-name>: View details of a specific node. For example, the status, label, capacity, usage, etc. of the node.
  • kubectl describe deployment <deployment-name>: View details for a specific deployment. For example, the deployment status, number of replicas, number of available replicas, associated pods, etc.
  • kubectl describe replicasets <replicaset-name>: View details for a specific replica set. For example, the status of the replica set, the number of replicas, the number of available replicas, associated pods, etc.
  • kubectl describe persistentvolume <persistentvolume-name>: View details for a specific persistent volume. For example, the status, type, capacity, access mode, etc. of the persistent volume.
  • kubectl describe persistentvolumeclaims <persistentvolumeclaim-name>: View details for a specific persistent volume claim. For example, the status of the persistent volume declaration, the name of the bound persistent volume, the access mode, etc.
  • kubectl describe namespace <namespace-name>: View details for a specific namespace. For example, the namespace status, label, creation time, etc.
  • kubectl describe configmap <configmap-name>: View details for a specific configuration map. For example, configure the map data, creation time, etc.
  • kubectl describe secret <secret-name>: View details for a specific key. For example, key type, data, creation time, etc.
  • kubectl describe event <event-name>: View details of a specific event. For example, the type, object, cause, message, etc. of the event.

Log related commands

These commands are used to view logs for specific pods in the Kubernetes cluster:

  • kubectl logs <pod-name>: View the logs of a specific pod. Use this command to quickly view the log information of the pod to locate the problem. For example, to view the logs for a pod named my-pod, you can use the following command:

    kubectl logs my-pod
    
  • kubectl logs -f <pod-name>: View the logs of a specific pod in real time. Use this command to view pod log information in real time to quickly identify problems. For example, to view the logs of a pod named my-pod in real time, you can use the following command:

    kubectl logs -f my-pod
    
  • kubectl logs --tail=<n> <pod-name>: View the last n log lines of a specific pod. Use this command to quickly view the last n lines of log information of the pod to locate the problem. For example, to view the last 100 lines of logs for a pod named my-pod, you can use the following command:

    kubectl logs --tail=100 my-pod
    
  • kubectl logs --since=<time> <pod-name>: View logs for a specific pod since the specified time. Use this command to view the log information of the pod after the specified time to locate the problem. For example, to view the logs of the pod named my-pod after 10:00 on May 20, 2023, you can use the following command:

    kubectl logs --since=2023-05-20T10:00:00 my-pod
    

Resource creation and deletion commands

These commands are used to create and delete resources in a Kubernetes cluster:

  • kubectl create -f <filename>: Create resources from YAML or JSON files. Usage scenarios include: creating new pods, services, deployments and other resources in the Kubernetes cluster. Example:

    kubectl create -f nginx.yaml
    
  • kubectl apply -f <filename>: Create or update resources from YAML or JSON files. Usage scenarios include: creating new resources or updating existing resources in a Kubernetes cluster. Example:

    kubectl apply -f nginx.yaml
    
  • kubectl delete <resource-type> <resource-name>: Delete the resource of the specified type and name. Usage scenarios include: deleting pods, services, deployments and other resources that are no longer needed in the Kubernetes cluster. Example:

    kubectl delete pod nginx
    
  • kubectl delete -f <filename>: Remove resources from YAML or JSON files. Usage scenarios include: deleting resources that are no longer needed in a Kubernetes cluster. Example:

    kubectl delete -f nginx.yaml
    

Resource update and expansion and contraction commands

These commands are used to update and scale resources in a Kubernetes cluster:

  • kubectl edit <resource-type> <resource-name>: Edit resources of specified type and name. Usage scenarios include: modifying the configuration of existing pods, services, deployments and other resources in the Kubernetes cluster. Example:

    kubectl edit deployment nginx
    
  • kubectl scale <resource-type> <resource-name> --replicas=<n>: Expand and shrink resources of the specified type and name. Usage scenarios include: increasing or reducing the number of copies of pods, services, deployments and other resources in a Kubernetes cluster. Example:

    kubectl scale deployment nginx --replicas=3
    
  • kubectl rollout status <resource-type> <resource-name>: View the rolling update status of resources of the specified type and name. Usage scenarios include: Viewing the rolling update status of deployments in a Kubernetes cluster. Example:

    kubectl rollout status deployment nginx
    
  • kubectl rollout history <resource-type> <resource-name>: View the rolling update history of resources of the specified type and name. Usage scenarios include: Viewing the rolling update history of deployments in a Kubernetes cluster. Example:

    kubectl rollout history deployment nginx
    
  • kubectl rollout undo <resource-type> <resource-name>: Rollback a rolling update of a resource of the specified type and name. Usage scenarios include: rolling back deployed rolling updates in a Kubernetes cluster. Example:

    kubectl rollout undo deployment nginx
    

Other commands

These commands are used for other operations:

  • kubectl version: View the version information of the Kubernetes cluster and client. Usage scenarios include: viewing version information in a Kubernetes cluster.

  • kubectl config use-context <context-name>: Switch the currently used context. Usage scenarios include: switching context in a Kubernetes cluster. Example:

    kubectl config use-context my-context
    
  • kubectl exec -it <pod-name> -- <command>: Execute the command in the specified pod. Usage scenarios include: executing commands in specified pods in a Kubernetes cluster. Example:

    kubectl exec -it nginx -- /bin/bash
    
  • kubectl port-forward <pod-name> <local-port>:<pod-port>: Forward the port of the specified pod to the local port. Usage scenarios include: forwarding the pod's port to local for debugging in the Kubernetes cluster. Example:

    kubectl port-forward nginx 8080:80
    
  • kubectl proxy: Start the Kubernetes API proxy server. Usage scenarios include: accessing the API server in a Kubernetes cluster.

I hope these commands can help you better manage and maintain your Kubernetes cluster!

Guess you like

Origin blog.csdn.net/weixin_42492572/article/details/130811654