09 · Kubernetes¶
官方文档:https://kubernetes.io/docs/ kubectl 速查:https://kubernetes.io/docs/reference/kubectl/cheatsheet/ Helm Hub:https://artifacthub.io/ EKS 文档:https://docs.aws.amazon.com/eks/latest/userguide/
1. 核心对象速查¶
# Deployment(无状态应用)
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: api-server
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0 # 零停机更新
template:
metadata:
labels:
app: api-server
spec:
containers:
- name: app
image: $ECR_REGISTRY/api-server:v1.2.3
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m"
memory: "256Mi"
limits:
cpu: "500m"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-secrets
key: db_password
- name: APP_ENV
valueFrom:
configMapKeyRef:
name: app-config
key: environment
# HPA 自动扩容(大赛期间自动应对流量峰值)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api-server-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api-server
minReplicas: 3
maxReplicas: 50
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
2. 常用 kubectl 命令¶
# 基本操作
kubectl get pods -n production -o wide # 查看 Pod 及所在节点
kubectl describe pod <name> -n production # 详细状态(排障首选)
kubectl logs <pod> -n production --tail=100 -f # 实时日志
kubectl logs <pod> -n production --previous # 上次崩溃的日志
kubectl exec -it <pod> -n production -- sh # 进入容器
# 部署操作
kubectl rollout status deployment/api -n production # 查看部署进度
kubectl rollout history deployment/api -n production # 部署历史
kubectl rollout undo deployment/api -n production # 回滚
# 资源状态
kubectl top pods -n production # CPU/内存实时使用
kubectl top nodes # 节点资源使用
kubectl get events -n production --sort-by='.lastTimestamp'
# Port-forward(本地调试)
kubectl port-forward svc/api-server 8080:80 -n production
3. Helm(包管理)¶
# 官方 Helm Hub:https://artifacthub.io/
# 添加常用 Chart 仓库
helm repo add stable https://charts.helm.sh/stable
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo add cert-manager https://charts.jetstack.io
helm repo update
# 安装
helm install nginx-ingress ingress-nginx/ingress-nginx \
--namespace ingress-nginx --create-namespace \
--set controller.replicaCount=2
# 查看 Release
helm list -A
helm status nginx-ingress -n ingress-nginx
# 升级
helm upgrade nginx-ingress ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--set controller.image.tag=v1.9.5
# 回滚
helm rollback nginx-ingress 1
官方文档¶
- K8s 文档:https://kubernetes.io/docs/
- kubectl 速查:https://kubernetes.io/docs/reference/kubectl/cheatsheet/
- Helm 文档:https://helm.sh/docs/
- k9s(终端 UI):https://github.com/derailed/k9s
- KodeKloud CKA 课程:https://kodekloud.com/courses/certified-kubernetes-administrator-cka/
- killer.sh(CKA 模拟考试):https://killer.sh/
最后更新:2025-04