Alek's Blog

Some Tips and Tricks for OCP (openshift)

Updated:

You will find here some all day work tips for OpenShift Cluster and for the origin (okd) version, some of the commands also works for kubectl – Command line tool.

tip
Recommendation

I strongly recommend to add the name space -n directly after the command!

get nodeportπŸ”—

oc get svc <your-service> -o jsonpath='{.spec.ports[?(@)].nodePort}'

get all released pvπŸ”—

oc get pv \
--template='{{range .items}}{{if eq .status.phase "Released" }}{{.metadata.name}}{{"\n"}}{{end}}{{end}}' \
--all-namespaces

The output.

pv020-volume
pv021-volume
pv022-volume
pv023-volume
pv025-volume
pv027-volume
pv034-volume
pv037-volume
pv038-volume
pv039-volume
pv042-volume
pv043-volume

Cleanup Released pvsπŸ”—

⚠ | You need jq to execute the following snipplet

for i in $(oc get pv --template='{{range .items}}{{if eq .status.phase "Released" }}{{.metadata.name}}{{"\n"}}{{end}}{{end}}' --all-namespaces ); 
do
  oc export pv $i -o json | jq 'del(.spec.claimRef)' > ${i}_no-claimRef.json
  oc delete pv $i && oc create -f ${i}_no-claimRef.json
  echo "rm -rf /mnt/$( echo $i|cut -d'-' -f1)/[a-z]*"
done

Running pods on NodesπŸ”—

This command shows which pod run on which node.

oc get po  --all-namespaces \
  -o go-template='{{range .items}}{{if eq .status.phase "Running" }}name: {{ printf "%19s" .metadata.name}}; Namespace: {{ printf "%15s" .metadata.namespace}}; Nodename: {{.spec.nodeName}}{{"\n"}}{{end}}{{end}}'

The output looks like this.

name:     router-51-f9gc4; Namespace:  dev-router; Nodename: app_n05
name:    router01-4-8z5gb; Namespace:  dev-router; Nodename: app_n01
name:    router02-4-mvd8l; Namespace:  dev-router; Nodename: app_n04
name:    router03-3-vnkn2; Namespace:  dev-router; Nodename: app_n04
name:    router04-2-gp4r7; Namespace:  dev-router; Nodename: app_n02
name:    router05-4-hmhnb; Namespace:  dev-router; Nodename: app_n02
name:    router12-4-vrcgc; Namespace:  dev-router; Nodename: app_n04
name:          ap-6-btgqf; Namespace:       dev01; Nodename: app_n05
name:          ap-1-4rz89; Namespace:       dev02; Nodename: app_n05
name:  ap-haproxy-1-q7tvq; Namespace:       dev03; Nodename: app_n02
name:          ap-1-blb8p; Namespace:       dev03; Nodename: app_n03

with perlπŸ”—

oc get node --show-labels --no-headers \ 
| perl -MData::Dumper -ane 'push(@{$hosts->{$F[0]}},split( "," , $F[5])) ; \ 
    END {foreach (reverse sort { $hosts->{$a} <=> $hosts->{$b} } keys %{$hosts}){ \
      printf("\n% 8s\n    %s\n",$_,join( " ; " , @{$hosts->{$_}}) );}}'

Output

nodei01
    beta.kubernetes.io/arch=amd64 ; beta.kubernetes.io/os=linux ; failure-domain.beta.kubernetes.io/zone=nova ; kubernetes.io/hostname=nodei01 ; logging-infra-fluentd=true ; node-role.kubernetes.io/compute=true ; region=primary ; role=infra ; router=yes ; zone=mgmt
nodei02
    beta.kubernetes.io/arch=amd64 ; beta.kubernetes.io/os=linux ; failure-domain.beta.kubernetes.io/zone=nova ; kubernetes.io/hostname=nodei02 ; logging-infra-fluentd=true ; node-role.kubernetes.io/compute=true ; region=primary ; role=infra ; router=yes ; zone=mgmt

without perlπŸ”—

In case that the perl module Data::Dumper isn’t available here the solution with build-in go-template.

oc get node  --no-headers \
  -o go-template='{{range .items}}name:{{ .metadata.name }}{{"\n"}}{{range $key, $value := .metadata.labels}}  {{ $key }}{{"="}}{{ $value }}{{"\n"}}{{end}}{{"\n"}}{{end}}'|less11111

Collect and compress must-gatherπŸ”—

When you create an Red Hat Case is most of the time a must-gather recommended. The output of must-gather could be quite big why it’s recommended to compress the archive.

The commands below are for a bash shell.

# Save timestamp for followup commands
CURRENT_DATE=$(date +"%Y-%m-%d-%S")

# Save output from must gather

oc adm must-gather \
  --dest-dir=${CURRENT_DATE}_must-gather \
  >${CURRENT_DATE}_must-gather.txt 2>&1

# Compress the stdout and stderr output
xz -9 ${CURRENT_DATE}_must-gather.txt

# Compress the archive
XZ_DEFAULTS=-9 tar cfJ ${CURRENT_DATE}_must-gather.tar.xz ${CURRENT_DATE}_must-gather

oc snippletsπŸ”—

get infosπŸ”—

 oc -n default \
     get dc ipfailover \
     -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'

json patchπŸ”—

oc -n default \
    patch dc/ipfailover \
    --type='json' \
    -p '[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"internal.registy/openshift3/ose-keepalived-ipfailover:v3.9.68"}]'

get secretsπŸ”—

The ClientID in the snippets below is a key in the secret, you must replace it with the key in your secret.

with go-templateπŸ”—

From β€œsecret” to plain text πŸ˜„.

$ oc -n ${NAMEPACE} get secrets ${SECRET} \
  -o go-template='{{.data.ClientID|base64decode}}{{"\n"}}'

with jsonpathπŸ”—

$ oc -n ${NAMEPACE} get secrets \
  ${SECRET} \
  -o jsonpath='{.data.ClientID}' \
  | base64 -d

get specπŸ”—

$ oc -n ${NAMEPACE} get deployment \
  ${DEPLOYMENT} \
  -o jsonpath='{.spec}' \
  | jq