> For the complete documentation index, see [llms.txt](https://notes.nomanaziz.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.nomanaziz.me/devops/orchestration/kubernetes/4.-yaml-configuration-file.md).

# 4. YAML Configuration File

### Introduction

* Config File is written in YAML Format
* Config File should be stored with the code inside SCM

***

### 3 Parts of K8 Config File

1. Metadata
2. Specification
3. Status (Auto Generated & Added by K8)
   * K8 Gets Status from etcd

***

### Template

* The specified **kind** has its own *metadata* and *spec*
* Pod has also a section of its own which contains its own configuration&#x20;

<figure><img src="/files/TMJnG4Y7otiwoaS3Tly6" alt=""><figcaption></figcaption></figure>

#### Labels & Connectors

* *Metadata* section contain the labels
* *Spec* section contains the selectors

**Connecting Deployment to Pods**

* Deployment looks which pods belong to it using labels (key value pairs)
* The label is matched by the selector&#x20;

<figure><img src="/files/BwBYcWUidF6YAkVZACpe" alt=""><figcaption></figcaption></figure>

**Connecting Services to Deployments**

* Similar concept as connecting deployment to pods&#x20;

<figure><img src="/files/CcJHa8VfQW9NiD5UN61c" alt=""><figcaption></figcaption></figure>

#### Ports in Service & Pods

* Service listens on port and forwards the request to target port which must be of a pod&#x20;

<figure><img src="/files/GXZVoHCbKi8CuhMKa0Rn" alt=""><figcaption></figcaption></figure>

### Minimal Deployment File

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

***

### Minimal Service File

```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
```

***
