琦彦,与你一起探讨应用迁移,GitOps,二次开发,解决方案,CNCF生态,及生活况味。
kubectl-ai 项目是一个kubectl
使用 OpenAI GPT 生成和应用 Kubernetes 清单的插件。
安装kubectl-ai
brew方式
添加以brew
点击并安装:
brew tap sozercan/kubectl-ai https://github.com/sozercan/kubectl-ai
brew install kubectl-ai
Krew方式
krew索引添加kubectl-ai并安装:
kubectl krew index add kubectl-ai https://github.com/sozercan/kubectl-ai
kubectl krew install kubectl-ai/kubectl-ai
执行过程如下
# krew索引添加kubectl-ai
$ kubectl krew index add kubectl-ai https://github.com/sozercan/kubectl-ai
WARNING: You have added a new index from "https://github.com/sozercan/kubectl-ai"
The plugins in this index are not audited for security by the Krew maintainers.
Install them at your own risk.
# krew安装kubectl-ai
$ kubectl krew install kubectl-ai/kubectl-ai
Updated the local copy of plugin index.
Updated the local copy of plugin index "kubectl-ai".
Installing plugin: kubectl-ai
Installed plugin: kubectl-ai
\
| Use this plugin:
| kubectl kubectl-ai
| Caveats:
| \
| | This plugin requires an OpenAI key.
| /
/
二进制方式
- 从GitHub releases下载二进制文件。
- 如果你想将其用作
kubectl
插件,请将kubectl-ai
二进制文件复制拷贝到你的PATH
文件夹。如果不需要将其用作kubectl
插件,你也可以独立使用kubectl-ai
二进制文件。
配置kubectl-ai
先决条件
kubectl-ai
需要OpenAI API密钥,或者Azure OpenAI服务的API密钥和端点,以及有效的Kubernetes配置。
对于 OpenAI 和 Azure OpenAI,你可以使用以下环境变量:
export OPENAI_API_KEY=<your OpenAI key>
export OPENAI_API_KEY=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
export OPENAI_DEPLOYMENT_NAME=<your OpenAI deployment/model name. defaults to "gpt-3.5-turbo">
支持以下OpenAI模型:
code-davinci-002
text-davinci-003
-
gpt-3.5-turbo-0301
(Azure须命名为gpt-35-turbo-0301
) gpt-3.5-turbo
gpt-35-turbo-0301
gpt-4-0314
gpt-4-32k-0314
对于 Azure OpenAI 服务,你可以使用以下环境变量:
export AZURE_OPENAI_ENDPOINT=<your Azure OpenAI endpoint, like "https://my-aoi-endpoint.openai.azure.com">
如果设置了AZURE_OPENAI_ENDPOINT
变量,则将使用Azure OpenAI服务。否则,它将使用OpenAI API。
标志和环境变量
- 可以设置
-require-confirmation
标志,或者REQUIRE_CONFIRMATION
环境变量,以在应用清单之前提示用户进行确认。默认为true。 - 可以将
-temperature
标志,或者TEMPERATURE
环境变量设置在0到1之间。较高的temperature将会生成更创意性的结果。较低的temperature表示将会生成更确定性的结果。默认为0。
使用kubectl-ai
创建指定值的资源清单
Deployment 是 Kubernetes 中一种用于管理 pod 副本数量和升级的资源类型。Deployment 通过创建 ReplicaSet 控制 pod 的副本数,并提供了滚动更新功能,可以实现无宕机升级。
首先,我们发出create an nginx deployment with 3 replicas
指令,
$ kubectl kubectl-ai "create an nginx deployment with 3 replicas ,and create an servie"
✨ Attempting to apply the following manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Reprompt/Apply/Don't Apply]:
+ Reprompt
▸ Apply
Don't Apply
这时候我们看到最后的提示Would you like to apply this?,表示你想应用这个资源清单吗?一共有三个选项
- Reprompt:重新提示以修改资源清单
- Apply :直接应用这个资源清单
- Don’t Apply:不应用资源清单
通过上下箭头,我们可以切换选择响应的选项,并通过执行回车键来执行。
重新提示以修改你的资源清单
...
$ Reprompt: update to 5 replicas and port 6080 and Service type is NodePort
✨ Attempting to apply the following manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 5
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 6080
targetPort: 80
selector:
app: nginx
sessionAffinity: None
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Reprompt/Apply/Don't Apply]:
+ Reprompt
▸ Apply
Don't Apply
执行成功的提示如下
✔ Apply
如果执行失败,可能提示如下。笔者这里之前允许过一个同名的nginx-deployment资源,与kubectl-ai生成的资源清单有冲突,所以提示如下。
✔ Apply
Error: Apply failed with 2 conflicts: conflicts with "kubectl-client-side-apply" using apps/v1:
- .spec.replicas
- .spec.template.spec.containers[name="nginx"].image
验证资源状态
kubectl get all -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-5d59d67564-9hxsx 1/1 Running 0 6m14s 10.244.39.38 master-1 <none> <none>
pod/nginx-deployment-5d59d67564-k7jr2 1/1 Running 0 6m14s 10.244.39.59 master-1 <none> <none>
pod/nginx-deployment-5d59d67564-n4pw4 1/1 Running 0 6m14s 10.244.39.35 master-1 <none> <none>
pod/nginx-deployment-5d59d67564-s8z8v 1/1 Running 0 6m14s 10.244.39.23 master-1 <none> <none>
pod/nginx-deployment-5d59d67564-zw6nw 1/1 Running 0 6m14s 10.244.39.51 master-1 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 290d <none>
service/nginx-service NodePort 10.102.192.38 <none> 6080:30562/TCP 6m13s app=nginx
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 5/5 5 5 6m14s nginx nginx:1.7.9 app=nginx
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-5d59d67564 5 5 5 6m14s nginx nginx:1.7.9 app=nginx,pod-template-hash=5d59d67564
验证nginx服务是否正常
$ curl 10.102.192.38:6080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
输出了Thank you for using nginx.
,表示服务是正常的。
一次创建多个对象
$ kubectl ai "create a foo namespace then create nginx pod in that namespace"
✨ Attempting to apply the following manifest:
apiVersion: v1
kind: Namespace
metadata:
name: foo
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: foo
spec:
containers:
- name: nginx
image: nginx:latest
Use the arrow keys to navigate: ↓ ↑ → ←
? Would you like to apply this? [Reprompt/Apply/Don't Apply]:
+ Reprompt
▸ Apply
Don't Apply
--require-confirmation
标志
$ kubectl ai "create a service with type LoadBalancer with selector as 'app:nginx'" --require-confirmation=false
✨ Attempting to apply the following manifest:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancer
请注意,插件还不知道集群的当前状态,所以它总是会生成完整的清单。
——————————————————————————————————————————————————————————————————————————————————————————————
琦彦,与你一起探讨应用迁移,GitOps,二次开发,解决方案,CNCF生态,及生活况味。
参考
[2] DevOps Use Cases for AI-Assisted Kubernetes