Where is the full Kubernetes YAML spec? - kubernetes

There must be "full-configuration" and example templates of Kubernetes YAML configs somewhere with comments itemizing what parameters do what with runnable examples somewhere.
Does anyone know where something like this might be? Or where the "full API" of the most commonly used Kubernetes components are?

There is documentation for every k8s api version available, for example check this link.
The way I found what every key in yaml file represent and what does it mean is via kubectl explain command.
For example:
$kubectl explain deploy.spec
Trick I use while doing CKAD to see full list could be:
$kubectl explain deploy --recursive > deployment_spec.txt
This will list all available options for kubernetes deployment that could you use in yaml file.
To generate some template there is option to use --dry-run and -o yaml in kubectl command, for example to create template for CronJob:
$kubectl run cron_job_name --image=busybox --restart=OnFailure --schedule="*/1 * * * * " --dry-run -o yaml > cron_job_name.yaml

Related

How to write Kubernetes annotations to the underlying YAML files?

I am looking to apply existing annotations on a Kubernetes resource to the underlying YAML configuration files. For example, this command will successfully find all pods with a label of "app=helloworld" or "app=testapp" and annotate them with "xyz=test_anno":
kubectl annotate pods -l 'app in (helloworld, testapp)' xyz=test_anno
However, this only applies the annotations to the running pods and doesn't change the YAML files. How do I force those changes to the YAML files so they're permanent, either after the fact or as part of kubectl annotate to start with?
You could use the kubectl patch command with a little tricks
kubectl patch $(k get po -l 'app in (helloworld, testapp)') -p '{"metadata":{"annotations":{"xyz":"test_anno"}}}'

How to view the manifest file used to create a Kubenetes resource?

I have K8s deployed on an EC2 based cluster,
There is an application running in the deployment, and I am trying to figure out the manifest files that were used to create the resources,
There were deployment, service and ingress files used to create the App setup.
I tried the following command, but I'm not sure if it's the correct one as it's also returning a lot of unusual data like lastTransitionTime, lastUpdateTime and status-
kubectl get deployment -o yaml
What is the correct command to view the manifest yaml files of an existing deployed resource?
There is no specific way to do that. You should store your source files in source control like any other code. Think of it like decompiling, you can do it, but what you get back is not the same as what you put in. That said, check for the last-applied annotation, if you use kubectl apply that would have a JSON version of a more original-ish manifest, but again probably with some defaulted fields.
You can try using the --export flag, but it is deprecated and may not work perfectly.
kubectl get deployment -o yaml --export
Refer: https://github.com/kubernetes/kubernetes/pull/73787
KUBE_EDITOR="cat" kubectl edit secrets rook-ceph-mon -o yaml -n rook-ceph 2>/dev/null >user.yaml

kubectl diff fails on AKS

I'd like to diff a Kubernetes YAML template against the actual deployed ressources. This should be possible using kubectl diff. However, on my Kubernetes cluster in Azure, I get the following error:
Error from server (InternalError): Internal error occurred: admission webhook "aks-webhook-admission-controller.azmk8s.io" does not support dry run
Is there something I can enable on AKS to let this work or is there some other way of achieving the diff?
As a workaround you can use standard GNU/Linux diff command in the following way:
diff -uN <(kubectl get pods nginx-pod -o yaml) example_pod.yaml
I know this is not a solution but just workaround but I think it still can be considered as full-fledged replacement tool.
Thanks, but that doesn't work for me, because it's not just one pod
I'm interested in, it's a whole Helm release with deployment,
services, jobs, etc. – dploeger
But anyway you won't compare everything at once, will you ?
You can use it for any resource you like, not only for Pods. Just substitute Pod by any other resource you like.
Anyway, under the hood kubectl diff uses diff command
In kubectl diff --help you can read:
KUBECTL_EXTERNAL_DIFF environment variable can be used to select your
own diff command. By default, the "diff" command available in your
path will be run with "-u" (unified diff) and "-N" (treat absent files
as empty) options.
The real problem in your case is that you cannot use for some reason --dry-run on your AKS Cluster, which is question to AKS users/experts. Maybe it can be enabled somehow but unfortunately I have no idea how.
Basically kubectl diff compares already deployed resource, which we can get by:
kubectl get resource-type resource-name -o yaml
with the result of:
kubectl apply -f nginx.yaml --dry-run --output yaml
and not with actual content of your yaml file (simple cat nginx.yaml would be ok for that purpose).
You can additionally use:
kubectl get all -l "app.kubernetes.io/instance=<helm_release_name>" -o yaml
to get yamls of all resources belonging to specific helm release.
As you can read in man diff it has following options:
--from-file=FILE1
compare FILE1 to all operands; FILE1 can be a directory
--to-file=FILE2
compare all operands to FILE2; FILE2 can be a directory
so we are not limited to comparing single files but also files located in specific directory. Only we can't use these two options together.
So the full diff command for comparing all resources belonging to specific helm release currently deployed on our kubernetes cluster with yaml files from a specific directory may look like this:
diff -uN <(kubectl get all -l "app.kubernetes.io/instance=<helm_release_name>" -o yaml) --to-file=directory_containing_yamls/

how to generate kubernetes deployment yaml file using template generator

I am kubernetes newbie, and I have a basic question
my understanding from https://kubernetes.io/docs/reference/kubectl/conventions/ is , we can generate yaml templates using "kubernetes run" command
But when I tried doing same, it didn't work as expected
kubectl run deployment-sample --image deployment-sample --dry-run -o yaml --generator=extensions/v1beta1
error: generator "extensions/v1beta1" not found
kubectl run deployment-sample --image deployment-sample --dry-run -o yaml --generator=apps/v1beta1
error: generator "apps/v1beta1" not found
Not sure if my understanding is wrong or something wrong in my command ?
I am on kubernetes 1.11
I find that I can create a Deployment with kubectl run --generator=deployment/v1beta1 foobar --image=nginx -o yaml --dry-run so your case would be kubectl run --generator=deployment/v1beta1 deployment-sample --image=deployment-sample -o yaml --dry-run. The kubectl conventions page you refer to does say this generator is 'recommended' for Deployments.
But I'm not sure why the docs list a non-recommended generator option that actually doesn't work. For a command like this you can recreate the expected output in a reference environment through the online tutorials at https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/ You get the same output there so it is not just you or your cluster. My best guess is that 'extensions/v1beta1' is too general to match to a deployment specifically. It could well be that the documentation needs changing on this.

kubectl YAML documentation for a resource

Is there a kubectl command that gives advice about essential fields required in the YAML, or YAML examples, for a given resource?
Currently, when I want a YAML example of a resource, for example a deployment, I search the online documentation. I'd rather stay in the terminal.
kubectl explain deployment works for resources which publish openapi specs.