首页> 教程 > 绝了!k3s

绝了!k3s

时间:2025-04-24 11:28:46 编辑:liun
wrap content ">

在容器编排领域,k3s (k8s) 无疑是备受关注的存在。本文重点介绍如何在 k3s (k8s) 环境中安装 ollama,并运行 deepseek。首先,我们需要关注一个关键的 yaml 文件 —— ollama.yaml。这个文件如同整个部署流程的指挥棒,规定了各项参数和配置信息。ollama.yaml 的内容如下:

# https://cloud.tencent.com/developer/article/2495842 环境变量
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ollama
  name: ollama
  namespace: moonfdd
spec:
  strategy:
    type: Recreate
  replicas: 1
  selector:
    matchLabels:
      app: ollama
  template:
    metadata:
      labels:
        app: ollama
    spec:
      containers:
        - env:
            - name: OLLAMA_HOST
              value: "0.0.0.0"
            - name: OLLAMA_PORT
              value: "11434"
            - name: OLLAMA_NUM_PARALLEL
              value: "20" # 默认是1
            - name: OLLAMA_ORIGINS
              value: "*"
            - name: OLLAMA_MODELS
              value: "/root/.ollama/models"
          image: 'ollama/ollama:0.5.12'
          command: ["ollama", "serve"]
          imagePullPolicy: IfNotPresent
          name: ollama
          volumeMounts:
            - mountPath: /root/.ollama/models/
              name: data
          resources:
            # nvidia.com/gpu: 1
            # memory: "24Gi"
            # limits:
            #   cpu: 200m
            #   memory: 200Mi
            # requests:
            #   cpu: 100m
            #   memory: 100Mi
        - image: 'ollama/ollama:0.5.12'
          command: ["sh", "-c", "while true; do ollama run deepseek-r1:1.5b; sleep 5; done"]
          imagePullPolicy: IfNotPresent
          name: ollamacmd
      volumes:
        - name: data
          hostPath:
            path: /root/k8s/moonfdd/ollama/root/.ollama/models/
            type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: ollama
  name: ollama
  namespace: moonfdd
spec:
  ports:
    - name: 11434-11434
      port: 11434
      protocol: TCP
      targetPort: 11434
      nodePort: 11434
  selector:
    app: ollama
  type: NodePort
登录后复制

?ollama 安装与运行实际效果展示

安装和运行 ollama 的命令如下:

kubectl apply -f ollama.yaml
登录后复制

运行结果如下:

?Go 语言调用 deepseek 接口代码分析

仅仅运行 ollama 环境还不够,还需要有与之交互的代码来发挥其功能。这里展示了 Go 语言调用 deepseek 接口的代码:

package main
<p>import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)</p><p>type Message struct {
Role    string json:"role"
Content string json:"content"
}</p><p>// 修改请求结构体,添加 Stream 字段
type DeepSeekRequest struct {
Model       string    json:"model"
Messages    []Message json:"messages"
MaxTokens   int       json:"max_tokens,omitempty"
Temperature float64   json:"temperature,omitempty"
Stream      bool      json:"stream" // 新增流式控制字段
}</p><p>// 保持其他结构体不变...</p><p>const (
localAPIURL = "<a href="https://www.php.cn/link/d50b2d3afe5ec033d560dd1318b2707b">https://www.php.cn/link/d50b2d3afe5ec033d560dd1318b2707b</a>"
)</p><p>func main() {
fmt.Println("开始")
requestData := DeepSeekRequest{
Model: "deepseek-r1:1.5b",
Messages: []Message{
{
Role:    "user",
Content: "微信公众号 福大大架构师每日一题 是谁",
},
},
MaxTokens:   512000,
Temperature: 1,
Stream:      true, // 启用流式模式
}
requestBody, err := json.Marshal(requestData)
if err != nil {
panic("JSON编码失败: " + err.Error())
}</p><pre class="brush:php;toolbar:false">// 配置更合理的超时时间
client := &http.Client{
    Transport: &http.Transport{
        DisableKeepAlives: true, // ? 关键设置:禁用连接复用
        MaxIdleConns:      1,
        IdleConnTimeout:   30 * time.Second,
    },
    Timeout: 10 * time.Minute, // 大模型响应时间较长
}

req, err := http.NewRequest("POST", localAPIURL, bytes.NewBuffer(requestBody))
if err != nil {
    panic("创建请求失败: " + err.Error())
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Connection", "close") // 显式关闭连接
req.Close = true

resp, err := client.Do(req)
if err != nil {
    panic("请求发送失败: " + err.Error())
}
defer resp.Body.Close()

// 流式响应处理
if resp.StatusCode != http.StatusOK {
    fmt.Printf("请求失败,状态码:%d
", resp.StatusCode)
    return
}

// 使用 Scanner 逐行读取流式响应
scanner := bufio.NewScanner(resp.Body)
scanner.Buffer(make([]byte, 1024), 10*1024*1024) // 扩大缓冲区
for scanner.Scan() {
    rawData := scanner.Bytes()
    if len(rawData) == 0 {
        continue
    }
    var chunk DeepSeekChunk
    err := json.Unmarshal(rawData[6:], &chunk)
    if err == nil {
        for i := 0; i < len(chunk.Choices); i++ {
            if chunk.Choices[i].Delta.Content != "" {
                fmt.Print(chunk.Choices[i].Delta.Content)
            }
        }
    }
}

if err := scanner.Err(); err != nil {
    fmt.Printf("读取响应时发生错误: %v
", err)
}
登录后复制

}

type DeepSeekChunk struct { Choices []struct { Delta struct { Content string json:"content" } json:"delta" } json:"choices" }

运行结果如下:

官网运行结果如下:

官网不联网运行结果如下:

?实际意义与展望

通过在 k3s (k8s) 上完成 ollama 的安装并运行 deepseek,以及编写 Go 语言调用接口代码这一系列操作,具有多方面的实际意义和深远的展望。从技术层面而言,这为开发者在特定的容器编排环境下集成模型服务提供了一套可参考的方法和实践经验。无论是对于后续想要在相似环境里部署其他模型,还是改进和优化当前模型的运行方式,都提供了宝贵参考范例。从应用场景角度来看,能够在这样的技术栈下调用模型进行文本处理、问答交互等,都能为诸多实际项目开发提供强大助力。比如开发智能客服系统、智能助手应用等。展望未来,这种技术实践将不断推动相关技术的发展和融合。随着模型的不断升级迭代,我们可以期待更多强大功能能够被整合进这样的环境里。同时,通过持续改进和优化 yaml 文件配置以及代码实现细节,将进一步提升系统的性能和稳定性。也相信会有更多开发者基于此进行创新和拓展,探索出更多的应用可能和技术思路。就像一颗石子投入平静湖面,会泛起层层涟漪般,这项技术实践也将在整个技术领域里引发新的探索和变革浪潮。

天晴下载,不仅是一个下载站,如果这篇教程文章对您有所助益,请记得分享给更多朋友。未来探索,我们一路同行!

相关文章

相关软件