Dennis Hemeier

K9S CLI - Interaktiver Kubernetes CLI Cheatsheet

Tools
K9s ist eine terminalbasierte Benutzeroberfläche zur Steuerung deiner Kubernetes-Cluster. Mit Hilfe von K9s lassen sich wiederkehrende Aufgaben in Kubernetes effizient über die favorisierte Shell interaktiv durchführen.
k9s - Interaktiver Kubernetes CheatSheet

FUNKTIONEN:

  • Echtzeitüberwachung von Kubernetes Ressourcen

  • Unterstützung von Custom Resource Definitions (CRD)

  • Überwachung von Cluster-Metriken auf Pod-, Container- & Node-Ebene

  • Tools zur Unterstützung von Debugging (z.B. Error Zoom)

  • Stellt standard kubectl Funktionen wie logs, scaling, exec, port-forwards, restarts uvm. bereit.

  • Individuelle Command-Shortcuts zur Vereinfachung von wiederkehrenden Aufgaben

  • Umfangreiche Filtermöglichkeiten von Ressourcen zur effizienten Navigation innerhalb des Clusters

  • Individualisierbare Skins (Look & Feel)

  • Listen- und Spaltenkonfiguration

  • Plugin-Unterstützung

  • Pulses, XRay und Popeye CLI

INSTALLATION

K9s ist verfügbar für MacOS, Linux und Windows.

# Via Homebrew 
brew install derailed/k9s/k9s 

# Via MacPort 
sudo port install k9s
  • Linux:

# Via LinuxBrew 
brew install derailed/k9s/k9s

# Via PacMan 
pacman -S k9s
  • Windows:

# Via scoop  
scoop install k9s

# Via chocolatey 
choco install k9s

