Get YAML for deployed Kubernetes objects

2 min read | by Jordi Prats

In case we have some objects on the kubernetes cluster that we want to modify but we don't have the original yaml, we can retrieve the object in yaml format from the cluster using kubectl

To do so we'll have to instruct kubectl to output the object in yaml format using the -o yaml option as follows:

apiVersion: v1
kind: Service
metadata:
  annotations:
    meta.helm.sh/release-name: pet2cattle
    meta.helm.sh/release-namespace: kube-system
  creationTimestamp: "2020-12-22T18:17:54Z"
  labels:
    app.kubernetes.io/instance: pet2cattle
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: pet2cattle
    app.kubernetes.io/version: "2.6"
    helm.sh/chart: pet2cattle-1
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:meta.helm.sh/release-name: {}
          f:meta.helm.sh/release-namespace: {}
        f:labels:
          .: {}
          f:app.kubernetes.io/instance: {}
          f:app.kubernetes.io/managed-by: {}
          f:app.kubernetes.io/name: {}
          f:app.kubernetes.io/version: {}
          f:helm.sh/chart: {}
      f:spec:
        f:ports:
          .: {}
          k:{"port":8000,"protocol":"TCP"}:
            .: {}
            f:name: {}
            f:port: {}
            f:protocol: {}
            f:targetPort: {}
        f:selector:
          .: {}
          f:app.kubernetes.io/instance: {}
          f:app.kubernetes.io/name: {}
        f:sessionAffinity: {}
        f:type: {}
    manager: Go-http-client
    operation: Update
    time: "2020-12-29T18:47:41Z"
  name: pet2cattle
  namespace: kube-system
  resourceVersion: "5284239"
  selfLink: /api/v1/namespaces/kube-system/services/pet2cattle
  uid: de1068f2-4da7-4a91-882d-4f5861b5c3fd
spec:
  clusterIP: 10.43.151.25
  ports:
  - name: http
    port: 8000
    protocol: TCP
    targetPort: http
  selector:
    app.kubernetes.io/instance: pet2cattle
    app.kubernetes.io/name: pet2cattle
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

There are going to be fields that we are not interested in, for example all the managedFields, the creationTimestamp or the object's uid, so we can just remove them from the yaml file


Posted on 11/01/2021

Categories