> For the complete documentation index, see [llms.txt](https://dinush-chathurya.gitbook.io/kustomize-e-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dinush-chathurya.gitbook.io/kustomize-e-book/kustomize-fundamentals/05-managing-environments.md).

# Managing Environments

Kustomize shines when it comes to managing different Kubernetes environments such as development, staging, and production. In this chapter, we’ll explore how to structure, maintain, and deploy to multiple environments efficiently and safely.

***

## Why Manage Environments Separately?

Each environment often has different:

* **Replica counts**
* **Resource limits**
* **Image versions**
* **External services**
* **Access policies**

Isolating environments allows teams to:

* Develop safely without affecting production
* Test features before release
* Apply stricter policies in production

***

## Folder Structure Revisited

Use a consistent layout under the `overlays/` directory:

```yaml
kustomize/
└── overlays/
    ├── dev/
    ├── staging/
    └── prod/
```

Each folder includes:

* A `kustomization.yaml` file
* A `patches/` directory for environment-specific changes

***

## Example Use Cases

### 1. Dev Environment

Run fewer replicas, enable debug mode, use `:dev` image tag.

```yaml
# overlays/dev/patches/replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
```

```yaml
# overlays/dev/patches/image.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
        - name: my-app
          image: my-app:dev
```

### 2. Staging Environment

Use real services but not full-scale. Same image as dev but with staging configs.

```yaml
# overlays/staging/patches/replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
```

### 3. Production Environment

* Use stable images
* Set proper resource limits
* Enable autoscaling (optional)

```yaml
# overlays/prod/patches/image.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
        - name: my-app
          image: my-app:stable
```

## Deployment Commands

You can deploy each environment using:

```bash
kustomize build overlays/dev | kubectl apply -f -
kustomize build overlays/staging | kubectl apply -f -
kustomize build overlays/prod | kubectl apply -f -
```

If you're using ArgoCD, each environment can be its own Application referencing a specific path.

## Best Practices

* Always test in dev before staging or prod
* Use the same base for all environments to reduce drift
* Automate deployment via CI/CD (we'll cover this later)
* Review each overlay in PRs to catch misconfigurations


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dinush-chathurya.gitbook.io/kustomize-e-book/kustomize-fundamentals/05-managing-environments.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
