Get values of deployed images helm/kubernetes - kubernetes

I'm looking for an easy way to find what version of my images I have deployed in my kubernetes environment.
The closest thing I can find to what I want is helm get values <namespace> -a
(but this gets values and dumps all (computed) values)
Is there an easier/clean way to get a list of images and versions deployed??
Thanks in advance

You can use kubectl to get all images form all pods running in the namespace/cluster. See List All Container Images Running in a Cluster.
For one namespace:
kubectl get pods -n <namespace> -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
For the whole cluster:
kubectl get pods --all-namespaces -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c

I use something like this:
kubectl get po --all-namespaces -o yaml | grep image: | cut -d ":" -f2,3 | sort | uniq
this command shows all images used in your cluster and removes the duplicates.

You can use the helm plugin - helm images.
URL: https://github.com/nikhilsbhat/helm-images
Usage:
images get [RELEASE] [CHART] [flags]

Related

How to get the count of the jobs running using kubectl command?

I am able to get the jobs under the namespace using kubectl command:
kubectl get jobs --namespace xxx
This is giving me the jobs information
I would like to print the count of jobs using kubectl command. How to do that?
go-template way, no pipes or installation needed. Just good old kubectl:
kubectl get jobs --namespace xxx -o go-template='{{printf "%d\n" (len .items)}}'
len is an inbuild function in go-template to return the number of elements to its argument. Eg: items.
If you are want to get number of job that are running in namespace you can get it using like this also
kubectl get jobs -n dcs | grep -v NAME | wc -l
You can use the, wc or jq for this
kubectl get jobs --output name | wc -l
with jq :
kubectl get jobs --output json | jq -j '.items | length'

How to get a list of kube-contexts using cli

I am trying to get a list of kube-contexts (and filtering for gke clusters) and with some tools ended up with that:
kubectl config get-contexts | tr -s ' ' | cut -d ' ' -f 2 | grep gke
output:
gke_dev-redacted
gke_prod-redacted
Is there an easier way to do that (which does not depend on the fact that the command output does not use tabs, but multiple whitespaces). yaml or json output is not supported by that command:
--output yaml is not available in kubectl config get-contexts; resetting to default output format
You can provide the --output flag to display only the contexts' names, for example:
$ kubectl config get-contexts --output=name
minikube
It's then easy to grep for GKE contexts:
$ kubectl config get-contexts --output=name | grep "gke_*"

Delete Kubernetes namespace only if it's empty?

I am using Helm to deploy multiple "components" of my application into a single namespace and using Jenkins to trigger create and destroy jobs. It doesn't seem that I can use Helm to delete the namespace thus I am looking to just use a Kubernetes command.
However, It seems that if I use kubectl delete namespace it will forcefully destroy the namespace and all its resources.
I'd like to destroy the namespace only if it is empty. Is there a command to do this?
I'd like destroy the namespace only if it is empty. Is there a command
to do this?
No there is not command to do that. This behavior is by design.
I would suggest a different approach. You should have all your deployment yamls in version control system for all of the components including namespace. When you want to create use kubectl create -f deployment.yaml and when you want to delete use kubectl delete -f deployment.yaml
See Remove Empty Namespaces Operator, it can do exactly what you want.
Why? Because it's not so easy to iterate over resources in the namespace to decide if it's empty or not. After all, there are "default resources" like default service account and probably other stuff from you tooling/operators.
So these resources should be excluded from iteration. Bash scripting becomes too complicated this way. And one day I decided to implement it with Python.
You can run kubectl get all --namespace YOUR_NAMESPACE and then depends on output call delete namespace
try this, better iterate over kube-api resources and this will give every resource list inside the namespace.
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n
<namespace>
or another approch
kubectl api-resources --verbs=list --namespaced -o name | `
%{ kubectl get $_ --show-kind --ignore-not-found -l <label>=<value> -n
<namespace> }
There's not a simple command to check a namespace before delete, it requires some kubectl scripting or a kube API client.
From the github issue discussing get alls limitations liggit provides an example and adding some jq processing you can get a (slow) command that errors unless it successfully finds all resource types are empty (no items):
set -o pipefail
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --ignore-not-found -n YOUR_NAMESPACE -o json \
| jq '.items[] | .kind + "/" + .metadata.name | error'
just use folloing to delete all empty namespaces
kubectl get ns --no-headers -o custom-columns=":metadata.name" | xargs -I{} kubectl get all -n {} 2>&1 | grep "No" | cut -d " " -f 5 | xargs -I{} kubectl delete namespace {}
you can list empty namespaces by this
kubectl get ns --no-headers -o custom-columns=":metadata.name" | xargs -I{} kubectl get all -n {} 2>&1 | grep "No" | cut -d " " -f 5

Get an environment variable from kubernetes pods and store it in an array

The use case is to get the environment variable *COUNTRY from all the pods running in a namespace
kubectl get pods podname -n namespace -o 'jsonpath={.spec.containers[0].env[?(#.name~="^COUNTRY")].value}'
This does not seem to work. any lead?
You can retrieve this information using the following command:
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.containers[*].env[*].name}{"\t"}{.spec.containers[*].env[*].value}{"\n"}{end}' | grep COUNTRY | cut -f 2
It will return the variables content as follows:
$ kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.containers[*].env[*].name}{"\t"}{.spec.containers[*].env[*].value}{"\n"}{end}' | grep VAR | cut -f 2
123456
7890123
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].env[?(#.name=="COUNTRY")].value}'
Hope this helps. I was just able to run it on mine and it worked the best.

How do I extract multiple values from kubectl with jsonpath

I've found jsonpath examples for testing multiple values but not extracting multiple values.
I want to get image and name from kubectl get pods.
this gets me name
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].name}' | xargs -n 1
this gets me image
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}' | xargs -n 1
but
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].[name,image}' | xargs -n 2
complains invalid array index image - is there a syntax for getting a list of node-adjacent values?
Use below command to get name and image:
kubectl get pods -Ao jsonpath='{range .items[*]}{#.metadata.name}{" "}{#.spec.template.spec.containers[].image}{"\n"}{end}'
It will give output like below:
name image
Useful command, I had to modify it a little to make it work (failed with -a flag). Also, I added a filter to app label and one more field to get: namespace, pod name, image
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{#.metadata.namespace}{"\t"}{#.metadata.name}{"\t"}{#.spec.containers[*].image}{"\n"}{end}' -l app=nginx
Thanks! I had to change a little bit, but this worked for me:
#!/bin/bash
releases=$(kubectl get deployment -A --output=jsonpath='{range .items[*]}{#.metadata.namespace}{"|"}{#.metadata.name}{"\n"}{end}')
for release in $releases; do
namespace=$( echo $release | cut -d "|" -f 1)
deployment=$( echo $release | cut -d "|" -f 2)
kubectl rollout restart deployments -n "${namespace}" "${deployment}"
done