Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (137 sloc) 5.76 KB

kubemacs

Introduction

ii brings a number of technologies together to create an open workflow. Using docker will reduce the setup and maintenance of sharing the core components with others.

Core Components

Build

IMAGE_TAG=gcr.io/apisnoop/kubemacs:0.9.23
docker build -t $IMAGE_TAG .
kind load docker-image --nodes kind-worker $IMAGE_TAG

Initializing only with Docker

docker run -it --rm --name kubemacs-init \
   -v "$HOME/.kube":/tmp/.kube \
   -v /var/run/docker.sock:/var/run/docker.sock \
   -e KUBEMACS_GIT_EMAIL="myemail@example.org" \
   -e KUBEMACS_GIT_NAME="My name" \
   -e KUBEMACS_INIT_DEFAULT_REPOS="https://github.com/cncf/apisnoop https://github.com/kubernetes/kubernetes" \
   -e KUBEMACS_INIT_ORG_FILE="\$HOME/Projects/apisnoop/deployment/k8s/xip.io/README.org" \
   -e KUBEMACS_DEFAULT_KIND_NAME=kind-self-init-test \
   -e HOST_UID="$(id -u)" \
   -e KUBEMACS_HOST_KUBECONFIG_NAME=config-kind-my-cluster \
   --user root \
   --network host \
   gcr.io/kubemacs/kubemacs:latest \
   bash /usr/local/bin/self-init.sh

Environment variables

PropertyDefault value
KUBEMACS_IMAGE
KUBEMACS_INIT_SELF_CONTAINER_NAMEkubemacs-init
KUBEMACS_INIT_DEBUGfalse
KUBEMACS_HOST_KUBECONFIG_NAMEconfig-kind
KUBEMACS_DEFAULT_KIND_NAMEkind
KUBEMACS_GIT_EMAIL
KUBEMACS_GIT_NAME
KUBEMACS_TIMEZONEPacific/Auckland
KUBEMACS_INIT_DEFAULT_REPOS
KUBEMACS_INIT_DEFAULT_REPOS_FOLDERhome/ii
KUBEMACS_INIT_DEFAULT_DIR/home/ii
KUBEMACS_INIT_ORG_FILE
HOST_UID0

Running in k8s

Deployment

kubectl apply -f kubemacs.yaml

Attaching to the Pod

PODNAME=$(kubectl -n kubemacs get pod --selector=app=kubemacs -o name  | sed s:pod/::)
kubectl exec -n kubemacs -t -i $PODNAME -- attach

dev-build loop

IIDE=gcr.io/apisnoop/kubemacs:0.9.23
PODNAME=$(kubectl -n kubemacs get pod --selector=app=kubemacs -o name  | sed s:pod/::)
docker build -t $IIDE .
kind load docker-image $IIDE
kubectl run --generator=run-pod/v1 $PODNAME --serviceaccount='admin-kubemacs' --image=$IIDE
kubectl exec -t -i $PODNAME -- attach # osc52 will be sent with tmate url / you can have multiple of these

Script to connect to a remote box and configure your kubeconfig

# configuration
export KUBECONFIG=~/.kube/config-my-remote
# [IMPORTANT] set your user
REMOTE_USER=root
# [IMPORTANT] set your remote box's IP
REMOTE_HOST=x.x.x.x

# fetch the remote kubeconfig
ssh $REMOTE_USER@$REMOTE_HOST kubectl config view --merge --minify --flatten > $KUBECONFIG

# find the port of the Kubernetes API in the kubeconfig and export it
export K8S_REMOTE_PORT=$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}' | cut -d ':' -f3)
# forward the port from the remote box to the localhost
ssh -fN -L $K8S_REMOTE_PORT:localhost:$K8S_REMOTE_PORT $REMOTE_USER@$REMOTE_HOST
kubectl get pods -A

Add a helper function to your ~/.bashrc

NamePurposeExample
KUBECONFIGthe kubeconfig to save to and use~/.kube/config-my-remote
REMOTE_USERthe remote user to login asii
REMOTE_HOSTthe remote host/ip to connect tomyhost.example.com
cat << EOF >> ~/.bashrc
function ii_setup_k8s_from_remote() {
# ensure:
# - remote server kubeconfig
# - defined $KUBECONFIG
# - remote server Kubernetes API forwarded to localhost

function ii_setup_k8s_from_remote_cleanup() {
  set +e
}

trap ii_setup_k8s_from_remote_cleanup EXIT

set -e

if [ -x /tmp/ii_setup_k8s_from_remote-hasrun ]; then
        return
fi

# configuration
if [ -z \$KUBECONFIG ]; then
  export KUBECONFIG=~/.kube/config-my-remote
fi

# [IMPORTANT] set your user
if [ -z \$REMOTE_USER ]; then
   echo "[error] please set \\\$REMOTE_USER"
   return
fi

# [IMPORTANT] set your remote box's IP
if [ -z \$REMOTE_HOST ]; then
   echo "[error] please set \\\$REMOTE_HOST"
   return
fi

# fetch the remote kubeconfig
ssh \$REMOTE_USER@\$REMOTE_HOST kubectl config view --merge --minify --flatten > \$KUBECONFIG

# find the port of the Kubernetes API in the kubeconfig and export it
export K8S_REMOTE_PORT=\$(kubectl config view -o jsonpath='{.clusters[0].cluster.server}' | cut -d ':' -f3)

# check if not already listening
if ! lsof -i:\$K8S_REMOTE_PORT 2>&1 > /dev/null; then
  # forward the port from the remote box to the localhost
  ssh -fN -L \$K8S_REMOTE_PORT:localhost:\$K8S_REMOTE_PORT \$REMOTE_USER@\$REMOTE_HOST
fi

export DOCKER_HOST="ssh://\\\$REMOTE_USER@\\\$REMOTE_HOST"
echo "[ok]"
touch /tmp/ii_setup_k8s_from_remote-hasrun
ii_setup_k8s_from_remote_cleanup

}
EOF
You can’t perform that action at this time.