kubernetes-deploying
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKubernetes Deploying
Kubernetes 应用部署
Deploy and manage applications on Kubernetes.
在Kubernetes上部署和管理应用。
Core Resources
核心资源
Deployment
Deployment
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-registry/my-app:v1.2.3
ports:
- containerPort: 3000
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 10
periodSeconds: 30
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: my-app-secrets
key: database-url
- name: NODE_ENV
value: productionyaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-registry/my-app:v1.2.3
ports:
- containerPort: 3000
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /healthz
port: 3000
initialDelaySeconds: 10
periodSeconds: 30
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 10
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: my-app-secrets
key: database-url
- name: NODE_ENV
value: productionService
Service
yaml
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 3000
type: ClusterIPyaml
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 3000
type: ClusterIPIngress
Ingress
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- app.example.com
secretName: my-app-tls
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 80yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts:
- app.example.com
secretName: my-app-tls
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 80ConfigMap & Secret
ConfigMap & Secret
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-app-config
data:
LOG_LEVEL: info
FEATURE_FLAGS: '{"darkMode": true}'
---
apiVersion: v1
kind: Secret
metadata:
name: my-app-secrets
type: Opaque
stringData:
database-url: postgresql://user:pass@host:5432/db
api-key: sk-abc123yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-app-config
data:
LOG_LEVEL: info
FEATURE_FLAGS: '{"darkMode": true}'
---
apiVersion: v1
kind: Secret
metadata:
name: my-app-secrets
type: Opaque
stringData:
database-url: postgresql://user:pass@host:5432/db
api-key: sk-abc123Common Commands
常用命令
bash
undefinedbash
undefinedApply manifests
Apply manifests
kubectl apply -f k8s/
kubectl apply -f k8s/
Check deployment status
Check deployment status
kubectl rollout status deployment/my-app
kubectl rollout status deployment/my-app
View pods
View pods
kubectl get pods -l app=my-app
kubectl get pods -l app=my-app
View logs
View logs
kubectl logs -f deployment/my-app
kubectl logs -f deployment/my-app
Execute into a pod
Execute into a pod
kubectl exec -it <pod-name> -- /bin/sh
kubectl exec -it <pod-name> -- /bin/sh
Scale
Scale
kubectl scale deployment/my-app --replicas=5
kubectl scale deployment/my-app --replicas=5
Rollback
Rollback
kubectl rollout undo deployment/my-app
kubectl rollout undo deployment/my-app
Port forward for local debugging
Port forward for local debugging
kubectl port-forward svc/my-app 3000:80
undefinedkubectl port-forward svc/my-app 3000:80
undefinedDeployment Strategies
部署策略
| Strategy | How | When |
|---|---|---|
| Rolling update (default) | Replace pods one at a time | Most deployments |
| Recreate | Kill all old pods, start new ones | When you can't run two versions simultaneously |
| Blue/green | Run two full environments, switch traffic | Need instant rollback |
| Canary | Route small % of traffic to new version | High-risk changes |
Rolling update config:
yaml
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0| 策略 | 实现方式 | 适用场景 |
|---|---|---|
| Rolling update (默认) | 逐个替换Pod | 大多数部署场景 |
| Recreate | 先终止所有旧Pod,再启动新Pod | 无法同时运行两个版本的场景 |
| Blue/green | 运行两套完整环境,切换流量路由 | 需要即时回滚的场景 |
| Canary | 将小比例流量路由到新版本 | 高风险变更场景 |
滚动更新配置:
yaml
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0Health Checks
健康检查
Always set both:
- livenessProbe: "Is the process healthy?" — restarts the pod if it fails
- readinessProbe: "Can it handle traffic?" — removes from service if it fails
Common probe types:
- : Hit an HTTP endpoint (most common)
httpGet - : Run a command in the container
exec - : Check if a port is open
tcpSocket
始终同时设置以下两种检查:
- livenessProbe:"进程是否健康?"——如果检查失败,将重启Pod
- readinessProbe:"是否可以处理流量?"——如果检查失败,将从服务中移除该Pod
常见的探针类型:
- :访问HTTP端点(最常用)
httpGet - :在容器内执行命令
exec - :检查端口是否开放
tcpSocket
Horizontal Pod Autoscaler
水平Pod自动扩缩器(Horizontal Pod Autoscaler)
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70Tips
小贴士
- Always set resource requests and limits
- Use namespaces to isolate environments (,
dev,staging)prod - Never put secrets in plaintext YAML committed to git — use Sealed Secrets, SOPS, or external secret managers
- Tag images with specific versions, never use in production
:latest - Set for high-availability workloads
PodDisruptionBudget
- 始终设置资源请求(requests)和限制(limits)
- 使用命名空间(namespaces)隔离环境(、
dev、staging)prod - 切勿将明文Secret提交到Git仓库——使用Sealed Secrets、SOPS或外部密钥管理器
- 为镜像打上具体版本标签,生产环境中绝不使用
:latest - 为高可用工作负载设置
PodDisruptionBudget