背景 在Kubernetes上,从部署Deployment到正常提供服务,整个流程可能会出现各种各样问题,有兴趣的可以浏览KubernetesDeployment的故障排查可视化指南(2021中文版)〔1〕。从可视化指南也可能看出这些问题实际上都是有迹可循,根据错误信息基本很容易找到解决方法。随着ChatGPT的流行,基于LLM的文本生成项目不断涌现,k8sgpt〔2〕便是其中之一。 k8sgpt是一个扫描Kubernetes集群、诊断和分类问题的工具。它将SRE经验编入其分析器,并通过AI帮助提取并丰富相关的信息。 其内置了大量的分析器: podAnalyzer pvcAnalyzer rsAnalyzer serviceAnalyzer eventAnalyzer ingressAnalyzer statefulSetAnalyzer deploymentAnalyzer cronJobAnalyzer nodeAnalyzer hpaAnalyzer(可选) pdbAnalyzer(可选) networkPolicyAnalyzer(可选) k8sgpt的能力是通过CLI来提供的,通过CLI可以对集群中的错误进行快速的诊断。 k8sgptanalyzeexplainfilterPodnamespacedefaultoutputjson{status:ProblemDetected,problems:1,results:〔{kind:Pod,name:defaulttest,error:〔{Text:Backoffpullingimageflomeshpipy2,Sensitive:〔〕}〕,details:TheKubernetessystemisexperiencingdifficultypullingtherequestedimagenamedflomeshpipy2。nnThesolutionmaybetocheckthattheimageiscorrectlyspelledortoverifythatitexistsinthespecifiedcontainerregistry。Additionally,ensurethatthenetworkinginfrastructurethatconnectsthecontainerregistryandKubernetessystemisworkingproperly。Finally,checkifthereareanyaccessrestrictionsorcredentialsrequiredtopulltheimageandensuretheyareprovidedcorrectly。,parentObject:test}〕} 但是,每次进行诊断都要执行命令,有点繁琐且限制较多。我想大家想要的肯定是能够监控到问题并自动诊断。这就有了今天要介绍的k8sgptoperator〔3〕 介绍 简单来说k8sgptoperator可以在集群中开启自动化的k8sgpt。它提供了两个CRD:K8sGPT和Result。前者可以用来设置k8sgpt及其行为;而后者则是用来展示问题资源的诊断结果。 apiVersion:core。k8sgpt。aiv1alpha1kind:K8sGPTmetadata:name:k8sgptsamplenamespace:kubesystemspec:model:gpt3。5turbobackend:openainoCache:falseversion:v0。2。7enableAI:truesecret:name:k8sgptsamplesecretkey:openaiapikey 演示 实验环境使用k3s集群。 exportINSTALLK3SVERSIONv1。23。8k3s2curlsfLhttps:get。k3s。ioshsdisabletraefikdisablelocalstoragedisableservicelbwritekubeconfigmode644writekubeconfig。kubeconfig 安装k8sgptoperator helmrepoaddk8sgpthttps:charts。k8sgpt。aihelmrepoupdatehelminstallreleasek8sgptk8sgptoperatornopenaicreatenamespace 安装完成后,可以看到随operator安装的两个CRD:k8sgpts和results。 kubectlapiresourcesgrepigptk8sgptscore。k8sgpt。aiv1alpha1trueK8sGPTresultscore。k8sgpt。aiv1alpha1trueResult 在开始之前,需要先生成一个OpenAI的key〔4〕,并保存到secret中。 OPENAITOKENxxxxkubectlcreatesecretgenerick8sgptsamplesecretfromliteralopenaiapikeyOPENAITOKENnopenai 接下来创建K8sGPT资源。 kubectlapplynopenaifEOFapiVersion:core。k8sgpt。aiv1alpha1kind:K8sGPTmetadata:name:k8sgptsamplespec:model:gpt3。5turbobackend:openainoCache:falseversion:v0。2。7enableAI:truesecret:name:k8sgptsamplesecretkey:openaiapikeyEOF 执行完上面的命令后在openai命名空间下会自动创建Deploymentk8sgptdeployment。 测试 使用一个不存在的镜像创建pod。 kubectlruntestimageflomeshpipy2ndefault 然后在openai命名空间下会看到一个名为defaulttest的资源。 kubectlgetresultnopenaiNAMEAGEdefaulttest5m7s 详细信息中可以看到诊断内容以及出现问题的资源。 kubectlgetresultnopenaidefaulttestoyamlapiVersion:core。k8sgpt。aiv1alpha1kind:Resultmetadata:creationTimestamp:20230502T09:00:32Zgeneration:1name:defaulttestnamespace:openairesourceVersion:1466uid:2ee27c2661c14ef5ae27e1301a40cd56spec:details:TheerrormessageisindicatingthatKubernetesishavingtroublepullingtheimageflomeshpipy2andisthereforebackingofffromtryingtodoso。nnThesolutiontothisissuewouldbetocheckthattheimageexistsandthatthespellingandsyntaxoftheimagenameiscorrect。Additionally,checkthattheimageisaccessiblefromtheKubernetesclusterandthatanyrequiredauthenticationorauthorizationisinplace。Iftheissuepersists,itmaybenecessarytotroubleshootthenetworkconnectivitybetweentheKubernetesclusterandtheimagerepository。error:text:Backoffpullingimageflomeshpipy2kind:Podname:defaulttestparentObject:test 参考资料 〔1〕 KubernetesDeployment的故障排查可视化指南(2021中文版):https:atbug。comtroubleshootingkubernetesdeploymentzhv2 〔2〕 k8sgpt:https:github。comk8sgptaik8sgpt 〔3〕 k8sgptoperator:https:github。comk8sgptaik8sgptoperator 〔4〕 OpenAI的key:https:platform。openai。comaccountapikeys