# YAML Manifest Management

## **YAML Manifest Management Overview**

In Kubernetes, YAML manifests are the standard way to define and manage the desired state of your resources, such as Pods, Services, Deployments, ConfigMaps, and Secrets. YAML manifests provide a clear and declarative method to describe how resources should be configured and deployed in your cluster. This section will guide you through the basics of YAML manifest management, including best practices for writing, applying, and version-controlling your manifests.

***

### **Understanding YAML Manifests**

A YAML manifest is a text file written in YAML (Yet Another Markup Language) format that defines the configuration of Kubernetes resources. Each manifest typically contains metadata about the resource, specifications for its desired state, and optional settings like labels, selectors, and environment variables.

* **Basic Structure of a YAML Manifest:**
  * **apiVersion:** Specifies the API version of the resource, such as `v1`, `apps/v1`.
  * **kind:** Defines the type of Kubernetes resource, such as `Pod`, `Deployment`, `Service`, etc.
  * **metadata:** Contains metadata about the resource, such as its `name`, `namespace`, and `labels`.
  * **spec:** Defines the desired state of the resource, including configurations, replicas, container images, and more.

Example of a simple Pod manifest:

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: myapp
spec:
  containers:
  - name: my-container
    image: nginx
    ports:
    - containerPort: 80
```

**Applying YAML Manifests**

To create or update resources in your Kubernetes cluster using a YAML manifest, you use the `kubectl apply` command. This command reads the manifest file and applies the specified configuration to the cluster, either creating new resources or updating existing ones to match the desired state.

* **Apply a YAML manifest:**

  ```bash
  kubectl apply -f <manifest.yaml>
  ```

  This command will create or update the resources defined in `manifest.yaml`.
* **Apply multiple manifests in a directory:**

  ```bash
  kubectl apply -f <directory>
  ```

  This command applies all the YAML manifests within a specified directory.

**Viewing and Managing Resources Defined by Manifests**

After applying a manifest, you may want to verify that the resources were created correctly or that their current state matches the desired configuration.

* **Get details about a resource:**

  ```bash
  kubectl get <resource-type> <resource-name>
  ```

  This command lists the resource and its current status.
* **Describe a resource in detail:**

  ```bash
  kubectl describe <resource-type> <resource-name>
  ```

  This provides detailed information about the resource, including its events, conditions, and any issues.
* **Delete resources defined in a manifest:**

  ```bash
  kubectl delete -f <manifest.yaml>
  ```

  This command removes the resources specified in the manifest from the cluster.

**Best Practices for Writing YAML Manifests**

1. **Use Meaningful Names:** Assign meaningful and descriptive names to your resources. This helps in identifying and managing them easily, especially in large clusters.
2. **Organize Manifests in Directories:** Store related manifests in organized directories, often categorized by environment (e.g., `dev/`, `staging/`, `production/`). This makes it easier to apply and manage resources for different environments.
3. **Use Labels and Annotations:** Apply labels and annotations to your resources to categorize and identify them. Labels are key-value pairs used for grouping and selecting resources, while annotations provide additional metadata.
4. **Parameterize Common Values:** Consider using tools like Helm or Kustomize to manage YAML templates and parameterize common values. This approach allows you to reuse and customize manifests across different environments without duplicating code.
5. **Version Control Manifests:** Store your YAML manifests in a version control system like Git. This enables you to track changes, roll back to previous configurations, and collaborate with team members. Each change to a manifest can be reviewed, tested, and deployed through CI/CD pipelines.
6. **Validate Manifests Before Applying:** Use tools like `kubectl apply --dry-run=client` to validate your manifests before applying them. This checks the manifest syntax and reports any potential issues without making changes to the cluster.
7. **Document Your Manifests:** Include comments in your YAML files to explain the purpose of each resource and any specific configurations. This documentation is valuable for team members who may need to understand or modify the manifests in the future.

**Advanced YAML Management Techniques**

* **Kustomize:** Kubernetes comes with built-in support for Kustomize, a tool that allows you to customize and manage multiple YAML files without altering the original manifests. Kustomize lets you apply overlays, patches, and configurations tailored to different environments.

  Example of applying a kustomization:

  ```bash
  kubectl apply -k <directory>
  ```
* **Helm Charts:** Helm is a package manager for Kubernetes that uses templated YAML manifests called charts. Helm allows you to manage complex applications with multiple resources, parameterizing values and managing dependencies.

  Example of deploying an application using Helm:

  ```bash
  helm install <release-name> <chart-name>
  ```

**Troubleshooting YAML Manifests**

* **Common YAML Errors:** YAML is sensitive to indentation and formatting, so common errors include incorrect indentation, missing colons, or unquoted special characters. Always ensure your YAML is properly formatted and validated before applying.
* **Debugging with `kubectl`:** If resources fail to deploy or behave unexpectedly, use `kubectl logs`, `kubectl describe`, and `kubectl get` commands to inspect logs, events, and resource statuses. This helps you identify and resolve issues related to manifest configurations.

***

### Kubectl Commands

#### **Applying and Managing YAML Manifests**

* **Apply a YAML manifest:**

  ```bash
  kubectl apply -f <manifest.yaml>
  ```

  This command creates or updates the resources defined in the `manifest.yaml` file.
* **Apply all YAML manifests in a directory:**

  ```bash
  kubectl apply -f <directory>
  ```

  This command applies all the YAML manifests within the specified directory, creating or updating the resources.
* **Dry-run to validate a manifest without applying it:**

  ```bash
  kubectl apply --dry-run=client -f <manifest.yaml>
  ```

  This command checks the manifest for any errors without actually applying it to the cluster.
* **View the details of a resource defined in a YAML manifest:**

  ```bash
  kubectl get <resource-type> <resource-name>
  ```

  Example:

  ```bash
  kubectl get pod my-pod
  ```

  This command retrieves the current status and details of the specified resource.
* **Describe a resource in detail:**

  ```bash
  kubectl describe <resource-type> <resource-name>
  ```

  Example:

  ```bash
  kubectl describe deployment my-deployment
  ```

  This command provides detailed information about the specified resource, including events, conditions, and any potential issues.
* **Delete resources defined in a YAML manifest:**

  ```bash
  kubectl delete -f <manifest.yaml>
  ```

  This command deletes the resources specified in the `manifest.yaml` file from the cluster.

#### **Advanced YAML Management**

* **Apply a kustomization directory (using Kustomize):**

  ```bash
  kubectl apply -k <directory>
  ```

  This command applies a kustomization, which is a set of YAML files managed by Kustomize, allowing you to customize Kubernetes resources.
* **Diff command to compare a local manifest with the live state:**

  ```bash
  kubectl diff -f <manifest.yaml>
  ```

  This command compares the current live configuration of the resource with the configuration defined in the `manifest.yaml` file.
* **View all applied resources:**

  ```bash
  kubectl get all -n <namespace>
  ```

  This command lists all resources in a specified namespace.

#### **Working with Labels and Annotations**

* **Add a label to a resource:**

  ```bash
  kubectl label <resource-type> <resource-name> <label-key>=<label-value>
  ```

  Example:

  ```bash
  kubectl label pod my-pod app=webserver
  ```

  This command adds a label to the specified resource, which can be used for selecting or managing resources.
* **Annotate a resource:**

  ```bash
  kubectl annotate <resource-type> <resource-name> <annotation-key>=<annotation-value>
  ```

  Example:

  ```bash
  kubectl annotate pod my-pod description="Production pod"
  ```

  This command adds an annotation to the specified resource, providing additional metadata.

#### **Debugging and Troubleshooting**

* **Get logs for a specific pod:**

  ```bash
  kubectl logs <pod-name>
  ```

  Example:

  ```bash
  kubectl logs my-pod
  ```

  This command retrieves the logs from the specified pod, useful for debugging issues.
* **View events related to a resource:**

  ```bash
  kubectl get events --field-selector involvedObject.name=<resource-name>
  ```

  This command lists all events related to the specified resource, helping to diagnose problems.
* **Get resource usage information:**

  ```bash
  kubectl top pod <pod-name>
  ```

  This command shows the CPU and memory usage of the specified pod.

***

### Summary

These commands are essential for managing YAML manifests in Kubernetes, ensuring that your resources are correctly applied, updated, and maintained according to the desired state defined in the manifests.

By mastering YAML manifest management, you’ll be able to define, deploy, and manage Kubernetes resources efficiently and consistently. Following best practices ensures that your Kubernetes environments remain organized, scalable, and easy to maintain.


---

# Agent Instructions: 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:

```
GET https://cthfm-k8s.gitbook.io/kubernetes/kubernetes-fundamentals/kubectl/yaml-manifest-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
