目 录CONTENT

文章目录

Kubernetes Pod 资源清单与配置指南

BKUN
2024-05-08 / 0 评论 / 0 点赞 / 694 阅读 / 1,353 字

🚀 Kubernetes Pod 资源清单与配置指南

这是一个关于 Kubernetes Pod 资源清单的详细说明,涵盖基本结构、常用配置及示例。让我们一步步探索吧!📜


📝 Pod 的资源清单基础结构

以下是 Pod 的 YAML 配置模板,包含所有常见字段及其说明:

apiVersion: v1           # 必选,API 版本号,例如 v1
kind: Pod                # 必选,资源类型,这里是 Pod
metadata:                # 必选,元数据
  name: <string>         # 必选,Pod 名称
  namespace: <string>    # Pod 所属命名空间,默认 "default"
  labels:                # 自定义标签
    - name: <string>     # 标签键值对
spec:                    # 必选,Pod 的详细定义
  containers:            # 必选,容器列表
  - name: <string>       # 必选,容器名称
    image: <string>      # 必选,容器镜像名称
    imagePullPolicy: [Always | Never | IfNotPresent]  # 镜像拉取策略
    command: [<string>]  # 容器启动命令(可选,默认使用镜像内置命令)
    args: [<string>]     # 启动命令参数
    workingDir: <string> # 容器工作目录
    volumeMounts:        # 挂载的存储卷
    - name: <string>     # 存储卷名称(需与 volumes 对应)
      mountPath: <string># 挂载路径(绝对路径,<512字符)
      readOnly: <boolean># 是否只读
    ports:               # 暴露的端口
    - name: <string>     # 端口名称
      containerPort: <int> # 容器监听端口
      hostPort: <int>    # 宿主机监听端口(可选)
      protocol: <string> # 协议(TCP/UDP,默认 TCP)
    env:                 # 环境变量
    - name: <string>     # 变量名
      value: <string>    # 变量值
    resources:           # 资源限制与请求
      limits:            # 上限
        cpu: <string>    # CPU 限制(单位:core)
        memory: <string> # 内存限制(单位:Mi/Gi)
      requests:          # 请求(下限)
        cpu: <string>    # CPU 请求
        memory: <string> # 内存请求
    lifecycle:           # 生命周期钩子
      postStart:         # 启动后执行
      preStop:           # 终止前执行
    livenessProbe:       # 存活探针
      exec:              # 执行命令检查
        command: [<string>]
      httpGet:           # HTTP 检查
        path: <string>
        port: <number>
      tcpSocket:         # TCP 检查
        port: <number>
      initialDelaySeconds: <int>  # 首次探测延迟
      timeoutSeconds: <int>       # 超时时间
      periodSeconds: <int>        # 探测间隔
  restartPolicy: [Always | Never | OnFailure]  # 重启策略
  nodeName: <string>         # 指定调度节点
  nodeSelector: <object>     # 节点选择器
  imagePullSecrets:          # 镜像拉取密钥
  - name: <string>
  hostNetwork: <boolean>     # 是否使用宿主网络
  volumes:                   # 存储卷定义
  - name: <string>           # 卷名称
    emptyDir: {}             # 临时目录
    hostPath:                # 宿主机目录
      path: <string>
    secret:                  # Secret 卷
      secretName: <string>
    configMap:               # ConfigMap 卷
      name: <string>

💡 kubectl 技巧

  • kubectl explain <资源类型> 查看一级属性
  • kubectl explain <资源类型>.<属性> 查看子属性

🌟 Kubernetes 资源的核心属性

Kubernetes 中几乎所有资源都遵循以下 5 个一级属性:

  1. apiVersion:版本号,由 Kubernetes 定义(可用 kubectl api-versions 查询)
  2. kind:资源类型(如 Pod,kubectl api-resources 可查)
  3. metadata:元数据(名称、命名空间、标签等)
  4. spec:资源详细配置(核心部分)
  5. status:状态信息(自动生成,无需定义)

spec 的常见子属性

  • containers:容器定义
  • nodeName:指定调度节点
  • nodeSelector:基于标签选择节点
  • hostNetwork:是否使用宿主网络
  • volumes:存储卷配置
  • restartPolicy:重启策略

🔧 Pod 配置实战

1️⃣ 基本配置

创建一个简单的 Pod,包含两个容器:

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: test
  labels:
    user: user1
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
  - name: busybox
    image: busybox:1.30

2️⃣ 镜像拉取策略

配置 imagePullPolicy,控制镜像拉取行为:

apiVersion: v1
kind: Pod
metadata:
  name: pod-imagepullpolicy
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    imagePullPolicy: Never
  - name: busybox
    image: busybox:1.30

策略说明

  • Always:总是远程拉取
  • IfNotPresent:本地有则用本地,否则远程拉取(默认)
  • Never:只用本地镜像,无则报错

默认规则

  • 具体版本(如 1.17.1):IfNotPresent
  • latest 标签:Always

🚀 创建命令:kubectl create -f pod-imagepullpolicy.yaml


3️⃣ 启动命令

解决 Busybox 容器自动退出问题,使用 command 保持运行:

apiVersion: v1
kind: Pod
metadata:
  name: pod-command1
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    imagePullPolicy: Never
  - name: busybox
    image: busybox:1.30
    imagePullPolicy: Never
    command: ["/bin/sh", "-c", "touch /tmp/hello.txt; while true; do echo $(date +%T) >> /tmp/hello.txt; sleep 3; done"]

命令解析

  • /bin/sh -c:使用 shell 执行
  • touch /tmp/hello.txt:创建文件
  • while true ...:每 3 秒写入时间

✅ 创建并验证:
kubectl create -f pod-command.yaml
kubectl get pods -n test


4️⃣ 端口配置

查看端口字段:

kubectl explain pod.spec.containers.ports

示例:

ports:
- name: http
  containerPort: 80
  hostPort: 8080
  protocol: TCP
  • containerPort:容器监听端口
  • hostPort:宿主机映射端口(可选)
  • protocol:协议(默认 TCP)

5️⃣ 资源配额

通过 resources 设置 CPU 和内存限制:

apiVersion: v1
kind: Pod
metadata:
  name: pod-resources
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    imagePullPolicy: Never
    resources:
      limits:         # 上限
        cpu: "2"
        memory: "10Gi"
      requests:       # 下限
        cpu: "1"
        memory: "10Mi"

操作示例

  1. 删除 Pod:kubectl delete -f pod-resources.yaml
  2. 修改 requests.memory10Gi
  3. 重新创建:kubectl create -f pod-resources.yaml
  4. 检查状态:kubectl get pods -n test

⚠️ 如果资源不足(如内存超限),Pod 会进入 Pending 状态,可用 kubectl describe pod 查看原因。

0

评论区