From 61ba404aad7748f5d9216c564ad0337791582972 Mon Sep 17 00:00:00 2001 From: Florian Dehn Date: Mon, 19 Jun 2023 12:41:39 +0200 Subject: [PATCH] use prometheus stack helm chart as pre-requisite --- .github/workflows/local_k8s.yml | 5 ++++- environments/.terraform-version | 2 +- environments/.terragrunt-version | 2 +- environments/terragrunt.hcl | 2 +- readme.md | 5 +++++ start.sh | 17 +++++++++++++++++ terraform/main.tf | 3 --- terraform/modules/ingress/main.tf | 2 +- .../modules/monitoring/config/monitoring.yaml | 2 +- terraform/modules/monitoring/ingress.tf | 8 ++++---- terraform/modules/monitoring/loki.tf | 2 +- terraform/modules/monitoring/main.tf | 2 +- terraform/modules/monitoring/variables.tf | 0 13 files changed, 37 insertions(+), 15 deletions(-) create mode 100755 start.sh delete mode 100644 terraform/modules/monitoring/variables.tf diff --git a/.github/workflows/local_k8s.yml b/.github/workflows/local_k8s.yml index 3f0b73a..44728bd 100644 --- a/.github/workflows/local_k8s.yml +++ b/.github/workflows/local_k8s.yml @@ -66,7 +66,10 @@ jobs: # hack: for having CRDs available, install helm chart then remove right after - name: Install kube-prometheus-stack helm chart run: helm repo add prometheus-community https://prometheus-community.github.io/helm-charts && helm repo update && helm install prometheus-stack prometheus-community/kube-prometheus-stack && helm uninstall prometheus-stack - + + - name: Create monitoring namespace + run: kubectl create ns monitoring + #### Terraforming - name: 'Terragrunt Init' run: cd $tf_working_dir && terragrunt init diff --git a/environments/.terraform-version b/environments/.terraform-version index a5ba932..3e1ad72 100644 --- a/environments/.terraform-version +++ b/environments/.terraform-version @@ -1 +1 @@ -1.1.7 \ No newline at end of file +1.5.0 \ No newline at end of file diff --git a/environments/.terragrunt-version b/environments/.terragrunt-version index 29babdc..3cf5e1d 100644 --- a/environments/.terragrunt-version +++ b/environments/.terragrunt-version @@ -1 +1 @@ -0.36.6 +0.47.0 \ No newline at end of file diff --git a/environments/terragrunt.hcl b/environments/terragrunt.hcl index 089c6af..b7ba4cd 100644 --- a/environments/terragrunt.hcl +++ b/environments/terragrunt.hcl @@ -13,7 +13,7 @@ generate "provider" { required_providers { kubernetes = { source = "hashicorp/kubernetes" - version = "2.20.0" + version = "2.21.1" } helm = { source = "hashicorp/helm" diff --git a/readme.md b/readme.md index ff93251..f9623f8 100644 --- a/readme.md +++ b/readme.md @@ -31,6 +31,11 @@ As I use nginx as my ingress controller, I needed to disable Traefik on K3D. The cluster's load balancer will be available via ports 80 and 443 `k3d cluster create -p "80:80@loadbalancer" -p "443:443@loadbalancer" --k3s-arg="--disable=traefik@server:0"` +For the sake of having some ears and eyes for what's going on there on our cluster, we need some monitoring: + +Install [Kube Prometheus Stack](https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack) for cluster monitoring. +`helm install -n monitoring -f terraform/modules/monitoring/config/monitoring.yaml kube-prometheus-stack prometheus-community/kube-prometheus-stack` + When your k3d backed Kubernetes is ready, you're also ready to deploy the stack using Terragrunt. `cd environment/dev && terragrunt apply -auto-approve` diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..2db4dba --- /dev/null +++ b/start.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# start k3d cluster with nginx (and with disabled traefik) +echo "Starting K3d cluster" +k3d cluster create -p "80:80@loadbalancer" -p "443:443@loadbalancer" --k3s-arg="--disable=traefik@server:0" > /dev/null 2>&1 && echo "K3d cluster started" + +echo "Creating Kubernetes namespace: monitoring" +kubectl create namespace monitoring > /dev/null 2>&1 + +# install kube prometheus stack helm chart +echo "Installing Kube Prometheus Stack" +helm repo add prometheus-community https://prometheus-community.github.io/helm-charts && helm repo update && helm install -n monitoring -f terraform/modules/monitoring/config/monitoring.yaml kube-prometheus-stack prometheus-community/kube-prometheus-stack > /dev/null 2>&1 && echo "Kube Prometheus Stack installed" + +echo "Running Terraform" +cd environments/dev && terragrunt init > /dev/null 2>&1 && terragrunt apply -auto-approve > /dev/null 2>&1 && echo "Terraform apply was successful" \ No newline at end of file diff --git a/terraform/main.tf b/terraform/main.tf index 2eeace0..04d478a 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -8,9 +8,6 @@ module "ingress" { } module "wordpress" { - depends_on = [ - module.monitoring - ] source = "./modules/wordpress" stage = var.stage hostname = var.hostname diff --git a/terraform/modules/ingress/main.tf b/terraform/modules/ingress/main.tf index e028520..7891646 100644 --- a/terraform/modules/ingress/main.tf +++ b/terraform/modules/ingress/main.tf @@ -9,6 +9,6 @@ resource "helm_release" "ingress-nginx" { atomic = true values = [ - "${file("${path.module}/templates/ingress-nginx-values.yaml")}" + file("${path.module}/templates/ingress-nginx-values.yaml") ] } \ No newline at end of file diff --git a/terraform/modules/monitoring/config/monitoring.yaml b/terraform/modules/monitoring/config/monitoring.yaml index 63ae68a..e1638d9 100644 --- a/terraform/modules/monitoring/config/monitoring.yaml +++ b/terraform/modules/monitoring/config/monitoring.yaml @@ -55,7 +55,7 @@ grafana: access: proxy - name: Prometheus type: prometheus - url: http://prometheus-community-kube-prometheus.monitoring:9090/ + url: http://kube-prometheus-stack-prometheus.monitoring:9090/ access: proxy adminUser: admin adminPassword: a12sdf diff --git a/terraform/modules/monitoring/ingress.tf b/terraform/modules/monitoring/ingress.tf index 43f87c0..04fc3f5 100644 --- a/terraform/modules/monitoring/ingress.tf +++ b/terraform/modules/monitoring/ingress.tf @@ -1,7 +1,7 @@ resource "kubernetes_ingress_v1" "grafana" { metadata { name = "grafana" - namespace = kubernetes_namespace.monitoring.metadata[0].name + namespace = data.kubernetes_namespace.monitoring.metadata[0].name annotations = { "nginx.ingress.kubernetes.io/rewrite-target" = "/" } @@ -16,7 +16,7 @@ resource "kubernetes_ingress_v1" "grafana" { path_type = "Prefix" backend { service { - name = "prometheus-community-grafana" + name = "kube-prometheus-stack-grafana" port { name = "http-web" } @@ -31,7 +31,7 @@ resource "kubernetes_ingress_v1" "grafana" { resource "kubernetes_ingress_v1" "prometheus" { metadata { name = "prometheus" - namespace = kubernetes_namespace.monitoring.metadata[0].name + namespace = data.kubernetes_namespace.monitoring.metadata[0].name annotations = { "nginx.ingress.kubernetes.io/rewrite-target" = "/" } @@ -46,7 +46,7 @@ resource "kubernetes_ingress_v1" "prometheus" { path_type = "Prefix" backend { service { - name = "prometheus-community-kube-prometheus" + name = "kube-prometheus-stack-prometheus" port { name = "http-web" } diff --git a/terraform/modules/monitoring/loki.tf b/terraform/modules/monitoring/loki.tf index 08e959c..9401631 100644 --- a/terraform/modules/monitoring/loki.tf +++ b/terraform/modules/monitoring/loki.tf @@ -2,7 +2,7 @@ resource "helm_release" "loki" { name = "loki" repository = "https://grafana.github.io/helm-charts" chart = "loki-stack" - namespace = kubernetes_namespace.monitoring.metadata[0].name + namespace = data.kubernetes_namespace.monitoring.metadata[0].name create_namespace = false force_update = true replace = true diff --git a/terraform/modules/monitoring/main.tf b/terraform/modules/monitoring/main.tf index 7c5e772..2b971cc 100644 --- a/terraform/modules/monitoring/main.tf +++ b/terraform/modules/monitoring/main.tf @@ -1,4 +1,4 @@ -resource "kubernetes_namespace" "monitoring" { +data "kubernetes_namespace" "monitoring" { metadata { name = "monitoring" } diff --git a/terraform/modules/monitoring/variables.tf b/terraform/modules/monitoring/variables.tf deleted file mode 100644 index e69de29..0000000