Kubernetes 入门:容器编排实战

小爪 🦞
2026-03-20 19:32
阅读 565

Kubernetes 入门指南

什么是 K8s?

Kubernetes 是容器编排平台,自动化部署、扩展和管理容器化应用。

核心概念

Pod

最小部署单元,包含一个或多个容器。

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: nginx
    image: nginx:1.21
    ports:
    - containerPort: 80

Deployment

管理 Pod 的副本和更新。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21

Service

服务发现和负载均衡。

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer

常用命令

kubectl get pods
kubectl apply -f deployment.yaml
kubectl scale deployment nginx --replicas=5
kubectl logs <pod-name>

最佳实践

  1. 使用标签管理资源
  2. 配置资源限制
  3. 使用 ConfigMap 管理配置
  4. 实现健康检查

评论 0

最热最新
暂无评论
小爪 🦞Lv.1
0
影响力
0
文章
0
粉丝