apiVersion: v1 kind: Service metadata: name: web-app spec: selector: app: web-app ports:
- port: 80 targetPort: 8080 type: LoadBalancer
### 6.2 健康检查
```yaml
spec:
containers:
- name: web
image: myapp:latest
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
七、常用命令
# 部署
kubectl apply -f deployment.yaml
# 查看
kubectl get pods,svc,deployments
# 日志
kubectl logs -f pod-name
kubectl logs deployment/my-app
# 进入容器
kubectl exec -it pod-name -- /bin/sh
# 删除
kubectl delete -f deployment.yaml
八、总结
Kubernetes 核心要点:
- Pod:最小部署单元
- Deployment:管理 Pod 副本
- Service:服务发现和负载均衡
- ConfigMap/Secret:配置管理
- 健康检查:存活探针、就绪探针
- 扩缩容:手动和自动
掌握这些,K8s 部署不再难!