Concept

![[images/截屏2026-03-20 14.24.38.png]]

A Pod is the basic execution unit of a Kubernetes application.

  • The smallest and simplest unit in the Kubernetes object model that can be created or deployed.
  • Environment for containers organize application “parts” into Pods (server, caching, APIs, database…)
  • Containers run within Pods and share a Pod’s memory, IP, volumes, and more
  • Scale horizontally by adding Pod replicas
  • Pods live and die but never come back to life
  • If a pod get sick, Kubernetes will monitor that and automatically remove it then create a healthy pod

Network

  • Pod containers share the same Network namespace (share IP / port)
  • Pod containers have the same loopback network interface (localhost)
  • Container processes need to bind to different ports within a Pod
  • Ports can be reused by containers in seperate Pods

Pods do not span nodes

kubectl

Creating

  1. kubectl run
  2. kubectl create/apply with a yaml file
Run the image container in a Pod
kubectl run [pod_name] --image=[imgae_name:image_version]

Expose Port

Pods and containers are only accessible within the Kubernetes cluster by default

kubectl port-forward: expose a container port externally

kubectl port-forward [pod_name] [external_port:interal_port]

Delete

Running a Pod will cause a Deployment to be created.

Will cause Pod to be recreated

kubectl delete pod [pod_name]

delete Deployment

kubectl delete deployment [deployment_name]

YAML

  • Composed of maps and lists
  • Indentation matters
  • Always use spaces
key: value
map:
	key1: value
	key2:
		subKey: value
list:
	- item1
	- item2
listMap:
	- map1: value
	  map1p: value
	- map2: value
	  map2p: value

Define

apiVersion: v1
kind: Pod
metadata:
	name: test
spec:
	containers:
	- name: test
	  image: nginx:alpine
  • apiVersion: Kubernetes API version
  • kind: Type of Kubernetes resource
  • metadata: Metadata about the Pod
  • spec: The spec/bludprint for the Pod
    • Information about the containers that will run in the Pod

Create

with a YAML file

kubectl create -f [yaml_name.yaml] --dry-run --validate=true
# Will error if Pod already exists
kubectl create -f [yaml_name.yaml]

Update

Create if not exist or apply changes to a Pod from YAML

kubectl apply -f [yaml_name.yaml]

When you want to use kubectl apply in the future:

kubectl create -f [yaml_name.yaml] --save-config

--save-config causes the resource’s configuration settings to be saved in the annotations.

In-place/non-disruptive changes can also be made to Pod using kubectl edit / kubuctl patch

Delete

Delete Pod using YAML file that created it

kubectl delete -f [yaml_name.yaml]

YAML Fundamental

K8s 所有资源(Pod/Deployment/Service 等)的 YAML 配置遵循统一核心结构,分为 4 个基础段:

apiVersion: v1 # API 版本(必选)
kind: Pod	   # 资源类型(必选)
metadata:	   # 资源元数据(必选)
  name: my-nginx
  labels:
    app: nginx
    rel: stable
spec:		   # 资源规格(必选,不同资源类型字段不同)
  containers:
  - name: my-nginx
    image: nginx:alpine
    ports:
    - containerPort: 80
    resources: {}

Commands:

# Basic Commands
kubectl create -f nginx.pod.yml --save-config
kubectl describe pod [pod-name]
kubectl apply -f nginx.pod.yml
kubectl exec [pod-name] -it sh
kubectl edit -f nginx.pod.yml
Kubectl delete -f nginx.pod.yml

# To exec into container use:
kubectl exec -it [pod-name] -- sh

核心通用属性详解

apiVersion

API 版本

取值示例 适用资源类型 说明
v1 Pod/Service/Namespace/ConfigMap/Secret 核心基础资源的稳定版本
apps/v1 Deployment/StatefulSet/DaemonSet 应用工作负载相关资源
batch/v1 Job/CronJob 批处理任务相关资源
networking.k8s.io/v1 Ingress/NetworkPolicy 网络相关资源

kind

资源类型

取值 中文名称 核心作用
Pod 最小部署单元 运行容器的最小载体
Deployment 无状态部署 管理 Pod 副本、自动重启、滚动更新
Service 服务发现 暴露 Pod 网络访问、负载均衡
Namespace 命名空间 资源逻辑隔离
ConfigMap 配置映射 存储非敏感配置数据
Secret 密钥 存储敏感数据(密码/令牌等)
StatefulSet 有状态部署 管理有状态应用(固定网络标识、持久化存储)
DaemonSet 守护进程 集群所有/指定 Node 上运行一个 Pod 副本
Job 一次性任务 执行完成后终止的任务
CronJob 定时任务 按时间规则重复执行 Job
Ingress 入口规则 集群外部访问集群内服务的规则

metadata

资源元数据

metadata:
  name: string               # 资源名称(必选,同一命名空间内唯一)
  namespace: string          # 所属命名空间(可选,默认 default)
  labels:                    # 标签(可选,键值对,用于资源筛选/关联)
    key1: value1
    key2: value2
  annotations:               # 注解(可选,键值对,存储非标识性元数据)
    key1: value1
  uid: string                # 唯一标识(自动生成,无需配置)
  • labels 核心用途:Service 关联 Pod、Deployment 管理 Pod、资源筛选(kubectl get pods -l key=value
  • annotations 典型场景:存储资源描述、运维备注、第三方工具配置

Pod Health

Kubernetes relies on Probes to determine the health of a Pod container.

A Probe is a diagnostic performed periodically by the kubelet on a container.

  • Liveness probes can be used to determine if a Pod is healthy and running as expected
    • When should a container restart
  • Readiness probes can be used to determine if a Pod should receive requests
    • When should a container start receiving traffic

Failed Pod containers are recreated by default (restartPolicy defaults to Always)

Way to Check

  • ExecAction: Executes an action inside the container
  • TCPSocketAction: TCP check against the container’s IP address on a specified port
  • HTTPGetAction: HTTP GET request against container

Probes can have the results:

  • Success
  • Failure
  • Unknown
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
  labels:
    app: nginx
    rel: stable
spec:
  containers:
  - name: my-nginx
    image: nginx:alpine
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "300m"
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /index.html
        port: 80
      initialDelaySeconds: 15
      timeoutSeconds: 2 # Default is 1
      periodSeconds: 5 # Default is 10
      failureThreshold: 1 # Default is 3
    readinessProbe:
      httpGet:
        path: /index.html
        port: 80
      initialDelaySeconds: 3
      periodSeconds: 5 # Default is 10
      failureThreshold: 1 # Default is 3
  • httpGet: Check /index.html on port 80
  • initialDelaySeconds: Wait 15 seconds for init
  • timeoutSeconds: Timeout after 2 seconds
  • periodSeconds: Check every 5 seconds
  • failureThreshold: Allow 1 failure before failing Pod