หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Easily Switch Between Product Types

You can click the drop-down list box to switch between different product types.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
Software Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Cloud Container Engine/ Kubernetes Basics/ Pods, Labels, and Namespaces/ Pod: the Smallest Scheduling Unit in Kubernetes

Pod: the Smallest Scheduling Unit in Kubernetes

Updated on 2024-12-26 GMT+08:00

Overview of Pod

A pod is the smallest, simplest unit in the Kubernetes object model that you create or deploy. A pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. Each pod has a separate IP address.

Pods can be used in either of the following ways:

  • A pod runs only one container. This is the most common usage of pods in Kubernetes. You can consider a pod as a container, but Kubernetes directly manages pods instead of containers.
  • A pod runs multiple containers that need to be tightly coupled. In this scenario, a pod contains a main container and several sidecar containers, as shown in Figure 1. For example, the main container is a web server that provides file services from a fixed directory, and sidecar containers periodically download files to this fixed directory.
    Figure 1 Pod running multiple containers

In Kubernetes, pods are rarely created directly. Instead, Kubernetes controller manages pods through pod instances such as Deployments and jobs. A controller typically uses a pod template to create pods. The controller can also manage multiple pods and provide functions such as replica management, rolling upgrade, and self-healing.

Creating a Pod

Kubernetes resources can be described using YAML or JSON files. The following example YAML file describes a pod named nginx. This pod contains a container named container-0 that uses the nginx:alpine image with 100m CPUs and 200 MiB of memory.

apiVersion: v1                      # Kubernetes API version
kind: Pod                           # Kubernetes resource type
metadata:
  name: nginx                       # Pod name
spec:                               # A pod specification
  containers:
  - image: nginx:alpine             # Image nginx:alpine
    name: container-0               # Container name
    resources:                      # Resources required for this container
      limits:
        cpu: 100m
        memory: 200Mi
      requests:
        cpu: 100m
        memory: 200Mi
  imagePullSecrets:                 # Secret used to pull the image, which must be default-secret on CCE
  - name: default-secret

As shown in the YAML comments, the YAML file includes:

  • metadata: information such as name, label, and namespace
  • spec: a pod specification such as image and volume used

If you check a Kubernetes resource, you can also see the status field, which indicates the status of the Kubernetes resource. This field does not need to be set when the resource is created. This example is a minimum set of parameters. Other parameters will be described later.

After defining the pod, you can use kubectl to create the pod. Assume that the preceding YAML file is named nginx.yaml, run the following command to create the pod. -f indicates that you will create the pod from a file.

$ kubectl create -f nginx.yaml
pod/nginx created

After the pod is created, you can run the kubectl get pods command to obtain the pod status.

$ kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
nginx          1/1     Running   0          40s

The preceding command output indicates that the nginx pod is in the Running state. READY is 1/1, indicating that this pod has one container that is in the Ready state.

You can run the kubectl get command to obtain the information of a pod. -o yaml indicates that the information is returned in YAML format, and -o json indicates that the information is returned in JSON format.

$ kubectl get pod nginx -o yaml

You can also run the kubectl describe command to view the pod details.

$ kubectl describe pod nginx

Before deleting a pod, Kubernetes terminates all the containers that are part of that pod. Kubernetes sends a SIGTERM signal to the containers' main process and waits a period (30 by default) for it to shut down gracefully. If the process is not shut down during this period, Kubernetes will send a SIGKILL signal to stop the process.

You can stop and delete a pod in multiple methods. For example, you can delete a pod by name, as shown below:

$ kubectl delete po nginx
pod "nginx" deleted

Delete multiple pods at one time:

$ kubectl delete po pod1 pod2

Delete all pods:

$ kubectl delete po --all
pod "nginx" deleted

Delete pods by labels. For details about labels, see Label for Managing Pods.

$ kubectl delete po -l app=nginx
pod "nginx" deleted

Environment Variables

You can use environment variables to set up a container runtime environment.

Environment variables add flexibility to configuration. The custom environment variables will take effect when the container is running. This frees you from rebuilding the container image.

In the following shows example, you only need to configure the environment variable spec.containers.env.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
    containers:
    - image: nginx:alpine
      name: container-0
      resources:
        limits:
          cpu: 100m
          memory: 200Mi
        requests:
          cpu: 100m
          memory: 200Mi
      env:                            # Environment variable
      - name: env_key
        value: env_value
    imagePullSecrets:
    - name: default-secret

Run the following command to check the environment variables in the container. The value of the env_key environment variable is env_value.

$ kubectl exec -it nginx -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx
TERM=xterm
env_key=env_value

Pods can use ConfigMap and secret as environment variables. For details, see Referencing a ConfigMap as an Environment Variable and Referencing a Secret as an Environment Variable.

Setting Container Startup Commands

Starting a container is to start its main process. You need to make some preparations before starting a main process. For example, you may need to configure or initialize MySQL databases before running MySQL servers. All of these operations can be performed by configuring ENTRYPOINT or CMD in a Dockerfile during image creation. As shown in the following example, configure the ENTRYPOINT ["top", "-b"] command in the Dockerfile. Then, the system will automatically perform the preparation operations during container startup.

FROM ubuntu
ENTRYPOINT ["top", "-b"]

When calling an API, you only need to configure pods' containers.command field to define the command and their arguments. The first parameter is the command and the following parameters are arguments.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200Mi
      requests:
        cpu: 100m
        memory: 200Mi
    command:                     # Boot command
    - top
    - "-b"
  imagePullSecrets:
   - name: default-secret

Container Lifecycle

Kubernetes provides container lifecycle hooks to enable containers to be aware of events in their management lifecycle and run code implemented in a handler when the corresponding lifecycle hook is executed. For example, if you want a container to perform a certain operation before it is stopped, you can register a hook. The following lifecycle hooks are provided:

  • postStart: triggered immediately after a pod is started
  • preStop: triggered immediately before a pod is stopped

You only need to set the lifecycle.postStart or lifecycle.preStop parameter of a pod, as shown in the following example:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200Mi
      requests:
        cpu: 100m
        memory: 200Mi
    lifecycle:
      postStart:                 # Post-start processing
        exec:
          command:
          - "/postStart.sh"
      preStop:                   # Pre-stop processing
        exec:
          command:
          - "/preStop.sh"
  imagePullSecrets:
   - name: default-secret

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback