https://github.com/engineerd/kube-exec
Lightweight Golang package for executing commands in remote Kubernetes pods
https://github.com/engineerd/kube-exec
Last synced: 10 months ago
JSON representation
Lightweight Golang package for executing commands in remote Kubernetes pods
- Host: GitHub
- URL: https://github.com/engineerd/kube-exec
- Owner: engineerd
- License: mit
- Created: 2018-01-06T13:50:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-22T06:24:14.000Z (about 7 years ago)
- Last Synced: 2025-03-16T02:08:58.849Z (about 1 year ago)
- Language: Go
- Homepage: https://engineerd.github.io/kube-exec/introduction/
- Size: 511 KB
- Stars: 93
- Watchers: 1
- Forks: 7
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
kube-exec
=========
[](https://circleci.com/gh/engineerd/kube-exec)
[](https://goreportcard.com/report/github.com/engineerd/kube-exec)
[](https://godoc.org/github.com/engineerd/kube-exec)
`kube-exec` is a library similar to [`os/exec`][1] that allows you to run commands in a Kubernetes pod, as if that command was executed locally.
> It is inspired from [`go-dexec`][2] by [ahmetb][3], which does the same thing, but for a Docker engine.
The interface of the package is similar to `os/exec`, and essentially this:
- creates a new pod in Kubernetes based on a user-specified image
- waits for the pod to be in [`Running`](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/) state
- attaches to the pod and allows you to stream data to the pod through `stdin`, and from the pod back to the program through `stdout` and `stderr`
How to use it
-------------
```go
cfg := kube.Config{
Kubeconfig: os.Getenv("KUBECONFIG"),
Image: "ubuntu",
Name: "kube-example",
Namespace: "default",
}
cmd := kube.Command(cfg, "/bin/sh", "-c", "sleep 2; echo Running from Kubernetes pod;")
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatalf("error: %v", err)
}
```
Here's a list of full examples you can find in this repo:
- [simple hello example](/examples/hello/main.go)
- [pass `stdin` to the pod](/examples/stdin/main.go)
- [pass Kubernetes secrets as environment variables](/examples/secrets/main.go)
[1]: https://golang.org/pkg/os/exec
[2]: https://github.com/ahmetb/go-dexec
[3]: https://twitter.com/ahmetb
[4]: /examples/main.go