Read Kubernetes deployment annotation - kubernetes

How can I get a particular Kubernetes annotation from a deployment resource using kubectl? I know I can dynamically set an annotation on a deployment using:
kubectl annotate deployment api imageTag=dev-ac5ba48.k1dp9
Is there a single kubectl command to then read this deployments imageTag annotation?

You can use the following command to get the imageTag annotation (given that annotation exists):
kubectl get deploy DEPLOY_NAME -o jsonpath='{.metadata.annotations.imageTag}'

You can use jsonpath for that:
kubectl get deployment api -o=jsonpath='{.metadata.annotations}'
The command above will give you all the annotations of your deployment api.
For reference you can take a look at this doc page as it may help.

Related

oc get deployment is returning No resources found

"oc get deployment" command is returning "No resources Found" as the result.
Even if I put an option of assigning or defining the namespace using -n as the option to above command, I am getting the same result.
Whereas, I am getting the correct result of oc get pods command.
Meanwhile, the oc version is
oc - v3.6.0
kubernetes - v1.6.1
openshift - v3.11.380
Check, if you connect to the correct kubernetes environment, (especially if you're running more than one).
If that is correct, I guess, either you don't have any deployments at all, or the deployments are in a different namespace than you think.
Try out listing all deployments:
oc get deployments -A
There are other objects that create pods such as statefulset or deamonset. Because it is OpenShift, my feeling is that the pods created by a deploymentconfig which is popular way to create applications.
Anyway, you can make sure which object is the owner of the pods by looking into the pod annotation. This command should work:
oc get pod -o yaml <podname> | grep ownerReference -A 6

linkerd Inject with helm or namespace?

I can't seem to find a simple answer to my question,
how to use linkerd inject command / options to add when using helm to install a package, e.g. like postgres?
I have done it with another package but that was by adding the annotation command to a values file and supplying that when running the helm install command.
With istio, all I have to do is to add a label on the namespace and it works?
So I started to look into adding the annotation to the namespaces I am working with, using the kubectl create namespace command?
However, I cant seem to find a way to add any annotation at the point of creating a namespace, unless I use a file.
So, I either need a way to add this annotation to the namespace with the create command or when installing packages with helm?
Thanks,
I think there are a couple of ways to do this. It all depends on what you are trying to achieve and how you'd like to manage your underlying infrastructure.
I assume you want to automate the installation of helm charts. If you are going to create the namespace using kubectl create namespace then you might be able to follow that up with kubectl annotate <created-namespace> "linkerd.io/inject=enabled".
Alternatively, you can make use of the Linkerd CLI and use the inject command provided -- the workflow here would involve a combination of kubectl and linkerd commands so I'm not sure it's what you're looking for. Nonetheless, you can do something like kubectl create namespace <my-namespace> -o yaml | linkerd inject - | kubectl apply -f -.
Last but not least, if you can use kubectl create namespace then you might be able to pipe the namespace manifest to kubectl directly and call it a day? You can use something similar to the snippet below:
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
name: foo
annotations:
linkerd.io/inject: enabled
EOF

How to give annotations by using run command in kubernetes to a pod

I attempted but there is an error..i also see See 'kubectl run --help' for usage.
but i can't fix it..
kubectl run pod pod4 --image=aamirpinger/helloworld:latest --port=80 --annotaions=createdBy="Muhammad Shahbaz" --restart=Never
Error: unknown flag: --annotaions
kubectl run supports specifying annotations via the --annotations flag that can be specified multiple times to apply multiple annotations.
For example:
$ kubectl run --image myimage --annotations="foo=bar" --annotations="another=one" mypod
results in the following:
$ kubectl get pod mypod -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
foo: bar
another: one
[...]
kubectl run doesn't have an option to set annotations.
Unless you're running a one-off debugging pod, it's usually better practice to write out the full (Deployment) YAML file, commit to source control, and install it using kubectl apply -f. That will let you specify any Kubernetes object property you need to.
As David Maze mentioned ,there is no --annotations flag for kubectl run command.It is better to write deployment yaml file compared to running using kubectl run command.
However you can add annotations to kubernetes resources using Kubectl annotate command.All Kubernetes objects support the ability to store additional data with the object as annotations.
Hope this helps.

kubernetes get values from already deployed pod/daemonset

someone before me deployed daemonset, and configmap is it possible for me to somehow get the values he used ? smth like kubectl edit <name> but the edit option has some temporary data in it too - the name of pod with random chars etc. - and to get pure values used in that deployments/daemonset what command would I need to use?
kubectl get --export had bugs like this and this and --export will be deprecated in k8s v1.18 per this link.
$ kubectl get --export
Flag --export has been deprecated, This flag is deprecated and will be removed in future.
...
kubectl get -o yaml can be used to get values of a k8s resource's manifest along with metadata and status of the resource. kubectl get -o yaml has the following three sections:
metadata
spec with the values of the k8s resource's manifest
status with the resource's status
How about kubectl get --export?

How to get annotation from namespace in side car injector

I am working with Istio. There are certain annotations that we add to our kubernetes namespace. One out of these namespace annotations also needs to be applied to the pods that are created with sidecar-enabled=true label. For this purpose, I looked at using the Istio sidecar injector webhook, but I am not able to find the reference to namespace's annotations.
Is there a way to do this?
You can find all need namespaces annotations using below command in Annotations: section.
kubectl describe namespaces
EDIT:
Your initial question is not clear. As far as I understand your question and additional clarification - you want to get annotations that are applied to a namespace from a configMap.
Official Istio Sidecar Injection Documentation says that
Manual and automatic injection both use the configuration from the
istio-sidecar-injector and istio ConfigMaps in the istio-system
namespace.
Based on this fact you can dump the configMap in the Istio cluster you are interested in by next command:
$ kubectl describe configmap --namespace=istio-system istio-sidecar-injector
This will show you references for pod annotations, global values, etc.
Example:
[[ annotation .ObjectMeta `traffic.sidecar.istio.io/includeOutboundIPRanges` "*" ]]
The above queries traffic.sidecar.istio.io/includeOutboundIPRanges annotation on the pod, and defaults to "*" if it's not present.