Kubernetes resource types¶
In Kubernetes, resources are the primary objects managed by the cluster. They define the desired state of various workloads, services, and configurations. Users interact with these resources primarily via the kubectl command-line tool, which encapsulates underlying RESTful API calls to the kube-apiserver.^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]
Command Syntax¶
The general syntax for managing resources via kubectl follows this structure:^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]
kubectl [command] [TYPE] [NAME] [flags]
command: Specifies the operation to perform (e.g.,create,get,describe,delete).^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]TYPE: Specifies the resource type. Types are case-insensitive and can be singular, plural, or abbreviated^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md] (e.g.,pod,pods, orpo).NAME: Specifies the name of the resource. Names are case-sensitive^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md]. If omitted, the command applies to all resources of the specified type in the default namespace.flags: Specifies optional parameters, such as-o widefor detailed output or-nfor specifying a namespace.
Common Resource Types¶
Kubernetes includes a wide variety of resource types categorized by their function within the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
Workload Resources¶
These resources manage the deployment and scaling of applications.
- Pod: The smallest deployable unit in Kubernetes, representing a single instance of a running process^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Deployment: Manages a replicated application (typically stateless)[^source-validation]. It ensures a specified number of Pod replicas are running at any given time^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
- DaemonSet: Ensures that a specific Pod runs on all (or a subset of) nodes in the cluster^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
Configuration and Storage¶
- Namespace: A virtual cluster within a physical cluster, used to divide cluster resources between multiple users or teams^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md] (e.g.,
default,kube-system). - ConfigMap: An API object used to store non-confidential data in key-value pairs^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. Pods can consume ConfigMaps as environment variables, command-line arguments, or configuration files in a volume.
- Secret: Similar to a ConfigMap, but specifically designed to hold sensitive data, such as passwords, OAuth tokens, or SSH keys.
Service and Networking¶
These resources manage network access to workloads.
- Service: An abstraction which defines a logical set of Pods and a policy by which to access them^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. Services provide a stable IP address and DNS name for a set of Pods, allowing them to be accessed even if individual Pods die or are recreated. Common types include:
- ClusterIP (default): Exposes the Service on an internal cluster IP^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
- NodePort: Exposes the Service on each Node’s IP at a static port.
- Ingress: An API object that manages external access to the Services, typically HTTP/HTTPS^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]. Ingress can provide load balancing, SSL Termination, and Name-based Virtual Hosting.
Resource Management Operations¶
Kubernetes supports two primary styles of managing resources: imperative and declarative^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
Imperative Commands¶
These commands act directly on live objects.
- Create: Instantiates a resource, often from a file or directly via arguments^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Example:
kubectl create deployment nginx-dp --image=nginx^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]
- Example:
- Expose: Creates a Service to expose a workload (e.g., a Deployment) on a specific port[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md][400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
- Scale: Manually changes the number of replicas for a Deployment^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
- Delete: Removes a resource from the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
Declarative Configuration¶
- Apply: Creates or updates resources based on configuration files (usually YAML or JSON)^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Example:
kubectl apply -f nginx-ds-svc.yaml^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]
- Example:
- Edit: Opens the live resource configuration in a text editor for direct modification^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md].
Inspection and Debugging¶
- Get: Lists one or more resources^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Describe: Displays detailed state of a specific resource, including initialization status and recent events^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Logs: Prints the log output from a container in a Pod^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md].
- Exec: Executes a command inside a running container^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md], commonly used for debugging (e.g.,
kubectl exec -ti <pod-name> -- /bin/bash).
Related Concepts¶
- Kubectl
- [[Namespaces]]
- [[Pods]]
Sources¶
^[400-devops__06-Kubernetes__k8s-ithelp__Day11__README.md] ^[400-devops__06-Kubernetes__k8s-paas__03.k8s集群.md]