Tempo fa scrissi usando Go questo tool chiamato Congruit. È fondamentale tenersi allenati per cui ogni tanto perchè non produrre qualche “bashone” e darlo in pasto a Cogruit 😀

Congruit è un configuration management tool tutto basato su strutture json/bash. Era l’epoca in cui IaC si faceva quasi solo con Puppet, Chef e Ansible in un modo non totalmente deterministico come quello basato su Kubernetes.

Lo userò per installare lo stack Prometheus sul laboratorio Kubernetes raccontato in questo post.

Ecco come installarlo. Clono il repo git del progetto.

[root@k8s1 ~]# git clone https://github.com/Congruit/congruit.git
Cloning into 'congruit'...
remote: Enumerating objects: 446, done.
remote: Total 446 (delta 0), reused 0 (delta 0), pack-reused 446
Receiving objects: 100% (446/446), 536.88 KiB | 4.63 MiB/s, done.
Resolving deltas: 100% (199/199), done.
[root@k8s1 ~]#
[root@k8s1 ~]#
[root@k8s1 ~]# cd congruit/
[root@k8s1 congruit]# ls
congruit-go  congruit.go  img  insecure-domain.crt  insecure-domain.key  LICENSE  README.md  Vagrantfile
[root@k8s1 congruit]# pwd
/root/congruit

Build di Congruit

[root@k8s1 congruit]# go build ./congruit.go
[root@k8s1 congruit]# echo $?
0

Clono il repo con tutti gli stockroom che scrissi all’epoca per prendere esempio. Creo però uno stockroom ad-hoc chiamato “prometheus-stockroom” una semplice directory con dentro le folder: places, works e workplaces_enabled.

[root@k8s1 ~]# git clone https://github.com/Congruit/main-stockroom.git
Cloning into 'main-stockroom'...
remote: Enumerating objects: 118, done.
remote: Total 118 (delta 0), reused 0 (delta 0), pack-reused 118
Receiving objects: 100% (118/118), 15.52 KiB | 1.72 MiB/s, done.
Resolving deltas: 100% (43/43), done

Per raggiungere il mio scopo, come prima, cosa devo pensare all’idempotenza. Quindi scrivere un “place” per capire quando Prometheus Stack è installato. La location è places/prometheus_installed.

[root@k8s1 main-stockroom]# cat places/prometheus_is_not_installed

helm list --all-namespaces | grep 'kube-prometheus-stack'

if [ $? -ne 0 ]
then
  exit 0
else
  exit 1
fi

Il “place” creato in precedenza si occuperà di verificare che Prometheus non sia stato ancora installato su k8s. Guardo in tutti i namespace ma è possibile usare variabili d’ambiente o place ad-hoc per andare sul namespace desiderato (sì, non ci sono i fact di Ansible o gli attributes di Chef).

È tempo di creare un “work” ovvero l’azione vera e propria che deve partire in caso il controllo di idempotenza fallisca (quindi se è vero prometheus_is_not_installed). La location è works/install_prometheus.

[root@k8s1 main-stockroom]# cat works/install_prometheus
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack -n default

Ora è necessario legare place e work in un “workplace“. La location è workplaces_enabled/prometheus_stack

[
  {
    "places": ["prometheus_is_not_installed"],
    "works": ["install_prometheus"]
  }
]

Un workplace è una struttura molto semplice fatta in JSON. Se è vero che prometheus_is_not_installed ritorna 0 allora procedo con install_prometheus. È una array per cui posso metterci N coppie di places e works.

Installo Prometheus su Kubernetes.

export WORKPLACES_ENABLED=prometheus_stack
[root@k8s1 congruit]# ./congruit  -stockroom-dir=/root/prometheus-stockroom -debug
                         _ _
 ___ ___ ___ ___ ___ _ _|_| |_
|  _| . |   | . |  _| | | |  _|
|___|___|_|_|_  |_| |___|_|_|
            |___|
Version: 1.1.0

