在Kubernetes集群中随机终止Pods。

huangapple go评论101阅读模式
英文:

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-goGithub链接)客户端包来列出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.

huangapple
  • 本文由 发表于 2017年7月12日 16:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/45051875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定