1

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?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150
  • Does this answer your question? [Get YAML for deployed Kubernetes services?](https://stackoverflow.com/questions/43941772/get-yaml-for-deployed-kubernetes-services) – Tummala Dhanvi Apr 27 '20 at 05:11
  • Yes looks similar but apparently no accepted answer to that question, let me try those suggestions If works I'll mark the correct or the closest correct answer here, else I'll answer what worked for me – Dev1ce Apr 27 '20 at 05:14

3 Answers3

3

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.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Is there a way to directly edit the manifest files, that were used to create the resource? I was told the files were directly edited and updated to modify the resources, how can I get the exact updated manifest file? – Dev1ce Apr 27 '20 at 04:53
  • You can use `kubectl edit` to edit the live object data if that's what you mean. There is no consistent way to recover the original manifest, that needs to be stored somewhere by you. – coderanger Apr 27 '20 at 05:02
  • if I try to edit the live object, I should be able to get the updated file yea? how can I do that? kubectl edit deployment.yaml ? – Dev1ce Apr 27 '20 at 05:11
  • 1
    `kubectl edit deployment -n somenamespace deploymentname` – coderanger Apr 27 '20 at 05:17
  • cool, I'm able to get the config what was used latest to update, can you add this to your answer please? – Dev1ce Apr 27 '20 at 05:20
  • It's not the answer to your question per se, edit is showing you the same thing as `get -o yaml`. Edit is just get, open in editor, update. – coderanger Apr 27 '20 at 05:47
  • @coderanger Some simplistic decompilers do an inadequate job, but some good decompilers do a good enough job. Hence, although no guarantee of 100% recovery, I'd hope that it's possible to create a tool (if people bother to write one) that filters out 90% of the junk for us (e.g, `lastTransitionTime`). But so far, there doesn't seem to be such a tool. And that leads me to guess that this problem is theoretically much worse than decompilers. My guess is that it's easy to reconstruct most of the original yaml of standard resources (e.g., `pod`) but impossible to anticipate custom resources. – Vincent Yin Jul 18 '21 at 14:02
2

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

Tummala Dhanvi
  • 3,007
  • 2
  • 19
  • 35
  • As mentioned on some other answers, this is deprecated because it does not work consistently. Don't use it. – coderanger Apr 27 '20 at 05:17
  • I agree that this doesn't solve the question exactly, but this is the closest solution that already exists AFIK, one can always build custom tools to drop the fields and get the same. – Tummala Dhanvi Apr 27 '20 at 05:59
  • You are incorrect, there is no generic way to do this :) Again, this is why the command option was deprecated. – coderanger Apr 27 '20 at 06:48
-1
KUBE_EDITOR="cat" kubectl edit secrets rook-ceph-mon -o yaml -n rook-ceph 2>/dev/null >user.yaml
John Karasev
  • 151
  • 2
  • 11