英文:
go: randomly terminate pods in kubernetes cluster
问题
我可以帮你翻译这段代码。以下是翻译的结果:
我想用Go语言在Kubernetes集群中随机关闭Pod。我已经编写了代码,可以登录到服务器并运行代码。
现在我需要读取集群中所有可用的Pod,随机选择一些并终止它们。(我对Go语言还不熟悉)
你能帮我完成这个吗?
这是我在集群/服务器上运行命令的方式:
```cli.ExecuteCmd("kubectl get pods")```
// 每个命令使用一个连接。
// 在需要时在客户端中捕获。
func (cli *SSHClient) ExecuteCmd(command string) {
conn, err := ssh.Dial("tcp", cli.Hostname+":22", cli.Config)
if err != nil {
logrus.Infof("%s@%s", cli.Config.User, cli.Hostname)
logrus.Info("提示:将您的密钥添加到ssh代理中:'ssh-add ~/.ssh/id_rsa'")
logrus.Fatal(err)
}
session, _ := conn.NewSession()
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
err = session.Run(command)
if err != nil {
logrus.Fatalf("运行失败:%v", err)
}
logrus.Infof(">%s", stdoutBuf.Bytes())
}
希望对你有帮助!如果你有任何其他问题,请随时提问。
英文:
I would like to randomly shut down pods in a kubernetes cluster with go. I already wrote code, which enables to login to the server and run code.
Now I would need to read all the available pods in the cluster, choose some randomly and terminate them. (I am new to go)
Could you please help me doing this?
This is how I am running commands on the cluster/server
cli.ExecuteCmd("kubectl get pods")
// Use one connection per command.
// Catch in the client when required.
func (cli *SSHClient)ExecuteCmd(command string){
conn, err := ssh.Dial("tcp", cli.Hostname+":22", cli.Config)
if err!=nil {
logrus.Infof("%s@%s", cli.Config.User, cli.Hostname)
logrus.Info("Hint: Add you key to the ssh agent: 'ssh-add ~/.ssh/id_rsa'")
logrus.Fatal(err)
}
session, _ := conn.NewSession()
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
err = session.Run(command)
if err != nil {
logrus.Fatalf("Run failed:%v", err)
}
logrus.Infof(">%s", stdoutBuf.Bytes())
}
答案1
得分: 1
使用k8s.io/client-go
(Github链接)客户端包来列出Kubernetes的Pod,并随机删除它们。
使用client.CoreV1().Pods()方法来列出和删除Pod。
英文:
Use k8s.io/client-go
(Github Link) client package to list kubernetes pods, and then delete them randomly.
Use client.CoreV1().Pods() methods to list and delete pods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论