3

Is there a possibility to deploy a kubernetes Pod using golang code instead of command line Linux with kubectl ?

1

2 Answers 2

13

Yes. Definitely you can. Kubernetes is written in golang. It has official client for go. You can create,delete,patch,list, control almost any Kubernetes resources with this go client.

Here is the official client repository: kubernetes/client-go

I have create a sample repository here to show how to create a pod using client-go.

You have to do following things to create a pod with this client. We are going to create a simple busybox pod.

  1. Create configuration using your kube-config file. Generally the configuration file is $HOME/.kube/config file. See example here.
  2. Create a clientset using this configuration. See example here.
  3. Now, generate a pod definition that we want to deploy. See example here.
  4. Finally, create the pod in kubernetes cluster using the clientset. See example here.
Sign up to request clarification or add additional context in comments.

Comments

-3

You can write a function and give the cell image yaml file as a parameter.

func cellDeploy(pathToFileName string) error {
    cmd := exec.Command("kubectl", "apply", "-f", pathToFileName)
    stdoutReader, _ := cmd.StdoutPipe()
    stdoutScanner := bufio.NewScanner(stdoutReader)
    go func() {
        for stdoutScanner.Scan() {
            fmt.Println(stdoutScanner.Text())
        }
    }()
    stderrReader, _ := cmd.StderrPipe()
    stderrScanner := bufio.NewScanner(stderrReader)
    go func() {
        for stderrScanner.Scan() {
            fmt.Println(stderrScanner.Text())
        }
    }()
    err := cmd.Start()
    if err != nil {
        fmt.Printf("Error : %v \n", err)
        os.Exit(1)
    }
    err = cmd.Wait()
    if err != nil {
        fmt.Printf("Error: %v \n", err)
        os.Exit(1)
    }

    return nil
}

1 Comment

This is still just simply using golang to call kubectl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.