In this blog post, we’ll shine a spotlight on the Keptn , unraveling its capabilities and showcasing its prowess in managing Kubernetes deployments. Today we will talk about what is keptn and its use cases.
Introduction
Keptn is a tool that works smoothly with cloud-native deployment tools like ArgoCD, Flux, and Gitlab to make your applications aware of what’s happening in your Kubernetes cluster. It goes beyond regular deployment tools by adding features to make sure your deployments are running smoothly.
Keptn comes with different features that you can use on their own or together. It focuses on three main things: Metrics, Observability, and Managing the lifecycle of your releases.
UseCases:
Metrics
The Keptn Metrics Operator functions as a centralized repository for aggregating and managing performance metrics within a Kubernetes cluster. It serves as a unifying interface, offering a consolidated view of application metrics sourced from diverse data platforms and multiple instances of any given data platform. In other word, the Metrics Operator serves as a sophisticated solution for managing, analyzing, and leveraging performance metrics in Kubernetes environments, enhancing operational visibility and facilitating informed decision-making.
Keptn metric basics:
We can implement metrics with two resources:
- KeptnMetric
- KeptnMetricsProvider
- KeptnMetric
apiVersion: metrics.keptn.sh/v1alpha2
kind: KeptnMetric
metadata:
name: available-cpus
namespace: simplenode-dev
spec:
provider:
name: prometheus
query: “sum(kube_node_status_capacity{resource=’cpu’})”
fetchIntervalSeconds: 10
—
apiVersion: metrics.keptn.sh/v1alpha2
kind: KeptnMetric
metadata:
name: availability-slo
namespace: simplenode-dev
spec:
provider:
name: dynatrace
query: “func:slo.availability_simplenodeservice”
fetchIntervalSeconds: 10
- KeptnMetricsProvider
Example file of metrics provider for prometheus
apiVersion: metrics.keptn.sh/v1alpha2
kind: KeptnMetricsProvider
metadata:
name: prometheus
namespace: simplenode-dev
spec:
targetServer: “http://prometheus-k8s.monitoring.svc.cluster.local:9090”
Example file of metrics provider for Dynatrace
apiVersion: metrics.keptn.sh/v1alpha2
kind: KeptnMetricsProvider
metadata:
name: dynatrace
spec:
targetServer: “<dynatrace-tenant-url>”
secretKeyRef:
name: dynatrace
key: DT_TOKEN
Release lifecycle management
In Keptn, Release Lifecycle Management refers to the end-to-end process of handling and managing the lifecycle of releases within a software delivery pipeline. This encompasses various stages from the initiation of a release, through testing and deployment, to monitoring and evaluation. Keptn provides a structured and automated approach to release management in cloud-native environments.
1. Pre-deployment Tasks
It is helps to identify and verify all the services . This involves ensuring that the required services are available, compatible, and meet the specified version requirements.
2. Pre-deployment Evaluations
Assessing whether the cluster has sufficient resources (CPU, memory, storage) for the upcoming deployment.
3. Post-deployment Evaluations
A comprehensive assessment of the deployed application to ensure that all components are functioning as intended. This includes checking for errors, monitoring system logs, and confirming that the deployment meets the specified criteria.
4. Post-deployment Tasks
Automated testing is initiated to validate the newly deployed application. This includes unit tests, integration tests, and end-to-end tests to ensure that the software functions as expected and doesn’t introduce new bugs or issues.
app.kubernetes.io/part-of: keptndemoappdemo2
app.kubernetes.io/name: nginx
app.kubernetes.io/version: 0.0.4
keptn.sh/pre-deployment-tasks: deployment-hello
keptn.sh/post-deployment-tasks: notify
Demo:
- Create a namespace
apiVersion: v1
kind: Namespace
metadata:
name: keptndemoo
annotations:
keptn.sh/lifecycle-toolkit: enabled
- Create a secret for slack token
- Now apply below yaml file
—
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: keptndemoo
labels:
app.kubernetes.io/name: nginx
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: nginx
template:
metadata:
labels:
app.kubernetes.io/part-of: keptndemoappdemo2
app.kubernetes.io/name: nginx
app.kubernetes.io/version: 0.0.4
keptn.sh/pre-deployment-tasks: deployment-hello
keptn.sh/post-deployment-tasks: notify
spec:
containers:
– name: nginx
image: nginx:1.14.2
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
cpu: 200m
memory: 128Mi
—
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: keptndemoo
spec:
selector:
app.kubernetes.io/name: nginx
ports:
– protocol: TCP
port: 8080
targetPort: 80
—
apiVersion: metrics.keptn.sh/v1alpha2
kind: KeptnMetricsProvider
metadata:
name: my-provider
namespace: keptndemoo
spec:
targetServer: “http://prometheus-k8s.monitoring.svc.cluster.local:9090”
—
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: deployment-hello
namespace: keptndemoo
spec:
function:
inline:
code: |
console.log(“Deployment Task has been executed”);
—
apiVersion: metrics.keptn.sh/v1alpha3
kind: KeptnMetric
metadata:
name: available-cpus
namespace: keptndemoo
spec:
provider:
name: my-provider
query: “sum(kube_node_status_capacity{resource=’cpu’})”
fetchIntervalSeconds: 10
—
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnEvaluationDefinition
metadata:
name: app-pre-deploy-eval-1
namespace: keptndemoo
spec:
objectives:
– keptnMetricRef:
name: available-cpus
namespace: keptndemoo
evaluationTarget: “>1”
—
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: notify
namespace: keptndemoo
spec:
function:
inline:
code: |
let text = Deno.env.get(“SECURE_DATA”), data, body, contextdata, context = Deno.env.get(“CONTEXT”);
if (text != undefined) {
data = JSON.parse(text);
}
if (context != undefined) {
contextdata = JSON.parse(context);
}
if (contextdata.objectType == “Application”) {
body = `{
“text”: “Application ${contextdata.appName}, Version ${contextdata.appVersion} has been deployed”
}`
}
if (contextdata.objectType == “Workload”) {
body = `{
“username” : “keptn”,
“blocks”: [
{
“type”: “section”,
“text”: {
“type”: “mrkdwn”,
“text”: “Workload *${contextdata.workloadName}* notification!!”
}
},
{
“type”: “section”,
“text”: {
“type”: “mrkdwn”,
“text”: “*Version:* ${contextdata.workloadVersion}\n*App:* ${contextdata.appName} deployment complete”
}
}
]
}`
}
console.log(body)
let resp = await fetch(“https://hooks.slack.com/services/” + data.slack_hook, {
method: “POST”,
body,
});
console.log(resp)
secureParameters:
secret: slack-notification
—
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnTaskDefinition
metadata:
name: container-sleep
namespace: keptndemoo
spec:
container:
name: testy-test
image: busybox:1.36.1
command:
– ‘sh’
– ‘-c’
– ‘sleep 30’