> For the complete documentation index, see [llms.txt](https://notebook.iuriioapps.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notebook.iuriioapps.com/containers-and-microservices/kubernetes/kubernetes-management-techniques/declarative-yaml.md).

# Declarative YAML

* Kubernetes configuration files - YAML or JSON
* Each file contains one or more manifests
* Each manifest describes an API object (deployment, job, secret)
* Each manifest needs 4 parts (root key:values in the file):
  * apiVersion - Which version of the Kubernetes API you’re using to create this object
  * kind - What kind of object you want to create
  * metadata - Data that helps uniquely identify the object, including a name string, UID, and optional namespace
  * spec - What state you desire for the object

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.17.3
    ports:
    - containerPort: 80
```

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.3
        ports:
        - containerPort: 80
```

Declaring multiple types in a single file - separate them with "---"

```yaml
apiVersion: v1
kind: Service
metadata:
  name: app-nginx-service
spec:
  type: NodePort
  ports:
  - port: 80
  selector:
    app: app-nginx
--- # !!! Separator
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app-nginx
  template:
    metadata:
      labels:
        app: app-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.3
        ports:
        - containerPort: 80
```

* `kubectl api-resources`
* `kubectl api-versions`
* We can get all the keys each kind supports: `kubectl explain <kind> --recursive`
* We can drill down into subnodes of the kind:

  &#x20; `kubectl explain <kind>.<subnode>`

  &#x20; `kubectl explain services.spec`

### Dry Runs with Apply YAML

* dry run client-side only:

  &#x20; `kubectl apply -f file.yml --dry-run`
* dry run server-side (will compare current file with what's already deployed on server):

  &#x20; `kubectl apply -f file.yml --server-dry-run`
* see the diff visually:

  &#x20; `kubectl diff -f file.yml`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notebook.iuriioapps.com/containers-and-microservices/kubernetes/kubernetes-management-techniques/declarative-yaml.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