2022/06/28 10:47:56 Loading works...
2022/06/28 10:47:56 Found work: install_prometheus
2022/06/28 10:47:56 Load work: install_prometheus
2022/06/28 10:47:56 Found place: prometheus_is_not_installed
2022/06/28 10:47:56 Load Place: prometheus_is_not_installed
2022/06/28 10:47:56 Found workplace: prometheus_stack
2022/06/28 10:47:56 Loading workplace: prometheus_stack@1
2022/06/28 10:47:56 Processing workplace prometheus_stack@1
2022/06/28 10:47:56 Checking places of workplace prometheus_stack@1
2022/06/28 10:47:56 Testing place prometheus_is_not_installed
2022/06/28 10:47:56 Place prometheus_is_not_installed returns 0
2022/06/28 10:47:56 Executing work:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack -n default


2022/06/28 10:48:44 command output: "prometheus-community" has been added to your repositories
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "hashicorp" chart repository
...Successfully got an update from the "kubeinvaders" chart repository
...Successfully got an update from the "prometheus-community" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!⎈
NAME: prometheus
LAST DEPLOYED: Tue Jun 28 10:48:05 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
kube-prometheus-stack has been installed. Check its status by running:
  kubectl --namespace default get pods -l "release=prometheus"

Visit https://github.com/prometheus-operator/kube-prometheus for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator.

2022/06/28 10:48:44 Extecuted works: 1

Ok ha funzionato. Controllo che Prometheus sia installato.

[root@k8s1 congruit]# helm list
NAME      	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                       	APP VERSION
prometheus	default  	1       	2022-06-28 10:48:05.410058887 +0000 UTC	deployed	kube-prometheus-stack-36.2.0	0.57.0
[root@k8s1 congruit]# kubectl get pods
NAME                                                     READY   STATUS            RESTARTS   AGE
alertmanager-prometheus-kube-prometheus-alertmanager-0   2/2     Running           0          48s
prometheus-grafana-648fc7cb4-rl7fl                       2/3     Running           0          57s
prometheus-kube-prometheus-operator-58f74dd8d8-8qw44     1/1     Running           0          57s
prometheus-kube-state-metrics-544b49996d-z7pvf           1/1     Running           0          57s
prometheus-prometheus-kube-prometheus-prometheus-0       0/2     PodInitializing   0          13s

prometheus-prometheus-node-exporter-lcc6m                1/1     Running           0          57s

Alcuni link utili:

  1. Lo stockroom usato per installare Prometheus
  2. Il repo di Congruit

Naturalmente il tool è enormemente piccolo in confronto alle cose già presenti nelle community open source. Però per chi vuole tenersi allenato su Bash è ottimo 😀

Ma l’idempotenza?

Eccola qui 😉

[root@k8s1 congruit]# ./congruit  -stockroom-dir=/root/prometheus-stockroom -debug
                         _ _
 ___ ___ ___ ___ ___ _ _|_| |_
|  _| . |   | . |  _| | | |  _|
|___|___|_|_|_  |_| |___|_|_|
            |___|
Version: 1.1.0

2022/06/28 10:58:17 Loading works...
2022/06/28 10:58:17 Found work: install_prometheus
2022/06/28 10:58:17 Load work: install_prometheus
2022/06/28 10:58:17 Found place: prometheus_is_not_installed
2022/06/28 10:58:17 Load Place: prometheus_is_not_installed
2022/06/28 10:58:17 Found workplace: prometheus_stack
2022/06/28 10:58:17 Loading workplace: prometheus_stack@1
2022/06/28 10:58:17 Processing workplace prometheus_stack@1
2022/06/28 10:58:17 Checking places of workplace prometheus_stack@1
2022/06/28 10:58:17 Testing place prometheus_is_not_installed
2022/06/28 10:58:17 Place prometheus_is_not_installed does not return 0. This is not a good place to run prometheus_stack@1
exit status 1
2022/06/28 10:58:17 Workplace prometheus_stack@1 not needed here
2022/06/28 10:58:17 Extecuted works: 0