Customize Kubernetes Change Context (KUBECONFIG)
_ ___ _ _
| | _( _ ) ___ ___ ___ _ __ | |_ _____ _| |_
| |/ / _ \/ __|_____ / __/ _ \| '_ \| __/ _ \ \/ / __|
| < (_) \__ \_____| (_| (_) | | | | || __/> <| |_
|_|\_\___/|___/ \___\___/|_| |_|\__\___/_/\_\\__|
===================================
[[ K8S-CONTEXT (K8C) ]] - v1.1.9
===================================
Usage:
k8c [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
get Get Kubernetes resources (ns, svc, deploy, po)
help Help about any command
list List all available Kubernetes contexts
load Load a kubeconfig file
merge Merge multiple kubeconfig files
show Describe / show kubernetes resources (po, logs, port, node)
switch Switch to different context
version Print the version number of k8s-context
Flags:
-h, --help help for k8s-context
--kubeconfig string Path to kubeconfig file (default "/home/devopscorner/.kube/config")
Use "k8c [command] --help" for more information about a command.
-
Clone this repository
git clone https://github.com/devopscorner/k8s-context.git -- or -- git clone git@github.com:devopscorner/k8s-context.git
-
Run Initialize
make init -- or -- cd src go mod tidy
-
Set Environment
export GO_APP=k8s-context export SOURCES=$(sh find . -name '*.go' | grep -v /vendor/) export VERSION=$(sh git describe --tags --always --dirty) export GOPKGS=$(sh go list ./ | grep -v /vendor/) export GO111MODULE=on export LDFLAGS=-X github.com/devopscorner/k8s-context/config.Version=$(VERSION) -w -s # Linux x86 export GOARCH=amd64 export GOOS=linux # Mac Intel export GOOS=darwin export GOARCH=amd64 # Mac M1/M2 (Arm) export GOOS=darwin export GOARCH=arm64
-
Build Binary
# Linux x86 make build # Mac Intel make build-mac-amd # Mac M1/M2 (Arm) make build-mac-arm -- or -- cd src GO111MODULE=$(GO111MODULE) GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -o build/$(GO_APP) $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" ./main.go
-
Running Binary
cd src/build ./k8c
-
Autocompletion Script
./k8c completion bash|fish|powershell|zsh
-
Using Contexts
-
Merge Multi Config
./k8c merge [new-config] [config-1] [config-2] ... [config-n] --- eg: (merge into single file $HOME/.kube/config) ./k8c merge $HOME/.kube/config $HOME/.kube/config-staging-dev $HOME/.kube/config-staging-uat $HOME/.kube/config-staging-qa
-
Discovery All KUBECONFIG Files Inside
~/.kube
Folder --- Interactive Mode./k8c load --- Select a kubeconfig file: [Use arrows to move, type to filter] > /home/devopscorner/.kube/.switch_tmp/config.1072356766.tmp /home/devopscorner/.kube/.switch_tmp/config.3841581520.tmp Available Kubernetes contexts: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-uat-staging arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-production
-
List Context(s)
KUBECONFIG=$HOME/.kube/config kubectl config get-contexts -- or -- ## Default KUBECONFIG path (`/.kube/config`) ## ./k8c list --- arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab ## Spesific Kubeconfig ## ./k8c list -f $HOME/.kube/config-cluster --- Available Kubernetes contexts: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-uat-staging
-
Switch (select) Context(s) --- Interactive Mode
## Default KUBECONFIG path (`/.kube/config`) ## ./k8c switch --- ? Select a context [Use arrows to move, type to filter] > arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab > Changed context to: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging ## Spesific Kubeconfig ## ./k8c switch - $HOME/.kube/config-cluster --- ? Select a context [Use arrows to move, type to filter] > arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorfner-uat-staging > Changed context to: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging
-
Run Spesific KUBECONFIG
KUBECONFIG=$HOME/.kube/config-new-cluster kubectl config use [cluster_name] -- or -- ./k8c switch -f $HOME/.kube/config-new-cluster
-
-
Get Resources Kubernetes
-
Namespaces
./k8c get namespace -- or -- ./k8c get ns
-
Services
./k8c get services -- or -- ./k8c get svc
-
Deployments
./k8c get deployments -- or -- ./k8c get deploy
-
Pods
./k8c get pods -- or -- ./k8c get po
-
Endpoints
./k8c get endpoints -- or -- ./k8c get ep
-
-
Get Resources By Filtering Namespace (Comma-Separated)
-
Namespaces
./k8c get namespace -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get namespace --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config -- or -- ./k8c get ns -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get ns --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
-
Services
./k8c get services -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get services --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config -- or -- ./k8c get svc -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get svc --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
-
Deployments
./k8c get deployments -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get deployments --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config -- or -- ./k8c get deploy -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get deploy --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
-
Pods
./k8c get pods -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get pods --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config -- or -- ./k8c get po -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config ./k8c get po --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
-
Endpoints
./k8c get endpoints -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config -- or -- ./k8c get ep -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
-
-
Show (Describe) Resources from Nodes, Pods, Logs & Port Forward
-
Pods
./k8c show po [pods_name] -n [namespace] ./k8c show po [pods_name] --namespace [namespace]
-
Logs
./k8c show logs [pods_name] -n [namespace] ./k8c show logs [pods_name] --namespace [namespace]
-
Port Forward
./k8c show port [pods_name] -n [namespace] ./k8c show port [pods_name] --namespace [namespace]
-
Node
./k8c show node [node_name]
-
-
Integrated with CI/CD
Dockerfile
Pipeline# Dockerfile COPY --from=devopscorner/k8s-context:latest /usr/local/bin/k8c /usr/local/bin/k8c
-
Docker version
docker version Client: Cloud integration: v1.0.22 Version: 20.10.17-rd API version: 1.41 Go version: go1.17.11 Git commit: c2e4e01 Built: Fri Jul 22 18:31:17 2022 OS/Arch: darwin/amd64 Context: default Experimental: true
-
Docker-Compose version
docker-compose -v --- Docker Compose version v2.11.1
-
Golang Version Manager (GVM)
gvm version --- Go Version Manager v1.0.22 installed at /Users/devopscorner/.gvm
-
Golang Version
go version --- go version go1.21.3 darwin/arm64
Make sure that you didn't push sensitive information in this repository
- AWS Credentials (AWS_ACCESS_KEY, AWS_SECRET_KEY)
- AWS Account ID
- AWS Resources ARN
- Username & Password
- Private (id_rsa) & Public Key (id_rsa.pub)
- DNS Zone ID
- APP & API Key
- Author: Dwi Fahni Denni (@zeroc0d3)
- Vendor: DevOps Corner Indonesia (devopscorner.id)
- License: Apache v2