华为云kubernetes部署deepseek r1、ollama和open-webui(已踩过坑)

1 概述

ollama是一个管理大模型的一个中间层,通过它你可以下载并管理deepseek R1、llama3等大模型。
open-webui是一个web界面(界面设计受到chatgpt启发),可以集成ollama API、 OpenAI的 API。
用常见的web应用架构来类比,open-webui是前端,ollama是后端,大模型是数据库。
在这里插入图片描述

文本介绍华为云kubernetes部署open-webui最新版、ollama最新版、DeepSeek-R1-Distill-Qwen-1.5B(因为小模型可以只使用CPU,节省本文测试的成本)。

2 云资源环境准备

2.1 购买文件存储SFS Turbo

在这里插入图片描述

2.2 购买kubernetes集群

在这里插入图片描述

2.3 在k8s中创建storageclass对象

参数everest.io/share-access-to是VPC的ID。
参数everest.io/share-export-location是sfs turbo实例的共享路径:自定义子目录,sfs turbo实例的共享路径是在sfs实例的详细页查询,自定义子目录可以是任意路径。
参数everest.io/volume-id是sfs turbo实例的ID。
只需要修改以上三个参数。

在本文,storageclass的名称叫做sfsturbo-subpath-sc。

apiVersion: storage.k8s.io/v1
allowVolumeExpansion: true
kind: StorageClass
metadata:
  name: sfsturbo-subpath-sc
mountOptions:
- lock
parameters:
  csi.storage.k8s.io/csi-driver-name: sfsturbo.csi.everest.io
  csi.storage.k8s.io/fstype: nfs
  everest.io/archive-on-delete: "true"
  everest.io/share-access-to: xxxxxxxxxxxxxxxxxx   # VPC ID
  everest.io/share-expand-type: bandwidth
  everest.io/share-export-location: xxxxx.sfsturbo.internal:/mydir   # sfs turbo实例的共享路径:自定义子目录
  everest.io/share-source: sfs-turbo
  everest.io/share-volume-type: STANDARD
  everest.io/volume-as: subpath
  everest.io/volume-id: xxxxxxxxxxxxx   # sfs turbo实例的ID
provisioner: everest-csi-provisioner
reclaimPolicy: Delete
volumeBindingMode: Immediate

在这里插入图片描述

2.4 购买用于暴露容器的负载均衡器ELB

在这里插入图片描述


3 部署

3.1 创建namespace

ollama和open webui都部署在此namespace。

kubectl create ns ollama

3.1 部署ollama

statefulset使用刚刚创建的存储类sfsturbo-subpath-sc。
确保PVC的磁盘容量能存储下所有待下载的大模型。

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ollama
  namespace: ollama
spec:
  serviceName: "ollama"
  replicas: 1
  selector:
    matchLabels:
      app: ollama
  template:
    metadata:
      labels:
        app: ollama
    spec:
      containers:
        - name: ollama
          image: swr.cn-south-1.myhuaweicloud.com/migrator/ollama:0.5.7
          ports:
            - containerPort: 11434
          resources:
            requests:
              cpu: "1000m"
              memory: "2Gi"
              # nvidia.com/gpu: "4"  # 如果要用英伟达GPU,请声明下GPU卡的数量
            limits:
              cpu: "4000m"
              memory: "4Gi"
          volumeMounts:
            - name: ollama-volume
              mountPath: /root/.ollama
          tty: true
  volumeClaimTemplates:
    - metadata:
        name: ollama-volume
      spec:
        storageClassName: sfsturbo-subpath-sc
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 200Gi  # 确保磁盘容量能存储下所有待下载的大模型
---
apiVersion: v1
kind: Service
metadata:
  name: ollama
  namespace: ollama
  labels:
    app: ollama
spec:
  type: ClusterIP
  ports:
    - port: 11434
      protocol: TCP
      targetPort: 11434
  selector:
    app: ollama

在这里插入图片描述