K9s nutzt den 256-Farben-Modus deines Terminals. Auf `Nix Systemen solltest du daher sicherstellen, dass dieser aktiv ist:

export TERM=xterm-256color

ERSTE SCHRITTE

K9s hat ein paar Startargumente, mit der sich die interaktive Kubernetes CLI in unterschiedlichen Konfigurationen starten lässt.

STARTARGUMENTE

# List all available CLI options 
k9s help

# Get info about K9s runtime (logs, configs, etc..) 
k9s info 

# Run K9s in a given namespace. 
k9s -n mynamespace 

# Run K9s and launch in pod view via the pod command. 
k9s -c pod 

# Start K9s in a non default KubeConfig context 
k9s --context coolCtx 

# Start K9s in readonly mode - with all modification commands disabled 
k9s --readonly

KURZBEFEHLE

KONFIGURATION

K9S

K9s verwaltet seine Konfiguration mit XDG unter $XDG_CONFIG_HOME/k9s. Die Hauptkonfiguration befindet sich in der Datei config.yml und beinhaltet zahlreiche Konfigurationsmöglichkeiten.

Das sich k9s aktuell noch im pre-release Status befindet, kann sich die Konfigurationsdatei noch jederzeit ändern.

# $XDG_CONFIG_HOME/k9s/config.yml
k9s:
  # Represents ui poll intervals. Default 2secs
  refreshRate: 2
  # Number of retries once the connection to the api-server is lost. Default 15.
  maxConnRetry: 5
  # Enable mouse support. Default false
  enableMouse: true
  # Set to true to hide K9s header. Default false
  headless: false
  # Set to true to hide K9s crumbs. Default false
  crumbsless: false
  # Indicates whether modification commands like delete/kill/edit are disabled. Default is false
  readOnly: false
  # Toggles icons display as not all terminal support these chars.
  noIcons: false

  # Logs configuration
  logger:
    # Defines the number of lines to return. Default 100
    tail: 200
    # Defines the total number of log lines to allow in the view. Default 1000
    buffer: 500
    # Represents how far to go back in the log timeline in seconds. Setting to -1 will show all available logs. Default is 5mins.
    sinceSeconds: 300
    # Go full screen while displaying logs. Default false
    fullScreenLogs: false
    # Toggles log line wrap. Default false
    textWrap: false
    # Toggles log line timestamp info. Default false
    showTime: false

  # Indicates the current Kube context. Defaults to current context
  currentContext: minikube
  # Indicates the current kube cluster. Defaults to current context cluster
  currentCluster: minikube
  # Persists per cluster preferences for favorite namespaces and view.

  clusters:
    cluster1:
      namespace:
        active: coolio
        favorites:
        - cassandra
        - default
      view:
        active: po
      featureGates:
        # Toggles NodeShell support. Allow K9s to shell into nodes if needed. Default false.
        nodeShell: false
      # Provide shell pod customization of feature gate is enabled
      shellPod:
        # The shell pod image to use.
        image: killerAdmin
        # The namespace to launch to shell pod into.
        namespace: fred
        # The resource limit to set on the shell pod.
        limits:
          cpu: 100m
          memory: 100Mi
      # The IP Address to use when launching a port-forward.
      portForwardAddress: 1.2.3.4
    cluster2:
      namespace:
        active: all
        favorites:
        - all
        - kube-system
        - default
      view:
        active: dp

ALIASES

Autovervollständigung

K9s Befehle verfügen über eine Autovervollständigung. Die Autovervollständigung basiert auf unterstützte Kubernetes Ressourcen in Einzahl, Mehrzahl oder auch Aliase (z.B. pod, pods & po). Der Befehlsmodus unterstützt folgende Kurzbefehle:

Aliase / Kurzbefehle

In K9s kannst du deine eigenen Kurzbefehle (Aliase) unter $XDG_CONFIG_HOME/k9s/alias.yml konfigurieren Beispiel:

  # $XDG_CONFIG_HOME/k9s/alias.yml
alias:
  # Use pp as an alias for Pod
  pp: v1/pods

  # Use crb as an alias for ClusterRoleBinding
  crb: rbac.authorization.k8s.io/v1/clusterrolebindings

  # Use cr as an alias for ClusterRole
  cr: rbac.authorization.k8s.io/v1/clusterroles

  # Use dep as an alias for Deployments
  dep: apps/v1/deployments

  # Use fred as an alias for CRD Frederick
  fred: acme.io/v1alpha1/fredericks

HotKeys

Das Aufrufen der Befehlseingabe und die Eingabe des Ressourcennamens kann für häufig wiederholte Aktionen umständlich sein. Durch die Nutzung von HotKeys kann K9s so konfiguriert werden, dass sich solche Aktionen über einen einzelnen Tastendruck auslösen lassen. Um HotKeys zu aktivieren, sind folgende Schritte notwendig:

  • Erstelle die Datei $XDG_CONFIG_HOME/k9s/hotkey.yml

  • Übernehme den Inhalt aus dem folgenden Beispiel und passe die Konfiguration nach deinen Vorlieben an.

# $XDG_CONFIG_HOME/k9s/hotkey.yml
hotKey:

  # Hitting Shift-0 navigates to your pod view
  shift-0:
    shortCut:    Shift-0
    description: Viewing pods
    command:     pods

  # Hitting Shift-1 navigates to your deployments
  shift-1:
    shortCut:    Shift-1
    description: View deployments
    command:     dp

  # Hitting Shift-2 navigates to your xray deployments
  shift-2:
    shortCut:    Shift-2
    description: XRay Deployments
    command:     xray deploy

SKINS

Du kannst auch das Look & Feel von K9s individuell anpassen. Die Konfiguration von Skins befinden sich ebenfalls unter $XDG_CONFIG_HOME/k9s/. Die Standard-Skin-Konfiguration findest du in der Datei: $XDG_CONFIG_HOME/k9s/skin.yml. Cluster spezifische Skins-Konfiguration können unter $XDG_CONFIG_HOME/k9s/[CLUSTERNAME]_skin.yml abgelegt werden. Beispiel:

# $XDG_CONFIG_HOME/k9s/in_the_navy_skin.yml
k9s:
  # General K9s styles
  body:
    fgColor: dodgerblue
    bgColor: '#ffffff'
    logoColor: '#0000ff'

  # ClusterInfoView styles
  info:
    fgColor: lightskyblue
    sectionColor: steelblue

  # Frame styles
  frame:
    # Borders styles
    border:
      fgColor: dodgerblue
      focusColor: aliceblue

    # MenuView attributes and styles
    menu:
      fgColor: darkblue
      keyColor: cornflowerblue
      # Used for favorite namespaces
      numKeyColor: cadetblue

    # CrumbView attributes for history navigation.
    crumbs:
      fgColor: white
      bgColor: steelblue
      activeColor: skyblue

    # Resource status and update styles
    status:
      newColor: '#00ff00'
      modifyColor: powderblue
      addColor: lightskyblue
      errorColor: indianred
      highlightcolor: royalblue
      killColor: slategray
      completedColor: gray

    # Border title styles.
    title:
      fgColor: aqua
      bgColor: white
      highlightColor: skyblue
      counterColor: slateblue
      filterColor: slategray
  # Specific views styles
  views:
    # TableView attributes.
    table:
      fgColor: blue
      bgColor: darkblue
      cursorColor: aqua
      # Header row styles.
      header:
        fgColor: white
        bgColor: darkblue
        sorterColor: orange

    # YAML info styles.
    yaml:
      keyColor: steelblue
      colonColor: blue
      valueColor: royalblue

    # Logs styles.
    logs:
      fgColor: white
      bgColor: black

SPALTENKONFIGURATION

In K9s lassen sich die Spaltenkonfigurationen für Listenansichten ebenfalls über eigene Konfigurationsdateien konfigurieren. Um individuelle Spaltenkonfigurationen nutzen zu können, müssen wir zunächst die $XDG_CONFIG_HOME/k9s/views.yml händisch anlegen. Wenn keine individuelle Spaltenkonfiguration in der Datei gefunden wird, wird die Standardspaltenkonfiguration der jeweiligen Kubernetes Ressource verwendet. Beispiel:

# $XDG_CONFIG_HOME/k9s/views.yml
k9s:
  views:
    # Alters the pod view column layout. Uses GVR as key
    v1/pods:
      columns:
        - AGE
        - NAMESPACE
        - NAME
        - IP
        - NODE
        - STATUS
        - READY

    # Alters the service view column layout
    v1/services:
      columns:
        - AGE
        - NAMESPACE
        - NAME
        - TYPE
        - CLUSTER-IP

NÜTZLICHE WORKFLOWS

SECRETS IM KLARTEXT ANZEIGEN LASSEN

  • k9s # K9s starten

  • :secrets [NAMESPACE] # Secrets anzeigen

  • /mongodb # Secrets filtern nach "mongodb"

  • Gewünschtes Secret selektieren und mit x Inhalt im Klartext anzeigen lassen

workflow_secrets.png
Mit K9s könnt ihr euch Secrets im Klartext anzeigen lassen

IN LAUFENDEN CONTAINER "JOINEN"

  • k9s # K9s starten

  • :pods [NAMESPACE] # Pods anzeigen lassen

  • /mongodb # Pods filtern nach "mongodb"

  • Gewünschten Pod selektieren und mit s direkt in den Container "joinen"

workflow_join_container.png
Shell innerhalb eines Containers

PORT-FORWARD STARTEN

  • k9s # K9s starten

  • :pods [NAMESPACE] # Pods anzeigen lassen / Alternativ auch services

  • /mongodb # Pods/Service filtern nach "mongodb"

  • Gewünschten Pod/Service selektieren und mit shift-f port-forwarding starten

workflow_port-forward.png
Erstellung eines dynamischen Port-Forwards

ROLLENBERECHTIGIGUNGEN PRÜFEN

  • k9s⏎ # K9s starten

  • :rolebindings or :role 

  • Gewünschte Ressource selektieren und mit ⏎ Details anzeigen lassen

workflow_rbac.png
RBAC Berechtigungen einsehen und prüfen

WEITERE INFORMATIONEN