Skip to content

Rancher Operations

rancher CLI
    export RANCHER_BEARER_TOKEN="token-xyz-xxxxx"
    # Log In
    rancher login --token $RANCHER_BEARER_TOKEN --name My_rancher https:my_rancher.example.com

    # Operation on rancher servers/instances
    rancher server current/ls/switch

    # Operation on downstream cluster
    rancher clusters          # list all clusters in rancher
    rancher  context current  #  see current context cluster
    rancher context switch [PROJECT_ID/PROJECT_NAME]

    # applied on current context cluster
    rancher kubectl ...
    rancher nodes
    rancher ssh [NODE_ID/NODE_NAME]

Manage CRD with kubectl

Get clusters

kubectl  api-resources
kubectl get crd
kubectl get -A  clusterregistrationtokens.management.cattle.io

# get clusters
kubectl get clusters.management.cattle.io

# List token
kubectl get -A tokens.management.cattle.io

Find user

kubectl get users.management.cattle.io -o yaml | grep "Username" -A20
u-azqqzyxkv8
kubectl get users.management.cattle.io u-azqqzyxkv8 -o yaml
kubectl get tokens.management.cattle.io kubeconfig-user-u-azqqzyxkv8  -o yaml

Get node template using 'Auto Replace' feature

kubectl get nodepools.management.cattle.io -A -o yaml | egrep "deleteNotReadyAfterSecs"

kubectl get nodepools.management.cattle.io -A -o yaml | egrep "deleteNotReadyAfterSecs: 1800" -A 10 -B 10

Get downstream cluster version

kubectl get clusters.management.cattle.io  -o \
jsonpath='{range .items[*]}{.metadata.name}: {.status.appliedSpec.rancherKubernetesEngineConfig.kubernetesVersion}{"\n"}{end}' > raw.lst

# Version <= 1.17
cat raw.lst| egrep -v "1.20|1.18|1.17|1.21|1.19"

Get cluster nodes ssh keys

kubectl get clusters.management.cattle.io/XXX -o \
jsonpath="{range .status.appliedSpec.rancherKubernetesEngineConfig.nodes[*]}{.hostnameOverride} {'\n'}{.sshKey}{'\n'}{end}"

Get nodes external ip

kubectl get nodes -o jsonpath="{ range .items[*]}{.metadata.name} ==> {.metadata.annotations['rke\.cattle\.io/external-ip']}{'\n\n'}{end}"

# Get only address IF  type=InternalIp
k get nodes  -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")]}{"\n"}{end}'

Get all images running in a cluster

kubectl get pod -A  -o jsonpath='{range .items[*].spec.containers[*]}{.image}{"\n"}{end}' | egrep "gns-hosting-dev|quay" | sort -u
kubectl get pods -A -o jsonpath="{.items[*].spec.containers[*].image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c

# image by pods
kubectl get pods -A -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{"\n"}{end}'
kubectl get pods -A -o jsonpath='{range .items[*]}{"\n"}{.metadata.name} {"\n"}{ .spec.containers[*].image}{"\n"}{end}'

Get node ssh key

Since version 2.6, node sshkey is not available in cluster manifest anymore. It is a secret now

k get secret -n cattle-system mc-m-w2hzm -o jsonpath='{.data.extractedConfig}' | base64 -d | base64 -d > dump.zip
tar -xvf dump.zip