Skip to content

kubectl command patterns

kubectl command patterns refer to the common syntactical structures and operational workflows used when managing a Kubernetes cluster via the command-line tool kubectl. These patterns generally fall into two categories: imperative commands (constructing resources directly via flags) and declarative configuration (applying resource definitions from YAML files)^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Core Resource Management

Basic administration involves standard lifecycle operations for Kubernetes resources like Namespaces, Deployments, and Pods^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Viewing Resources

The get subcommand is the primary method for inspecting resources^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. * General listing: kubectl get all [-n <namespace>] * Namespaces: kubectl get namespace (or ns) * Detailed view: Adding -o wide displays additional information such as the IP or node where a Pod is running^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. * Details: The describe subcommand provides detailed status and events, e.g., kubectl describe deployment <name> -n <namespace>^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Creation and Deletion

  • Create: Resources can be created directly. For example, creating a namespace: kubectl create ns app^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
  • Delete: Resources are removed using delete. Note that deleting a Pod managed by a Deployment controller will cause the Pod to be automatically restarted to meet the desired state^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
    • Syntax: kubectl delete [Pod](<./pod.md>) <pod-name> -n <namespace>

Execution

To interact with containers running inside a Pod, the exec command is used^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. * Pattern: kubectl exec -ti <pod-name> /bin/bash -n <namespace> * Flags: * -t: Allocates a pseudo-terminal (TTY). * -i: Passes standard input to the container. * These are typically combined as -it to gain an interactive shell^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Application Management

Managing application lifecycles involves Deployments, Services, and scaling^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Deployments

  • Create: kubectl create deployment <name> --image=<image-url> -n <namespace>^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
  • Scaling: To change the number of Pod replicas, use the scale command^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
    • Pattern: kubectl scale deployment <name> --replicas=<count> -n <namespace>

Services

To network applications, expose is used to create a Service resource that selects Pods based on labels^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. * Pattern: kubectl expose deployment <name> --port=<port> -n <namespace>

Declarative Configuration (YAML)

For complex or reproducible infrastructure, kubectl operates on YAML manifests defining the desired state^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Applying Files

The apply command is used to create or update resources based on a configuration file^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. * Pattern: kubectl apply -f <file-name>.yaml * Remote Source: In operational workflows, configuration files can be applied directly from an HTTP source, such as kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].

Editing

  • Live Edit: To modify a resource running on the cluster, kubectl edit opens the resource's configuration in a text editor (e.g., kubectl edit svc <name>)^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
  • Export: To retrieve the current configuration of a resource for use as a template, redirect the output of get with the -o yaml flag^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
    • Pattern: kubectl get pods <name> -o yaml -n <namespace>

Sources

^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]