<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zulkarnen on Medium]]></title>
        <description><![CDATA[Stories by Zulkarnen on Medium]]></description>
        <link>https://medium.com/@zulkarnen-force?source=rss-94c2026b2429------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mw2aPAHyeiXK7BRRMtKG9Q@2x.jpeg</url>
            <title>Stories by Zulkarnen on Medium</title>
            <link>https://medium.com/@zulkarnen-force?source=rss-94c2026b2429------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 14:54:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zulkarnen-force/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Set Up Monitoring with Grafana and Loki: A Beginner’s Guide to Watching Your Nginx Server]]></title>
            <link>https://zulkarnen-force.medium.com/set-up-monitoring-with-grafana-and-loki-a-beginners-guide-to-watching-your-nginx-server-a2a6dbba2089?source=rss-94c2026b2429------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2a6dbba2089</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Zulkarnen]]></dc:creator>
            <pubDate>Sat, 21 Dec 2024 10:19:23 GMT</pubDate>
            <atom:updated>2024-12-21T10:19:23.881Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Your Nginx server has secrets; Grafana and Loki spill the beans.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D1s64GDqwnW0G-qW" /><figcaption>Photo by <a href="https://unsplash.com/@behy_studio?utm_source=medium&amp;utm_medium=referral">Behnam Norouzi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Monitoring your server’s activity doesn’t have to be rocket science. Let’s set up Grafana and Loki to monitor your Nginx server like a pro. With simple steps, minimal setup, and powerful insights, you’ll be on your way to spotting errors and optimizing performance in no time. Follow along as we walk through the process with clear explanations and practical examples.</p><p><strong>Why Monitor Your Nginx Server?</strong></p><p>Nginx is often used as a proxy server, handling tons of requests daily. Knowing what’s happening under the hood can help you:</p><ul><li>Spot Issues Quickly: Identify errors like 404s or server overloads.</li><li>Improve Performance: Optimize slow pages or routes.</li><li>Keep Things Secure: Monitor unusual access patterns.</li></ul><p>Using Grafana with Loki gives you a dashboard-friendly, log-powered way to visualize and analyze your Nginx server’s activity.</p><p><strong>Setting Up the Stack</strong></p><p>We’ll deploy Nginx, Loki, Promtail, and Grafana using Docker Compose. Each tool plays a crucial role:</p><ul><li><strong>Nginx</strong>: Serves and logs requests.</li><li><strong>Loki</strong>: Collects and stores logs.</li><li><strong>Promtail</strong>: Reads logs and ships them to Loki.</li><li><strong>Grafana</strong>: Visualizes the logs with cool dashboards.</li></ul><ol><li><strong>Docker Compose Configuration</strong></li></ol><p>Here’s the complete docker-compose.yaml file to get everything running:</p><pre>services:<br>  nginx:<br>    image: nginx:latest<br>    container_name: nginx<br>    ports:<br>      - &quot;80:80&quot;<br>    volumes:<br>      - ./config/nginx.conf:/etc/nginx/nginx.conf<br>      - ./logs:/var/log/nginx<br>    networks:<br>      - monitoring<br><br>  loki:<br>    image: grafana/loki<br>    container_name: loki<br>    command: -config.file=/etc/loki/local-config.yaml<br>    volumes:<br>      - ./loki-config:/etc/loki<br>      - loki_data:/loki<br>    networks:<br>      - monitoring<br><br>  promtail:<br>    image: grafana/promtail<br>    container_name: promtail<br>    volumes:<br>      - ./promtail/promtail-config.yaml:/etc/promtail/config.yaml<br>      - ./logs:/var/log/nginx<br>      - /var/log:/var/log<br>    command: -config.file=/etc/promtail/config.yaml<br>    networks:<br>      - monitoring<br><br>  grafana:<br>    image: grafana/grafana:latest<br>    container_name: grafana<br>    ports:<br>      - &quot;3000:3000&quot;<br>    volumes:<br>      - ./grafana_data:/var/lib/grafana<br>    environment:<br>      - GF_SECURITY_ADMIN_USER=admin<br>      - GF_SECURITY_ADMIN_PASSWORD=admin<br>    networks:<br>      - monitoring<br><br>volumes:<br>  loki_data:<br><br>networks:<br>  monitoring:<br>    driver: bridge</pre><p>Save this as docker-compose.yaml.</p><p><strong>2. Configure Nginx for JSON Logs</strong></p><p>Update your nginx.conf to log requests in JSON format:</p><pre>user  nginx;<br>worker_processes  auto;<br><br>events {<br>    worker_connections  1024;<br>}<br><br>http {<br>    log_format json_combined escape=json &#39;{&#39;<br>        &#39;&quot;time_local&quot;:&quot;$time_local&quot;,&#39;<br>        &#39;&quot;remote_addr&quot;:&quot;$remote_addr&quot;,&#39;<br>        &#39;&quot;request&quot;:&quot;$request&quot;,&#39;<br>        &#39;&quot;uri&quot;:&quot;$uri&quot;,&#39;<br>        &#39;&quot;status&quot;:&quot;$status&quot;,&#39;<br>        &#39;&quot;body_bytes_sent&quot;:&quot;$body_bytes_sent&quot;,&#39;<br>        &#39;&quot;http_referer&quot;:&quot;$http_referer&quot;,&#39;<br>        &#39;&quot;http_user_agent&quot;:&quot;$http_user_agent&quot;,&#39;<br>        &#39;&quot;request_time&quot;:&quot;$request_time&quot;,&#39;<br>        &#39;&quot;upstream_response_time&quot;:&quot;$upstream_response_time&quot;,&#39;<br>        &#39;&quot;server_name&quot;:&quot;$server_name&quot;&#39;<br>    &#39;}&#39;;<br><br>    access_log /var/log/nginx/access.log json_combined;<br><br>    include /etc/nginx/conf.d/*.conf;<br>}<br></pre><p>Save this file as nginx.conf.</p><p><strong>3. Loki Configuration</strong></p><p>Create a local-config.yaml file for Loki:</p><pre>auth_enabled: false<br><br>server:<br>  http_listen_port: 3100<br><br>common:<br>  ring:<br>    instance_addr: 127.0.0.1<br>    kvstore:<br>      store: inmemory<br>  replication_factor: 1<br>  path_prefix: /tmp/loki<br><br>schema_config:<br>  configs:<br>    - from: 2020-05-15<br>      store: tsdb<br>      object_store: filesystem<br>      schema: v13<br>      index:<br>        prefix: index_<br>        period: 24h<br><br>storage_config:<br>  filesystem:<br>    directory: /tmp/loki/chunks</pre><p>Save this file as local-config.yaml in a loki-config folder.</p><p><strong>4. Promtail Configuration</strong></p><p>Create a promtail-config.yaml file for Promtail:</p><pre>server:<br>  http_listen_port: 9080<br><br>clients:<br>  - url: http://loki:3100/loki/api/v1/push<br><br>positions:<br>  filename: /tmp/positions.yaml<br><br>scrape_configs:<br>  - job_name: nginx_logs<br>    static_configs:<br>      - targets:<br>          - localhost<br>        labels:<br>          job: nginx<br>          host: vps<br>          __path__: /var/log/nginx/access.log<br><br>  - job_name: system<br>    static_configs:<br>      - targets:<br>          - localhost<br>        labels:<br>          job: syslog<br>          __path__: /var/log/syslog</pre><p>Save this file as promtail-config.yaml in a promtail folder.</p><p><strong>5. Start the Stack</strong></p><p>Run the following command to start everything:</p><pre>docker compose up -d</pre><p><strong>Setting Up Grafana</strong></p><ol><li>Visit Grafana at <a href="http://localhost:3000/">http://localhost:3000</a>.</li><li>Log in with the default credentials:<br>Username: admin<br>Password: admin</li><li>Add Loki as a data source:<br>- Go to Configuration &gt; Data Sources.<br>- Select Loki.<br>- Enter the URL: <a href="http://loki:3100/">http://loki:3100</a>.<br>- Save &amp; Test.</li></ol><p><strong>Example PromQL Queries</strong></p><p>Now that everything is set up, use this PromQL query to analyze errors:</p><pre>sum by (server_name, uri, status) (<br>count_over_time({job=&quot;nginx&quot;} | json | status =~ &quot;404|400|401|500&quot; [1m])<br>)</pre><p>This query shows error counts grouped by server name, URI, and status.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ty5S2Zxfur-IfEXLHH0FlQ.png" /></figure><p><strong>Example Dashboard</strong></p><p>Here’s a simple example of what your Grafana dashboard might look like after setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Y3rCgTmY7De_YmDUXotkg.png" /><figcaption>Example Unsuccessful Status Code Dashboard</figcaption></figure><p><strong>Wrapping Up</strong></p><p>You’ve just set up a powerful monitoring stack for your Nginx server using Grafana and Loki. With JSON logs, Promtail scraping, and Grafana dashboards, you’re ready to explore insights and improve your server’s performance.</p><p><strong>Next Steps</strong></p><ul><li>Create custom dashboards to suit your needs.</li><li>Explore more PromQL queries for deeper insights.</li><li>Share your setup with your team!</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2a6dbba2089" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes Readiness and Liveness Probe’s]]></title>
            <link>https://zulkarnen-force.medium.com/kubernetes-readiness-and-liveness-probes-c5876d19ac2c?source=rss-94c2026b2429------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5876d19ac2c</guid>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Zulkarnen]]></dc:creator>
            <pubDate>Thu, 15 Aug 2024 05:51:28 GMT</pubDate>
            <atom:updated>2024-08-15T05:55:57.588Z</atom:updated>
            <content:encoded><![CDATA[<p>#kubernetes #k8s</p><h3>Overview</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oQfdAaP2sW6tInR9c-UI2A.png" /></figure><p>In Kubernetes, <em>liveness probes</em> and <em>readiness probes</em> are essential tools for managing the health and availability of applications running in pods. These probes help ensure that your application remains healthy and can handle traffic effectively.</p><p>Liveness Probe: Monitors the health of the application to determine if it should be restarted. If the liveness check fails, Kubernetes assumes something is wrong with the application and restarts the container to recover from potential issues.<br> <br>Readiness Probe: Checks whether the application is ready to handle incoming requests. This is particularly important during startup or when the application experiences temporary issues. If the readiness check fails, Kubernetes will stop routing traffic to the application until it signals readiness.</p><p>The probes are configured with specific endpoints in the application, such as `/healthz` for liveness and `/readyz` for readiness, to provide real-time feedback on the application’s state. Proper configuration of these probes helps maintain the reliability and availability of applications in a Kubernetes environment.</p><h3>Key Concepts</h3><p><em>Liveness Probe</em> is used to check if your application is still alive. If the liveness check fails, Kubernetes assumes that something is wrong with your application and will restart the container to try and fix the problem.</p><p><em>Readiness Probe </em>is used to checks if your application is ready to handle requests. This is especially important during the startup phase or when your application is temporarily unable to process traffic (e.g., during a heavy load). If the readiness check fails, Kubernetes will temporarily stop sending traffic to your application until it’s ready again.</p><h3>Analogy</h3><h4><em>Liveness Probe</em></h4><p>Think of the liveness probe as a heart monitor for your application. Just like a heart monitor checks if a patient’s heart is still beating, the liveness probe checks if your application is still running. If the monitor detects no heartbeat, doctors will take action (like trying to resuscitate). Similarly, if the liveness probe detects that your application isn’t responding or has “stopped beating,” Kubernetes will restart it.</p><h4>Readiness Probe</h4><p>Imagine you’re opening a restaurant. The readiness probe is like a sign on the front door that says “Open” or “Closed.” Even if the restaurant is physically there (alive), it might not be ready to serve customers yet (perhaps the kitchen is still preparing or the staff isn’t ready). The “Closed” sign ensures that no customers walk in until everything is set up. Similarly, the readiness probe tells Kubernetes, “I’m ready to handle requests” or “I’m not ready yet.”</p><h3>Practical Applications</h3><p>Practical examples of configuring liveness and readiness probes in a Kubernetes Deployment.</p><pre>app.get(“/healthz”, (req, res) =&gt; {<br> res.status(200).send(“OK”);<br>});<br><br>app.get(&quot;/readyz&quot;, async (req, res) =&gt; {<br> try {<br> // Perform a simple query to check if the database connection is working<br> const users = await prisma.user.findMany({<br> take: 1, // Limit the query to 1 user<br> });<br>// If the query is successful and returns a result, the application is ready<br> if (users.length &gt; 0) {<br> res.status(200).json({ status: &quot;Ready&quot; });<br> } else {<br> // If the query returns no users, consider the app ready<br> res.status(200).json({ status: &quot;Ready, but no users found&quot; });<br> }<br> } catch (error) {<br> // If the query fails, the application is not ready<br> res.status(503).json({ status: &quot;Not Ready&quot;, error: error.message });<br> }<br>});</pre><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: express-app<br>  namespace: myapp<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: express<br>  template:<br>    metadata:<br>      labels:<br>        app: express<br>    spec:<br>      nodeSelector:<br>        role: app<br>      containers:<br>        - name: express<br>          image: zulkarnen/example-express:latest # Replace with your actual image<br>          env:<br>            - name: DATABASE_URL<br>              value: &quot;postgresql://postgres:postgres@postgres-service:5432/postgres&quot;<br>          ports:<br>            - containerPort: 3000<br>          resources:<br>            limits:<br>              memory: 512Mi<br>              cpu: &quot;1&quot;<br>            requests:<br>              memory: 256Mi<br>              cpu: &quot;0.2&quot;<br><br>          # Liveness Probe to ensure the container is healthy<br>          livenessProbe:<br>            httpGet:<br>              path: /healthz<br>              port: 3000<br>            initialDelaySeconds: 30<br>            periodSeconds: 10<br>            timeoutSeconds: 5<br>            failureThreshold: 3<br><br>          # Readiness Probe to ensure the container is ready to serve traffic<br>          readinessProbe:<br>            httpGet:<br>              path: /readyz<br>              port: 3000<br>            initialDelaySeconds: 10<br>            periodSeconds: 5<br>            timeoutSeconds: 3<br>            successThreshold: 1<br>            failureThreshold: 3<br><br>      # Tolerations for handling node conditions<br>      tolerations:<br>        - key: &quot;node.kubernetes.io/unreachable&quot;<br>          operator: &quot;Exists&quot;<br>          effect: &quot;NoExecute&quot;<br>          tolerationSeconds: 60<br>        - key: &quot;node.kubernetes.io/not-ready&quot;<br>          operator: &quot;Exists&quot;<br>          effect: &quot;NoExecute&quot;<br>          tolerationSeconds: 60</pre><h3>Further Reading &amp; References</h3><p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/">Configure Liveness, Readiness and Startup Probes</a></p><h3><strong>Conclusion</strong></h3><p>Liveness probe ensures that your application is running (and restarts it if it’s not).<br>Readiness probe ensures that your application is ready to handle traffic (and stops sending traffic if it’s not).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5876d19ac2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>