1. Rancher与Kubernetes集群管理概述在云原生技术栈中Kubernetes简称k8s已成为容器编排的事实标准但原生k8s的部署和管理对许多团队来说仍存在较高门槛。这正是Rancher这类容器管理平台的价值所在——它通过可视化界面和自动化工具将复杂的k8s集群创建过程简化为几次点击操作。我首次使用Rancher部署生产级集群是在2018年当时团队需要快速搭建跨AZ的高可用集群从手动编写kubeadm配置到最终采用Rancher部署时间从2天缩短到2小时。Rancher的核心优势在于多集群统一管理支持同时管理数百个k8s集群开箱即用的工具链内置监控、日志、告警等运维组件多云适配能力AWS EKS、Azure AKS、Google GKE等云服务的统一接入层应用商店提供Helm Chart等应用的一键部署重要提示虽然Rancher简化了操作但建议在正式使用前仍需要理解底层k8s的基本架构包括控制平面组件API Server、Controller Manager等和工作节点组成。2. 环境准备与Rancher部署2.1 基础环境要求在开始创建k8s集群前需要准备符合以下要求的服务器资源角色最低配置推荐配置数量要求Rancher节点2核CPU/4GB内存/50GB存储4核CPU/8GB内存/100GB存储1高可用需3节点k8s控制节点2核CPU/4GB内存/40GB存储4核CPU/16GB内存/100GB存储奇数个通常3k8s工作节点2核CPU/4GB内存/40GB存储8核CPU/32GB内存/200GB存储根据负载动态扩展操作系统建议选择RHEL/CentOS 7.9需禁用SwapUbuntu 20.04 LTSSUSE Linux Enterprise 15 SP3网络要求节点间双向网络互通开放TCP端口6443k8s API、2379-2380etcd、10250kubelet建议10Gbps内网带宽生产环境2.2 Rancher Server安装通过Docker快速部署Rancher Server单节点模式# 最新稳定版截止2023年8月为v2.7.5 docker run -d --restartunless-stopped \ -p 80:80 -p 443:443 \ --privileged \ -v /opt/rancher:/var/lib/rancher \ rancher/rancher:latest高可用部署建议使用Helmhelm repo add rancher-stable https://releases.rancher.com/server-charts/stable helm install rancher rancher-stable/rancher \ --namespace cattle-system \ --set hostnamerancher.yourdomain.com \ --set replicas3 \ --set ingress.tls.sourceletsEncrypt首次登录后需要设置admin账户密码配置服务器URL如https://rancher.example.com选择默认项目视图推荐集群管理视图3. 通过Rancher创建k8s集群3.1 集群创建流程详解在Rancher UI中创建自定义集群的完整步骤基础配置集群名称遵守DNS命名规范Kubernetes版本选择建议选择Rancher验证过的稳定版网络插件Calico/Flannel/CanalIngress控制器Nginx/Traefik容器运行时Docker/containerd/cri-o节点角色分配etcd节点存储集群状态必须奇数个Control Plane节点运行控制平面组件Worker节点运行工作负载节点注册命令生成为每类节点生成专属的docker run命令示例worker节点注册命令sudo docker run -d --privileged --restartunless-stopped \ --nethost -v /etc/kubernetes:/etc/kubernetes \ -v /var/run:/var/run rancher/rancher-agent:v2.7.5 \ --server https://rancher.example.com \ --token xxxx --ca-checksum yyyy --worker高级选项配置集群自动伸缩策略节点自动修复设置云提供商集成AWS/Azure等授权集群端点直接访问k8s API3.2 关键参数解析网络插件选型对比特性CalicoFlannelCanal网络策略支持完整支持不支持通过Calico组件支持性能较高IPIP模式最高VXLAN模式中等适用场景需要网络隔离的环境简单扁平网络平衡策略与性能需求资源消耗较高最低中等Kubernetes版本选择建议生产环境选择n-2版本如当前最新1.27则选1.25测试环境可尝试较新版本但避免使用alpha/beta特性长期支持版关注k8s官方LTS版本公告4. 集群运维与问题排查4.1 日常监控与维护Rancher内置的监控栈基于Prometheus配置示例# cluster-monitoring-config.yaml apiVersion: monitoring.coreos.com/v1 kind: Prometheus metadata: name: rancher-monitoring namespace: cattle-monitoring-system spec: retention: 7d resources: requests: memory: 4Gi cpu: 2 alerting: alertmanagers: - namespace: cattle-monitoring-system name: alertmanager-main port: web关键运维操作证书更新kubectl --kubeconfig kube_config.yaml get secret -n kube-system \ k3s-serving -o jsonpath{.data.tls\.crt} | base64 --decode tls.crt备份恢复# etcd快照备份 rancher etcd snapshot-save --name pre-upgrade.db \ --config kube_config_cluster.yml4.2 常见问题解决方案节点NotReady状态排查检查kubelet日志journalctl -u kubelet -n 100 --no-pager验证网络连通性curl -k https://localhost:10250/healthz常见修复步骤graph TD A[节点NotReady] -- B{检查kubelet状态} B --|停止| C[systemctl restart kubelet] B --|运行中| D[检查容器运行时] D -- E[docker ps -a] E -- F[清理异常容器]Pod创建失败排查流程查看事件记录kubectl describe pod pod-name -n namespace检查资源配额kubectl describe quota -n namespace验证镜像拉取kubectl get events --field-selector involvedObject.namepod-name5. 高级配置与优化5.1 性能调优实践kubelet参数优化# /var/lib/kubelet/config.yaml apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration maxPods: 150 kubeAPIQPS: 50 kubeAPIBurst: 100 serializeImagePulls: false evictionHard: memory.available: 500Mi nodefs.available: 10%内核参数调整# /etc/sysctl.d/99-k8s.conf net.ipv4.ip_forward 1 net.bridge.bridge-nf-call-iptables 1 fs.inotify.max_user_watches 1048576 vm.swappiness 105.2 安全加固措施RBAC权限控制apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: dev-team-limited rules: - apiGroups: [] resources: [pods, pods/log] verbs: [get, list, watch]Pod安全策略apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: restricted spec: privileged: false allowPrivilegeEscalation: false requiredDropCapabilities: - ALL runAsUser: rule: MustRunAsNonRoot网络策略示例apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: frontend-isolation spec: podSelector: matchLabels: role: frontend policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: role: backend ports: - protocol: TCP port: 80806. 实际应用场景扩展6.1 CI/CD流水线集成通过Rancher Projects实现多环境管理创建开发、测试、生产三个Project配置资源配额限制部署ArgoCD实现GitOpshelm repo add argo https://argoproj.github.io/argo-helm helm install argocd argo/argo-cd \ --namespace argocd \ --set server.service.typeLoadBalancer6.2 GPU工作负载调度配置NVIDIA设备插件apiVersion: apps/v1 kind: DaemonSet metadata: name: nvidia-device-plugin namespace: kube-system spec: selector: matchLabels: name: nvidia-device-plugin template: spec: tolerations: - key: nvidia.com/gpu operator: Exists effect: NoSchedule containers: - image: nvidia/k8s-device-plugin:v0.12.3 name: nvidia-device-plugin securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL]6.3 混合云集群管理注册外部集群到Rancher的步骤在目标集群上安装Rancher Agentcurl --insecure -sfL https://rancher.example.com/v3/import/xxxx.yaml | kubectl apply -f -等待集群状态变为Active配置统一监控和日志收集7. 版本升级与迁移7.1 集群升级策略滚动升级最佳实践先升级Rancher到目标版本按顺序升级集群先升级etcd节点逐个进行再升级control plane节点最后升级worker节点验证关键工作负载kubectl get pods -A --field-selectorstatus.phase!Running7.2 数据迁移方案使用Velero进行应用迁移# 源集群备份 velero backup create full-cluster-backup \ --include-namespaces* \ --include-resources* \ --wait # 目标集群恢复 velero restore create --from-backup full-cluster-backup \ --restore-volumestrue \ --wait8. 成本优化技巧8.1 节点自动伸缩配置集群自动伸缩器配置示例apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: php-apache spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: php-apache minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 508.2 资源利用率提升请求/限制优化建议resources: requests: cpu: 500m memory: 512Mi limits: cpu: 2 memory: 2Gi经验之谈实际生产环境中建议先设置requests接近实际使用量limits设为requests的1.5-2倍再根据监控数据逐步调整。