SmallThinker-3B-Preview部署教程:Kubernetes集群中Ollama StatefulSet编排方案

张开发
2026/5/9 23:57:59 15 分钟阅读

分享文章

SmallThinker-3B-Preview部署教程:Kubernetes集群中Ollama StatefulSet编排方案
SmallThinker-3B-Preview部署教程Kubernetes集群中Ollama StatefulSet编排方案1. 环境准备与要求在开始部署SmallThinker-3B-Preview模型之前需要确保你的Kubernetes集群满足以下基本要求系统要求Kubernetes版本1.20或更高版本节点内存每个Pod至少需要8GB RAM节点存储至少20GB可用磁盘空间GPU支持可选NVIDIA GPU节点可加速推理工具准备kubectl命令行工具Helm可选用于简化部署NVIDIA GPU Operator如果使用GPU命名空间创建kubectl create namespace ollama-deployment kubectl config set-context --current --namespaceollama-deployment2. 部署Ollama StatefulSetStatefulSet适合有状态应用部署能保证Pod的持久化存储和稳定网络标识。2.1 创建持久化存储首先创建PersistentVolumeClaim为模型数据提供持久化存储# storage.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: ollama-model-pvc namespace: ollama-deployment spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi storageClassName: standard应用配置kubectl apply -f storage.yaml2.2 部署Ollama StatefulSet创建StatefulSet配置文件专门针对SmallThinker-3B-Preview模型优化# smallthinker-statefulset.yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: ollama-smallthinker namespace: ollama-deployment spec: serviceName: ollama-service replicas: 1 selector: matchLabels: app: ollama-smallthinker template: metadata: labels: app: ollama-smallthinker spec: containers: - name: ollama image: ollama/ollama:latest ports: - containerPort: 11434 volumeMounts: - name: model-storage mountPath: /root/.ollama resources: requests: memory: 6Gi cpu: 2 limits: memory: 8Gi cpu: 4 env: - name: OLLAMA_HOST value: 0.0.0.0:11434 - name: OLLAMA_MODELS value: /root/.ollama/models volumes: - name: model-storage persistentVolumeClaim: claimName: ollama-model-pvc volumeClaimTemplates: - metadata: name: model-data spec: accessModes: [ ReadWriteOnce ] resources: requests: storage: 20Gi部署StatefulSetkubectl apply -f smallthinker-statefulset.yaml3. 配置服务访问3.1 创建Service为了让应用能够被访问需要创建对应的Service# service.yaml apiVersion: v1 kind: Service metadata: name: ollama-service namespace: ollama-deployment spec: selector: app: ollama-smallthinker ports: - protocol: TCP port: 11434 targetPort: 11434 type: LoadBalancer应用服务配置kubectl apply -f service.yaml3.2 获取访问地址获取服务的访问地址kubectl get svc -n ollama-deployment ollama-service4. 下载和配置SmallThinker模型4.1 进入Pod下载模型进入运行中的Pod下载SmallThinker-3B-Preview模型# 获取Pod名称 POD_NAME$(kubectl get pods -n ollama-deployment -l appollama-smallthinker -o jsonpath{.items[0].metadata.name}) # 进入Pod执行命令 kubectl exec -it -n ollama-deployment $POD_NAME -- ollama pull smallthinker:3b-preview4.2 验证模型下载检查模型是否成功下载kubectl exec -it -n ollama-deployment $POD_NAME -- ollama list应该能看到类似输出NAME ID SIZE MODIFIED smallthinker:3b-preview xxxxxxxxxxx 3.1GB 2 minutes ago5. 测试模型功能5.1 基本功能测试通过端口转发进行本地测试kubectl port-forward -n ollama-deployment svc/ollama-service 11434:11434然后在另一个终端测试curl http://localhost:11434/api/generate -d { model: smallthinker:3b-preview, prompt: 你好请介绍一下你自己, stream: false }5.2 创建测试脚本创建自动化测试脚本# test-model.sh #!/bin/bash SERVICE_IP$(kubectl get svc -n ollama-deployment ollama-service -o jsonpath{.status.loadBalancer.ingress[0].ip}) PORT11434 echo 测试SmallThinker-3B-Preview模型连接... curl -s http://$SERVICE_IP:$PORT/api/tags echo -e \n测试模型推理能力... curl -s http://$SERVICE_IP:$PORT/api/generate -d { model: smallthinker:3b-preview, prompt: 解释一下机器学习的基本概念, stream: false } | jq .response给脚本执行权限并运行chmod x test-model.sh ./test-model.sh6. 高级配置和优化6.1 GPU加速配置可选如果集群有GPU可以配置GPU加速# 在StatefulSet的container部分添加 resources: limits: nvidia.com/gpu: 1 requests: nvidia.com/gpu: 16.2 自动扩缩容配置配置Horizontal Pod Autoscaler# hpa.yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: ollama-hpa namespace: ollama-deployment spec: scaleTargetRef: apiVersion: apps/v1 kind: StatefulSet name: ollama-smallthinker minReplicas: 1 maxReplicas: 3 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 706.3 健康检查配置添加健康检查确保服务稳定性# 在container部分添加 livenessProbe: httpGet: path: /api/tags port: 11434 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /api/tags port: 11434 initialDelaySeconds: 5 periodSeconds: 57. 监控和日志7.1 查看Pod状态kubectl get pods -n ollama-deployment -w7.2 查看日志kubectl logs -f -n ollama-deployment deployment/ollama-smallthinker7.3 资源使用监控kubectl top pods -n ollama-deployment8. 故障排除8.1 常见问题解决问题1模型下载失败# 检查网络连接 kubectl exec -it -n ollama-deployment $POD_NAME -- curl -I https://ollama.com # 手动下载重试 kubectl exec -it -n ollama-deployment $POD_NAME -- ollama pull smallthinker:3b-preview问题2内存不足# 调整资源限制 kubectl patch statefulset ollama-smallthinker -n ollama-deployment -p {spec:{template:{spec:{containers:[{name:ollama,resources:{limits:{memory:10Gi},requests:{memory:8Gi}}}]}}}}问题3存储空间不足# 扩展PVC kubectl patch pvc ollama-model-pvc -n ollama-deployment -p {spec:{resources:{requests:{storage:30Gi}}}}9. 总结通过本教程你已经在Kubernetes集群中成功部署了SmallThinker-3B-Preview模型的Ollama服务。这个部署方案提供了稳定运行使用StatefulSet保证有状态应用的稳定运行持久化存储模型数据不会因Pod重启而丢失灵活扩展支持水平扩缩容应对不同负载资源隔离合理的资源限制保证集群稳定性监控维护完整的健康检查和日志监控方案SmallThinker-3B-Preview作为一个轻量级模型特别适合在资源受限的环境中部署为边缘计算和快速推理场景提供了优秀的解决方案。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章