{"id":854,"date":"2022-01-27T02:46:00","date_gmt":"2022-01-27T02:46:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=854"},"modified":"2022-01-27T02:46:00","modified_gmt":"2022-01-27T02:46:00","slug":"alert-manager-kubernetes-guide","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=854","title":{"rendered":"Setting Up Alert Manager on Kubernetes &#8211; Beginners Guide"},"content":{"rendered":"<p>AlertManager is an open-source alerting system that works with the Prometheus Monitoring system. This blog is part of the Prometheus Kubernetes tutorial series.<\/p>\n<p>In our previous posts, we have looked at the following.<\/p>\n<ol>\n<li><a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">Setup Prometheus on Kubernetes<\/a><\/li>\n<li><a href=\"https:\/\/devopscube.com\/setup-kube-state-metrics\/\" rel=\"noreferrer noopener\">Setup Kube State Metrics<\/a><\/li>\n<\/ol>\n<p>In this guide, I will cover the Alert Manager setup and its integration with Prometheus.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> In this guide, all the Alert Manager Kubernetes objects will be created inside a <b><strong style=\"white-space: pre-wrap;\">namespace called monitoring<\/strong><\/b>. If you use a different namespace, you can replace it in the YAML files.<\/div>\n<\/div>\n<h2 id=\"alertmanager-on-kubernetes\">Alertmanager on Kubernetes<\/h2>\n<p>Alert Manager setup has the following key configurations.<\/p>\n<ol>\n<li>A config map for AlertManager configuration<\/li>\n<li>A config Map for AlertManager alert templates<\/li>\n<li>Alert Manager <a href=\"https:\/\/devopscube.com\/kubernetes-deployment-tutorial\/\" rel=\"noreferrer noopener\">Kubernetes Deployment<\/a><\/li>\n<li>Alert Manager service to access the web UI.<\/li>\n<\/ol>\n<h2 id=\"important-setup-notes\">Important Setup Notes<\/h2>\n<p>You should have a working Prometheus setup up and running.<\/p>\n<p>Prometheus should have the correct alert manager service endpoint in its <code>config.yaml<\/code> as shown below to send the alert to Alert Manager.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note:<\/strong><\/b> If you are following my tutorial on <a href=\"https:\/\/devopscube.com\/setup-prometheus-monitoring-on-kubernetes\/\" rel=\"noreferrer noopener\">Prometheus Setup On Kubernetes<\/a>, you don&#8217;t have to add the following configuration because it is part of the <a href=\"https:\/\/github.com\/bibinwilson\/kubernetes-prometheus\/blob\/master\/config-map.yaml?ref=devopscube.com\" rel=\"noreferrer noopener\">Prometheus configmap<\/a>.<\/div>\n<\/div>\n<pre><code>alerting:\n   alertmanagers:\n      - scheme: http\n        static_configs:\n        - targets:\n          - \"alertmanager.monitoring.svc:9093\"<\/code><\/pre>\n<p>All the alerting rules have to be present on Prometheus config based on your needs. It should be created as part of <a href=\"https:\/\/github.com\/bibinwilson\/kubernetes-prometheus\/blob\/master\/config-map.yaml?ref=devopscube.com\" rel=\"noopener noreferrer\">the Prometheus config map<\/a> with a file named <code><strong>prometheus.rules<\/strong><\/code> and added to the <code>config.yaml<\/code> in the following way.<\/p>\n<pre><code>rule_files:\n      - \/etc\/prometheus\/prometheus.rules<\/code><\/pre>\n<p>Alert manager alerts can be written based on the metrics you receive on Prometheus.<\/p>\n<p>For receiving emails for alerts, you need to have a <strong>valid SMTP host<\/strong> in the alert manager <code>config.yaml<\/code> (smarthost parameter). You can customize the email template as per your needs in the Alert Template config map. We have given the generic template in this guide.<\/p>\n<p>Let&#8217;s get started with the setup.<\/p>\n<h2 id=\"alertmanager-kubernetes-manifests\">Alertmanager Kubernetes Manifests<\/h2>\n<p>All the Kubernetes manifests used in this tutorial can be found in this Github link.<\/p>\n<p>Clone the Github repository using the following command.<\/p>\n<pre><code>git clone https:\/\/github.com\/bibinwilson\/kubernetes-alert-manager.git<\/code><\/pre>\n<h2 id=\"config-map-for-alert-manager-configuration\">Config Map for Alert Manager Configuration<\/h2>\n<p>Alert Manager reads its configuration from a <strong>config.yaml <\/strong>file. It contains the configuration of alert template path, email, and other alert receiving configurations.<\/p>\n<p>In this setup, we are using email and slack webhook receivers. You can have a look at all the supported alert <a href=\"https:\/\/prometheus.io\/docs\/alerting\/configuration\/?ref=devopscube.com#%3Creceiver%3E\" rel=\"noopener noreferrer\">receivers from here<\/a>.<\/p>\n<p>Create a file named <strong><code>AlertManagerConfigmap.yaml<\/code><\/strong> and copy the following contents.<\/p>\n<pre><code>kind: ConfigMap\napiVersion: v1\nmetadata:\n  name: alertmanager-config\n  namespace: monitoring\ndata:\n  config.yml: |-\n    global:\n    templates:\n    - '\/etc\/alertmanager\/*.tmpl'\n    route:\n      receiver: alert-emailer\n      group_by: ['alertname', 'priority']\n      group_wait: 10s\n      repeat_interval: 30m\n      routes:\n        - receiver: slack_demo\n        # Send severity=slack alerts to slack.\n          match:\n            severity: slack\n          group_wait: 10s\n          repeat_interval: 1m\n \n    receivers:\n    - name: alert-emailer\n      email_configs:\n      - to: demo@devopscube.com\n        send_resolved: false\n        from: from-email@email.com\n        smarthost: smtp.eample.com:25\n        require_tls: false\n    - name: slack_demo\n      slack_configs:\n      - api_url: https:\/\/hooks.slack.com\/services\/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sz4Qk\n        channel: '#devopscube-demo'<\/code><\/pre>\n<p>Let&#8217;s create the config map using kubectl.<\/p>\n<pre><code>kubectl create -f AlertManagerConfigmap.yaml<\/code><\/pre>\n<h2 id=\"config-map-for-alert-template\">Config Map for Alert Template<\/h2>\n<p>We need alert templates for all the receivers we use (email, Slack, etc). Alert manager will dynamically substitute the values and deliver alerts to the receivers based on the template. You can customize these templates based on your needs.<\/p>\n<p>Create a file named <strong><code>AlertTemplateConfigMap.yaml<\/code><\/strong> and copy the contents from this file link ==&gt; <a href=\"https:\/\/raw.githubusercontent.com\/devopscube\/kubernetes-alert-manager\/master\/AlertManagerConfigmap.yaml?ref=devopscube.com\" rel=\"noopener noreferrer\">Alert Manager Template YAML<\/a><\/p>\n<p>Create the configmap using kubectl.<\/p>\n<pre><code>kubectl create -f AlertTemplateConfigMap.yaml<\/code><\/pre>\n<h2 id=\"create-a-deployment\">Create a Deployment<\/h2>\n<p>In this deployment, we will mount the two config maps we created.<\/p>\n<p>Create a file called <strong><code>Deployment.yaml<\/code><\/strong> with the following contents.<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: alertmanager\n  namespace: monitoring\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: alertmanager\n  template:\n    metadata:\n      name: alertmanager\n      labels:\n        app: alertmanager\n    spec:\n      containers:\n      - name: alertmanager\n        image: prom\/alertmanager:latest\n        args:\n          - \"--config.file=\/etc\/alertmanager\/config.yml\"\n          - \"--storage.path=\/alertmanager\"\n        ports:\n        - name: alertmanager\n          containerPort: 9093\n        resources:\n            requests:\n              cpu: 500m\n              memory: 500M\n            limits:\n              cpu: 1\n              memory: 1Gi\n        volumeMounts:\n        - name: config-volume\n          mountPath: \/etc\/alertmanager\n        - name: templates-volume\n          mountPath: \/etc\/alertmanager-templates\n        - name: alertmanager\n          mountPath: \/alertmanager\n      volumes:\n      - name: config-volume\n        configMap:\n          name: alertmanager-config\n      - name: templates-volume\n        configMap:\n          name: alertmanager-templates\n      - name: alertmanager\n        emptyDir: {}<\/code><\/pre>\n<p>Create the alert manager deployment using kubectl.<\/p>\n<pre><code>kubectl create -f Deployment.yaml<\/code><\/pre>\n<h2 id=\"create-the-alert-manager-service-endpoint\">Create the Alert Manager Service Endpoint<\/h2>\n<p>We need to expose the alert manager using NodePort or Load Balancer just to access the Web UI. Prometheus will talk to the alert manager using the internal service endpoint.<\/p>\n<p>Create a <strong><code>Service.yaml<\/code><\/strong> file with the following contents.<\/p>\n<pre><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: alertmanager\n  namespace: monitoring\n  annotations:\n      prometheus.io\/scrape: 'true'\n      prometheus.io\/port:   '9093'\nspec:\n  selector: \n    app: alertmanager\n  type: NodePort  \n  ports:\n    - port: 9093\n      targetPort: 9093\n      nodePort: 31000<\/code><\/pre>\n<p>Create the service using kubectl.<\/p>\n<pre><code>kubectl create -f Service.yaml<\/code><\/pre>\n<p>Now, you will be able to access Alert Manager on Node Port <code>31000<\/code>. For example,<\/p>\n<pre><code>http:\/\/35.114.150.153:31000<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-324.png\" class=\"kg-image\" alt=\"kubernetes alertmanager dasbboard\" loading=\"lazy\" width=\"1198\" height=\"502\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-324.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-324.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-324.png 1198w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/alert-manager-kubernetes-guide\/\" target=\"_blank\" rel=\"noopener noreferrer\">Setting Up Alert Manager on Kubernetes &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/alert-manager-kubernetes-guide\/<\/p>\n","protected":false},"author":1,"featured_media":855,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=854"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/855"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}