Prerequisites

To get the most out of this guide, you will need to:

Connection Configuration

NameTypeDescription
KUBECONFIGfilesystemA https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ with permission to exec into pods in a namespace

Connection Command

hoop admin create conn k8s -a <agent> -- kubectl

The assumption is that the service account where the agent is deployed has the necessary permissions to interact with the Kubernetes API. Alternatively, create the connection passing your local KUBECONFIG. Example:

hoop admin create conn k8s \
	-a <agent> \
	-e b64-filesystem:KUBECONFIG=$(cat $HOME/.kube/config |base64) \
	-- kubectl

How to Use

# view pods in the default namespace
hoop exec k8s -- get pods
# restart an app
hoop exec k8s -- rollout restart deployment/myapp
# scale up an app
hoop exec k8s -- scale --replicas=3 deployment/myapp

It’s possible to narrow down the commands into distinct connections; this gives a better user experience.

  • Update the connection with the command below
hoop admin create conn k8s -a <agent> -- \
  kubectl --namespace prod rollout

Then it’s possible

hoop exec k8s -- restart deployment/myapp
hoop exec k8s -- undo deployment/myapp

Interactive


kubectl provides ways to create interactive sessions with pods. It’s possible to map these commands to Hoop to obtain interactive sessions allocating a pseudo-TTY.

Connection Command

hoop admin create conn k8s-exec -a <agent> -- \
  kubectl exec --tty --stdin deployment/myapp --

How to Use

Start an interactive bash session with a deployment/pod

hoop connect myapp -- bash

This will open an interactive session with the deployment myapp. It’s possible to map any command that spawns an interactive session.

Then, it’s possible to gain a rails console session

hoop connect myapp -- rails console

Note that kubectl exec is used with -tty and --stdin arguments. These flags are required when using hoop connect

One-off scripts


Processes can be spawned in an ad-hoc manner using kubectl exec. However, in some cases, an interactive shell may be overly permissive.

Connection Configuration

NameTypeDescription
KUBECONFIGfilesystemA https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ with permission to exec into pods in a namespace

Connection Command

hoop admin create conn exec-oneoff -a <agent> -- \
	kubectl exec --stdin deployment/myapp --

How to Use

This example executes a one-off process using ruby.

hoop exec exec-oneoff -- rails runner 'puts Rails.env'

Narrowing down the exec arguments allows passing the stdin and executing ruby scripts.

  • Edit the connection command with the bellow content
hoop admin create conn exec-oneoff -a <agent> -- \
	kubectl exec --stdin deployment/myapp -- rails runner -

Then, it’s possible

hoop exec exec-oneoff <<EOF
myvar='Hello'
puts myvar
EOF

The connection now runs a one-off process accepting ruby scripts from the standard input.