Have you seen a comma in a kubectl command?
Like the following command:
kubectl get deployments,ing -n my-system
Is it like a pipe grap or?
It is just a separator between k8s resources, that you would like to do something with (get/describe/etc...). Your command will output the list of Ingress resources next to the list of Deployments
Related
I'm trying to use Kubectl get namespaces command it is fetching the data.
kubectl get namespace
NAME STATUS AGE
default Active 1d
kube-node-lease Active 1d
kube-public Active 1d
kube-system Active 1d
but I want to filter it with name only. So when u run the script it should show like this.
kubectl get namespace
NAME
default
kube-node-lease
kube-public
kube-system
I've tried some powershell command but it is not working out for me.
Try any one of the command
kubectl get namespace --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
kubectl get namespace | awk '{print $1}'
kubectl get namespace --no-headers -o custom-columns=":metadata.name"
kubectl get namespace -o=name | sed "s/^.\{10\}//"
I presume the output of kubectrl is pure text so to manipulate it you will need to either do text parsing (error-prone and not fun) or you need to get the data into a PowerShell object.
ConvertFrom-SourceTable
This will allow you to read and parse the text/string data into a PowerShell object. Then you can simply Select-Object name to get the Name column only.
The second option is to use text/string parsing but for that, you will have to use length detections to extract the columns. An example of that can be found here.
This code works for me:
kubectl get ns | awk '{print $1}'
i have lots of deployments yaml files and lots of services yaml files, i want to know if it is possible to execute all the yaml files at once ?
i tried this command:
kubectl create -f *.yaml
But it's not working
Thanks
If you are inside any directory and you want to apply all the YAML
kubectl apply -f ./
or else you merge many YAML files in single YAML file
YAML1 content
---
YAML 2 content
---
YAML 3 contnet
Addition to #Harsh answer.
Apply resources from a directory:
kubectl apply -k dir/
Include the sub directories to include all manifest files from sub-directories.
kubectl apply -R -f dir/
-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests
organized within the same directo
merge those by using
---
three dashes in one file
I have several namespaces in my cluster and would like a log like:
NAMESPACE NAME PODS
MY_NAMESPACE my_ns6446f67599-25g7f 10
You can also try this one liner:
kubectl get pods --all-namespaces | awk '{print $1}' | sort | uniq -c | sort -k1 -n -r
Which will yield:
136 some-ns
133 kube-system
119 other-ns
Explaining a bit:
kubectl get pods --all-namespaces will list all pods with namespace in the first column.
awk { print $1 } will "filter out" the first column which is the namespace
sort will sort the name of namespaces alphabetically
uniq -c will count how many times each namespace appeared and aggregate with a count as the first column (e.g. 136 some-ns means that some-ns appeared 136 times).
sort -k1 -n -r this one will sort from the namespace which appeared the most (i.e. had most pods) to the one that appeared the least. -k1 means that I am using the first column to sort, -n I am using numeric comparison and -r and I am doing a reverse ordering.
You can use --all-namespaces flag to get pods.
kubectl get pods --all-namespaces
From your output, it looks like you are trying to print the replicasets as there is a PODs count column in the output.
kubectl get replicaset --all-namespaces
If you want to limit the resulting columns, we can use the --0 custom-columns= parameter as below.
$ kubectl get replicaset --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,PODS:.status.replicas
NAMESPACE NAME PODS
MY_NAMESPACE my_ns6446f67599-25g7f 10
This solution combines the json output of kubectl get pods and kubectl get namespaces in order to also display the namespaces where no pods are deployed.
kubectl get pods -o json --all-namespaces | jq --argjson ns "$(kubectl get namespaces -o json)" '.items | group_by(.metadata.namespace) | map({namespace: .[].metadata.namespace, count: . | length}) | . += ($ns.items | map({namespace: .metadata.name, count: 0})) | unique_by(.namespace) | sort_by(.count)'
The solution provides an array of json objects sorted by number of pods in ascending order and can be easily extended to obtain the desired output.
The "magic" happens when we add the contents of the $ns variable -which contains the namespaces- to the end of the array with count=0. These entries will be discarded by unique_by if the namespace was already found before -which means it contains pods-.
Note: the solution relies on jq's unique_by function to discard the second appearance of an object with the same key value, which as for the documentation it is not granted but it happens to be implemented in this way.
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 $_ }
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.