示例资源清单
示例部署资源清单:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: whoami-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: whoami
      release: canary
  template:
    metadata:
      labels:
        app: whoami
        release: canary
    spec:
      containers:
      - name: whoami
        image: traefik/whoami:v1.10
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: whoami
spec:
  type: NodePort
  selector:
    app: whoami
    release: canary
  ports:
  - name: http
    port: 80
    targetPort: 80 # pod port
    nodePort: 30080部署:
kubectl apply -f xxx.yaml关键参数说明
我来为您解释这个 Kubernetes 配置文件中的关键参数:
Deployment 部分
apiVersion: apps/v1  # K8s API 版本
kind: Deployment     # 资源类型为 Deploymentmetadata:
- name: whoami-deploy- Deployment 的名称
spec:
- replicas: 1- 指定要运行的 Pod 副本数量
- selector- 用于选择要管理哪些 Pod- matchLabels- Pod 必须包含这些标签才会被该 Deployment 管理- app: whoami
- release: canary
 
 
template: - Pod 的模板定义
- metadata/labels- 为创建的 Pod 添加标签
- containers- 容器配置- image: traefik/whoami:v1.10- 使用的容器镜像
- containerPort: 80- 容器内部暴露的端口
 
Service 部分
kind: Service       # 资源类型为 Service
type: NodePort      # Service 类型为 NodePort,允许从集群外访问selector: - 选择要暴露哪些 Pod
- 与 Deployment 中的标签对应: - app: whoami
- release: canary
 
ports:
- port: 80- Service 的端口
- targetPort: 80- Pod 的目标端口
- nodePort: 30080- 节点上暴露的端口(可以通过 <节点IP>:30080 访问)
重要概念说明:
- 这是一个完整的应用部署配置,包含了 Deployment(管理 Pod)和 Service(暴露服务)两部分
- 标签(Labels)在这里起到了关键作用: - Deployment 使用标签选择器来管理 Pod
- Service 使用相同的标签来确定要暴露哪些 Pod
 
- 端口映射关系: - 容器端口 80
- Service 端口 80
- 节点端口 30080(外部访问端口)
 
- NodePort 类型的 Service 允许从集群外部访问服务,端口范围通常在 30000-32767 之间