Kubernetes - show help for a command but only show examples - kubernetes

Is it possible to get the help for a k8s command but only show the Examples section of the help?
For example, if I run:
kubectl run --help
It outputs different sections, but I only want to see examples on the screen.

This command only show line 2 to line 30
kubectl run --help | head -n30 | tail -n28

Like many other help pages, K8s help docs come in this order:
Description
Examples
Options
Usage
you could use sed to cut out the part you need:
kubectl run --help | sed -n "/Examples:/,/Options:/p"
or better:
kubectl run --help | sed -n "/Examples:/,/Options:/p" | grep -v "Options:"

Related

How to exclude taint nodes from outputs in kubectl get nodes?

I try to check how many nodes are ready (not including nodes tainted NoSchedule) and write the number to text file output.txt.
Could you give me any advice?
I believe that kubectl get nodes doesn't show taints, so you can't just filter with grep. In that case you can set the output as json and use jq (or yaml and use yq) to process it:
kubectl get nodes -o json | jq -c '.items[].spec.taints' | grep -v NoSchedule | wc -l > output.txt
-c option in jq is to output each element in a single line, instead of pretty printing it, in case you have multiple taints. The rest has already been explained in Abdennour TOUMI's answer
kubectl get nodes | grep Ready | grep -v NotReady | grep -v NoSchedule \
| wc -l > output.txt
This single command will do the job for you:
Notes:
While grep includes lines, grep -v excludes lines
wc -l counts the number of lines.
number of output's lines is the same number of nodes with criteria you described
Full proof Query to get nodes except node has taint effect NoSchedule on it
kubectl get node -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.taints[*].effect}{"\n"}{end}' | grep -v NoSchedule | wc -l
The following command can be used without jq or where jq is not installed.
kubectl get nodes --selector='!node-role.kubernetes.io/master' --no-headers | grep -v SchedulingDisabled | wc -l > output.txt

How to delete multiple Kubernetes pods having the same name expression except the latest one?

I have multiple pods running as below. I want to delete them all except the one having minimum age. How to do it?
Something like this? Perhaps also add -l app=value to filter for a specific app
kubectl get pods --sort-by=.metadata.creationTimestamp -o name | head -n -1 | xargs echo kubectl delete
(Remove echo to do it for realz)
You could just use PowerShell and pipe the output to the Foreach-Object, which then loops over each line:
kubectl get pods -n default -o name | ForEach-Object { kubectl delete $_ }

JSONPath range not working when using kubectl

I'm accessing Kubernetes through the CLI tool kubectl and I'm trying to get a list of all context names, one per line.
I know that JSONPath can be used to extract and format specific output. I get really close to what I want with
kubectl config view -o=jsonpath="{.contexts[*].name}"
but this puts all the names on the same line. I'm trying to use range to list all names separated by newlines:
kubectl config view -o=jsonpath='{range .contexts[*]}{.name}{"\n"}{end}'
But this just gives me an error:
error: unexpected arguments: [.contexts[*]}{.name}{"\n"}{end}]
See 'kubectl config view -h' for help and examples.
I've reviewed the kubectl documentation and what I'm doing is really similar to https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-containers-by-pod, where the command is
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
sort
but I can't see where I'm going wrong.
Your command works for me in kubectl 1.9.2
If it still doesn't work, you can use tr in bash to replace spaces with new lines:
kubectl config view -o=jsonpath="{.contexts[*].name}" | tr " " "\n"
I figured it out. I had been using #ahmetb's kubectl-aliases script, which works fine with no problem, but one of the suggestions in the README was:
Print the full command before running it: Add this to your .bashrc or .zshrc file:
function kubectl() { echo "+ kubectl $#"; command kubectl $#; }
I had that function declaration in my .bashrc and it was stripping off the quotes for my jsonpath argument. As soon as I commented out that declaration and opened a new shell, the command worked correctly.

How to feed autocomplete data to a fish alias?

I am starting with fish and one of the things I could not find in the extensive documentation was autocomplete feeds.
There is mention of Tab Completions in the tutorial but it addresses existing the existence of the mechanism itself, not its configuration.
I have a bunch of virtual machines I connect to via
machinectl shell <name of machine> /bin/bash
I could make alises for all my machines via
function cm
machinectl $argv shell /bin/bash;
end
but this requires to remember and type the machine name.
How could I use the output of machinectl list | tail -n +2 | head -n -2 | cut -f1 -d' ' as a feed/hint to my cm command so that it shows them when using Tab?
EDIT: I somehow missed this right at the top of the documentation: Tab completion (I found it after reviewing the answers)
This should get you off to a good start:
complete --command cm --no-files \
--arguments '(machinectl list | tail -n +2 | head -n -2 | cut -f1 -d" ")'
Entering that at the command line will activate it for the current session; to make it permanent add the line to a completions file as Kurtis describes (~/.config/fish/completions/cm.fish).
See help complete. You'll find the completions that ship with fish, including completions for ssh, in $__fish_datadir/completions. A completion you write for a private function or command would be placed in ~/.config/fish/completions/$cmd_name.fish

Simple Terminal Command Question - Is there a command to pull a certain line out of an input?

I have a simple question. Is there a command that allows you to pull a certain line out of an input? Like if I wanted the 7th line from ifconfig. Is there a way to do this: ifconfig | [command] 7?
Thanks!
YOu can use sed to extract a particular line from a file and/or from standard in as follows
sed -n '7p' filename or
some_command | sed -n '7p'
This should do it
ifconfig | head -n 7 | tail -n 1