在 Kubernetes 集群的 GPU 节点上部署 Ollama

在前一篇流水账博文中已经记录过 k8s 集群上部署 ollama,在这篇博文中重新整理记录一下。

准备 GPU 服务器

准备 GPU 节点服务器,这里准备的是阿里云共享型 GPU 实例 sgn7i,8核31G,4G显存。

操作系统用的是 Ubuntu 20.04,容器环境用的是 containerd,没有使用 docker,主机名是 kube-gpu-8c31g。

安装 GPU 驱动

安装 GPU 驱动,这里用的是阿里云共享型 GPU实例,通过下面的命令安装 GRID 驱动。

acs-plugin-manager --exec --plugin grid_driver_install

通过 nvidia-smi 命令确认驱动是否安装成功

安装容器 GPU 环境

安装与配置 NVIDIA Container Toolkit⁠

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \
    | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
    | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
    | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=containerd --set-as-default=true
sudo systemctl restart containerd

上面的命令中需要注意2个命令行参数:

  • --runtime=containerd: 这台服务器的容器环境没有使用 docker,用的是 containerd
  • --set-as-default=true:默认就使用 GPU 环境启动容器

通过下面的命令启动容器确认 nvidia container toolkit 是否正常工作

nerdctl run -it --rm --gpus all nvidia/cuda:12.3.1-base-ubuntu20.04 nvidia-smi

将 GPU 服务器加入 Kubernetes 集群

将节点服务器加入到 k8s 集群

kubeadm join k8s-api:6443 --token xxx --discovery-token-ca-cert-hash yyy

给这个节点打上 taint

kubectl taint nodes kube-gpu-8c31g nvidia.com/gpu=:NoSchedule

准备 DeepSeek 模型文件

节点服务器上准备模型文件,保存在 /root/.ollama 中,可以安装 ollama 然后拉取模型文件,这里从一个容器镜像中复制 deepseek-r1:7b 模型文件

nerdctl run -it -rm ollama-deepseek-r1-7b
nerdctl cp 4009ebafda61:/root/.ollama .

Helm 部署 ollama

创建专门的 k8s namespace

kubectl create namespace ai

添加 ollama-helm helm repository

helm repo add ollama-helm https://otwld.github.io/ollama-helm/
helm repo update

准备 helm 清单文件 ollama-deepseek-values.yaml

image:
  repository: ollama/ollama
  tag: latest

tolerations:
- effect: NoSchedule
  key: nvidia.com/gpu
  operator: Exists

podLabels:
  app: cnblogs-ollama

ollama:
  mountPath: "/root/.empty"

volumeMounts:
- mountPath: /root/.ollama
  name: ollama-data-local

volumes:
- name: ollama-data-local
  hostPath:
    path: /root/.ollama
    type: Directory

运行下面的 helm 命令进行部署

helm upgrade -i --namespace ai ollama-deepseek ollama-helm/ollama --values ollama-deepseek-values.yaml

确认 pod 是否成功运行

# kubectl get pods -n ai        
NAME                               READY   STATUS    RESTARTS   AGE
ollama-deepseek-578cbc98dd-wjk9z   1/1     Running   0          55s

运行 deepseek-r1:7b 模型

执行下面的命令在容器内用 ollama 运行 deepseek-r1:7b 模型

kubectl exec -it deployment/ollama-deepseek -n ai -- ollama run deepseek-r1:7b 

然后就可以通过命令行与 deepseek-r1:7b 模型进行对话了

>>> Which model are you using?
<think>
Greetings! I'm DeepSeek-R1, an artificial intelligence assistant created by DeepSeek. I'm at your service and would be delighted to assist you with any inquiries or tasks you may have.
</think>

Greetings! I'm DeepSeek-R1, an artificial intelligence assistant created by DeepSeek. I'm at your service and would be delighted to assist you with any inquiries or tasks you may have.

>>> 

部署完成。

posted @ 2025-02-20 17:41  dudu  阅读(362)  评论(0)    收藏  举报