1

Kubernetes 使用 YAML 文件定义资源对象(如 Pod、Deployment、Service 等)。以下是常见资源的核心参数详解,附带示例和注释。

1 Pod 基础配置

apiVersion: v1                 # Kubernetes API 版本(Pod 属于 core/v1)
kind: Pod                      # 资源类型(Pod、Deployment、Service 等)
metadata:
  name: my-pod                 # Pod 名称(集群内唯一)
  namespace: default           # 所属命名空间(默认 default)
  labels:                      # 标签(用于 Service/Deployment 关联)
    app: my-app
    tier: frontend
spec:                          # Pod 的核心配置
  containers:                  # 容器列表
  - name: nginx-container      # 容器名称
    image: nginx:1.21          # 容器镜像
    imagePullPolicy: IfNotPresent  # 镜像拉取策略(Always/Never/IfNotPresent)
    ports:                     # 容器暴露的端口(仅声明作用,不直接映射)
    - containerPort: 80
      protocol: TCP

2 Deployment 配置

apiVersion: apps/v1            # Deployment 属于 apps/v1 API 组
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3                  # 副本数(Pod 数量)
  selector:                    # 选择器(关联管理的 Pod)
    matchLabels:
      app: my-app
  template:                    # Pod 模板(与 Pod 配置类似)
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        resources:             # 资源限制
          limits:
            cpu: "1"           # 最大 CPU 使用量(1 核)
            memory: "512Mi"    # 最大内存
          requests:
            cpu: "0.5"         # 请求的 CPU(调度依据)
            memory: "256Mi"
        livenessProbe:         # 存活探针(检查容器是否健康)
          httpGet:
            path: /healthz
            port: 80
          initialDelaySeconds: 15
          periodSeconds: 20
        env:                   # 环境变量
        - name: ENV_KEY
          value: "production"

3 Service 配置

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort               # Service 类型(ClusterIP/NodePort/LoadBalancer)
  selector:                    # 关联的后端 Pod 标签
    app: my-app
  ports:
  - protocol: TCP
    port: 80                   # Service 端口(集群内访问)
    targetPort: 80             # Pod 的容器端口
    nodePort: 31000            # NodePort 类型时节点的端口(30000-32767)

4 ConfigMap 和 Volume

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:                          # 配置数据(键值对)
  config.properties: |
    server.port=8080
    logging.level=INFO
---
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: app
        image: my-app:latest
        volumeMounts:          # 挂载卷到容器路径
        - name: config-volume
          mountPath: /etc/config
      volumes:                 # 定义卷
      - name: config-volume
        configMap:             # 使用 ConfigMap 作为卷
          name: app-config

5 资源限制(Resources)

resources:
  limits:    # 容器能使用的资源上限
    cpu: "2"           # 单位:核(可写为 0.5 或 "500m")
    memory: "1Gi"      # 单位:Ki, Mi, Gi
  requests:  # 调度时请求的资源(节点必须有足够资源才会调度)
    cpu: "1"
    memory: "512Mi"

6 健康检查(Probes)

livenessProbe:     # 存活探针(失败则重启容器)
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 10  # 容器启动后等待时间
  periodSeconds: 5         # 检查间隔
readinessProbe:    # 就绪探针(失败则从 Service 移除端点)
  tcpSocket:
    port: 8080
  timeoutSeconds: 1

7 环境变量

env:
- name: DB_HOST
  value: "mysql-service"   # 直接赋值
- name: DB_PORT
  valueFrom:               # 从其他资源获取值
    configMapKeyRef:
      name: db-config
      key: db.port
- name: NODE_IP
  valueFrom:
    fieldRef:              # 引用 Pod 字段
      fieldPath: status.hostIP

8 完整示例:Deployment + Service + ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
data:
  app.conf: |
    theme=dark
    language=en
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web-server
        image: nginx:1.21
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config
          mountPath: /etc/web-config
        env:
        - name: ENVIRONMENT
          value: "production"
      volumes:
      - name: config
        configMap:
          name: web-config
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: NodePort
  selector:
    app: web
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080

9 常用字段速查表

image.png


苦逼的小运维
1 声望1 粉丝