https://github.com/mhausenblas/kn
A collection of shell functions for Kubernetes native dabbling
https://github.com/mhausenblas/kn
cli development kubernetes testing troubleshooting
Last synced: 12 months ago
JSON representation
A collection of shell functions for Kubernetes native dabbling
- Host: GitHub
- URL: https://github.com/mhausenblas/kn
- Owner: mhausenblas
- License: apache-2.0
- Created: 2018-10-22T03:00:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-23T13:32:18.000Z (over 7 years ago)
- Last Synced: 2025-06-23T09:08:08.156Z (12 months ago)
- Topics: cli, development, kubernetes, testing, troubleshooting
- Language: Shell
- Homepage: https://hackernoon.com/meet-kn-a-kubernetes-native-experience-bd87239a11ff
- Size: 23.4 KB
- Stars: 52
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A Kubernetes native shell experience: `kn`
The basic idea behind `kn` is to use the fact that pods in Kubernetes are modelled after machines. That is, the apps running in containers in the pod can communicate with each other via `localhost` and transfer data via volumes they can mount into their own filesystem hierarchy as they see fit.
So, imagine you want to try something out real quick? Do a short iteration using a scripting language such as Python, Ruby, or Node.js? Might want to jump on a container to debug something in-cluster? Run a quick load test? Then, `kn` is for you: it offers a collection of shell functions allowing you to quickly launch a pod, jump into it and have the code and data available you need to carry out your task.
**Note that this tool is meant to be used in dev and test environments. Use at your own risk.**
## Install
Simply Git clone or download this repo from the [release page](https://github.com/mhausenblas/kn/releases/latest), and copy `kn*.sh` somewhere on your path. If you're super fancy, you can set an alias like so: (since I moved the script to `/Users/mhausenblas/bin/`):
```shell
alias kn='/Users/mhausenblas/bin/kn.sh'
```
I've tested `kn` in the Bash shell v3.2 on macOS and Linux. Note that in order to work `kn` requires you to have `kubectl` [installed and configured](https://kubernetes.io/docs/tasks/tools/install-kubectl/). If you also want to use the (optional) feature to expose an environment to the public, you need to have [ngrok](https://ngrok.com/) installed and configured.
## Use
### Config
The following environment variables are used (set global or per invocation):
- `KN_BASE_IMAGE` … set the base image to use; defaults to `centos:7`.
- `KN_SYNC` … if set to `true`, the content of the current directory will be copied into the pod at `/tmp/work`; defaults to `true`.
- `KN_POLICY` … if set to `public`, services will be made available on the public Web using `ngrok`; defaults to `local`.
- `KN_MODE` … if set to `daemon`, the environment is detached and we assume there's some kind of networked service running; defaults to `interactive`.
### Commands
The following commands are available:
- `up [NAME] [PORT]` … creates environment, copies files of current directory unless disabled by `KN_SYNC=false`.
- `connect NAME` … puts you into the running environment.
- `down NAME` … deletes environment, removes all resources associated with it.
- `publish NAME PORT` … publishes the daemonized environment `NAME` by using port-forwarding of `PORT` in the environment (assuming something serves on this port in the container) to port `9898` locally, and, if enabled by `KN_POLICY`, makes it also publicly available using `ngrok`.
- `ls` … lists all resources manged by `kn`.
## Examples
Launching an interactive environment with `kn`:
```shell
## launch interactive environment:
$ kn up
.......
Copied content of /Users/mhausenblas/tmp to /tmp/work in the environment
The environment [sandbox] is now ready!
## list all environments:
$ kn ls
NAME SINCE
sandbox 2018-10-22T10:16:13Z
## jump into the environment:
$ kn connect
connecting to sandbox-64dc6d6bf9-s6gzjsh-4.2#
sh-4.2#
sh-4.2# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:16 ? 00:00:00 sleep 86400
root 27 0 0 10:17 pts/0 00:00:00 sh
root 36 27 0 10:17 pts/0 00:00:00 ps -ef
sh-4.2# exit
exit
## destroy the environment:
$ kn down
The environment [sandbox] has been destroyed, all data is gone the way of the dodo
```
Publishing a daemonized environment using the container image `quay.io/mhausenblas/pingsvc:2` that serves on port `8888`:
```shell
## launch as daemonized env:
$ KN_BASE_IMAGE=quay.io/mhausenblas/pingsvc:2 KN_MODE=daemon kn up psvc 8888
The daemonized environment [psvc] is now ready!
To publish your environment, do: kn publish psvc 8888
## now make it publicly available using ngrok:
$ KN_POLICY=public kn publish psvc 8888
ngrok by @inconshreveable
Session Status online
Account Michael Hausenblas (Plan: Pro)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://0cf727cf.ngrok.io -> localhost:9898
Forwarding https://0cf727cf.ngrok.io -> localhost:9898
Connections ttl opn rt1 rt5 p50 p90
1 0 0.01 0.00 0.30 0.30
HTTP Requests
-------------
GET /ping 200 OK
## in a second terminal session, check if we can get to the service:
$ curl localhost:9898/ping
pong
$ curl http://0cf727cf.ngrok.io/ping
pong
## now we can get rid of the environment:
$ kn down psvc
The environment [psvc] has been destroyed, all data is gone the way of the dodo
```