Skip to content

Kubernetes 部署配置

入门 约 30 分钟

你将构建什么

设置的 Kubernetes YAML manifests 那个部署 web application 使用 3 replicas, expose 它通过 Service, and 配置它 使用 ConfigMap.

学习目标

  • 编写 Kubernetes 部署 manifest
  • 创建 Service 到 expose application
  • Use ConfigMap for 配置
  • 部署 and 验证 application in cluster

前置要求

  • 基础理解的 容器 and Docker
  • kubectl 安装 and 已连接到 Kubernetes cluster (or minikube)
  • 容器镜像到 部署 (e.g., nginx)

步骤

  1. 1

    创建 ConfigMap

    创建 ConfigMap YAML file 那个存储非-sensitive 配置 for 你的 application, 如环境设置.

    代码
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: app-config
    data:
      ENV: "production"
      LOG_LEVEL: "info"

    预期结果

    ConfigMap YAML file 是创建使用配置值.

  2. 2

    编写部署 manifest

    创建部署那个运行 3 replicas 的 nginx, injects ConfigMap as 环境变量, and 设置 resource 限制.

    代码
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-app
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: web-app
      template:
        metadata:
          labels:
            app: web-app
        spec:
          containers:
            - name: nginx
              image: nginx:latest
              ports:
                - containerPort: 80
              envFrom:
                - configMapRef:
                    name: app-config
              resources:
                requests:
                  cpu: 100m
                  memory: 128Mi
                limits:
                  cpu: 200m
                  memory: 256Mi

    预期结果

    部署 YAML file 是创建使用 3 replicas and ConfigMap injection.

  3. 3

    编写 Service manifest

    创建 Service 那个 exposes 部署 on stable 内部 IP, 加载-balancing 跨越 3 replicas.

    代码
    apiVersion: v1
    kind: Service
    metadata:
      name: web-app-service
    spec:
      type: ClusterIP
      selector:
        app: web-app
      ports:
        - port: 80
          targetPort: 80

    预期结果

    Service YAML file 是创建那个 routes traffic 到部署's Pods.

  4. 4

    部署到 cluster

    Use kubectl 到 apply 所有 three manifests 到你的 cluster. 然后验证部署, Pods, and Service 是运行正确地.

    代码
    kubectl apply -f configmap.yaml
    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
    kubectl get pods -l app=web-app
    kubectl get service web-app-service

    预期结果

    Three Pods 是运行, and Service 是创建使用 cluster IP.

  5. 5

    添加健康检查端点

    为应用添加 /health 端点返回 200 OK,供编排系统(Kubernetes、负载均衡器)探测存活。健康检查是生产部署的基础设施。

    代码
    # app.py (Flask example)
    from flask import Flask, jsonify
    app = Flask(__name__)
    
    @app.route("/health")
    def health():
        return jsonify({"status": "ok"}), 200
    
    @app.route("/ready")
    def ready():
        # Check DB connection etc.
        return jsonify({"ready": True}), 200

    预期结果

    curl localhost:5000/health 返回 {"status":"ok"} 和 200 状态码。

自己动手试试

你学到了什么

你 wrote Kubernetes manifests for 部署, Service, and ConfigMap, and deployed 它们到 cluster. 这个是 核心的 Kubernetes application 部署.

相关资源

相关术语

相关术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题