How to get the custom attributes in Kubernetes? - kubernetes

How to list the current deployments running in Kubernetes with custom columns displayed as mentioned below:
DEPLOYMENT CONTAINER_IMAGE READY_REPLICAS NAMESPACE
The data should be sorted by the increasing order of the deployment name.

Look a the -o custom-columns feature. https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns shows the basics. The hard one would be container_image, since a pod can contain more than one, but assuming you just want the first, something like .spec.template.containers[0].image? Give a shot and see how it goes.

Command to get the custom columns as per the question:
Kubectl get deployments -o custom-columns=DEPLOYMENT:.metadata.name,CONTAINER_IMAGE:..image,READY_REPLICAS:..replicas,NAMESPACE:..namespace

Related

Command for listing all the possible values you can put in the yaml

For example I want to list all the possible values I can put in the yaml file to create a Pod. I want to also look at the meanings of those values. e.g. in the container section you put in the details of the container
You can see field documentation for a Pod with
kubectl explain Pod
then you can look deeper into the structure with e.g. (or any other field)
kubectl explain Pod.spec

How to `kubectl get all` in k9s?

Instead of navigating a namespace via e.g. :service, then :pod etc, I would like to see everything that's in the namespace in a single view. As if you would type kubectl -n argocd get all.
Can't find the info in the docs. Is this even possible?
Thanks for any suggestion!
Posting community wiki answer based on GitHub topic - Show multiple resource types without having to switch. Feel free to expand it.
That's true, there is no information about this in the documentation because simply there is no such possibility. There is open issue with this request on the GitHub page of k9s:
Is your feature request related to a problem? Please describe.
Oftentimes I'm watching/working on multiple resource types at the same time and it's very helpful to not have to switch from one to another. This is something very like kubectl get pod,deploy,... or kubectl get-all commands allows
Describe the solution you'd like
Being able to see multiple or all resources in the same screen without having to switch between resource types like:
:pod,configmap shows all pods & configmaps in the current namespace
or
:all shows all resources in the current namespace (get-all like)
Last pinged November 4 2021.
You can try
kubectl get all -n argocd -o yaml
or
kubectl get all -n argocd -o json
to list all common resources in a particular namespace
Note: It will not list the CRD or other custom resources like helm

Manually creating and editing Kubernetes objects

Most Kubernetes objects can be created with kubectl create, but if you need e.g. a DaemonSet — you're out of luck.
On top of that, the objects being created through kubectl can only be customized minimally (e.g. kubectl create deployment allows you to only specify the image to run and nothing else).
So, considering that Kubernetes actually expects you to either edit a minimally configured object with kubectl edit to suit your needs or write a spec from scratch and then use kubectl apply to apply it, how does one figure out all possible keywords and their meanings to properly describe the object they need?
I expected to find something similar to Docker Compose file reference, but when looking at DaemonSet docs, I found only a single example spec that doesn't even explain most of it's keys.
The spec of the resources in .yaml file that you can run kubectl apply -f on is described in Kubernetes API reference.
Considering DeamonSet, its spec is described here. It's template is actually the same as in Pod resource.

kubectl set image error: arguments in resource/name form may not have more than one slash (kubernetes)

I want to deploy my project to the Kubernetes cluster. I want to deploy it by using command:
- kubectl set image deployment/$CLUSTER_NAME gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest
But here I get error :
It's a misleading error message.
Essentially instead of abcxyz/abcxyz:example you also need to specify the container name that the image should be assigned to so for example example=abcxyz/abcxyz:example.
It's quite complicated and misleading, I got to say. Public docs don't help much but the kubectl set image --help does.
The problem is that you might have MULTIPLE instances in the deployment. If you have only one you can do something like this (note that this works but it's not AS SPECIFIC as you might want):
# The part before = is the spec.template.spec.containers.name which is image's brother
kubectl set image deployments goliardiait-staging=gcr.io/goliardia-prod/goliardia-it-matrioska:2.12 --all
I'll update when I find what nails it. In your case:
kubectl set image deployment $CLUSTER_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest --all
- kubectl set image deployment/$CLUSTER_NAME $INSTANSE_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest
It is working with using command like this

Get replica set of the deployment

I can get the ReplicaSet if given the replica-set-name using the api as below:
GET /apis/apps/v1/namespaces/{namespace}/replicasets/{name}
But how can I get the ReplicaSet based on the deployment?
Any help is appreciated.
Thank you
But how can I get the ReplicaSet based on the deployment?
With quite some gymnastics... If you inspect how kubectl does it (by executing kubectl -n my-namespace describe deploy my-deployment --v=9) you will see that it dos the following:
first gets deployment details with: /apis/extensions/v1beta1/namespaces/my-namespace/deployments/my-deployment. From there it gets labels for replicaset selection.
then gets replicaset details using labels from deployment in previous step (say labels were my-key1:my-value1 and my-key2:my-value2) like so: /apis/extensions/v1beta1/namespaces/my-namespace/replicasets?labelSelector=my-key1%3Dmy-value1%2Cmy-key2%3Dmy-value2
Funny part here begins with extracting multiple labels from deployment output and formatting them for replicaset call, and that is task for grep, awk, jq or even python, depending on your actual use case (from bash, python, some client or whatever...)