Kubernetes Overview
理解 Kubernetes 架构, pods, 部署, and services for 容器 orchestration.
你将学到
- 什么 Kubernetes 是 and 为什么它 exists
- 核心概念: pods, 部署, services
- control plane and 工作者节点
- 基础 kubectl commands
概念讲解
什么是 Kubernetes?
Kubernetes (K8s) is a container orchestration platform. When you have a few containers, Docker Compose works fine. But when you have hundreds or thousands of containers across multiple servers, you need a way to schedule them, keep them running, scale them, and connect them. Kubernetes automates all of this. It decides which server runs which container, restarts failed containers, scales them up or down based on load, and rolls out updates without downtime.
Core Concepts
Pod 是 the smallest unit in Kubernetes — it runs one or more containers together. A Deployment manages a set of identical pods, handling rolling updates and scaling. A Service provides a stable network address for a set of pods, load-balancing across them. A ConfigMap stores configuration, and a Secret stores sensitive data. These abstractions let you describe your application declaratively — Kubernetes works to make reality match your description.
Architecture
A Kubernetes cluster has a control plane (the brain) and worker nodes (the muscle). The control plane runs the API server, scheduler, and controller manager — it makes decisions. Worker nodes run your pods — they do the actual work. Each node runs kubelet (which talks to the control plane) and kube-proxy (which handles networking). You interact with the control plane via kubectl, the Kubernetes command-line tool.
kubectl Basics
kubectl 获取 pods lists running pods. kubectl apply -f 部署.yaml creates or updates resources from a file. kubectl logs <pod> shows a pod's logs. kubectl exec -它 <pod> -- bash opens a shell in a pod. kubectl scale 部署 myapp --replicas=5 scales a deployment. kubectl 描述 pod <pod> shows detailed information for debugging. These commands are the daily tools of a Kubernetes engineer.
Helm and Package Management
Helm is the package manager for Kubernetes. A Helm chart is a collection of templates that generate Kubernetes manifests, parameterized by a values file. Instead of writing YAML from scratch, you install a chart: helm install myapp ./chart. The public Helm repository has charts for databases, monitoring tools, and common applications. Helm makes it easy to share, version, and reuse Kubernetes configurations, just as package managers do for application code.
示例代码
# A Kubernetes Deployment and Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: web
image: myapp:1.0
ports:
- containerPort: 80
resources:
limits:
memory: "256Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 80
type: LoadBalancer
部署运行 3 replicas 的 myapp 容器使用 resource 限制. Service exposes 它们通过加载 balancer. --- separator 划分多个 resources in one file.
动手试一试
- YAML Validator
验证 Kubernetes manifest YAML files.
- YAML 到 JSON
Convert K8s manifests 到 JSON for inspection.
- Docker Compose Generator
Generate compose files as K8s alternative.
常见错误
错误写法
不设置 resource 请求 and 限制
正确做法
总是设置 CPU and memory 请求 and 限制 on 容器. 不使用它们, Kubernetes cannot schedule efficiently, and 单个 pod 能 consume 所有节点 resources.
错误写法
Using :latest 标签 for 容器镜像
正确做法
Use 特定 version 标签. Kubernetes won't 拉取新 :latest 镜像如果它 already 有 one cached. Versioned 标签 ensure 更新是 actually deployed.
练习 Kubernetes Overview
4 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。