ทำความเข้าใจการนำไปใช้งานของ Kubernetes
เรียนรู้ว่าออบเจ็กต์ Deployment จัดการสถานะที่ต้องการของแอปพลิเคชันอย่างไร พร้อมรองรับการอัปเดตแบบทยอยและการย้อนกลับ
ทำความเข้าใจการนำไปใช้งานของ Kubernetes เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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:Deploymentmetadata: 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: 80Checking 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:
- Creates new Pods with the updated version.
- 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: 80Reverting 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทำความเข้าใจการนำไปใช้งานของ Kubernetes”
เรียนรู้ว่าออบเจ็กต์ Deployment จัดการสถานะที่ต้องการของแอปพลิเคชันอย่างไร พร้อมรองรับการอัปเดตแบบทยอยและการย้อนกลับ คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ทำความเข้าใจการนำไปใช้งานของ Kubernetes” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม
ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจการนำไปใช้งานของ Kubernetes
- การเปิดให้แอปพลิเคชันเข้าถึงด้วยบริการ
- การปรับขนาดและการกู้คืนแอปพลิเคชันด้วยตนเอง
- การอัปเดตแบบทยอยและการย้อนกลับ