前情提要
前面两篇分别讲了如何使用结合本地大模型使用K8sGPT
K8sGPT+Ollama Kubernetes 智能诊断助手
以及 K8sGPT源码分析
对于机器性能不足以本地部署大模型以及没有网络条件使用 openai的 api 的同学可以参考官方https://github.com/k8sgpt-ai/k8sgpt/issues/990 使用自定义大模型,本文也是参考这个 issue实验所得。这里我以DeepSeek为例,其他国产大模型也是类似方法。
DeepSeek
注册
网址如下,目前送 10 元额度,个人使用免费额度肯定是够用了。
https://platform.deepseek.com/
申请 apikey
https://platform.deepseek.com/api_keys
申请后是一串 sk-开头的字符串记得保存好,后面会需要用到
- API key:sk-xxxxxx
查看 api文档
https://api-docs.deepseek.com/zh-cn/
这里需要记一下关键参数后面会用到
- base_url : https://api.deepseek.com
- model: deepseek-chat
K8sGPT
安装
这里以 mac 为例,其他方式可参考https://docs.k8sgpt.ai/getting-started/installation/
brew install k8sgpt
新增后端模型
这里用到的一些参数就是前文需要留存的参数,对应关系如下:
- model : deepseek-chat
- password : sk-xxxxxx
- baseurl : https://api.deepseek.com
k8sgpt auth add --backend localai --model deepseek-chat --password sk-xxxxxx --baseurl https://api.deepseek.com
如何之前新增过,那么可以使用更新命令
k8sgpt auth update --backend localai --model deepseek-chat --password sk-xxxxxx --baseurl https://api.deepseek.com
设置默认 provider,那么K8sGPT分析时默认就会用这个 provider 的大模型,否则需要增加参数进行指定
k8sgpt auth default --provider localai
测试
添加一个镜像错误
kubectl create deployment demo1 --image=httpd44444 --port=80
开始分析
k8sgpt analyze --explain --filter=Pod -n=default
这里就给出了发生错误的信息和一些解决方案
100% |███████████████████████████████████████████████| (1/1, 735 it/s)
AI Provider: localai
0: Pod default/demo1-594db5c476-pslgz(Deployment/demo1)
- Error: Back-off pulling image "httpd44444"
Error: Back-off pulling image "httpd44444"
Solution:
1. Check if the image exists on Docker Hub or your registry.
2. Verify the image name and tag are correct.
3. Run docker pull httpd44444
to test the image pull.
4. If still failing, check network connectivity and firewall rules.
5. Update your Kubernetes configuration with the correct image path.
中文支持
这里输出还是英文的,官方提供了一个支持中文输出的参数 -l 可选 Chinese,但是实际测试返回仍为英文输出,
所以如果需要中文输出最好修改提示词模版新增中文输出的字样,
具体在pkg/analysis/analysis.go内新增这一行即可
// Process template.
promptTmpl += " 请用中文输出"
然后执行编译
make build
编译后的二进制文件路径在代码仓库 bin/k8sgpt
我们再测试下分析命令看下效果,为了防止缓存带来的影响这里务必增加-c 参数
bin/k8sgpt analyze --explain --filter=Pod -n=default -c
我们再来看下输出
100% |██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| (1/1, 20 it/min)
AI Provider: localai
0: Pod default/demo1-594db5c476-87fnl(Deployment/demo1)
- Error: failed to pull and unpack image "docker.io/library/httpd44444:latest": failed to resolve reference "docker.io/library/httpd44444:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
Error: 无法拉取和解压镜像 "docker.io/library/httpd44444:latest",原因是镜像不存在、访问被拒绝或需要授权。
Solution:
1. 检查镜像名称是否正确。
2. 确认镜像仓库是否存在。
3. 确保有访问权限或提供正确的凭证。
4. 尝试拉取官方镜像 "httpd:latest"。
中文支持修改的代码可见我 fork 的仓库下的 cn 分支
https://github.com/jaychenthinkfast/k8sgpt/tree/cn