1. Kubernetes Deployment 与 Service 基础概念在 Kubernetes 生态中Deployment 和 Service 是两个核心资源对象它们共同构成了应用部署和服务的基石。Deployment 负责管理 Pod 的生命周期而 Service 则为这些 Pod 提供稳定的网络访问入口。1.1 Deployment 的核心作用Deployment 本质上是一个控制器Controller它通过声明式配置来管理 Pod 的部署和更新。当您创建一个 Deployment 时它会自动创建 ReplicaSet而 ReplicaSet 则确保指定数量的 Pod 副本始终运行。Deployment 的主要特性包括滚动更新与回滚支持无缝升级应用版本出现问题时可以快速回退副本数量维护自动确保运行的 Pod 数量与期望状态一致健康检查结合 Readiness Probe 确保只有健康的 Pod 才会接收流量版本历史保留更新历史方便回滚到特定版本一个典型的 Deployment 定义如下apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.19.10 ports: - containerPort: 801.2 Service 的核心价值Service 解决了 Kubernetes 中一个关键问题在动态的 Pod 环境中如何为前端应用提供稳定的后端访问入口。由于 Pod 是临时的它们的 IP 地址会随着创建销毁而变化Service 通过抽象层提供了不变的访问点。Service 的核心功能包括服务发现自动跟踪后端 Pod 的变化负载均衡将请求分发到多个 Pod 实例访问策略定义服务如何被集群内外部访问多端口支持单个 Service 可以暴露多个端口2. Deployment 实战演练2.1 创建基础 Deployment让我们从创建一个简单的 Nginx Deployment 开始kubectl apply -f - EOF apiVersion: apps/v1 kind: Deployment metadata: name: web-server spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.21.6 ports: - containerPort: 80 resources: requests: cpu: 100m memory: 128Mi limits: cpu: 200m memory: 256Mi EOF验证 Deployment 状态kubectl get deployments kubectl get pods -l appweb2.2 Deployment 更新策略Kubernetes 提供了两种主要的更新策略RollingUpdate默认渐进式更新确保服务不中断strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 25% maxSurge: 25%Recreate先删除所有旧 Pod再创建新 Pod会导致短暂服务中断执行滚动更新示例kubectl set image deployment/web-server nginxnginx:1.22.0观察更新过程kubectl rollout status deployment/web-server2.3 回滚与历史记录当更新出现问题时可以轻松回退到之前的版本查看更新历史kubectl rollout history deployment/web-server回滚到特定版本kubectl rollout undo deployment/web-server --to-revision12.4 扩缩容操作根据负载情况调整 Pod 数量手动扩缩容kubectl scale deployment/web-server --replicas5自动扩缩容需安装 Metrics Serverkubectl autoscale deployment/web-server --min2 --max10 --cpu-percent803. Service 实战演练3.1 创建 ClusterIP ServiceClusterIP 是默认的 Service 类型提供集群内部访问kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF验证 Servicekubectl get svc web-service3.2 NodePort Service 实践NodePort 在 ClusterIP 基础上在每个节点上开放静态端口kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-nodeport spec: type: NodePort selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30080 EOF访问服务假设节点IP为192.168.1.100curl http://192.168.1.100:300803.3 LoadBalancer Service 配置在云环境中LoadBalancer 类型会自动配置外部负载均衡器kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-lb spec: type: LoadBalancer selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF获取外部IPkubectl get svc web-lb3.4 Headless Service 特殊用例当不需要负载均衡时可以使用 Headless Servicekubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-headless spec: clusterIP: None selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF这种 Service 会返回所有后端 Pod 的 IP适合有状态应用如数据库。4. 高级配置与优化4.1 会话保持配置某些应用需要保持客户端与特定 Pod 的会话apiVersion: v1 kind: Service metadata: name: sticky-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 sessionAffinity: ClientIP sessionAffinityConfig: clientIP: timeoutSeconds: 36004.2 多端口 Service一个 Service 可以暴露多个端口apiVersion: v1 kind: Service metadata: name: multi-port spec: selector: app: web ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: metrics protocol: TCP port: 9090 targetPort: 90904.3 外部流量策略控制外部流量的路由方式apiVersion: v1 kind: Service metadata: name: web-service spec: externalTrafficPolicy: Local selector: app: web ports: - protocol: TCP port: 80 targetPort: 80Local策略会保留客户端 IP但可能导致负载不均衡。4.4 资源配额与限制为 Deployment 设置合理的资源限制resources: requests: cpu: 500m memory: 512Mi limits: cpu: 1000m memory: 1Gi5. 常见问题排查5.1 Pod 无法被 Service 访问检查步骤确认 Service 的 selector 与 Pod 的 labels 匹配kubectl get pods --show-labels kubectl describe svc service-name检查 Endpoints 是否正确kubectl get endpoints service-name验证网络策略是否阻止访问kubectl get networkpolicy5.2 滚动更新卡住可能原因及解决方案Readiness Probe 失败kubectl describe pod pod-name资源不足kubectl describe nodes镜像拉取失败kubectl get events --sort-by.metadata.creationTimestamp5.3 服务无法从外部访问排查流程检查 Service 类型是否正确kubectl get svc service-name验证 NodePort 范围cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep service-node-port-range检查云提供商负载均衡器状态kubectl describe svc service-name6. 生产环境最佳实践6.1 部署策略优化蓝绿部署通过两个完全相同的环境切换实现零停机金丝雀发布逐步将流量切换到新版本A/B 测试基于用户属性路由流量示例金丝雀发布kubectl set image deployment/web-server nginxnginx:1.22.0 kubectl rollout pause deployment/web-server # 验证新版本后继续 kubectl rollout resume deployment/web-server6.2 监控与日志配置 Prometheus 监控annotations: prometheus.io/scrape: true prometheus.io/port: 9090集中日志收集方案# Fluentd 示例配置 kubectl apply -f https://raw.githubusercontent.com/fluent/fluentd-kubernetes-daemonset/master/fluentd-daemonset.yaml6.3 安全加固使用 NetworkPolicy 限制 Pod 间通信配置 Pod 安全标准Pod Security Standards启用服务账户令牌卷投射示例 NetworkPolicyapiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: web-allow-specific spec: podSelector: matchLabels: app: web policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: role: frontend ports: - protocol: TCP port: 806.4 成本优化使用 Horizontal Pod Autoscaler 自动扩缩容配置合理的资源请求和限制考虑使用 Spot 实例运行可中断的工作负载HPA 示例kubectl autoscale deployment web-server --cpu-percent50 --min1 --max10在实际生产环境中Deployment 和 Service 的合理配置对应用的稳定性、性能和可维护性至关重要。建议结合具体业务需求不断调整和优化这些配置参数。
Kubernetes Deployment与Service核心概念与实战指南
发布时间:2026/7/14 6:46:46
1. Kubernetes Deployment 与 Service 基础概念在 Kubernetes 生态中Deployment 和 Service 是两个核心资源对象它们共同构成了应用部署和服务的基石。Deployment 负责管理 Pod 的生命周期而 Service 则为这些 Pod 提供稳定的网络访问入口。1.1 Deployment 的核心作用Deployment 本质上是一个控制器Controller它通过声明式配置来管理 Pod 的部署和更新。当您创建一个 Deployment 时它会自动创建 ReplicaSet而 ReplicaSet 则确保指定数量的 Pod 副本始终运行。Deployment 的主要特性包括滚动更新与回滚支持无缝升级应用版本出现问题时可以快速回退副本数量维护自动确保运行的 Pod 数量与期望状态一致健康检查结合 Readiness Probe 确保只有健康的 Pod 才会接收流量版本历史保留更新历史方便回滚到特定版本一个典型的 Deployment 定义如下apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.19.10 ports: - containerPort: 801.2 Service 的核心价值Service 解决了 Kubernetes 中一个关键问题在动态的 Pod 环境中如何为前端应用提供稳定的后端访问入口。由于 Pod 是临时的它们的 IP 地址会随着创建销毁而变化Service 通过抽象层提供了不变的访问点。Service 的核心功能包括服务发现自动跟踪后端 Pod 的变化负载均衡将请求分发到多个 Pod 实例访问策略定义服务如何被集群内外部访问多端口支持单个 Service 可以暴露多个端口2. Deployment 实战演练2.1 创建基础 Deployment让我们从创建一个简单的 Nginx Deployment 开始kubectl apply -f - EOF apiVersion: apps/v1 kind: Deployment metadata: name: web-server spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.21.6 ports: - containerPort: 80 resources: requests: cpu: 100m memory: 128Mi limits: cpu: 200m memory: 256Mi EOF验证 Deployment 状态kubectl get deployments kubectl get pods -l appweb2.2 Deployment 更新策略Kubernetes 提供了两种主要的更新策略RollingUpdate默认渐进式更新确保服务不中断strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 25% maxSurge: 25%Recreate先删除所有旧 Pod再创建新 Pod会导致短暂服务中断执行滚动更新示例kubectl set image deployment/web-server nginxnginx:1.22.0观察更新过程kubectl rollout status deployment/web-server2.3 回滚与历史记录当更新出现问题时可以轻松回退到之前的版本查看更新历史kubectl rollout history deployment/web-server回滚到特定版本kubectl rollout undo deployment/web-server --to-revision12.4 扩缩容操作根据负载情况调整 Pod 数量手动扩缩容kubectl scale deployment/web-server --replicas5自动扩缩容需安装 Metrics Serverkubectl autoscale deployment/web-server --min2 --max10 --cpu-percent803. Service 实战演练3.1 创建 ClusterIP ServiceClusterIP 是默认的 Service 类型提供集群内部访问kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF验证 Servicekubectl get svc web-service3.2 NodePort Service 实践NodePort 在 ClusterIP 基础上在每个节点上开放静态端口kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-nodeport spec: type: NodePort selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30080 EOF访问服务假设节点IP为192.168.1.100curl http://192.168.1.100:300803.3 LoadBalancer Service 配置在云环境中LoadBalancer 类型会自动配置外部负载均衡器kubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-lb spec: type: LoadBalancer selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF获取外部IPkubectl get svc web-lb3.4 Headless Service 特殊用例当不需要负载均衡时可以使用 Headless Servicekubectl apply -f - EOF apiVersion: v1 kind: Service metadata: name: web-headless spec: clusterIP: None selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 EOF这种 Service 会返回所有后端 Pod 的 IP适合有状态应用如数据库。4. 高级配置与优化4.1 会话保持配置某些应用需要保持客户端与特定 Pod 的会话apiVersion: v1 kind: Service metadata: name: sticky-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 sessionAffinity: ClientIP sessionAffinityConfig: clientIP: timeoutSeconds: 36004.2 多端口 Service一个 Service 可以暴露多个端口apiVersion: v1 kind: Service metadata: name: multi-port spec: selector: app: web ports: - name: http protocol: TCP port: 80 targetPort: 80 - name: metrics protocol: TCP port: 9090 targetPort: 90904.3 外部流量策略控制外部流量的路由方式apiVersion: v1 kind: Service metadata: name: web-service spec: externalTrafficPolicy: Local selector: app: web ports: - protocol: TCP port: 80 targetPort: 80Local策略会保留客户端 IP但可能导致负载不均衡。4.4 资源配额与限制为 Deployment 设置合理的资源限制resources: requests: cpu: 500m memory: 512Mi limits: cpu: 1000m memory: 1Gi5. 常见问题排查5.1 Pod 无法被 Service 访问检查步骤确认 Service 的 selector 与 Pod 的 labels 匹配kubectl get pods --show-labels kubectl describe svc service-name检查 Endpoints 是否正确kubectl get endpoints service-name验证网络策略是否阻止访问kubectl get networkpolicy5.2 滚动更新卡住可能原因及解决方案Readiness Probe 失败kubectl describe pod pod-name资源不足kubectl describe nodes镜像拉取失败kubectl get events --sort-by.metadata.creationTimestamp5.3 服务无法从外部访问排查流程检查 Service 类型是否正确kubectl get svc service-name验证 NodePort 范围cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep service-node-port-range检查云提供商负载均衡器状态kubectl describe svc service-name6. 生产环境最佳实践6.1 部署策略优化蓝绿部署通过两个完全相同的环境切换实现零停机金丝雀发布逐步将流量切换到新版本A/B 测试基于用户属性路由流量示例金丝雀发布kubectl set image deployment/web-server nginxnginx:1.22.0 kubectl rollout pause deployment/web-server # 验证新版本后继续 kubectl rollout resume deployment/web-server6.2 监控与日志配置 Prometheus 监控annotations: prometheus.io/scrape: true prometheus.io/port: 9090集中日志收集方案# Fluentd 示例配置 kubectl apply -f https://raw.githubusercontent.com/fluent/fluentd-kubernetes-daemonset/master/fluentd-daemonset.yaml6.3 安全加固使用 NetworkPolicy 限制 Pod 间通信配置 Pod 安全标准Pod Security Standards启用服务账户令牌卷投射示例 NetworkPolicyapiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: web-allow-specific spec: podSelector: matchLabels: app: web policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: role: frontend ports: - protocol: TCP port: 806.4 成本优化使用 Horizontal Pod Autoscaler 自动扩缩容配置合理的资源请求和限制考虑使用 Spot 实例运行可中断的工作负载HPA 示例kubectl autoscale deployment web-server --cpu-percent50 --min1 --max10在实际生产环境中Deployment 和 Service 的合理配置对应用的稳定性、性能和可维护性至关重要。建议结合具体业务需求不断调整和优化这些配置参数。