0Pricing
Docker & Kubernetes for Developers · 课时

了解 Kubernetes 部署

学习 Deployment 如何管理应用的期望状态,从而支持滚动更新和回滚。

了解 Kubernetes 部署 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Meet Kubernetes Deployments

Welcome to the world of Kubernetes Deployments! After learning about Pods, you might wonder how to manage many identical Pods reliably.

Deployments are a powerful Kubernetes resource that helps you declare and manage the desired state of your applications.

Why Deployments are Essential

Imagine running a web server. You need multiple copies (replicas) for high availability and to handle traffic. If one crashes, you need another to take its place.

Deployments automate these tasks:

  • Self-Healing: Replaces failed Pods automatically.
  • Scaling: Easily adjust the number of Pod replicas.
  • Updates: Perform rolling updates without downtime.
  • Rollbacks: Revert to previous versions if something goes wrong.

Desired State in Action

The core concept behind a Deployment is its desired state. You tell Kubernetes what you want your application to look like, and it works to make it happen.

For example, if you say "I want 3 Nginx Pods running version 1.21," the Deployment will ensure exactly 3 Pods with Nginx 1.21 are running, even if some fail.

Deployments and ReplicaSets

Under the hood, Deployments use another Kubernetes resource called a ReplicaSet.

  • A ReplicaSet ensures a specified number of Pod replicas are running at all times.
  • When you create a Deployment, it automatically creates a ReplicaSet.
  • When you update a Deployment, it creates a new ReplicaSet and gracefully scales down the old one while scaling up the new one.

Building a Deployment Manifest

Like most Kubernetes resources, Deployments are defined using YAML. Key fields include:

  • apiVersion: apps/v1 (for Deployments)
  • kind: Deployment
  • metadata: Name, labels to identify the Deployment.
  • spec:
    • replicas: The desired number of Pods.
    • selector: How the Deployment finds its Pods (using labels).
    • template: The Pod definition (just like a standalone Pod YAML!).

Example: Nginx Deployment

Let's create a simple Deployment for Nginx. This YAML specifies 3 replicas of an Nginx web server.

You would typically save this as nginx-deployment.yaml and apply it using kubectl apply -f nginx-deployment.yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21.6
        ports:
        - containerPort: 80

Checking Deployment Status

Once you've applied your Deployment, you can check its status using kubectl commands:

  • kubectl get deployments: Shows a summary of your deployments.
  • kubectl get pods -l app=nginx: See the Pods managed by your Nginx deployment.
  • kubectl describe deployment nginx-deployment: Get detailed information, including events and associated ReplicaSets.

Seamless App Updates

One of the best features of Deployments is rolling updates. This allows you to update your application version without downtime.

When you change the image or configuration in your Deployment's YAML, Kubernetes:

  1. Creates new Pods with the updated version.
  2. Gradually terminates old Pods as new ones become ready.

This ensures your application remains available throughout the update process.

Performing a Rolling Update

Let's update our Nginx Deployment to a newer version. You would edit your nginx-deployment.yaml file and change the image to nginx:1.22.1.

Then, apply the changes: kubectl apply -f nginx-deployment.yaml. Kubernetes will perform a rolling update!

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.22.1 # Changed from 1.21.6
        ports:
        - containerPort: 80

Reverting Changes

Sometimes an update goes wrong. Deployments allow you to easily rollback to a previous stable version.

Kubernetes keeps a history of your Deployments' revisions. You can see this history and revert to an earlier state if needed, minimizing the impact of bad deployments.

  • kubectl rollout history deployment nginx-deployment: View past revisions.
  • kubectl rollout undo deployment nginx-deployment --to-revision=1: Revert to a specific revision.

Deployment Knowledge Check

You've learned about the power of Kubernetes Deployments!

Which of the following capabilities is NOT directly provided by a Kubernetes Deployment?

Deployment Power-Up!

Congratulations! You've successfully understood Kubernetes Deployments.

We covered:

  • What Deployments are and why they are crucial for managing applications.
  • The concept of desired state and how Deployments use ReplicaSets.
  • How to create, inspect, update, and rollback Deployments.

Next, we'll explore how to make your applications accessible using Kubernetes Services!

常见问题解答

「了解 Kubernetes 部署」课时是免费的吗?

是的 — 「了解 Kubernetes 部署」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「了解 Kubernetes 部署」这节课中我会学到什么?

学习 Deployment 如何管理应用的期望状态,从而支持滚动更新和回滚。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「了解 Kubernetes 部署」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 了解 Kubernetes 部署
  2. 使用服务暴露应用
  3. 扩展与自愈应用
  4. 滚动更新与回滚
← 返回 Docker & Kubernetes for Developers