当前位置:   article > 正文

K8sGPT+Ollama:免费的 Kubernetes 自动化诊断方案

K8sGPT+Ollama:免费的 Kubernetes 自动化诊断方案

周末检查博客草稿,发现了这篇。记得当时是与 Kubernetes 自动化诊断工具:k8sgpt-operator 一起写的,算算过去了一年之久,这拖延症也算是病入膏肓了。原本想使用 K8sGPT + LocalAI 的方案,由于之前试过 Ollama,感觉使用起来也更加友好,而且 Ollama 同样提供了 对 OpenAI API 的支持,索性改成用 Ollama 吧。


介绍 k8sgpt-operator 的文章发布后,有小伙伴反馈 OpenAI 的使用门槛,这个问题确实比较棘手,但也不是不能解决。不过本文并不是介绍如何解决这种问题的,而是介绍 OpenAI 的替代方案: Ollama。

对 k8sgpt 和 k8sgpt-operator 就不做过多介绍了,有兴趣的可以看回 上一篇,去年底 k8sgpt 进入了 CNCF Sandbox

1. 安装 Ollama

Ollama 是一个开源的大模型工具,使用它可以在本地或云端轻松的安装和运行 多种流量的大模型。它的操作非常友好,只需简单的命令就能运行。在 macOS 上可以通过 homebrew 一键安装:

brew install ollama

    当前最新的版本是 0.1.44。

    ollama -v 
    Warning: could not connect to a running Ollama instance
    Warning: client version is 0.1.44
    • 1
    • 2

    在 Linux 上也可以通过官方的脚本一键安装。

    curl -sSL https://ollama.com/install.sh | sh

      启动 Ollama,通过环境变量将 Ollama 的监听地址设置为 0.0.0.0,便于后面从容器或者 K8s 集群访问。

      OLLAMA_HOST=0.0.0.0 ollama start
      
      ...
      time=2024-06-16T07:54:57.329+08:00 level=INFO source=routes.go:1057 msg="Listening on 127.0.0.1:11434 (version 0.1.44)"
      time=2024-06-16T07:54:57.329+08:00 level=INFO source=payload.go:30 msg="extracting embedded files" dir=/var/folders/9p/2tp6g0896715zst_bfkynff00000gn/T/ollama1722873865/runners
      time=2024-06-16T07:54:57.346+08:00 level=INFO source=payload.go:44 msg="Dynamic LLM libraries [metal]"
      time=2024-06-16T07:54:57.385+08:00 level=INFO source=types.go:71 msg="inference compute" id=0 library=metal compute="" driver=0.0 name="" total="21.3 GiB" available="21.3 GiB"
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

      2. 下载并运行大模型

      Llama3 流行的大模型之一,由 Meta 在 4 月开源。Llama3 有两个版本:8B 和 70B。

      我是在 macOS 上运行,所以选择 8B 的版本。8B 的版本大小 4.7 GB,网速快的话 3-4 分钟就可以完成下载。

      ollama run llama3

        我是 m1 pro + 32g 内存,启动 12s 多。

        time=2024-06-17T09:30:25.070+08:00 level=INFO source=server.go:572 msg="llama runner started in 12.58 seconds"

          执行一次 query 的时间在 14s 左右。

          curl http://localhost:11434/api/generate -d '{
            "model": "llama3",
            "prompt": "Why is the sky blue?",
            "stream": false
          }'
          
          ....
          "total_duration":14064009500,"load_duration":1605750,"prompt_eval_duration":166998000,"eval_count":419,"eval_duration":13894579000}
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7

          3. 配置 K8sGPT CLI 后端

          如果你想测试 k8sgpt-operator,可以跳过这一步。

          我们将使用 Ollama REST API 作为 k8sgpt 的后端,作为推理的 provider,这里后端类型选择 localai。因为 LocalAI 与 Ollama 同样兼容 OpenAI API,真正的 provider 还是 Ollama 运行的 Llama。

          k8sgpt auth add --backend localai --model llama3 --baseurl http://localhost:11434/v1

            同时将其设置成默认的 provider。

            k8sgpt auth default --provider localai
            Default provider set to localai
            • 1

            测试:

            我们在 k8s 上创建一个 pod,使用镜像 image-not-exit

            kubectl get po k8sgpt-test
            NAME          READY   STATUS         RESTARTS   AGE
            k8sgpt-test   0/1     ErrImagePull   0          6s
            • 1
            • 2

            使用 k8sgpt 对错误进行分析。

            k8sgpt analyze --explain --filter=Pod --=default --output=json
            
            {
              "provider": "localai",
              "errors": null,
              "status": "ProblemDetected",
              "problems": 1,
              "results": [
                {
                  "kind": "Pod",
                  "name": "default/k8sgpt-test",
                  "error": [
                    {
                      "Text": "Back-off pulling image \"image-not-exist\"",
                      "KubernetesDoc": "",
                      "Sensitive": []
                    }
                  ],
                  "details": "Error: Back-off pulling image \"image-not-exist\"\n\nSolution: \n1. Check if the image exists on Docker Hub or your local registry.\n2. If not, create the image using a Dockerfile and build it.\n3. If the image exists, check the spelling and try again.\n4. Verify the image repository URL in your Kubernetes configuration file (e.g., deployment.yaml).",
                  "parentObject": ""
                }
              ]
            }
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 17
            • 18
            • 19
            • 20
            • 21
            • 22

            4. 部署并配置 k8sgpt-operator

            k8sgpt-operator 可以在集群中开启自动化的 k8sgpt。可以通过 Helm 来安装

            helm repo add k8sgpt https://charts.k8sgpt.ai/
            helm repo update
            helm install release k8sgpt/k8sgpt-operator -n k8sgpt --create-namespace
            • 1
            • 2

            k8sgpt-operator 提供了两个 CRD:K8sGPT 配置 k8sgpt;Result 输出分析结果。

            kubectl api-resources  | grep -i gpt
            k8sgpts                                        core.k8sgpt.ai/v1alpha1                true         K8sGPT
            results                                        core.k8sgpt.ai/v1alpha1                true         Result
            • 1
            • 2

            配置 K8sGPT,这里 baseUrl 要使用 Ollama 的 IP 地址。

            kubectl apply -n k8sgpt -f - << EOF
            apiVersion: core.k8sgpt.ai/v1alpha1
            kind: K8sGPT
            metadata:
              name: k8sgpt-ollama
            spec:
              ai:
                enabled: true
                model: llama3
                backend: localai
                baseUrl: http://198.19.249.3:11434/v1
              noCache: false
              filters: ["Pod"]
              repository: ghcr.io/k8sgpt-ai/k8sgpt
              version: v0.3.8
            EOF
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15

            创建 CR K8sGPT 之后,operator 会自动为其创建 Pod。检查 CR Result 也可以看到同样的结果。

            kubectl get result -n k8sgpt -o jsonpath='{.items[].spec}' | jq .
            {
              "backend": "localai",
              "details": "Error: Kubernetes is unable to pull the image \"image-not-exist\" due to it not existing.\n\nSolution: \n1. Check if the image actually exists.\n2. If not, create the image or use an alternative one.\n3. If the image does exist, ensure that the Docker daemon and registry are properly configured.",
              "error": [
                {
                  "text": "Back-off pulling image \"image-not-exist\""
                }
              ],
              "kind": "Pod",
              "name": "default/k8sgpt-test",
              "parentObject": ""
            }
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12

            关注"云原生指北"微信公众号 (转载本站文章请注明作者和出处乱世浮生,请勿用于任何商业用途)

            声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/772979
            推荐阅读
            相关标签
              

            闽ICP备14008679号