[{"content":"I wanted to deploy a few basic services that would make my life a little easier when working with my homelab. These include a cache for Docker images, a place to host Git repositories, and somewhere to store and manage secrets.\nI already had a Raspberry Pi 4 with 4 cores and 8GB of RAM available, which seemed more than capable of handling these workloads. For storage, I wanted to avoid using an SD card due to its reliability issues, so I used an external Samsung T7 1TB SSD instead.\nFor the operating system, I went with Alpine Linux, which worked out great. After getting the system up and running, I kept things simple and installed only Docker and Docker Compose.\nThe goal is to run all services as containers, making the setup easy to maintain and portable. Below we will see how to deploy and configure these services using Docker Compose.\nHAProxy All services run behind an HAProxy instance, which acts as a reverse proxy that routes traffic to the appropriate backend service and handles TLS termination.\nHAProxy listens on ports 80 and 443 on the host. It also listens on one more port on the host for Forgejo’s SSH service. We could use port 22 for this if the host\u0026rsquo;s SSH server is configured to listen on a different port.\nZot We deploy Zot as a virtual registry that mirrors the following upstream registries..\nquay.io registry.k8s.io codeberg.org ghcr.io docker.io Zot fetches and caches images from the configured upstream registries on demand, serving future image pulls directly from its local cache. This makes image pulls faster, reduces traffic to upstream registries, and helps avoid rate limits.\nAssuming our domain name is example.com we can use the virtual registry in the following ways..\npull a specific image tag or digest..\ndocker pull registry.example.com/alpine:3.24.4 docker pull registry.example.com/alpine@sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf use it in Docker Compose files..\n# set VIRTUAL_REGISTRY=\u0026#34;registry.example.com\u0026#34; in the compose .env file services: alpine: image: ${VIRTUAL_REGISTRY:-docker.io}/alpine:3.24.4 command: [\u0026#34;sleep\u0026#34;, \u0026#34;infinity\u0026#34;] use it in supported Helm charts..\nglobal: imageRegistry: registry.example.com use it in K8s with Kyverno to enforce the use of the virtual registry when pods are created\nZot must be accessible over HTTPS with a valid TLS certificate, this is handled in our case by HAProxy. A web UI is available at https://registry.${DOMAIN_NAME}, where we can browse our cached images and view useful information about them.\nNote Docker may time out when pulling images for the first time through the virtual registry. If this happens, give the registry a couple of minutes to complete the downloads before retrying.\nForgejo We use Forgejo to host and manage Git repositories. Our installation is very opinionated, with most unnecessary features disabled through environment variables. For its database, we use SQLite as it is more than sufficient for our needs. The SSH port we configure determines the port included in the SSH clone URLs provided by Forgejo.\nAfter deploying Forgejo, we can create users using the following command..\nUSERNAME=\u0026#34;\u0026#34; docker compose exec -u git forgejo \\ forgejo admin user create \\ --admin \\ --must-change-password \\ --username \u0026#34;${USERNAME}\u0026#34; \\ --email \u0026#34;${USERNAME}@forgejo.internal\u0026#34; \\ --password \u0026#39;c7D94STZD5QSEMwZ\u0026#39; The web UI is available at https://git.${DOMAIN_NAME}, we log in using the credentials of the user we created. On the first login we will be prompted to change our password.\nFor more information about Forgejo\u0026rsquo;s available environment variables and their options, check its sample configuration file.\nIf the plan is to use Git repositories hosted on Forgejo with Argo CD, we need to add Forgejo\u0026rsquo;s SSH host keys to Argo CD\u0026rsquo;s trusted hosts. To do this, we run the following command and add its output under configs.ssh.extraHosts in the Argo CD values file..\nDOMAIN_NAME=\u0026#34;\u0026#34; FORGEJO_SSH_PORT=\u0026#34;\u0026#34; ssh-keyscan -p \u0026#34;${FORGEJO_SSH_PORT}\u0026#34; git.${DOMAIN_NAME} | sed \u0026#39;/^#/d\u0026#39; OpenBao We use OpenBao to store, manage, and distribute secrets. By default after a restart, OpenBao is sealed and requires an unseal key to become operational. For simplicity we use auto-unseal with the required key passed to OpenBao through an environment variable.\nTo generate the auto-unseal key we run..\nopenssl rand -hex 32 After deployment, OpenBao must be manually initialized to generate the initial root token and recovery keys. Again for simplicity we configure a single recovery key with a threshold of one..\ndocker compose exec openbao bao operator init -recovery-shares=1 -recovery-threshold=1 Warning This is a simplified OpenBao deployment that should not be considered production ready.\nThe web UI is available at https://vault.${DOMAIN_NAME}. Log in using the root token that was created during initialization.\nInitial Setup To deploy the services we do the following..\nDecide which domain or subdomain to use for the deployment (we will use the subdomain dev.example.com throughout the remaining steps). Configure DNS records for both dev.example.com and *.dev.example.com and point them to the IP address of the machine that will host the services. Make sure the TLS certificate covers both dev.example.com and *.dev.example.com. Download the .env and compose.yaml files on the machine that will host the services. Fill in the missing values in the .env file. Start the services with docker compose up -d. Create the initial Forgejo user. Initialize OpenBao and generate its root token and recovery keys. Show .env file ARCHITECTURE=\u0026#34;\u0026#34; # arm64 DOMAIN_NAME=\u0026#34;\u0026#34; # dev.example.com IP_ADDRESS=\u0026#34;\u0026#34; # 192.168.1.10 TLS_CERTIFICATE_PATH=\u0026#34;\u0026#34; # $HOME/.tls/tls.pem FORGEJO_SSH_PORT=\u0026#34;\u0026#34; # 2222 BAO_SEAL_KEY=\u0026#34;\u0026#34; # fbc739c7fa4cc08a11b20bcbaebb02e0354f7959dda9d8e65d6adc041f4a0b3f Show compose.yaml file name: backend networks: backend: driver: bridge volumes: forgejo: {} openbao: {} zot: {} services: forgejo: environment: FORGEJO____APP_NAME: \u0026#34;Forgejo\u0026#34; FORGEJO__server__DOMAIN: \u0026#34;git.${DOMAIN_NAME:?}\u0026#34; FORGEJO__server__ROOT_URL: \u0026#34;https://git.${DOMAIN_NAME:?}/\u0026#34; FORGEJO__server__SSH_DOMAIN: \u0026#34;git.${DOMAIN_NAME:?}\u0026#34; FORGEJO__server__SSH_PORT: \u0026#34;${FORGEJO_SSH_PORT:?}\u0026#34; FORGEJO__server__OFFLINE_MODE: \u0026#34;true\u0026#34; FORGEJO__server__LANDING_PAGE: \u0026#34;login\u0026#34; FORGEJO__security__INSTALL_LOCK: \u0026#34;true\u0026#34; FORGEJO__service__DISABLE_REGISTRATION: \u0026#34;true\u0026#34; FORGEJO__service__REQUIRE_SIGNIN_VIEW: \u0026#34;true\u0026#34; FORGEJO__service__DEFAULT_KEEP_EMAIL_PRIVATE: \u0026#34;true\u0026#34; FORGEJO__service__DEFAULT_ALLOW_CREATE_ORGANIZATION: \u0026#34;false\u0026#34; FORGEJO__service__DEFAULT_USER_VISIBILITY: \u0026#34;private\u0026#34; FORGEJO__service__DEFAULT_ORG_VISIBILITY: \u0026#34;private\u0026#34; FORGEJO__service__ENABLE_USER_HEATMAP: \u0026#34;false\u0026#34; FORGEJO__service__ENABLE_TIMETRACKING: \u0026#34;false\u0026#34; FORGEJO__service__SHOW_MILESTONES_DASHBOARD_PAGE: \u0026#34;false\u0026#34; FORGEJO__service__AUTO_WATCH_NEW_REPOS: \u0026#34;false\u0026#34; FORGEJO__service.explore__DISABLE_USERS_PAGE: \u0026#34;true\u0026#34; FORGEJO__service.explore__DISABLE_ORGANIZATIONS_PAGE: \u0026#34;true\u0026#34; FORGEJO__repository__FORCE_PRIVATE: \u0026#34;true\u0026#34; FORGEJO__repository__DEFAULT_PRIVATE: \u0026#34;private\u0026#34; FORGEJO__repository__DISABLED_REPO_UNITS: \u0026#34;repo.issues,repo.ext_issues,repo.wiki,repo.ext_wiki,repo.projects\u0026#34; FORGEJO__repository__DISABLE_MIGRATIONS: \u0026#34;true\u0026#34; FORGEJO__repository__DISABLE_STARS: \u0026#34;true\u0026#34; FORGEJO__repository__DISABLE_FORKS: \u0026#34;true\u0026#34; FORGEJO__repository__DEFAULT_BRANCH: \u0026#34;master\u0026#34; FORGEJO__ui__SHOW_USER_EMAIL: \u0026#34;false\u0026#34; FORGEJO__picture__DISABLE_GRAVATAR: \u0026#34;true\u0026#34; FORGEJO__other__SHOW_FOOTER_VERSION: \u0026#34;false\u0026#34; FORGEJO__other__SHOW_FOOTER_TEMPLATE_LOAD_TIME: \u0026#34;false\u0026#34; FORGEJO__other__SHOW_FOOTER_POWERED_BY: \u0026#34;false\u0026#34; FORGEJO__other__ENABLE_SITEMAP: \u0026#34;false\u0026#34; FORGEJO__other__ENABLE_FEED: \u0026#34;false\u0026#34; FORGEJO__metrics__ENABLED: \u0026#34;true\u0026#34; healthcheck: test: [\u0026#34;CMD\u0026#34;, \u0026#34;wget\u0026#34;, \u0026#34;--no-verbose\u0026#34;, \u0026#34;--tries=1\u0026#34;, \u0026#34;--spider\u0026#34;, \u0026#34;http://127.0.0.1:3000/api/healthz\u0026#34;] interval: 30s timeout: 5s retries: 3 start_period: 30s start_interval: 5s image: codeberg.org/forgejo/forgejo:17.0.1 networks: - backend restart: unless-stopped volumes: - forgejo:/data haproxy: configs: - source: haproxy-01 target: /usr/local/etc/haproxy/haproxy.cfg healthcheck: test: [\u0026#34;CMD\u0026#34;, \u0026#34;wget\u0026#34;, \u0026#34;--no-verbose\u0026#34;, \u0026#34;--tries=1\u0026#34;, \u0026#34;--spider\u0026#34;, \u0026#34;http://127.0.0.1:8404\u0026#34;] interval: 30s timeout: 5s retries: 3 start_period: 30s start_interval: 5s image: docker.io/haproxytech/haproxy-alpine:3.4 networks: - backend ports: - ${IP_ADDRESS:?}:${FORGEJO_SSH_PORT:?}:${FORGEJO_SSH_PORT:?} - ${IP_ADDRESS:?}:80:80 - ${IP_ADDRESS:?}:443:443 restart: unless-stopped volumes: - ${TLS_CERTIFICATE_PATH:?}:/usr/local/etc/haproxy/tls/haproxy.pem:ro openbao: command: - \u0026#39;server\u0026#39; configs: - source: openbao-01 target: /openbao/config/config.hcl environment: BAO_ADDR: \u0026#34;http://127.0.0.1:8200\u0026#34; BAO_SEAL_KEY: \u0026#34;${BAO_SEAL_KEY:?}\u0026#34; healthcheck: test: [\u0026#34;CMD\u0026#34;, \u0026#34;wget\u0026#34;, \u0026#34;--no-verbose\u0026#34;, \u0026#34;--tries=1\u0026#34;, \u0026#34;--spider\u0026#34;, \u0026#34;http://127.0.0.1:8200/v1/sys/health?standbyok=true\u0026amp;sealedcode=204\u0026amp;uninitcode=204\u0026#34;] interval: 30s timeout: 5s retries: 3 start_period: 30s start_interval: 5s image: docker.io/openbao/openbao:2.7.2 networks: - backend restart: unless-stopped volumes: - openbao:/openbao/file zot: command: - \u0026#39;serve\u0026#39; - \u0026#39;/etc/zot/config.json\u0026#39; configs: - source: zot-01 target: /etc/zot/config.json healthcheck: test: [\u0026#34;CMD\u0026#34;, \u0026#34;/usr/local/bin/zot-linux-${ARCHITECTURE:?}\u0026#34;, \u0026#34;healthcheck\u0026#34;, \u0026#34;--insecure-skip-verify\u0026#34;, \u0026#34;/etc/zot/config.json\u0026#34;] interval: 30s timeout: 5s retries: 3 start_period: 30s start_interval: 5s image: ghcr.io/project-zot/zot:v2.1.23 networks: - backend restart: unless-stopped volumes: - zot:/var/lib/registry configs: haproxy-01: content: | global user haproxy group haproxy stats socket /var/run/api.sock user haproxy group haproxy mode 660 level admin expose-fd listeners log stdout format raw local0 info defaults log global maxconn 2000 timeout connect 2s timeout client 30s timeout server 30s timeout tunnel 1h timeout http-keep-alive 5s timeout http-request 10s timeout queue 10s timeout check 5s resolvers docker nameserver dns1 127.0.0.11:53 resolve_retries 3 timeout resolve 1s timeout retry 1s hold valid 10s hold obsolete 30s hold other 10s frontend haproxy-stats mode http bind *:8404 stats enable stats uri / stats refresh 5m no log frontend docker-frontend-ssh mode tcp bind *:${FORGEJO_SSH_PORT:?} default_backend docker-backend-ssh frontend docker-frontend-http mode http bind *:80 bind *:443 ssl crt /usr/local/etc/haproxy/tls/ option httplog http-request redirect scheme https unless { ssl_fc } option forwardfor http-request set-header X-Real-IP %[src] http-request set-header X-Forwarded-Proto https if { ssl_fc } compression algo gzip compression type text/html text/plain text/css application/javascript default_backend docker-backend-default acl host_git hdr(host) -i git.${DOMAIN_NAME:?} acl host_registry hdr(host) -i registry.${DOMAIN_NAME:?} acl host_vault hdr(host) -i vault.${DOMAIN_NAME:?} use_backend docker-backend-git if host_git use_backend docker-backend-registry if host_registry use_backend docker-backend-vault if host_vault backend docker-backend-ssh mode tcp server s1 forgejo:22 check resolvers docker init-addr none backend docker-backend-git mode http server s1 forgejo:3000 check resolvers docker init-addr none backend docker-backend-registry mode http server s1 zot:5000 check resolvers docker init-addr none backend docker-backend-vault mode http server s1 openbao:8200 check resolvers docker init-addr none backend docker-backend-default mode http http-request return status 404 content-type \u0026#34;text/html\u0026#34; string \u0026#34;\u0026lt;h1\u0026gt;404 Not Found\u0026lt;/h1\u0026gt;\u0026#34; openbao-01: content: | ui = true disable_mlock = true api_addr = \u0026#34;https://vault.${DOMAIN_NAME:?}\u0026#34; cluster_addr = \u0026#34;https://openbao-01.openbao.internal:8201\u0026#34; telemetry { disable_hostname = true enable_hostname_label = true prometheus_retention_time = \u0026#34;24h\u0026#34; } storage \u0026#34;raft\u0026#34; { path = \u0026#34;/openbao/file\u0026#34; node_id = \u0026#34;openbao-01\u0026#34; } seal \u0026#34;static\u0026#34; { current_key_id = \u0026#34;20261102-1\u0026#34; current_key = \u0026#34;env://BAO_SEAL_KEY\u0026#34; } listener \u0026#34;tcp\u0026#34; { address = \u0026#34;0.0.0.0:8200\u0026#34; cluster_address = \u0026#34;0.0.0.0:8201\u0026#34; tls_disable = true } listener \u0026#34;tcp\u0026#34; { address = \u0026#34;0.0.0.0:8202\u0026#34; tls_disable = true telemetry { unauthenticated_metrics_access = true metrics_only = true metrics_path = \u0026#34;/metrics\u0026#34; } } zot-01: content: | { \u0026#34;distSpecVersion\u0026#34;: \u0026#34;1.1.1\u0026#34;, \u0026#34;storage\u0026#34;: { \u0026#34;rootDirectory\u0026#34;: \u0026#34;/var/lib/registry\u0026#34; }, \u0026#34;http\u0026#34;: { \u0026#34;address\u0026#34;: \u0026#34;0.0.0.0\u0026#34;, \u0026#34;port\u0026#34;: 5000, \u0026#34;compat\u0026#34;: [\u0026#34;docker2s2\u0026#34;] }, \u0026#34;log\u0026#34;: { \u0026#34;level\u0026#34;: \u0026#34;info\u0026#34; }, \u0026#34;extensions\u0026#34;: { \u0026#34;lint\u0026#34;: { \u0026#34;enable\u0026#34;: false }, \u0026#34;metrics\u0026#34;: { \u0026#34;enable\u0026#34;: true, \u0026#34;prometheus\u0026#34;: { \u0026#34;path\u0026#34;: \u0026#34;/metrics\u0026#34; } }, \u0026#34;scrub\u0026#34;: { \u0026#34;enable\u0026#34;: true, \u0026#34;interval\u0026#34;: \u0026#34;24h\u0026#34; }, \u0026#34;search\u0026#34;: { \u0026#34;enable\u0026#34;: true, \u0026#34;cve\u0026#34;: { \u0026#34;updateInterval\u0026#34;: \u0026#34;2h\u0026#34; } }, \u0026#34;sync\u0026#34;: { \u0026#34;enable\u0026#34;: true, \u0026#34;registries\u0026#34;: [ { \u0026#34;urls\u0026#34;: [\u0026#34;https://quay.io\u0026#34;], \u0026#34;onDemand\u0026#34;: true, \u0026#34;manifestCheckInterval\u0026#34;: \u0026#34;0s\u0026#34;, \u0026#34;tlsVerify\u0026#34;: true, \u0026#34;maxRetries\u0026#34;: 3, \u0026#34;retryDelay\u0026#34;: \u0026#34;10s\u0026#34;, \u0026#34;syncTimeout\u0026#34;: \u0026#34;3h\u0026#34;, \u0026#34;content\u0026#34;: [ { \u0026#34;prefix\u0026#34;: \u0026#34;**\u0026#34; } ] }, { \u0026#34;urls\u0026#34;: [\u0026#34;https://registry.k8s.io\u0026#34;], \u0026#34;onDemand\u0026#34;: true, \u0026#34;manifestCheckInterval\u0026#34;: \u0026#34;0s\u0026#34;, \u0026#34;tlsVerify\u0026#34;: true, \u0026#34;maxRetries\u0026#34;: 3, \u0026#34;retryDelay\u0026#34;: \u0026#34;10s\u0026#34;, \u0026#34;syncTimeout\u0026#34;: \u0026#34;3h\u0026#34;, \u0026#34;content\u0026#34;: [ { \u0026#34;prefix\u0026#34;: \u0026#34;**\u0026#34; } ] }, { \u0026#34;urls\u0026#34;: [\u0026#34;https://codeberg.org\u0026#34;], \u0026#34;onDemand\u0026#34;: true, \u0026#34;manifestCheckInterval\u0026#34;: \u0026#34;0s\u0026#34;, \u0026#34;tlsVerify\u0026#34;: true, \u0026#34;maxRetries\u0026#34;: 3, \u0026#34;retryDelay\u0026#34;: \u0026#34;10s\u0026#34;, \u0026#34;syncTimeout\u0026#34;: \u0026#34;3h\u0026#34;, \u0026#34;content\u0026#34;: [ { \u0026#34;prefix\u0026#34;: \u0026#34;**\u0026#34; } ] }, { \u0026#34;urls\u0026#34;: [\u0026#34;https://ghcr.io\u0026#34;], \u0026#34;onDemand\u0026#34;: true, \u0026#34;manifestCheckInterval\u0026#34;: \u0026#34;0s\u0026#34;, \u0026#34;tlsVerify\u0026#34;: true, \u0026#34;maxRetries\u0026#34;: 3, \u0026#34;retryDelay\u0026#34;: \u0026#34;10s\u0026#34;, \u0026#34;syncTimeout\u0026#34;: \u0026#34;3h\u0026#34;, \u0026#34;content\u0026#34;: [ { \u0026#34;prefix\u0026#34;: \u0026#34;**\u0026#34; } ] }, { \u0026#34;urls\u0026#34;: [\u0026#34;https://registry-1.docker.io\u0026#34;], \u0026#34;onDemand\u0026#34;: true, \u0026#34;manifestCheckInterval\u0026#34;: \u0026#34;0s\u0026#34;, \u0026#34;tlsVerify\u0026#34;: true, \u0026#34;maxRetries\u0026#34;: 3, \u0026#34;retryDelay\u0026#34;: \u0026#34;10s\u0026#34;, \u0026#34;syncTimeout\u0026#34;: \u0026#34;3h\u0026#34;, \u0026#34;content\u0026#34;: [ { \u0026#34;prefix\u0026#34;: \u0026#34;**\u0026#34; } ] } ] }, \u0026#34;trust\u0026#34;: { \u0026#34;enable\u0026#34;: false }, \u0026#34;ui\u0026#34;: { \u0026#34;enable\u0026#34;: true } } } Ongoing Maintenance Just a few things to keep in mind to keep everything happy. Renew the TLS certificate before it expires, update the container image tags when new versions are available, and make regular backups of the data stored in the Docker volumes.\n","permalink":"https://blog.korliaftis.com/posts/hosting-our-services.html","summary":"\u003cp\u003eI wanted to deploy a few basic services that would make my life a little easier when working with my homelab. These include a cache for Docker images, a place to host Git repositories, and somewhere to store and manage secrets.\u003c/p\u003e\n\u003cp\u003eI already had a Raspberry Pi 4 with 4 cores and 8GB of RAM available, which seemed more than capable of handling these workloads. For storage, I wanted to avoid using an SD card due to its reliability issues, so I used an external Samsung T7 1TB SSD instead.\u003c/p\u003e","title":"Hosting our Services"},{"content":"Sometimes everything looks fine on the surface but appearances can be deceiving. Here we will see how to set up the Prometheus monitoring stack to monitor our stuff and send alerts when needed.\nThe core component of the Prometheus monitoring stack is the Prometheus server. We configure targets for the server (meaning endpoints that expose metrics) and the server connects to them at regular intervals to scrape their metrics and store them locally in a time series database (TSDB).\nWe also configure rules for the server. Rules are PromQL expressions that run at regular intervals against the data stored in the TSDB and trigger alerts when certain conditions are met.\nWhen an alert is triggered the server forwards it to the Alertmanager which is another component of the Prometheus monitoring stack. The Alertmanager applies some routing and grouping logic before sending a notification out to its configured receivers.\nSome systems and services do not expose metrics by default. In these cases we use Prometheus exporters which are installed on systems or alongside services to collect metrics and expose them through endpoints. These endpoints can then be configured as targets for the Prometheus server.\nFinally there are cases where we need to collect metrics from ephemeral jobs that complete before the Prometheus server has an opportunity to scrape them. In these situations we can configure the jobs to push their metrics to the Pushgateway allowing Prometheus to scrape the metrics from there instead.\nBelow we will see some more details about each component of the Prometheus monitoring stack and how to install them.\nNote All scripts below support both x86 and ARM systems running Debian with systemd.\nServices bind to 0.0.0.0 by default. To restrict access update their unit files or use a firewall.\nEach component uses a dedicated service account except cAdvisor which runs as root.\nPrometheus Server The script we will use to install the Prometheus server will place all the configuration files under the /etc/prometheus directory..\n. |-- prometheus.yaml # configuration file |-- rules # rule files | |-- alert.yaml | `-- recording.yaml `-- targets # target files |-- servers |-- services `-- sites The main configuration file for the server is the /etc/prometheus/prometheus.yaml, where we configure things like how often to scrape targets, how often to evaluate rules, where is the Alertmanager, where are the rules, and where are the targets.\nNote that any change we make to this configuration file will not take effect until we restart the Prometheus systemd service.\nOn the configuration file we have configured targets for all the components of the Prometheus monitoring stack that we will install. In addition to these we have also configured three scrape jobs with the names \u0026ldquo;servers\u0026rdquo;, \u0026ldquo;services\u0026rdquo; and \u0026ldquo;sites\u0026rdquo;. These jobs are using the file based discovery mechanism which allows us to add, update and remove targets without having to restart the Prometheus systemd service each time.\nThe target files are stored under /etc/prometheus/targets and are categorized according to the following convention. The /etc/prometheus/targets/servers directory contains Node Exporter targets for physical and virtual machines. The /etc/prometheus/targets/services directory contains targets for software that exposes metrics. The /etc/prometheus/targets/sites directory contains targets monitored through the Blackbox Exporter.\nTo add a target under servers..\nTARGET_IP=\u0026#34;\u0026#34; # 10.0.0.3 TARGET_PORT=\u0026#34;\u0026#34; # 9100 TARGET_NAME=\u0026#34;\u0026#34; # server cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/prometheus/targets/servers/${TARGET_NAME}.yaml - targets: - \u0026#34;${TARGET_IP}:${TARGET_PORT}\u0026#34; labels: instance: \u0026#34;${TARGET_NAME}\u0026#34; EOF To add a target under services..\nTARGET_IP=\u0026#34;\u0026#34; # 127.0.0.1 TARGET_PORT=\u0026#34;\u0026#34; # 9323 TARGET_NAME=\u0026#34;\u0026#34; # docker cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/prometheus/targets/services/${TARGET_NAME}.yaml - targets: - \u0026#34;${TARGET_IP}:${TARGET_PORT}\u0026#34; labels: instance: \u0026#34;${TARGET_NAME}\u0026#34; EOF To add a target under sites..\nTARGET_URL=\u0026#34;\u0026#34; # https://google.com TARGET_NAME=\u0026#34;\u0026#34; # google cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/prometheus/targets/sites/${TARGET_NAME}.yaml - targets: - \u0026#34;${TARGET_URL}\u0026#34; EOF The names of the files under /etc/prometheus/targets don\u0026rsquo;t really matter and we can have files with multiple targets defined in them. The IP address we provide for the TARGET_IP needs to be reachable by the Prometheus server. We give a value for the instance label so it doesn\u0026rsquo;t automatically get the value from the __address__ label (more info on that here).\nFinally, the script will also create rule files under the /etc/prometheus/rules directory. We can find a collection of alert rules here. Note that any change we make to the rule files will not take effect until we restart the Prometheus systemd service.\nShow prometheus.sh #!/bin/bash # This script installs Prometheus on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;prometheus\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/prometheus/prometheus/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/prometheus ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; ALERTMANAGER_IP=\u0026#34;\u0026#34; ALERTMANAGER_PORT=\u0026#34;\u0026#34; BLACKBOX_EXPORTER_IP=\u0026#34;\u0026#34; BLACKBOX_EXPORTER_PORT=\u0026#34;\u0026#34; CADVISOR_IP=\u0026#34;\u0026#34; CADVISOR_PORT=\u0026#34;\u0026#34; NODE_EXPORTER_IP=\u0026#34;\u0026#34; NODE_EXPORTER_PORT=\u0026#34;\u0026#34; PROMETHEUS_IP=\u0026#34;\u0026#34; PROMETHEUS_PORT=\u0026#34;\u0026#34; PUSHGATEWAY_IP=\u0026#34;\u0026#34; PUSHGATEWAY_PORT=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading prometheus %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz https://github.com/prometheus/prometheus/releases/download/${APP_VERSION}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz tar -xzf ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz --directory ${TEMP_DIR} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop prometheus.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/prometheus /usr/local/bin/prometheus install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/promtool /usr/local/bin/promtool systemctl --quiet start prometheus.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; useradd --system --no-create-home --shell /bin/false ${APP_SA} mkdir /etc/prometheus mkdir /etc/prometheus/rules mkdir /etc/prometheus/targets mkdir /etc/prometheus/targets/servers mkdir /etc/prometheus/targets/services mkdir /etc/prometheus/targets/sites mkdir /var/lib/prometheus cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/prometheus/prometheus.yaml global: scrape_interval: 15s evaluation_interval: 15s alerting: alertmanagers: - static_configs: - targets: - ${ALERTMANAGER_IP:-127.0.0.1}:${ALERTMANAGER_PORT:-9093} rule_files: - /etc/prometheus/rules/*.yml - /etc/prometheus/rules/*.yaml scrape_configs: - job_name: alertmanager static_configs: - targets: - ${ALERTMANAGER_IP:-127.0.0.1}:${ALERTMANAGER_PORT:-9093} labels: instance: alertmanager - job_name: blackbox-exporter static_configs: - targets: - ${BLACKBOX_EXPORTER_IP:-127.0.0.1}:${BLACKBOX_EXPORTER_PORT:-9115} labels: instance: blackbox-exporter - job_name: cadvisor static_configs: - targets: - ${CADVISOR_IP:-127.0.0.1}:${CADVISOR_PORT:-8080} labels: instance: cadvisor - job_name: node-exporter static_configs: - targets: - ${NODE_EXPORTER_IP:-127.0.0.1}:${NODE_EXPORTER_IP:-9100} labels: instance: node-exporter - job_name: prometheus static_configs: - targets: - ${PROMETHEUS_IP:-127.0.0.1}:${PROMETHEUS_PORT:-9090} labels: instance: prometheus - job_name: pushgateway honor_labels: true static_configs: - targets: - ${PUSHGATEWAY_IP:-127.0.0.1}:${PUSHGATEWAY_PORT:-9091} labels: instance: pushgateway - job_name: servers metrics_path: /metrics file_sd_configs: - files: - /etc/prometheus/targets/servers/*.yml - /etc/prometheus/targets/servers/*.yaml - /etc/prometheus/targets/servers/*.json - job_name: services metrics_path: /metrics file_sd_configs: - files: - /etc/prometheus/targets/services/*.yml - /etc/prometheus/targets/services/*.yaml - /etc/prometheus/targets/services/*.json - job_name: sites metrics_path: /probe params: module: [http_2xx] file_sd_configs: - files: - /etc/prometheus/targets/sites/*.yml - /etc/prometheus/targets/sites/*.yaml - /etc/prometheus/targets/sites/*.json relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: ${BLACKBOX_EXPORTER_IP:-127.0.0.1}:${BLACKBOX_EXPORTER_PORT:-9115} EOF cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; /etc/prometheus/rules/alert.yaml groups: - name: alertmanager rules: - alert: alertmanager_configuration_reload_failure expr: \u0026#39;alertmanager_config_last_reload_successful != 1\u0026#39; for: 0m - alert: alertmanager_too_many_restarts expr: \u0026#39;changes(process_start_time_seconds{job=\u0026#34;alertmanager\u0026#34;}[15m]) \u0026gt; 2\u0026#39; for: 0m - alert: alertmanager_notifications_failing expr: \u0026#39;rate(alertmanager_notifications_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - name: blackbox_exporter rules: - alert: blackbox_exporter_configuration_reload_failure expr: \u0026#39;blackbox_exporter_config_last_reload_successful != 1\u0026#39; for: 0m - alert: blackbox_exporter_too_many_restarts expr: \u0026#39;changes(process_start_time_seconds{job=\u0026#34;blackbox-exporter\u0026#34;}[15m]) \u0026gt; 2\u0026#39; for: 0m - alert: blackbox_exporter_probe_failed expr: \u0026#39;probe_success == 0\u0026#39; for: 0m - alert: blackbox_exporter_probe_http_failure expr: \u0026#39;probe_http_status_code \u0026lt;= 199 OR probe_http_status_code \u0026gt;= 400\u0026#39; for: 0m - alert: blackbox_exporter_ssl_certificate_expiration_7_days expr: \u0026#39;3 \u0026lt;= round((last_over_time(probe_ssl_earliest_cert_expiry[10m]) - time()) / 86400, 0.1) \u0026lt; 7\u0026#39; for: 0m - alert: blackbox_exporter_ssl_certificate_expiration_3_days expr: \u0026#39;0 \u0026lt;= round((last_over_time(probe_ssl_earliest_cert_expiry[10m]) - time()) / 86400, 0.1) \u0026lt; 3\u0026#39; for: 0m - alert: blackbox_exporter_ssl_certificate_expired expr: \u0026#39;round((last_over_time(probe_ssl_earliest_cert_expiry[10m]) - time()) / 86400, 0.1) \u0026lt; 0\u0026#39; for: 0m - name: cadvisor rules: - alert: cadvisor_metrics_absent expr: \u0026#39;absent(container_last_seen)\u0026#39; for: 5m - alert: cadvisor_container_unhealthy expr: \u0026#39;container_health_state == 0\u0026#39; for: 5m - name: node_exporter rules: - alert: node_exporter_cpu_load_high expr: \u0026#39;(sum by (instance) (avg by (mode, instance) (rate(node_cpu_seconds_total{mode!=\u0026#34;idle\u0026#34;}[2m]))) \u0026gt; 0.8) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 10m - alert: node_exporter_cpu_steal_high expr: \u0026#39;(avg by(instance) (rate(node_cpu_seconds_total{mode=\u0026#34;steal\u0026#34;}[5m])) * 100 \u0026gt; 10) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 0m - alert: node_exporter_cpu_iowait_high expr: \u0026#39;(avg by (instance) (rate(node_cpu_seconds_total{mode=\u0026#34;iowait\u0026#34;}[5m])) * 100 \u0026gt; 10) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 0m - alert: node_exporter_memory_available_low expr: \u0026#39;(node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 \u0026lt; 10) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_memory_pressure_high expr: \u0026#39;(rate(node_vmstat_pgmajfault[1m]) \u0026gt; 1000) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_swap_usage_high expr: \u0026#39;((1 - (node_memory_SwapFree_bytes / node_memory_SwapTotal_bytes)) * 100 \u0026gt; 80) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_oom_kill_detected expr: \u0026#39;(increase(node_vmstat_oom_kill[1m]) \u0026gt; 0) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 0m - alert: node_exporter_disk_space_low expr: \u0026#39;((node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes \u0026lt; 10 and ON (instance, device, mountpoint) node_filesystem_readonly == 0) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_disk_inodes_low expr: \u0026#39;(node_filesystem_files_free{fstype!=\u0026#34;msdosfs\u0026#34;} / node_filesystem_files{fstype!=\u0026#34;msdosfs\u0026#34;} * 100 \u0026lt; 10 and ON (instance, device, mountpoint) node_filesystem_readonly == 0) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_filesystem_error expr: \u0026#39;node_filesystem_device_error == 1\u0026#39; for: 2m - alert: node_exporter_network_receive_errors expr: \u0026#39;(rate(node_network_receive_errs_total[2m]) / rate(node_network_receive_packets_total[2m]) \u0026gt; 0.01) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_network_transmit_errors expr: \u0026#39;(rate(node_network_transmit_errs_total[2m]) / rate(node_network_transmit_packets_total[2m]) \u0026gt; 0.01) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_network_interface_saturated expr: \u0026#39;((rate(node_network_receive_bytes_total{device!~\u0026#34;^tap.*|^vnet.*|^veth.*|^tun.*\u0026#34;}[1m]) + rate(node_network_transmit_bytes_total{device!~\u0026#34;^tap.*|^vnet.*|^veth.*|^tun.*\u0026#34;}[1m])) / node_network_speed_bytes{device!~\u0026#34;^tap.*|^vnet.*|^veth.*|^tun.*\u0026#34;} \u0026gt; 0.8 \u0026lt; 10000) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 1m - alert: node_exporter_network_conntrack_entries_high expr: \u0026#39;(node_nf_conntrack_entries / node_nf_conntrack_entries_limit \u0026gt; 0.8) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 5m - alert: node_exporter_hardware_temp_high expr: \u0026#39;((node_hwmon_temp_celsius * ignoring(label) group_left(instance, job, node, sensor) node_hwmon_sensor_label{label!=\u0026#34;tctl\u0026#34;} \u0026gt; 75)) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 5m - alert: node_exporter_hardware_temp_alarm expr: \u0026#39;((node_hwmon_temp_crit_alarm_celsius == 1) or (node_hwmon_temp_alarm == 1)) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 0m - alert: node_exporter_time_offset_detected expr: \u0026#39;((node_timex_offset_seconds \u0026gt; 0.05 and deriv(node_timex_offset_seconds[5m]) \u0026gt;= 0) or (node_timex_offset_seconds \u0026lt; -0.05 and deriv(node_timex_offset_seconds[5m]) \u0026lt;= 0)) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 10m - alert: node_exporter_time_not_synchronising expr: \u0026#39;(min_over_time(node_timex_sync_status[1m]) == 0 and node_timex_maxerror_seconds \u0026gt;= 16) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 2m - alert: node_exporter_reboot_required expr: \u0026#39;(node_reboot_required \u0026gt; 0) * on(instance) group_left (nodename) node_uname_info{nodename=~\u0026#34;.+\u0026#34;}\u0026#39; for: 4h - name: prometheus rules: - alert: prometheus_configuration_reload_failure expr: \u0026#39;prometheus_config_last_reload_successful != 1\u0026#39; for: 0m - alert: prometheus_too_many_restarts expr: \u0026#39;changes(process_start_time_seconds{job=\u0026#34;prometheus\u0026#34;}[15m]) \u0026gt; 2\u0026#39; for: 0m - alert: prometheus_timeseries_cardinality_high expr: \u0026#39;label_replace(count by(__name__) ({__name__=~\u0026#34;.+\u0026#34;}), \u0026#34;name\u0026#34;, \u0026#34;$1\u0026#34;, \u0026#34;__name__\u0026#34;, \u0026#34;(.+)\u0026#34;) \u0026gt; 10000\u0026#39; for: 0m - alert: prometheus_notifications_queue_length expr: \u0026#39;min_over_time(prometheus_notifications_queue_length[10m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_notifications_alertmanagers_discovered expr: \u0026#39;prometheus_notifications_alertmanagers_discovered \u0026lt; 1\u0026#39; for: 0m - alert: prometheus_template_text_expansion_failures expr: \u0026#39;increase(prometheus_template_text_expansion_failures_total[3m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_rule_evaluation_failures expr: \u0026#39;increase(prometheus_rule_evaluation_failures_total[3m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_rule_evaluation_slow expr: \u0026#39;prometheus_rule_group_last_duration_seconds \u0026gt; prometheus_rule_group_interval_seconds\u0026#39; for: 5m - alert: prometheus_target_missing expr: \u0026#39;up == 0\u0026#39; for: 0m - alert: prometheus_target_scrape_slow expr: \u0026#39;prometheus_target_interval_length_seconds{quantile=\u0026#34;0.9\u0026#34;} / on (interval, instance, job) prometheus_target_interval_length_seconds{quantile=\u0026#34;0.5\u0026#34;} \u0026gt; 1.05\u0026#39; for: 5m - alert: prometheus_target_scrape_large expr: \u0026#39;increase(prometheus_target_scrapes_exceeded_sample_limit_total[10m]) \u0026gt; 10\u0026#39; for: 5m - alert: prometheus_target_scrape_duplicate expr: \u0026#39;increase(prometheus_target_scrapes_sample_duplicate_timestamp_total[5m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_checkpoint_creation_failed expr: \u0026#39;increase(prometheus_tsdb_checkpoint_creations_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_checkpoint_deletion_failed expr: \u0026#39;increase(prometheus_tsdb_checkpoint_deletions_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_compactions_failed expr: \u0026#39;increase(prometheus_tsdb_compactions_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_head_truncations_failed expr: \u0026#39;increase(prometheus_tsdb_head_truncations_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_reload_failures expr: \u0026#39;increase(prometheus_tsdb_reloads_failures_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_wal_corruptions expr: \u0026#39;increase(prometheus_tsdb_wal_corruptions_total[1m]) \u0026gt; 0\u0026#39; for: 0m - alert: prometheus_tsdb_wal_truncations_failed expr: \u0026#39;increase(prometheus_tsdb_wal_truncations_failed_total[1m]) \u0026gt; 0\u0026#39; for: 0m - name: pushgateway rules: - alert: pushgateway_too_many_restarts expr: \u0026#39;changes(process_start_time_seconds{job=\u0026#34;pushgateway\u0026#34;}[15m]) \u0026gt; 2\u0026#39; for: 0m - name: watchdog rules: - alert: watchdog expr: \u0026#39;vector(1)\u0026#39; for: 0m EOF cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; /etc/prometheus/rules/recording.yaml groups: - name: prometheus interval: 30s rules: - record: prometheus:http_requests:rate5m expr: rate(prometheus_http_requests_total[5m]) labels: aggregation: rate5m EOF install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/prometheus /usr/local/bin/prometheus install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/prometheus-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/promtool /usr/local/bin/promtool chown -R ${APP_SA}:${APP_SA} /etc/prometheus chown -R ${APP_SA}:${APP_SA} /var/lib/prometheus cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/prometheus.service [Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/prometheus \\ --config.file=\u0026#34;/etc/prometheus/prometheus.yaml\u0026#34; \\ --web.enable-lifecycle \\ --web.enable-admin-api \\ --web.enable-remote-write-receiver \\ --log.level=\u0026#34;info\u0026#34; \\ --log.format=\u0026#34;logfmt\u0026#34; \\ --web.listen-address=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}:${APP_LISTEN_PORT:-9090}\u0026#34; \\ --storage.tsdb.path=\u0026#34;/var/lib/prometheus\u0026#34; \\ --storage.tsdb.retention.time=\u0026#34;30d\u0026#34; \\ --storage.tsdb.retention.size=\u0026#34;64GB\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start prometheus.service systemctl --quiet enable prometheus.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; Alertmanager The script we will use to install Alertmanager will place all configuration files under the /etc/alertmanager directory. The /etc/alertmanager/alertmanager.yaml file contains the main Alertmanager configuration, including the location of the notification templates, how alerts are routed to the appropriate receivers, and which receivers are used to send notifications.\nNote that any change we make to the configuration file will not take effect until we restart the Alertmanager systemd service.\nThe script will create a minimal configuration that works well for \u0026ldquo;slow\u0026rdquo; environments that will route all alerts (except the Watchdog alert) to the webhooks receiver. If you want to receive alerts you can configure a ntfy.sh or a Discord webhook URL there.\nThe Watchdog alert is a dead man\u0026rsquo;s switch type of alert designed to always fire. When it stops firing it means that something is wrong with the monitoring system. We will route this alert to a healthchecks.io webhook that will notify us only when it stops firing.\nShow alertmanager.sh #!/bin/bash # This script installs Alertmanager on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;alertmanager\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/prometheus/alertmanager/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/alertmanager ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; DISCORD_WEBHOOK_URL=\u0026#34;\u0026#34; NTFY_WEBHOOK_URL=\u0026#34;\u0026#34; HEALTHCHECKS_WEBHOOK_URL=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading alertmanager %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz https://github.com/prometheus/alertmanager/releases/download/${APP_VERSION}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz tar -xzf ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz --directory ${TEMP_DIR} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop alertmanager.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/alertmanager /usr/local/bin/alertmanager install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/amtool /usr/local/bin/amtool systemctl --quiet start alertmanager.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; useradd --system --no-create-home --shell /bin/false ${APP_SA} mkdir /etc/alertmanager mkdir /etc/alertmanager/templates mkdir /var/lib/alertmanager cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/alertmanager/alertmanager.yaml templates: - \u0026#34;/etc/alertmanager/templates/*.tmpl\u0026#34; route: group_by: [\u0026#39;...\u0026#39;] group_wait: 0s group_interval: 15s repeat_interval: 1d receiver: webhooks routes: - matchers: - alertname=\u0026#34;watchdog\u0026#34; continue: false group_by: [\u0026#39;...\u0026#39;] group_wait: 0s group_interval: 15s repeat_interval: 15s receiver: healthchecks receivers: - name: webhooks discord_configs: - send_resolved: true webhook_url: \u0026#34;${DISCORD_WEBHOOK_URL:-https://discord.com/api/webhooks/1234567890123456789/aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789_AbCdEfGhIjKlMnOpQrStUvWxYz012345}\u0026#34; webhook_configs: - send_resolved: true url: \u0026#34;${NTFY_WEBHOOK_URL:-https://ntfy.sh/zlqukzyfvsdfmlkg}?template=alertmanager\u0026#34; - name: healthchecks webhook_configs: - send_resolved: false url: \u0026#34;${HEALTHCHECKS_WEBHOOK_URL:-https://hc-ping.com/8e3b7b7e-1c9c-4c22-8f4e-6e3f1e9c8f9d}\u0026#34; EOF install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/alertmanager /usr/local/bin/alertmanager install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/alertmanager-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/amtool /usr/local/bin/amtool chown -R ${APP_SA}:${APP_SA} /etc/alertmanager chown -R ${APP_SA}:${APP_SA} /var/lib/alertmanager cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/alertmanager.service [Unit] Description=Alertmanager Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/alertmanager \\ --config.file=\u0026#34;/etc/alertmanager/alertmanager.yaml\u0026#34; \\ --cluster.listen-address=\u0026#34;\u0026#34; \\ --log.level=\u0026#34;info\u0026#34; \\ --log.format=\u0026#34;logfmt\u0026#34; \\ --web.listen-address=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}:${APP_LISTEN_PORT:-9093}\u0026#34; \\ --storage.path=\u0026#34;/var/lib/alertmanager\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start alertmanager.service systemctl --quiet enable alertmanager.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; Blackbox Exporter This exporter can be used to expose metrics for a wide range of targets but we mainly use it to monitor HTTP endpoints and collect information about availability, latency and TLS certificate expiration.\nCompared to other exporters the Blackbox Exporter works differently. It comes bundled with modules that define different types of checks. When the Prometheus server scrapes the Blackbox Exporter it provides the target and the module to use for the specific check.\nShow blackbox-exporter.sh #!/bin/bash # This script installs Blackbox Exporter on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;blackbox_exporter\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/prometheus/blackbox_exporter/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/blackbox_exporter ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading blackbox exporter %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz https://github.com/prometheus/blackbox_exporter/releases/download/${APP_VERSION}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz tar -xzf ${TEMP_DIR}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz --directory ${TEMP_DIR} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop blackbox_exporter.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/blackbox_exporter /usr/local/bin/blackbox_exporter systemctl --quiet start blackbox_exporter.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; useradd --system --no-create-home --shell /bin/false ${APP_SA} mkdir /etc/blackbox_exporter cp ${TEMP_DIR}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/blackbox.yml /etc/blackbox_exporter/blackbox_exporter.yaml install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/blackbox_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/blackbox_exporter /usr/local/bin/blackbox_exporter chown -R ${APP_SA}:${APP_SA} /etc/blackbox_exporter cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/blackbox_exporter.service [Unit] Description=Blackbox Exporter Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/blackbox_exporter \\ --config.file=\u0026#34;/etc/blackbox_exporter/blackbox_exporter.yaml\u0026#34; \\ --history.limit=\u0026#34;100\u0026#34; \\ --log.level=\u0026#34;info\u0026#34; \\ --log.format=\u0026#34;logfmt\u0026#34; \\ --web.listen-address=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}:${APP_LISTEN_PORT:-9115}\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start blackbox_exporter.service systemctl --quiet enable blackbox_exporter.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; Node Exporter This exporter collects and exposes system-level metrics from the underlying host. It monitors key resources such as CPU, memory, disk usage, filesystem health, and network activity.\nShow node-exporter.sh #!/bin/bash # This script installs Node Exporter on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;node_exporter\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/prometheus/node_exporter/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/node_exporter ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading node exporter %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/node_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz https://github.com/prometheus/node_exporter/releases/download/${APP_VERSION}/node_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz tar -xzf ${TEMP_DIR}/node_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz --directory ${TEMP_DIR} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop node_exporter.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/node_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/node_exporter /usr/local/bin/node_exporter systemctl --quiet start node_exporter.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; useradd --system --no-create-home --shell /bin/false ${APP_SA} install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/node_exporter-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/node_exporter /usr/local/bin/node_exporter cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/node_exporter.service [Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/node_exporter \\ --log.level=\u0026#34;info\u0026#34; \\ --log.format=\u0026#34;logfmt\u0026#34; \\ --web.listen-address=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}:${APP_LISTEN_PORT:-9100}\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start node_exporter.service systemctl --quiet enable node_exporter.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; cAdvisor This exporter exposes metrics for all containers running on a system. We could already expose metrics for the Docker engine by adding the metrics-addr option in the /etc/docker/daemon.json configuration file but with cAdvisor we can expose metrics for the actual containers.\nShow cadvisor.sh #!/bin/bash # This script installs cAdvisor on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;root\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/google/cadvisor/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/cadvisor ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading cadvisor %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/cadvisor-${APP_VERSION}-linux-${SYSTEM_ARCH} https://github.com/google/cadvisor/releases/download/${APP_VERSION}/cadvisor-${APP_VERSION}-linux-${SYSTEM_ARCH} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop cadvisor.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/cadvisor-${APP_VERSION}-linux-${SYSTEM_ARCH} /usr/local/bin/cadvisor systemctl --quiet start cadvisor.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/cadvisor-${APP_VERSION}-linux-${SYSTEM_ARCH} /usr/local/bin/cadvisor cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/cadvisor.service [Unit] Description=cAdvisor Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/cadvisor \\ --docker_only=true \\ --housekeeping_interval=\u0026#34;10s\u0026#34; \\ --docker=\u0026#34;unix:///var/run/docker.sock\u0026#34; \\ --listen_ip=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}\u0026#34; \\ --port=\u0026#34;${APP_LISTEN_PORT:-8080}\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start cadvisor.service systemctl --quiet enable cadvisor.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; Pushgateway When scraping the Pushgateway it is important to set honor_labels: true in the Prometheus configuration to preserve the labels provided by the pushed metrics.\nHere is a simple example of pushing a metric to the Pushgateway using curl..\necho \u0026#34;batch_job_duration_seconds 21\u0026#34; | \\ curl --data-binary @- \\ http://localhost:9091/metrics/job/batch/instance/server This pushes the batch_job_duration_seconds metric with a value of 21 for the batch job and server instance.\nShow pushgateway.sh #!/bin/bash # This script installs Pushgateway on Debian based systems. set -e set -u set -o pipefail ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( curl jq ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### app variables APP_SA=\u0026#34;pushgateway\u0026#34; APP_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/prometheus/pushgateway/releases/latest | jq -r .tag_name)\u0026#34; SYSTEM_ARCH=\u0026#34;$(dpkg --print-architecture)\u0026#34; APP_UPGRADE=false if [[ -f /usr/local/bin/pushgateway ]]; then APP_UPGRADE=true fi TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT APP_LISTEN_ADDRESS=\u0026#34;\u0026#34; APP_LISTEN_PORT=\u0026#34;\u0026#34; ### download printf \u0026#34;%sINFO:%s downloading pushgateway %s for %s architecture\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${APP_VERSION}\u0026#34; \u0026#34;${SYSTEM_ARCH}\u0026#34; curl -sLo ${TEMP_DIR}/pushgateway-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz https://github.com/prometheus/pushgateway/releases/download/${APP_VERSION}/pushgateway-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz tar -xzf ${TEMP_DIR}/pushgateway-${APP_VERSION#v}.linux-${SYSTEM_ARCH}.tar.gz --directory ${TEMP_DIR} ### upgrade if [[ \u0026#34;${APP_UPGRADE}\u0026#34; == true ]]; then printf \u0026#34;%sINFO:%s upgrading the existing installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; systemctl --quiet stop pushgateway.service install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/pushgateway-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/pushgateway /usr/local/bin/pushgateway systemctl --quiet start pushgateway.service printf \u0026#34;%sINFO:%s upgrade succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 0 fi ### install printf \u0026#34;%sINFO:%s starting the installation\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; useradd --system --no-create-home --shell /bin/false ${APP_SA} mkdir /var/lib/pushgateway install -m 0755 -o ${APP_SA} -g ${APP_SA} ${TEMP_DIR}/pushgateway-${APP_VERSION#v}.linux-${SYSTEM_ARCH}/pushgateway /usr/local/bin/pushgateway chown -R ${APP_SA}:${APP_SA} /var/lib/pushgateway cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/systemd/system/pushgateway.service [Unit] Description=Pushgateway Wants=network-online.target After=network-online.target [Service] Type=simple Restart=always User=${APP_SA} Group=${APP_SA} ExecStart=/usr/local/bin/pushgateway \\ --persistence.file=\u0026#34;/var/lib/pushgateway/pushgateway.data\u0026#34; \\ --persistence.interval=\u0026#34;5m\u0026#34; \\ --web.enable-lifecycle \\ --web.enable-admin-api \\ --log.level=\u0026#34;info\u0026#34; \\ --log.format=\u0026#34;logfmt\u0026#34; \\ --web.listen-address=\u0026#34;${APP_LISTEN_ADDRESS:-0.0.0.0}:${APP_LISTEN_PORT:-9091}\u0026#34; [Install] WantedBy=multi-user.target EOF systemctl --quiet daemon-reload systemctl --quiet start pushgateway.service systemctl --quiet enable pushgateway.service printf \u0026#34;%sINFO:%s installation succeeded\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; Troubleshooting If you create a target through a file and it does not appear on the Prometheus dashboard at the URL http://${PROMETHEUS_IP}:9090/targets it means that there is an issue with the file you created in /etc/prometheus/targets.\nIf you create a target and it appears on the Prometheus dashboard but its state is DOWN it means that either Prometheus can\u0026rsquo;t reach the ${TARGET_IP} or that the metrics are not exposed at the URL http://${TARGET_IP}:${TARGET_PORT}/metrics.\nIf you create a rule file and it doesn\u0026rsquo;t appear on the Prometheus dashboard at the URL http://${PROMETHEUS_IP}:9090/rules restart the Prometheus systemd service.\nIf you see alerts firing at the URL http://${PROMETHEUS_IP}:9090/alerts but you are not receiving notifications, check that..\nthere is no issue with the configuration file of the Alertmanager.. amtool check-config /etc/alertmanager/alertmanager.yaml the Alertmanager service is running.. systemctl status alertmanager in /etc/prometheus/prometheus.yaml under the alerting: section the IP of the Alertmanager is correct and reachable by the system hosting the Prometheus server in /etc/alertmanager/alertmanager.yaml you have configured your webhooks correctly If cAdvisor is using too much CPU check this GitHub issue.\nOngoing Maintenance The scripts we used for the installation can also upgrade the components they install. On the first run they perform a full installation, subsequent runs upgrade the components to their latest versions without changing their configuration files.\nClosing Thoughts So we have seen how to install and configure the Prometheus monitoring stack. You might be wondering, why not just run everything in Docker?\nRunning everything in Docker would definitely make sense, but for my use case, I treat Docker as ephemeral while I want my monitoring stack to be more permanent.\nFinally there are two useful tools worth mentioning, pint which is a Prometheus rule linter and sloth which can help you create SLOs.\n","permalink":"https://blog.korliaftis.com/posts/monitoring-with-prometheus.html","summary":"\u003cp\u003eSometimes everything looks fine on the surface but appearances can be deceiving. Here we will see how to set up the Prometheus monitoring stack to monitor our stuff and send alerts when needed.\u003c/p\u003e\n\u003cp\u003eThe core component of the Prometheus monitoring stack is the Prometheus server. We configure targets for the server (meaning endpoints that expose metrics) and the server connects to them at regular intervals to scrape their metrics and store them locally in a time series database (TSDB).\u003c/p\u003e","title":"Monitoring with Prometheus"},{"content":"☝️\n","permalink":"https://blog.korliaftis.com/posts/diving-into-rust.html","summary":"Everything we have done so far was just setting the stage for our real goal, which is getting our hands dirty with the Rust programming language. It\u0026rsquo;s a modern engineering marvel destined to make all other languages obsolete. Either we jump on the Rust wagon now or get left behind with the memory-unsafe losers.","title":"Diving into Rust"},{"content":"We now have a working system with several tools installed but in most cases we want these tools to work together and reach a specific state before we can start using them. Once we define what that state should look like we also need a simple way to create and tear down environments as needed.\nFor my use case, that state includes a Kubernetes cluster running Traefik, Prometheus and Argo CD. I automate the environment creation process with a Bash script and Terraform.\nShow Bash script #!/bin/bash # This script automates the deployment and configuration of a Kubernetes environment using Minikube. set -e set -u set -o pipefail ### script variables DOMAIN_NAME=\u0026#34;\u0026#34; # domain name for the deployment - \u0026#34;domain.xyz\u0026#34; GITHUB_USERNAME=\u0026#34;\u0026#34; # github username - \u0026#34;user\u0026#34; GITHUB_REPOSITORY=\u0026#34;\u0026#34; # github repository name - \u0026#34;argocd\u0026#34; MINIKUBE_NODES=\u0026#34;\u0026#34; # number of minikube nodes to create - \u0026#34;3\u0026#34; TLS_CERTIFICATE_PATH=\u0026#34;\u0026#34; # full path to the tls certificate - \u0026#34;/home/user/.tls/fullchain.crt\u0026#34; TLS_PRIVATE_KEY_PATH=\u0026#34;\u0026#34; # full path to the tls private key - \u0026#34;/home/user/.tls/tls.key\u0026#34; SSH_PRIVATE_KEY_PATH=\u0026#34;\u0026#34; # full path to the ssh private key - \u0026#34;/home/user/.ssh/id_ed25519\u0026#34; ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -eq 0 ]]; then printf \u0026#34;%sERROR:%s script running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( docker minikube terraform helm kubectl ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done VARIABLES=( DOMAIN_NAME GITHUB_USERNAME GITHUB_REPOSITORY MINIKUBE_NODES TLS_CERTIFICATE_PATH TLS_PRIVATE_KEY_PATH SSH_PRIVATE_KEY_PATH ) for i in \u0026#34;${VARIABLES[@]}\u0026#34;; do if [[ -z \u0026#34;${!i:-}\u0026#34; ]]; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s variable \\\u0026#34;%s\\\u0026#34; is not set\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done FILES=( TLS_CERTIFICATE_PATH TLS_PRIVATE_KEY_PATH SSH_PRIVATE_KEY_PATH ) for i in \u0026#34;${FILES[@]}\u0026#34;; do VALUE=\u0026#34;${!i}\u0026#34; if [[ -n \u0026#34;${VALUE}\u0026#34; \u0026amp;\u0026amp; ! -f \u0026#34;${VALUE}\u0026#34; ]]; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s file does not exist\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${VALUE}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### reset for i in {5..1}; do printf \u0026#34;%s%s%s\\n\u0026#34; \u0026#34;${CYAN}\u0026#34; \u0026#34;$i\u0026#34; \u0026#34;${RESET}\u0026#34; sleep 1 done if [[ -d \u0026#34;${HOME}/minikube\u0026#34; ]]; then rm -rf \u0026#34;${HOME}/minikube\u0026#34; fi mkdir \u0026#34;${HOME}/minikube\u0026#34; mkdir \u0026#34;${HOME}/minikube/values\u0026#34; cd \u0026#34;${HOME}/minikube\u0026#34; ### helm values cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; values/traefik.yaml deployment: replicas: 2 ingressClass: enabled: true isDefaultClass: true name: traefik providers: kubernetesCRD: ingressClass: traefik kubernetesIngress: ingressClass: traefik logs: access: enabled: true metrics: prometheus: service: enabled: true serviceMonitor: enabled: true ports: web: nodePort: 31080 redirections: entryPoint: to: websecure scheme: https websecure: asDefault: true nodePort: 31443 tlsStore: default: defaultCertificate: secretName: default-tls service: type: NodePort resources: requests: cpu: \u0026#34;100m\u0026#34; memory: \u0026#34;128Mi\u0026#34; limits: cpu: \u0026#34;1000m\u0026#34; memory: \u0026#34;1024Mi\u0026#34; affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchLabels: app.kubernetes.io/name: \u0026#39;{{ template \u0026#34;traefik.name\u0026#34; . }}\u0026#39; app.kubernetes.io/instance: \u0026#39;{{ .Release.Name }}-{{ include \u0026#34;traefik.namespace\u0026#34; . }}\u0026#39; topologyKey: kubernetes.io/hostname EOF cat \u0026lt;\u0026lt; EOF \u0026gt; values/kube-prometheus-stack.yaml crds: enabled: false defaultRules: disabled: Watchdog: true alertmanager: ingress: enabled: true ingressClassName: traefik hosts: - alertmanager.${DOMAIN_NAME} alertmanagerSpec: resources: requests: cpu: \u0026#34;100m\u0026#34; memory: \u0026#34;128Mi\u0026#34; limits: cpu: \u0026#34;1000m\u0026#34; memory: \u0026#34;1024Mi\u0026#34; grafana: enabled: false kubeControllerManager: enabled: false kubeEtcd: enabled: false kubeScheduler: enabled: false prometheusOperator: kubeletEndpointsEnabled: false kubeletEndpointSliceEnabled: true prometheus: ingress: enabled: true ingressClassName: traefik hosts: - prometheus.${DOMAIN_NAME} prometheusSpec: serviceMonitorSelectorNilUsesHelmValues: false retention: 30d resources: requests: cpu: \u0026#34;100m\u0026#34; memory: \u0026#34;128Mi\u0026#34; limits: cpu: \u0026#34;1000m\u0026#34; memory: \u0026#34;1024Mi\u0026#34; serviceDiscoveryRole: \u0026#34;EndpointSlice\u0026#34; EOF cat \u0026lt;\u0026lt; EOF \u0026gt; values/argo-cd.yaml global: domain: argocd.${DOMAIN_NAME} configs: params: server.insecure: true server: ingress: enabled: true ingressClassName: traefik EOF ### terraform manifests cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; terraform.tf terraform { required_version = \u0026#34;\u0026gt;= 1.8.0\u0026#34; required_providers { helm = { source = \u0026#34;hashicorp/helm\u0026#34; version = \u0026#34;3.1.1\u0026#34; } kubernetes = { source = \u0026#34;hashicorp/kubernetes\u0026#34; version = \u0026#34;3.0.1\u0026#34; } } } EOF cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; providers.tf provider \u0026#34;helm\u0026#34; { kubernetes = { config_path = \u0026#34;~/.kube/config\u0026#34; } } provider \u0026#34;kubernetes\u0026#34; { config_path = \u0026#34;~/.kube/config\u0026#34; } EOF cat \u0026lt;\u0026lt; EOF \u0026gt; traefik.tf resource \u0026#34;kubernetes_namespace_v1\u0026#34; \u0026#34;traefik\u0026#34; { metadata { name = \u0026#34;traefik\u0026#34; } } resource \u0026#34;helm_release\u0026#34; \u0026#34;traefik\u0026#34; { chart = \u0026#34;traefik\u0026#34; name = \u0026#34;traefik\u0026#34; namespace = kubernetes_namespace_v1.traefik.id repository = \u0026#34;https://traefik.github.io/charts\u0026#34; values = [file(\u0026#34;values/traefik.yaml\u0026#34;)] version = \u0026#34;38.0.1\u0026#34; timeout = 600 max_history = 10 } resource \u0026#34;kubernetes_secret_v1\u0026#34; \u0026#34;default_tls\u0026#34; { metadata { name = \u0026#34;default-tls\u0026#34; namespace = kubernetes_namespace_v1.traefik.id } data = { \u0026#34;tls.crt\u0026#34; = file(\u0026#34;${TLS_CERTIFICATE_PATH}\u0026#34;) \u0026#34;tls.key\u0026#34; = file(\u0026#34;${TLS_PRIVATE_KEY_PATH}\u0026#34;) } type = \u0026#34;kubernetes.io/tls\u0026#34; } EOF cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; kube-prometheus-stack.tf resource \u0026#34;kubernetes_namespace_v1\u0026#34; \u0026#34;monitoring\u0026#34; { metadata { name = \u0026#34;monitoring\u0026#34; } } resource \u0026#34;helm_release\u0026#34; \u0026#34;prometheus_operator_crds\u0026#34; { chart = \u0026#34;prometheus-operator-crds\u0026#34; name = \u0026#34;prometheus-operator-crds\u0026#34; namespace = kubernetes_namespace_v1.monitoring.id repository = \u0026#34;https://prometheus-community.github.io/helm-charts\u0026#34; version = \u0026#34;25.0.1\u0026#34; timeout = 600 max_history = 10 } resource \u0026#34;helm_release\u0026#34; \u0026#34;kube_prometheus_stack\u0026#34; { chart = \u0026#34;kube-prometheus-stack\u0026#34; name = \u0026#34;kube-prometheus-stack\u0026#34; namespace = kubernetes_namespace_v1.monitoring.id repository = \u0026#34;https://prometheus-community.github.io/helm-charts\u0026#34; values = [file(\u0026#34;values/kube-prometheus-stack.yaml\u0026#34;)] version = \u0026#34;80.12.0\u0026#34; timeout = 600 max_history = 10 } EOF cat \u0026lt;\u0026lt; EOF \u0026gt; argo-cd.tf resource \u0026#34;kubernetes_namespace_v1\u0026#34; \u0026#34;argocd\u0026#34; { metadata { name = \u0026#34;argocd\u0026#34; } } resource \u0026#34;helm_release\u0026#34; \u0026#34;argocd\u0026#34; { chart = \u0026#34;argo-cd\u0026#34; name = \u0026#34;argo-cd\u0026#34; namespace = kubernetes_namespace_v1.argocd.id repository = \u0026#34;https://argoproj.github.io/argo-helm\u0026#34; values = [file(\u0026#34;values/argo-cd.yaml\u0026#34;)] version = \u0026#34;9.2.4\u0026#34; timeout = 600 max_history = 10 } resource \u0026#34;kubernetes_secret_v1\u0026#34; \u0026#34;github\u0026#34; { metadata { name = \u0026#34;github\u0026#34; namespace = kubernetes_namespace_v1.argocd.id labels = { \u0026#34;argocd.argoproj.io/secret-type\u0026#34; = \u0026#34;repository\u0026#34; } } data = { url = \u0026#34;ssh://git@github.com/${GITHUB_USERNAME}/${GITHUB_REPOSITORY}.git\u0026#34; sshPrivateKey = file(\u0026#34;${SSH_PRIVATE_KEY_PATH}\u0026#34;) } type = \u0026#34;Opaque\u0026#34; } EOF ### setup minikube delete || true minikube start --driver=\u0026#34;docker\u0026#34; --nodes=\u0026#34;${MINIKUBE_NODES}\u0026#34; --addons=\u0026#34;metrics-server\u0026#34; terraform init terraform apply -auto-approve \\ -target=\u0026#34;helm_release.prometheus_operator_crds\u0026#34; terraform apply -auto-approve ### outputs printf \u0026#34;%sINFO:%s the password for ArgoCD is %s\\n\u0026#34; \u0026#34;${CYAN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;$(kubectl get secret -n argocd argocd-initial-admin-secret -o jsonpath=\u0026#39;{.data.password}\u0026#39; | base64 --decode)\u0026#34; To run the script, you need:\na domain with A records pointing to your system’s local IP address a wildcard TLS certificate a GitHub account with at least one repository an SSH key configured in the GitHub account Before running the script, make sure to edit it and update the following variables..\nDOMAIN_NAME=\u0026#34;\u0026#34; # domain name for the deployment - \u0026#34;domain.xyz\u0026#34; GITHUB_USERNAME=\u0026#34;\u0026#34; # github username - \u0026#34;user\u0026#34; GITHUB_REPOSITORY=\u0026#34;\u0026#34; # github repository name - \u0026#34;argocd\u0026#34; MINIKUBE_NODES=\u0026#34;\u0026#34; # number of minikube nodes to create - \u0026#34;3\u0026#34; TLS_CERTIFICATE_PATH=\u0026#34;\u0026#34; # full path to the tls certificate - \u0026#34;/home/user/.tls/fullchain.crt\u0026#34; TLS_PRIVATE_KEY_PATH=\u0026#34;\u0026#34; # full path to the tls private key - \u0026#34;/home/user/.tls/tls.key\u0026#34; SSH_PRIVATE_KEY_PATH=\u0026#34;\u0026#34; # full path to the ssh private key - \u0026#34;/home/user/.ssh/id_ed25519\u0026#34; In summary, the script first performs a few preflight checks to ensure everything is in order, it then generates Terraform manifests in the ${HOME}/minikube directory and finally applies them in two stages to avoid dependency errors.\nCaution When you run the script it will delete all existing Minikube clusters and contents from the ${HOME}/minikube directory.\nIn the following sections we will see more details about some key components.\nMinikube We use Minikube with the Docker driver. During installation we created a custom Docker network named minikube allowing the nodes to receive predictable IP addresses.\nThe number of nodes to be created is defined by the MINIKUBE_NODES variable in the script.\nSome of the workloads we deploy require at least 2 nodes otherwise some pods will not be scheduled and remain in a Pending state.\nHAProxy To access HTTP workloads running on Minikube we use the HAProxy we installed on our system.\nIt listens on ports 80 and 443 of the host system and forwards all incoming traffic to the minikube Docker network on ports 31080 and 31443 respectively. For this setup to work, the Kubernetes Ingress Controller (Istio, Traefik, etc) must be configured to create NodePort services on those ports.\nWe have configured HAProxy with active TCP healthchecks so it routes traffic only to backends that have a server running. I have tried some alternatives to HAProxy but none of them could do this simple thing out of the box.. for example NGINX only offers passive TCP healthchecks in it\u0026rsquo;s free version and for active TCP healthchecks we need to use their paid version. I also tried Traefik but as of today it doesn\u0026rsquo;t support any TCP healthchecks for it\u0026rsquo;s backends.\nIn general HAProxy just works, we can access it\u0026rsquo;s dashboard in port 8404 and view it\u0026rsquo;s logs by doing..\nsudo journalctl -u haproxy -f One alternative approach to the whole \u0026ldquo;reverse proxy in front of Minikube\u0026rdquo; thing would be to use MetalLB but I could not make it work consistently with Minikube. That said, it\u0026rsquo;s been some time since I last tested it and it may work great now.\nOne disadvantage of this setup is that ports 80 and 443 of the host system are always bound to HAProxy even when Minikube is not running. If this is something that bothers you, disable the autostart of the HAProxy systemd service and start it only when you need it or even better use a different network interface for HAproxy.\nTraefik As discussed in the HAProxy section, our Ingress Controller needs to use NodePort services on ports 31080 and 3144 so we configure this using the Traefik Helm chart values file.\nAnother thing we configure is a Kubernetes secret on the traefik namespace with the certificate for our domain. Traefik will use this secret to create a TLSStore which will handle TLS for our domain automatically.\nAfter the installation an IngressClass with the name \u0026ldquo;traefik\u0026rdquo; will be created and it will be configured to be the default IngressClass of the cluster. This means that we don\u0026rsquo;t have to set the ingressClassName: field in every Ingress resource we create.\nIf in the future we decide to use multiple Ingress Controllers we can use this field to select which one will handle each Ingress in a case by case basis..\napiVersion: networking.k8s.io/v1 kind: Ingress metadata: # [...] spec: ingressClassName: \u0026#34;traefik\u0026#34; # [...] One final note, Traefik evaluates routing rules based on their length and not based on their \u0026ldquo;specificity\u0026rdquo; meaning that if we create an Ingress for the host blue.domain.xyz and another one for the host *.domain.xyz the rules that will be created will be..\nHost('blue.domain.xyz' HostRegexp('{subdomain:[a-zA-Z0-9-]+}.domain.xyz') The second rule is longer so it will be evaluated first and since it will match the request to blue.domain.xyz it will always take precedence over the more specific route.\nTo solve this we can manually set the priority on the Ingress using an annotation..\napiVersion: networking.k8s.io/v1 kind: Ingress metadata: # [...] annotations: traefik.ingress.kubernetes.io/router.priority: \u0026#34;1\u0026#34; # [...] spec: # [...] The priority can be any value between 1 and 1000 (higher is better). We could either annotate the \u0026ldquo;wildcard\u0026rdquo; Ingress with a low priority or annotate the \u0026ldquo;non-wildcard\u0026rdquo; Ingress with a high priority, either way our problem will be solved.\nPrometheus We install 2 Helm charts related to Prometheus, the prometheus-operator-crds and the kube-prometheus-stack.\nWe mostly use default values for both except from the way the Prometheus operator detects ServiceMonitor resources. The default behavior is for Prometheus to scrape a ServiceMonitor only when it has matching labels. This is useful if we have multiple Prometheus operators on the same cluster.\nIn summary the way it works is the following..\nif serviceMonitorSelectorNilUsesHelmValues: false is configured in the values file we need to run kubectl get prometheuses.monitoring.coreos.com -n monitoring -o yaml | grep -A8 serviceMonitorSelector and add the output as labels to our ServiceMonitor if serviceMonitorSelectorNilUsesHelmValues: true is configured in the values file the ServiceMonitor will be scraped automatically In either case the service monitors can be in any namespace.\nAfter the installation we can access Prometheus at https://prometheus.${DOMAIN_NAME} and Alertmanager at https://alertmanager.${DOMAIN_NAME}.\nArgo CD We install the argo-cd Helm chart and we just configure ingress related settings through the values file. Additionally we create a Kubernetes secret to store the GitHub account credentials and repository information.\nThe web interface is available at https://argocd.${DOMAIN_NAME}, to login use admin as the username and the output of the following command as the password..\nkubectl get secret -n argocd argocd-initial-admin-secret -o jsonpath=\u0026#34;{.data.password}\u0026#34; | base64 --decode \u0026amp;\u0026amp; echo Troubleshooting Usually everything works fine but if there is any issue, start troubleshooting the path of the requests..\nIs DNS pointing to the IP of our system? Does HAProxy see the backends as online? Do the HAProxy logs show our connection attempts? Have all the Helm charts installed correctly? Are all the pods running? This is not an exhaustive list of everything that could have gone wrong but most likely it\u0026rsquo;s one of these things.\nOngoing Maintenance To keep everything up to date, we periodically need to edit the Bash script and update the kubernetes and helm Terraform providers, as well as the traefik, kube-prometheus-stack, prometheus-operator-crds, and argo-cd Helm charts.\nFinally, if you have not automated the process, don\u0026rsquo;t forget to generate a new TLS certificate for your domain every 90 days.\n","permalink":"https://blog.korliaftis.com/posts/connecting-the-dots.html","summary":"\u003cp\u003eWe now have a working system with several tools installed but in most cases we want these tools to work together and reach a specific state before we can start using them. Once we define what that state should look like we also need a simple way to create and tear down environments as needed.\u003c/p\u003e\n\u003cp\u003eFor my use case, that state includes a Kubernetes cluster running Traefik, Prometheus and Argo CD. I automate the environment creation process with a Bash script and Terraform.\u003c/p\u003e","title":"Connecting the Dots"},{"content":"Since I started working with computers I always loved the idea of a homelab, a safe space to mess with stuff, break stuff and sometimes learn a few things along the way.\nBack when I was trying to land my first tech job my homelab was a bunch of noisy Cisco switches and routers. Along the way I discovered GNS3 and eventually those physical switches and routers were virtualized on my Sony Vaio VGN-N31Z with it\u0026rsquo;s Core 2 Duo, 2GB of RAM and 128GB of spinning rust.\nA few years later when I started working with Windows my homelab transitioned into a Windows Server 2012 R2 virtual machine running on VirtualBox. It didn’t take long to realize that if I wanted to run more than just a couple of VMs at the same time I would need some serious hardware. So I ended up breaking the bank on a desktop running the free version of VMware ESXi packed with an Intel i7, 64GB of RAM and a bunch of SSDs.\nFast forward again and with everything moving to the public cloud my homelab shifted to an AWS account. What was once a capital expense with finite resources became an operational expense with \u0026ldquo;limitless\u0026rdquo; capabilities.\nThese days my homelab is a vintage Dell Latitude with an Intel i3, 16GB of RAM and a 1TB SSD running Debian. Here is how I set it up..\nFoundation This guide begins after we have selected the hardware, downloaded the Debian 13 ISO, created a bootable USB drive, and completed a minimal installation with only \u0026ldquo;standard system utilities\u0026rdquo; and \u0026ldquo;SSH server\u0026rdquo; selected.\nAt this point, we should know the machine\u0026rsquo;s IP address on the network and have successfully connected to it via SSH using the user account we created during the installation process.\nLet’s get started..\nswitch to the root user..\nsu - install a few packages..\napt update \u0026amp;\u0026amp; apt upgrade -y \u0026amp;\u0026amp; apt install -y \\ ansible \\ apt-transport-https \\ bash-completion \\ btm \\ buildah \\ ca-certificates \\ curl \\ ethtool \\ git \\ gnupg \\ jq \\ unzip \\ vim \\ htop \\ nmap \\ rsync \\ skopeo \\ strace \\ tcpdump \\ tmux \\ tree \\ uuid-runtime \\ yq install and configure sudo..\nUSERNAME=\u0026#34;user\u0026#34; apt install -y sudo usermod -a -G sudo ${USERNAME} update some kernel parameters..\ncat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt;\u0026gt; /etc/sysctl.conf net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 kernel.printk = 1 4 1 7 fs.inotify.max_user_instances = 16384 EOF sysctl -p create a systemd service to apply our updated kernel parameters at boot..\ncat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt;\u0026gt; /etc/systemd/system/sysctl.service [Unit] After=network.target [Service] Type=oneshot ExecStart=/sbin/sysctl -p [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable sysctl.service configure ssh..\nUSERNAME=\u0026#34;user\u0026#34; install -d -m 0700 -o ${USERNAME} -g ${USERNAME} /home/${USERNAME}/.ssh install -m 0600 -o ${USERNAME} -g ${USERNAME} /dev/null /home/${USERNAME}/.ssh/config install -m 0600 -o ${USERNAME} -g ${USERNAME} /dev/null /home/${USERNAME}/.ssh/authorized_keys su - ${USERNAME} -c \u0026#34;ssh-keygen -t ed25519 -f /home/${USERNAME}/.ssh/id_ed25519 -N \u0026#39;\u0026#39; -C \u0026#39;\u0026#39;\u0026#34; create a few files and directories to keep our stuff organized..\nUSERNAME=\u0026#34;user\u0026#34; install -d -m 0775 -o ${USERNAME} -g ${USERNAME} /home/${USERNAME}/.tls install -d -m 0775 -o ${USERNAME} -g ${USERNAME} /home/${USERNAME}/.scripts install -m 0644 -o ${USERNAME} -g ${USERNAME} /dev/null /home/${USERNAME}/.bash_aliases change the way the bash prompt looks..\nUSERNAME=\u0026#34;user\u0026#34; printf \u0026#39;%s\\n\u0026#39; \u0026#39;export PS1=\u0026#34;\\[\\e[37m\\][\\[\\e[93m\\]\\w\\[\\e[37m\\]] \\[\\e[0m\\]\u0026#34;\u0026#39; \u0026gt;\u0026gt; \u0026#34;/home/${USERNAME}/.bashrc\u0026#34; printf \u0026#39;%s\\n\u0026#39; \u0026#39;export PS1=\u0026#34;\\[\\e[37m\\][\\[\\e[91m\\]\\w\\[\\e[37m\\]] \\[\\e[0m\\]\u0026#34;\u0026#39; \u0026gt;\u0026gt; \u0026#34;/root/.bashrc\u0026#34; Building Blocks Now that we have established the foundation, we can begin adding our building blocks.\nSince I work as a Site Reliability Engineer the tools I use include the following..\ndocker USERNAME=\u0026#34;user\u0026#34; curl -L https://download.docker.com/linux/debian/gpg | gpg --dearmor \u0026gt; /etc/apt/keyrings/docker.gpg echo \u0026#34;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release \u0026amp;\u0026amp; echo \u0026#34;${VERSION_CODENAME}\u0026#34;) stable\u0026#34; \u0026gt; /etc/apt/sources.list.d/docker.list apt update \u0026amp;\u0026amp; apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin usermod -aG docker ${USERNAME} systemctl daemon-reload systemctl enable docker.service systemctl start docker.service By default Docker binds published ports to all interfaces (0.0.0.0) making containers accessible from anywhere. I prefer the opposite, containers should be private unless explicitly exposed.\nsystemctl stop docker.service cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; /etc/docker/daemon.json { \u0026#34;ip\u0026#34;: \u0026#34;127.0.0.1\u0026#34;, \u0026#34;metrics-addr\u0026#34;: \u0026#34;0.0.0.0:9323\u0026#34; } EOF systemctl start docker.service gvisor curl -L https://gvisor.dev/archive.key | gpg --dearmor \u0026gt; /etc/apt/keyrings/gvisor.gpg echo \u0026#34;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/gvisor.gpg] https://storage.googleapis.com/gvisor/releases release main\u0026#34; \u0026gt; /etc/apt/sources.list.d/gvisor.list apt update \u0026amp;\u0026amp; apt install -y runsc kubectl TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; curl -Lo ${TEMP_DIR}/kubectl \u0026#34;https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl\u0026#34; install -m 0755 -o root -g root ${TEMP_DIR}/kubectl /usr/local/bin/kubectl kubectl completion bash \u0026gt; /etc/bash_completion.d/kubectl k9s TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; K9S_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/derailed/k9s/releases/latest | jq -r .tag_name)\u0026#34; curl -Lo ${TEMP_DIR}/k9s_Linux_amd64.tar.gz https://github.com/derailed/k9s/releases/download/${K9S_VERSION}/k9s_Linux_amd64.tar.gz tar -xzf ${TEMP_DIR}/k9s_Linux_amd64.tar.gz --directory ${TEMP_DIR} install -m 0755 -o root -g root ${TEMP_DIR}/k9s /usr/local/bin/k9s k9s completion bash \u0026gt; /etc/bash_completion.d/k9s minikube TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; curl -Lo ${TEMP_DIR}/minikube_latest_amd64.deb https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb dpkg -i ${TEMP_DIR}/minikube_latest_amd64.deb minikube completion bash \u0026gt; /etc/bash_completion.d/minikube When Minikube starts it checks for a Docker network with the same name and creates it automatically if it doesn\u0026rsquo;t exist. By creating this network manually we can define a custom IP range and make the node IPs predictable.\ndocker network rm minikube \u0026gt; /dev/null 2\u0026gt;\u0026amp;1 docker network create \u0026#34;minikube\u0026#34; --driver=\u0026#34;bridge\u0026#34; --subnet=\u0026#34;10.20.30.0/24\u0026#34; --gateway=\u0026#34;10.20.30.1\u0026#34; haproxy A simple reverse proxy to easily access HTTP workloads running on Minikube. It listens on ports 80 and 443 of the system and forwards connections to the Minikube network.\nHAPROXY_LISTEN_ADDRESS=\u0026#34;0.0.0.0\u0026#34; apt install -y haproxy systemctl daemon-reload systemctl enable haproxy.service systemctl stop haproxy.service echo \u0026#39;EXTRAOPTS=\u0026#34;-q\u0026#34;\u0026#39; \u0026gt;\u0026gt; /etc/default/haproxy cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/haproxy/haproxy.cfg global log /dev/log local0 info defaults log global timeout client 30000 timeout connect 5000 timeout server 30000 frontend stats mode http bind ${HAPROXY_LISTEN_ADDRESS}:8404 stats enable stats uri / stats refresh 30s no log frontend metrics mode http bind ${HAPROXY_LISTEN_ADDRESS}:8405 http-request use-service prometheus-exporter no log frontend haproxy-http mode tcp bind ${HAPROXY_LISTEN_ADDRESS}:80 default_backend minikube-http option tcplog frontend haproxy-https mode tcp bind ${HAPROXY_LISTEN_ADDRESS}:443 default_backend minikube-https option tcplog backend minikube-http mode tcp server minikube 10.20.30.2:31080 check server minikube-m02 10.20.30.3:31080 check server minikube-m03 10.20.30.4:31080 check server minikube-m04 10.20.30.5:31080 check server minikube-m05 10.20.30.6:31080 check backend minikube-https mode tcp server minikube 10.20.30.2:31443 check server minikube-m02 10.20.30.3:31443 check server minikube-m03 10.20.30.4:31443 check server minikube-m04 10.20.30.5:31443 check server minikube-m05 10.20.30.6:31443 check EOF haproxy -c -f /etc/haproxy/haproxy.cfg systemctl start haproxy.service helm curl -L https://packages.buildkite.com/helm-linux/helm-debian/gpgkey | gpg --dearmor \u0026gt; /etc/apt/keyrings/helm.gpg echo \u0026#34;deb [signed-by=/etc/apt/keyrings/helm.gpg] https://packages.buildkite.com/helm-linux/helm-debian/any any main\u0026#34; \u0026gt; /etc/apt/sources.list.d/helm.list apt update \u0026amp;\u0026amp; apt install -y helm helm completion bash \u0026gt; /etc/bash_completion.d/helm terraform USERNAME=\u0026#34;user\u0026#34; curl -L https://apt.releases.hashicorp.com/gpg | gpg --dearmor \u0026gt; /etc/apt/keyrings/hashicorp.gpg echo \u0026#34;deb [signed-by=/etc/apt/keyrings/hashicorp.gpg] https://apt.releases.hashicorp.com $(. /etc/os-release \u0026amp;\u0026amp; echo \u0026#34;${VERSION_CODENAME}\u0026#34;) main\u0026#34; \u0026gt; /etc/apt/sources.list.d/hashicorp.list apt update \u0026amp;\u0026amp; apt install -y terraform echo \u0026#39;complete -C /usr/bin/terraform terraform\u0026#39; \u0026gt;\u0026gt; /home/${USERNAME}/.bashrc opentofu USERNAME=\u0026#34;user\u0026#34; curl -L https://get.opentofu.org/opentofu.gpg \u0026gt; /etc/apt/keyrings/opentofu.gpg curl -L https://packages.opentofu.org/opentofu/tofu/gpgkey | gpg --dearmor \u0026gt; /etc/apt/keyrings/opentofu-repo.gpg cat \u0026lt;\u0026lt; \u0026#39;EOF\u0026#39; \u0026gt; /etc/apt/sources.list.d/opentofu.list deb [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main deb-src [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main EOF apt update \u0026amp;\u0026amp; apt install -y tofu echo \u0026#39;complete -C /usr/bin/tofu tofu\u0026#39; \u0026gt;\u0026gt; /home/${USERNAME}/.bashrc grafana curl -L https://apt.grafana.com/gpg.key | gpg --dearmor \u0026gt; /etc/apt/keyrings/grafana.gpg echo \u0026#34;deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main\u0026#34; \u0026gt; /etc/apt/sources.list.d/grafana.list apt update \u0026amp;\u0026amp; apt install -y grafana systemctl daemon-reload systemctl enable grafana-server.service systemctl start grafana-server.service The web interface is on port 3000, login with admin as the username and password.\nact TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; ACT_VERSION=\u0026#34;$(curl -Ls https://api.github.com/repos/nektos/act/releases/latest | jq -r .tag_name)\u0026#34; curl -Lo ${TEMP_DIR}/act_Linux_x86_64.tar.gz https://github.com/nektos/act/releases/download/${ACT_VERSION}/act_Linux_x86_64.tar.gz tar -xzf ${TEMP_DIR}/act_Linux_x86_64.tar.gz --directory ${TEMP_DIR} install -m 0755 -o root -g root ${TEMP_DIR}/act /usr/local/bin/act Tactical Enhancements This part of the guide covers things that are nice to have but they are not strictly necessary.\nA nice upgrade is to register a domain. With a domain we can set up DNS records that take advantage of the global DNS network and also issue valid TLS certificates.\nAfter registering a domain through a registrar, we create a Cloudflare account and transfer the domain\u0026rsquo;s DNS management to it. On Cloudflare, we configure A records that point to the local IP of our system (domain.xyz A 192.168.1.2, *.domain.xyz A 192.168.1.2) and create an API token with the \u0026ldquo;DNS Write\u0026rdquo; permission policy.\nWith the API token we can run a Bash script that uses acme.sh to generate a wildcard TLS certificate and save it to the ~/.tls directory of our system.\nShow Bash script #!/bin/bash # This script generates a wildcard TLS certificate using acme.sh and the Cloudflare API. set -e set -u set -o pipefail ### script variables USERNAME=\u0026#34;\u0026#34; # your debian username - \u0026#34;user\u0026#34; DOMAIN_NAME=\u0026#34;\u0026#34; # your domain name - \u0026#34;domain.xyz\u0026#34; CLOUDFLARE_API_TOKEN=\u0026#34;\u0026#34; # your cloudflare api token - \u0026#34;6_z3K2aFdSpIq6VBkaz3i4xHT_uYiBNmDftWNZ-K\u0026#34; ### color variables GRAY=$\u0026#39;\\033[0;90m\u0026#39; RED=$\u0026#39;\\033[0;91m\u0026#39; GREEN=$\u0026#39;\\033[0;92m\u0026#39; YELLOW=$\u0026#39;\\033[0;93m\u0026#39; BLUE=$\u0026#39;\\033[0;94m\u0026#39; MAGENTA=$\u0026#39;\\033[0;95m\u0026#39; CYAN=$\u0026#39;\\033[0;96m\u0026#39; WHITE=$\u0026#39;\\033[0;97m\u0026#39; RESET=$\u0026#39;\\033[0m\u0026#39; ### preflight checks PREFLIGHT_CHECKS_FAIL=\u0026#34;false\u0026#34; if [[ \u0026#34;${EUID}\u0026#34; -ne 0 ]]; then printf \u0026#34;%sERROR:%s script not running as root\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; exit 1 fi BINARIES=( docker ) for i in \u0026#34;${BINARIES[@]}\u0026#34;; do if ! command -v \u0026#34;${i}\u0026#34; \u0026gt; /dev/null 2\u0026gt;\u0026amp;1; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s %s is not installed\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done VARIABLES=( USERNAME DOMAIN_NAME CLOUDFLARE_API_TOKEN ) for i in \u0026#34;${VARIABLES[@]}\u0026#34;; do if [[ -z \u0026#34;${!i:-}\u0026#34; ]]; then PREFLIGHT_CHECKS_FAIL=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s variable \\\u0026#34;%s\\\u0026#34; is not set\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${PREFLIGHT_CHECKS_FAIL}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi ### acme.sh TEMP_DIR=\u0026#34;$(mktemp -d)\u0026#34; trap \u0026#39;rm -rf \u0026#34;$TEMP_DIR\u0026#34;\u0026#39; EXIT TLS_DIR=\u0026#34;/home/${USERNAME}/.tls\u0026#34; docker run \\ --rm \\ --env CF_Token=\u0026#34;${CLOUDFLARE_API_TOKEN}\u0026#34; \\ --volume \u0026#34;${TEMP_DIR}:/acme.sh/tls\u0026#34; \\ neilpang/acme.sh:latest \\ acme.sh \\ --dns dns_cf \\ --domain \u0026#34;${DOMAIN_NAME}\u0026#34; \\ --domain \u0026#34;*.${DOMAIN_NAME}\u0026#34; \\ --domain \u0026#34;*.dev.${DOMAIN_NAME}\u0026#34; \\ --domain \u0026#34;*.ops.${DOMAIN_NAME}\u0026#34; \\ --issue \\ --keylength ec-256 \\ --server letsencrypt \\ --cert-file /acme.sh/tls/tls.crt \\ --key-file /acme.sh/tls/tls.key \\ --ca-file /acme.sh/tls/ca.crt \\ --fullchain-file /acme.sh/tls/fullchain.crt FILES_MISSING=\u0026#34;false\u0026#34; FILES=( tls.crt tls.key ca.crt fullchain.crt ) for i in \u0026#34;${FILES[@]}\u0026#34;; do if [[ ! -f \u0026#34;${TEMP_DIR}/${i}\u0026#34; ]]; then FILES_MISSING=\u0026#34;true\u0026#34; printf \u0026#34;%sERROR:%s file \\\u0026#34;%s\\\u0026#34; was not created\\n\u0026#34; \u0026#34;${RED}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${i}\u0026#34; fi done if [[ \u0026#34;${FILES_MISSING}\u0026#34; == \u0026#34;true\u0026#34; ]]; then exit 1 fi install -d -m 0700 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TLS_DIR}\u0026#34; install -m 644 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TEMP_DIR}/tls.crt\u0026#34; \u0026#34;${TLS_DIR}/tls.crt\u0026#34; install -m 600 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TEMP_DIR}/tls.key\u0026#34; \u0026#34;${TLS_DIR}/tls.key\u0026#34; install -m 644 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TEMP_DIR}/ca.crt\u0026#34; \u0026#34;${TLS_DIR}/ca.crt\u0026#34; install -m 644 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TEMP_DIR}/fullchain.crt\u0026#34; \u0026#34;${TLS_DIR}/fullchain.crt\u0026#34; cat \u0026#34;${TEMP_DIR}/fullchain.crt\u0026#34; \u0026#34;${TEMP_DIR}/tls.key\u0026#34; \u0026gt; \u0026#34;${TEMP_DIR}/tls.pem\u0026#34; install -m 600 -o \u0026#34;${USERNAME}\u0026#34; -g \u0026#34;${USERNAME}\u0026#34; \u0026#34;${TEMP_DIR}/tls.pem\u0026#34; \u0026#34;${TLS_DIR}/tls.pem\u0026#34; printf \u0026#34;%sINFO:%s certificate files saved to \\\u0026#34;%s\\\u0026#34;\\n\u0026#34; \u0026#34;${GREEN}\u0026#34; \u0026#34;${RESET}\u0026#34; \u0026#34;${TLS_DIR}\u0026#34; Another useful upgrade to consider is setting up remote access. We could rent a VPS and set up WireGuard tunnels between our devices but this adds latency. A better option is to use something like Nebula where the VPS can act as a broker and not as a relay. Another valid option would be to use Cloudflare tunnels. However, the simplest option is to use a managed service like Tailscale or ZeroTier which can handle everything for us.\nThe last upgrade to consider is configuring wake-on-lan. Most systems support this feature but it must be enabled in both the BIOS and on the network interface. We use ethtool to check if wake-on-lan is enabled on our system\u0026rsquo;s ethernet interface..\nINTERFACE=\u0026#34;\u0026#34; # the name of the ethernet interface - enp2s0 ethtool ${INTERFACE} | grep \u0026#34;Wake-on:\u0026#34; If the output is Wake-on: g it means that wake-on-lan is already enabled, if not we do the following to enable it..\nINTERFACE=\u0026#34;\u0026#34; # the name of the ethernet interface - enp2s0 cat \u0026lt;\u0026lt; EOF \u0026gt; /etc/network/interfaces.d/${INTERFACE} auto ${INTERFACE} iface ${INTERFACE} inet dhcp ethernet-wol g EOF systemctl restart networking.service ethtool ${INTERFACE} | grep \u0026#34;Wake-on:\u0026#34; If we pair wake-on-lan with a Raspberry Pi on the same network and a remote access solution we will be able to power on our system and control it from anywhere.\nOngoing Maintenance Setting things up once is fine but without updates we are stuck with a snapshot of the system from the day it was installed.\nRunning apt update \u0026amp;\u0026amp; apt upgrade -y regularly will keep both the system and most of our tools up to date. To update kubectl, k9s, minikube and act we can simply run their installation commands again.\nThe TLS certificate is valid for 90 days and we can issue a fresh certificate by running the script again.\nClosing Thoughts Allright, we saw a bunch of things and touched many topics but one thing we did not cover is security.\nSo is it secure? Well, we prioritized convenience over security in a few areas but at the end of the day this is a homelab not a production system, it will never store sensitive data and if someone breaks into my house to steal my stuff my crusty old hardware will be the least of my concerns.\nAt the end of the day this is all about experimentation and learning so take what works for you and make it your own.\n","permalink":"https://blog.korliaftis.com/posts/laying-the-foundation.html","summary":"\u003cp\u003eSince I started working with computers I always loved the idea of a homelab, a safe space to mess with stuff, break stuff and sometimes learn a few things along the way.\u003c/p\u003e\n\u003cp\u003eBack when I was trying to land my first tech job my homelab was a bunch of noisy Cisco switches and routers. Along the way I discovered GNS3 and eventually those physical switches and routers were virtualized on my Sony Vaio VGN-N31Z with it\u0026rsquo;s Core 2 Duo, 2GB of RAM and 128GB of spinning rust.\u003c/p\u003e","title":"Laying the Foundation"}]