# deployments

Kubernetes provides advanced deployment strategies like rolling updates and canary releases to help manage the deployment of applications with minimal downtime and risk. Here's an overview of how to use these strategies:

#### 1. **Rolling Updates:**

A rolling update gradually replaces instances of the old application with the new one, ensuring continuous availability during the deployment.

**Example Rolling Update Deployment YAML:**

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app:v2
          ports:
            - containerPort: 80
```

* **Apply the Deployment:**

  ```bash
  kubectl apply -f deployment.yaml
  ```
* **Monitor the Rolling Update:**

  ```bash
  kubectl rollout status deployment/my-app
  ```
* **Rollback to Previous Version:**

  ```bash
  kubectl rollout undo deployment/my-app
  ```

#### 2. **Canary Releases:**

A canary release introduces a new version of the application to a subset of users or traffic, allowing testing and monitoring before a full rollout.

**Example Canary Release Deployment YAML:**

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 5
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app:v1
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-app-ingress
spec:
  rules:
    - host: my-app.example.com
      http:
        paths:
          - backend:
              serviceName: my-app-service
              servicePort: 80
```

* **Apply the Canary Release Deployment:**

  ```bash
  kubectl apply -f canary-deployment.yaml
  ```
* **Monitor and Test:** Observe metrics, logs, and user feedback from the canary release before proceeding.
* **Promote Canary to Full Release:** If the canary release is successful, update the Deployment to the new version for all instances.

  ```bash
  kubectl set image deployment/my-app my-app=my-app:v2
  ```
* **Rollback Canary Release:** If issues are detected, rollback to the previous version.

  ```bash
  kubectl rollout undo deployment/my-app
  ```

#### Additional Tips:

* **Automate Deployments:** Use CI/CD tools like Jenkins, GitLab CI, or GitHub Actions to automate the deployment process.
* **Monitoring and Observability:** Integrate monitoring and observability tools (e.g., Prometheus, Grafana, ELK stack) to gain insights into the performance and behavior of your applications during deployments.
* **Progressive Delivery Tools:** Consider using progressive delivery tools like Istio for more advanced traffic management and canary release capabilities.
* **Security Considerations:** Implement security best practices, and ensure that your deployments follow security policies and guidelines.
* **Documentation:** Document deployment processes and strategies for the team, making it easier for others to understand and contribute.

Deploying applications on Kubernetes involves a mix of YAML configuration, kubectl commands, and potentially additional tools for monitoring and management. Tailor the examples above to match your specific application and deployment requirements.


---

# 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://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/kubernetes-basics/deployments.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.
