Skip to content

Kubernetes & OpenShift: Access & secrets

Kubernetes & OpenShift commands for access & secrets (tags: #access #secrets #ssh).

Index
6 items

Kubernetes4

Kubernetes: Exec into a pod

Safe

Interactive shell inside the pod's (first) container.

kubectl exec -it <pod> -n <namespace> -- sh
What each part does
  1. kubectl execrun a command in a container of a running pod
  2. -itinteractive + terminal
  3. <pod>the pod (add -c <container> if it has several)
  4. -n <namespace>namespace of the pod
  5. -- sheverything after -- is the command to run; use bash if the image has it

Also known as: kubectl exec · shell into pod

Kubernetes: Port-forward to a service

Safe

Reach a ClusterIP service from your laptop on localhost:<local-port>.

kubectl port-forward svc/<service> <local-port>:<remote-port> -n <namespace>
What each part does
  1. kubectl port-forwardtunnel a local port to a pod through the API server
  2. svc/<service>pick a pod behind this service
  3. <local-port>:<remote-port>listen locally on the first, forward to the second
  4. -n <namespace>namespace of the service
Example
$ kubectl port-forward svc/prometheus-operated 9090:9090 -n monitoring
# then open http://localhost:9090

Also known as: port forward · kubectl port-forward · prometheus ui · grafana ui

Kubernetes: Switch kubeconfig context

Safe

Point kubectl at another cluster. List them with kubectl config get-contexts.

kubectl config use-context <context>
What each part does
  1. kubectl config use-contextmake a context current (cluster + user + namespace)
  2. <context>context name from kubectl config get-contexts

My note Check twice before prod. kubectl config current-context.

Also known as: kubectx · change cluster · kubeconfig

Kubernetes: Set default namespace for current context

Safe

Stop typing -n on every command.

kubectl config set-context --current --namespace=<namespace>
What each part does
  1. kubectl config set-contextchange a context in your kubeconfig
  2. --currentthe context you're using now
  3. --namespace=<namespace>default namespace, so you can drop -n

Also known as: kubens · default namespace

OpenShift2

OpenShift: Log in with a token

Safe

Authenticate oc using the token from 'Copy login command' in the web console.

oc login --token=<token> --server=<api-url>
What each part does
  1. oc loginlog in to an OpenShift cluster
  2. --token=<token>API token (web console: your name > Copy login command)
  3. --server=<api-url>API server URL, e.g. https://api.cluster.example.com:6443

Also known as: oc login · Unauthorized · ocp login

OpenShift: Switch project

Safe

Change the default project (namespace). Run oc projects to list them.

oc project <project>
What each part does
  1. oc projectswitch the current project (namespace); with no argument, show the current one
  2. <project>project name

Also known as: oc project · namespace

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…