3.1 部署open webui(重点)

  • deployment挂载一个固定的PVC,PVC使用刚刚创建的存储类sfsturbo-subpath-sc。
  • OLLAMA_BASE_URL环境变量是ollama的地址。
  • 无法连接huggingface.co:
    由于在国内环境是无法连接huggingface.co,最终导致open webui的界面是一片空白(应用日志报错:MaxRetryError("HTTPSConnectionPool(host=‘huggingface.co’, port=443)),因此需要增加环境变量HF_ENDPOINT=https://hf-mirror.com。
  • 无法连接openai:
    由于不使用openai,因此将环境变量OPENAI_API_BASE_URL和OPENAI_API_KEY都设置成None,否则open webui在国内环境是无法连接openai,最终导致open webui的界面是一片空白(应用日志报错:Connection error: Cannot connect to host api.openai.com:443)。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: webui-pvc
  namespace: ollama
  labels:
    app: webui
spec:
  storageClassName: sfsturbo-subpath-sc
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webui
  namespace: ollama
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
        - name: webui
          image: swr.cn-south-1.myhuaweicloud.com/migrator/open-webui:main
          env:
            - name: OLLAMA_BASE_URL             # 这是ollama的地址
              value: http://ollama:11434            
            - name: HF_ENDPOINT                 # 国内环境无法连接huggingface.co
              value: https://hf-mirror.com
            - name: OPENAI_API_KEY
              value: None
            - name: OPENAI_API_BASE_URL
              value: None
          tty: true
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: "500m"
              memory: "500Mi"
            limits:
              cpu: "1000m"
              memory: "1Gi"
          volumeMounts:
            - name: webui-volume
              mountPath: /app/backend/data
      volumes:
        - name: webui-volume
          persistentVolumeClaim:
            claimName: webui-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: webui
  namespace: ollama
  labels:
    app: webui
spec:
  type: ClusterIP
  ports:
    - port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: webui

在这里插入图片描述
接着为open webui容器添加ingress路由以在公网暴露:
在这里插入图片描述

4 下载模型

进入ollama容器:

kubectl exec -it ollama-0 -n ollama bash

在容器内执行ollama pull命令下载大模型DeepSeek-R1-Distill-Qwen-1.5B。

nohup ollama pull deepseek-r1:1.5b &
tail -f nohup.out

有哪些deepseek模型可以下载,请去https://ollama.com/library/deepseek-r1地址里搜索。


5 与大模型对话

在浏览器地址输入负载均衡器ELB的公网IP,打开网页后需要先设置open webui的管理员账号密码,登录成功后即可选择刚刚下载的deepseek模型来聊天。
在这里插入图片描述

6 小结

文本介绍使用华为云kubernetes部署open-webui最新版、ollama最新版、DeepSeek-R1-Distill-Qwen-1.5B。在实际过程中,花费时间最多的是open-webui,因为它默认去访问在国内无法访问的两个外国地址:huggingface.co和api.openai.com,而访问这些地址最终又导致界面变成空白。

### 探索本地部署DeepSeek的替代方法 除了Ollama之外,存在多种方式可以实现在本地环境中部署DeepSeek。一种常见的做法是利用Docker容器化技术来简化安装过程并确保环境一致性[^1]。 #### 使用Docker进行部署 通过创建特定于DeepSeek应用的Docker镜像文件(`Dockerfile`),能够封装所有必要的依赖项以及配置设置。这使得任何拥有Docker运行时环境的人都能轻松启动预配置的应用实例而无需担心兼容性问题。 ```dockerfile FROM python:3.9-slim-buster WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "deepseek_app.py"] ``` 上述示例展示了如何基于Python 3.9版本构建一个精简的基础映像,并复制项目所需的包列表到工作目录内完成软件栈搭建;最后将整个应用程序源码拷贝进去准备执行入口脚本。 #### 利用虚拟机(VM) 对于那些偏好传统操作系统级别的隔离方案来说,在物理硬件上或者云平台上建立专门用于承载DeepSeek服务的Linux/Windows Server类型的虚拟机会是一个不错的选择。这种方法允许更细粒度的安全策略实施同时也便于管理维护资源分配情况。 #### Kubernetes集群中的Pods 如果组织已经具备Kubernetes基础设施,则考虑把DeepSeek作为微服务的一部分纳入现有架构体系之中不失为明智之举。借助Helm图表工具定义复杂的服务拓扑结构及其关联组件之间的关系变得异常简单快捷。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: deepseek-deployment spec: replicas: 2 selector: matchLabels: app: deepseek-app template: metadata: labels: app: deepseek-app spec: containers: - name: deepseek-container image: myrepo/deepseek-image:latest ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: deepseek-service spec: type: LoadBalancer selector: app: deepseek-app ports: - protocol: TCP port: 80 targetPort: 8080 ``` 此YAML片段描述了一个简单的Kubernetes部署服务对象组合,它指定了两个副本集规模下的pod布局模式连同对外暴露HTTP端口的具体细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值