# Optimize cost of a Kubernetes microservice while preserving SLOs in production

In this example, you will use Akamas live optimization to minimize the cost of a Kubernetes deployment, while preserving application performance and reliability requirements.

## Prerequisites

In this example, you need:

* an Akamas instance
* a Kubernetes cluster, with a deployment to be optimized
* the `kubectl` command installed in the Akamas instance, configured to access the target Kubernetes and with privileges to get and update the deployment configurations
* a supported telemetry data source (e.g. Prometheus or Dynatrace) configured to collect metrics from the target Kubernetes cluster

## **Optimization setup**

### **Optimization packs**

This example leverages the following optimization packs:

* [Kubernetes](https://docs.akamas.io/akamas-docs/3.6/reference/optimization-packs/kubernetes-pack)
* [Web application](https://docs.akamas.io/akamas-docs/3.6/reference/optimization-packs/web-application-pack/web-application)

### System

The system represents the Kubernetes deployment to be optimized (let's call it "frontend"). You can create a `system.yaml` manifest like this:

```yaml
name: frontend
description: Kubernetes frontend deployment
```

Create the new system resource:

```bash
akamas create system system.yaml
```

The system will then have two components:

* A Kubernetes container component, which contains container-level metrics like CPU usage and parameters to be tuned like CPU limits
* A Web Application component, which contains service-level metrics like throughput and response time

In this example, we assume the deployment to be optimized is called *frontend,* with a container named *server*, and is located within the *boutique* namespace. We also assume that Dynatrace is used as a telemetry provider.

#### Kubernetes component

Create a `component-container.yaml` manifest like the following:

```yaml
name: container
description: Kubernetes container, part of the frontend deployment
componentType: Kubernetes Container
properties:
  dynatrace:
    type: CONTAINER_GROUP_INSTANCE
    kubernetes:
      namespace: boutique
      containerName: server
      basePodName: frontend-*
```

Then run:

```bash
akamas create component component-container.yaml frontend
```

Now create a `component-webapp.yaml` manifest like the following:

```yaml
name: webapp
description: The service related to the frontend deployment
componentType: Web Application
properties:
  dynatrace:
    id: <TELEMETRY_DYNATRACE_WEBAPP_ID>
```

Then run:

```bash
akamas create component component-webapp.yaml frontend
```

### **Workflow**

The workflow in this example is composed of three main steps:

1. Update the Kubernetes deployment manifest with the parameters (CPU and memory limits) recommended by Akamas
2. Apply the new parameters (kubectl apply)
3. Wait for the rollout to complete
4. Sleep for 30 minutes (observation interval)

Create a `workflow.yaml` manifest like the following:

```yaml
name: frontend
tasks:
  - name: configure
    operator: FileConfigurator
    arguments:
      source:
        hostname: mymachine
        username: user
        key: /home/user/.ssh/key
        path: frontend.yaml.templ
      target:
        hostname: mymachine
        username: user
        key: /home/user/.ssh/key
        path: frontend.yaml

  - name: apply
    operator: Executor
    arguments:
      timeout: 5m
      host:
        hostname: mymachine
        username: user
        key: /home/user/.ssh/key
      command: kubectl apply -f frontend.yaml

  - name: verify
    operator: Executor
    arguments:
      timeout: 5m
      host:
        hostname: mymachine
        username: user
        key: /home/user/.ssh/key
      command: kubectl rollout status --timeout=5m deployment/frontend -n boutique;

  - name: observe
    operator: Sleep
    arguments:
      seconds: 1800
```

Then run:

```bash
akamas create workflow workflow.yaml
```

### **Telemetry**

Create the `telemetry.yaml`manifest like the following:

```yaml
provider: Dynatrace
config:
  url: <YOUR_DYNATRACE_URL>
  token: <YOUR_DYNATRACE_TOKEN>
  pushEvents: false
```

Then run:

```bash
akamas create telemetry-instance telemetry.yaml frontend
```

### **Study**

In this live optimization:

* the **goal** is to reduce the cost of the Kubernetes deployment. In this example, the cost is based on the amount of CPU and memory limits (assuming requests = limits).
* the approval mode is set to **manual,** a new recommendation is generated **daily**
* to avoid impacting application performance, constraints are specified on desired **response times and error rates**
* to avoid impacting application reliability, constraints are specified on **peak resource usage** and **out-of-memory kills**
* the parameters to be tuned are the container **CPU and memory limits** (we assume requests=limits in the deployment file)

Create a `study.yaml` manifest like the following:

```yaml
name: frontend
system: frontend
workflow: frontend
requireApproval: true

goal:
  objective: minimize
  function:
    formula: (((container.container_cpu_limit/1000) * 3) + (container.container_memory_limit/(1024*1024*1024)))
  constraints:
    absolute:
      - name: Response Time
        formula: webapp.requests_response_time <= 300
      - name: Error Rate
        formula: webapp.service_error_rate:max <= 0.05
      - name: Container CPU saturation
        formula: container.container_cpu_util:p95 < 0.8
      - name: Container memory saturation
        formula: container.container_memory_util:max < 0.7
      - name: Container out-of-memory kills
        formula: container.container_oom_kills_count == 0

parametersSelection:
  - name: container.cpu_limit
    domain: [300, 1000]
  - name: container.memory_limit
    domain: [800, 1536]

windowing:
  type: trim
  trim: [5m, 0m]
  task: observe

workloadsSelection:
  - name: webapp.requests_throughput

steps:
  - name: baseline
    type: baseline
    numberOfTrials: 48
    values:
      container.cpu_limit: 1000
      container.memory_limit: 1536

  - name: optimize
    type: optimize
    numberOfTrials: 48
    numberOfExperiments: 100
    numberOfInitExperiments: 0
    maxFailedExperiments: 50
```

Then run:

```bash
akamas create study study.yaml
```

You can now follow the live optimization progress and explore the results using the Akamas UI for Live optimizations.
