<?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 Guillaume Vincent on Medium]]></title>
        <description><![CDATA[Stories by Guillaume Vincent on Medium]]></description>
        <link>https://medium.com/@guivin?source=rss-9c301642b4c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WabQzxKUwtlZDzL0GXdZrw.jpeg</url>
            <title>Stories by Guillaume Vincent on Medium</title>
            <link>https://medium.com/@guivin?source=rss-9c301642b4c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:16:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@guivin/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[Top 5 Kubernetes Best Practices for Ensuring High Availability and Scalability]]></title>
            <link>https://levelup.gitconnected.com/top-5-kubernetes-best-practices-for-ensuring-high-availability-and-scalability-9656af28e595?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9656af28e595</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Fri, 26 Jul 2024 16:01:07 GMT</pubDate>
            <atom:updated>2024-07-26T16:01:07.893Z</atom:updated>
            <content:encoded><![CDATA[<h4>Key Practices for Achieving Reliable and Scalable Kubernetes Deployments</h4><figure><img alt="The cover image of the article" src="https://cdn-images-1.medium.com/max/1024/1*sPrzeceJYyOoXeyz9zOwtg.jpeg" /><figcaption>Photo de <a href="https://unsplash.com/fr/@jayrheike?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Jay Heike</a> sur <a href="https://unsplash.com/fr/photos/jauges-analogiques-grises-et-multicolores-M6xNMyYi4H8?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><p>Kubernetes has become a cornerstone for managing containerized applications, but to fully harness its power, following best practices is crucial. This article outlines the top five Kubernetes best practices to help you achieve high availability and scalability, ensuring your applications perform optimally under various conditions.</p><h3>1. Utilize Readiness and Liveness Probes</h3><h4>What Are They?</h4><ul><li><strong>Readiness Probes</strong>: Check if a container is ready to accept traffic.</li></ul><figure><img alt="Gif showing how the Kubernetes readiness probe works" src="https://cdn-images-1.medium.com/max/1010/1*fdNZeCl7TESpFtrCcZKYcg.gif" /><figcaption>Readiness probe (source: <a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes?hl=en">cloud.google.com</a>)</figcaption></figure><ul><li><strong>Liveness Probes</strong>: Monitor if a container is still running and healthy.</li></ul><figure><img alt="Gif showing how the Kubernetes liveness probe works" src="https://cdn-images-1.medium.com/max/1010/1*Bew_sz-oQSpLpgok6lEPbA.gif" /><figcaption>Liveness probe (source: <a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes?hl=en">cloud.google.com</a>)</figcaption></figure><h4>Why They Matter</h4><p>Readiness and liveness probes are critical for managing application health. They allow Kubernetes to automatically detect and address issues such as slow starts or unresponsive containers, ensuring minimal disruption.</p><h4>Best Practices</h4><ul><li><strong>Configure Probes in Your YAML Files</strong>: Define readiness and liveness probes in your Pod specifications to provide Kubernetes with vital health information.</li></ul><pre>readinessProbe:<br>  httpGet:<br>    path: /healthz<br>    port: 8080<br>  initialDelaySeconds: 10<br>  periodSeconds: 10</pre><pre>livenessProbe:<br>  httpGet:<br>    path: /healthz<br>    port: 8080<br>  initialDelaySeconds: 30<br>  periodSeconds: 30</pre><ul><li><strong>Tune Parameters Appropriately</strong>: Adjust the initialDelaySeconds, periodSeconds, and other parameters based on your application’s needs to avoid false positives and ensure accurate health checks.</li></ul><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>2. Implement Horizontal Pod Autoscaling</h3><h4>What Is It?</h4><figure><img alt="Diagram showing how the Kubernetes horizontal pod autoscaler (HPA) works with a deployment" src="https://cdn-images-1.medium.com/max/1024/1*oSU6bwv1e5SlDGNlkeXcOw.png" /><figcaption>Horizontal Pod Autoscaling (source:<a href="https://kubernetes.io/docs/home/"> https://kubernetes.io/</a>)</figcaption></figure><p>Horizontal Pod Autoscaling (HPA) automatically scales the number of pod replicas based on observed metrics like CPU utilization or custom metrics.</p><h4>Why It Matters</h4><p>HPA helps maintain optimal performance by dynamically adjusting the number of pods based on current load, preventing over-provisioning and under-provisioning of resources.</p><h4>Best Practices</h4><ul><li><strong>Set Resource Requests and Limits</strong>: Define CPU and memory requests and limits for your containers to provide HPA with accurate metrics for scaling.</li></ul><pre>resources:<br>  requests:<br>    cpu: &quot;100m&quot;<br>    memory: &quot;256Mi&quot;<br>  limits:<br>    cpu: &quot;500m&quot;<br>    memory: &quot;512Mi&quot;</pre><ul><li><strong>Create HPA Objects</strong>: Configure HPA objects to scale your pods based on specific metrics.</li></ul><pre>apiVersion: autoscaling/v1<br>kind: HorizontalPodAutoscaler<br>metadata:<br>  name: my-app-hpa<br>spec:<br>  scaleTargetRef:<br>    apiVersion: apps/v1<br>    kind: Deployment<br>    name: my-app<br>  minReplicas: 2<br>  maxReplicas: 10<br>  targetCPUUtilizationPercentage: 50</pre><p><a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/">Horizontal Pod Autoscaling</a></p><h3>3. Use StatefulSets for Stateful Applications</h3><h4>What Are StatefulSets?</h4><p>StatefulSets manage stateful applications that require persistent storage and stable network identities. They ensure that pods are deployed with unique, consistent identities.</p><h4>Why They Matter</h4><p>StatefulSets are essential for applications that need stable, persistent storage and consistent network identities, such as databases.</p><h4>Best Practices</h4><ul><li><strong>Define Persistent Volumes</strong>: Use PersistentVolumeClaims (PVCs) to ensure data persists across pod restarts.</li></ul><pre>volumeClaimTemplates:<br>- metadata:<br>    name: data<br>  spec:<br>    accessModes: [&quot;ReadWriteOnce&quot;]<br>    resources:<br>      requests:<br>        storage: 1Gi</pre><ul><li><strong>Use Headless Services</strong>: Implement headless services to manage network identities for StatefulSets.</li></ul><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: my-app-headless<br>spec:<br>  clusterIP: None<br>  selector:<br>    app: my-app</pre><h3>4. Implement Rolling Updates and Rollbacks</h3><h4>What Are They?</h4><figure><img alt="A GIF showing a Kubernetes rolling upgrade" src="https://cdn-images-1.medium.com/max/800/1*_Xe44-nSuZoCIKaw3oWkaA.gif" /><figcaption>Kubernetes rolling update (source:<a href="https://kubernetes.io/docs/home/"> https://kubernetes.io/</a>)</figcaption></figure><ul><li><strong>Rolling Updates</strong>: Gradually deploy updates to your application, allowing new versions to be introduced without downtime.</li><li><strong>Rollbacks</strong>: Revert to a previous stable version if the new deployment encounters issues.</li></ul><h4>Why They Matter</h4><p>Rolling updates and rollbacks enable smooth updates and recovery from failed deployments, reducing the risk of downtime and maintaining user experience.</p><h4>Best Practices</h4><ul><li><strong>Configure Rolling Update Strategies</strong>: Define rolling update strategies in your Deployment to control update behavior.</li></ul><pre>strategy:<br>  type: RollingUpdate<br>  rollingUpdate:<br>    maxSurge: 1<br>    maxUnavailable: 1</pre><ul><li><strong>Monitor Deployments</strong>: Keep track of deployment progress and respond quickly to any issues detected during updates.</li></ul><h3>5. Design for Fault Tolerance</h3><h4>What Is It?</h4><p>Fault tolerance involves designing your applications and infrastructure to handle failures gracefully, ensuring continued availability even when components fail.</p><h4>Why It Matters</h4><p>Building fault tolerance improves the resilience of your applications, minimizing downtime and maintaining service availability.</p><h4>Best Practices</h4><ul><li><strong>Distribute Pods Across Nodes</strong>: Use anti-affinity rules to spread pods across different nodes, reducing the impact of node failures.</li></ul><pre>affinity:<br>  podAntiAffinity:<br>    requiredDuringSchedulingIgnoredDuringExecution:<br>    - labelSelector:<br>        matchExpressions:<br>        - key: app<br>          operator: In<br>          values:<br>          - my-app<br>      topologyKey: &quot;kubernetes.io/hostname&quot;</pre><ul><li><strong>Deploy Across Multiple Availability Zones</strong>: Utilize multiple availability zones to enhance fault tolerance and reduce the risk of regional failures.</li><li><strong>Employ Health Checks</strong>: Regularly test the health and performance of your applications to proactively address potential issues.</li></ul><p><a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/">Assigning Pods to Nodes</a></p><h3>Conclusion</h3><p>By implementing these best practices — utilizing readiness and liveness probes, employing Horizontal Pod Autoscaling, using StatefulSets, managing rolling updates and rollbacks, and designing for fault tolerance — you can enhance the reliability and scalability of your Kubernetes deployments. These strategies will help you build robust applications that perform well under varying conditions and adapt to changing demands.</p><p>Incorporate these practices into your Kubernetes workflows to optimize your deployments, improve application stability, and ensure seamless scaling. As you become more familiar with these techniques, you’ll be well-equipped to handle the complexities of modern containerized applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9656af28e595" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/top-5-kubernetes-best-practices-for-ensuring-high-availability-and-scalability-9656af28e595">Top 5 Kubernetes Best Practices for Ensuring High Availability and Scalability</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimize Kubernetes Resource Management with Time-To-Live (TTL) for Cleaner Cluster]]></title>
            <link>https://faun.pub/optimize-kubernetes-resource-management-with-time-to-live-ttl-for-cleaner-cluster-ea1c6e0c1e73?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea1c6e0c1e73</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Wed, 25 Jan 2023 16:50:07 GMT</pubDate>
            <atom:updated>2023-01-25T16:50:07.894Z</atom:updated>
            <content:encoded><![CDATA[<h4>Streamline Kubernetes Resource Management: Learn How to Use Time-To-Live (TTL) to Keep Your Cluster Clean and Optimized</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9aty688TIxJbR_SF" /><figcaption>Photo by <a href="https://unsplash.com/@aronvisuals?utm_source=medium&amp;utm_medium=referral">Aron Visuals</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Kubernetes is a powerful and widely used container orchestration system. However, as your cluster grows, managing resources can become a challenge. One way to optimize resource management in Kubernetes is by using a Time-To-Live (TTL) approach.</p><p>By setting a TTL on your resources, you can ensure that they are automatically cleaned up after a certain period of time, keeping your cluster clean and running smoothly.</p><p>This can help reduce the risk of resource leaks, improve cluster efficiency, and reduce costs. In this guide, we’ll explore the benefits of using a TTL approach for Kubernetes resource management and show you how to implement it in your own cluster.</p><h3>Kube-Janitor: Understanding and Optimizing Kubernetes Resource Management</h3><p><a href="https://codeberg.org/hjacobs/kube-janitor">Kube-Janitor</a> is a Kubernetes resource management tool that helps users optimize and manage their resources more effectively. It allows users to identify and clean up resources that are no longer needed, such as stale and orphaned resources, freeing up valuable resources and improving overall cluster performance.</p><p>One of the key features of Kube-Janitor is its ability to automatically detect and delete resources that are no longer in use. This can include resources that were created by mistake or are no longer needed due to changes in the application or environment. The tool also allows users to set custom rules and policies for resource management, giving them greater control over how resources are managed in their cluster.</p><p>Another important feature of Kube-Janitor is its ability to identify and clean up stale resources. By identifying and cleaning up stale resources, Kube-Janitor helps users to optimize their cluster and reduce the risk of potential security vulnerabilities.</p><p>Overall, Kube-Janitor is a powerful tool that helps users to manage their Kubernetes resources more effectively. It frees up valuable resources, improves overall cluster performance, and reduces the risk of potential security vulnerabilities.</p><h3>Deploy Kube-Janitor</h3><p>Clone the repository for the kube-janitor tool from GitHub:</p><pre>$ git clone https://codeberg.org/hjacobs/kube-janitor.git<br>$ cd deploy/</pre><p>To install Kustomize on a Linux or macOS system, you can use the following command:</p><pre>$ curl -s &quot;https://raw.githubusercontent.com/kubernetes-sigs/kustomize/main/hack/install_kustomize.sh&quot;  | bash</pre><p>This command will download the install script and execute it to download the latest version of Kustomize and place the binary in the PATH.</p><p>Replace the default rules with empty ones, as they will be modified later:</p><pre>$ cat &gt; rules.yaml &lt;&lt; EOF<br># example rules configuration to set TTL for arbitrary objects<br># see https://codeberg.org/hjacobs/kube-janitor for details<br>rules: []<br>EOF</pre><p>Deploy the kube-janitor tool in your Kubernetes cluster:</p><pre>$ kubectl apply -k deploy/</pre><blockquote>💡 By default, kube-janitor is in dry-run mode. Comment the line in deployment.yaml when you feel confident with your rules configuration</blockquote><h3>Configuring Kube-Janitor</h3><p>Edit the previous rules.yamlfile, which defines the resources that you want to clean up and the TTL for each resource type.</p><pre>apiVersion: janitor.k8s.io/v1alpha1<br>kind: JanitorConfig<br>metadata:<br>  name: example-config<br>spec:<br>  rules:<br>  - resources:<br>    - apiGroup: &quot;&quot;<br>      resource: pods<br>    ttl: 1h</pre><p>In this example, the apiGroup and resource fields specify the resource type to be cleaned up (in this case, pods), and the ttl field specifies the TTL for that resource type (in this case, 1 hour).</p><p>You can also configure kube-janitor to clean up multiple resource types by adding multiple rules to the configuration file, like this:</p><pre>apiVersion: janitor.k8s.io/v1alpha1<br>kind: JanitorConfig<br>metadata:<br>  name: example-config<br>spec:<br>  rules:<br>  - resources:<br>    - apiGroup: &quot;&quot;<br>      resource: pods<br>    ttl: 1h<br>  - resources:<br>    - apiGroup: &quot;&quot;<br>      resource: services<br>    ttl: 2h</pre><p>You can also use Kubernetes label selectors to filter which resources should be cleaned up.</p><pre>apiVersion: janitor.k8s.io/v1alpha1<br>kind: JanitorConfig<br>metadata:<br>  name: example-config<br>spec:<br>  rules:<br>  - resources:<br>    - apiGroup: &quot;&quot;<br>      resource: pods<br>    ttl: 1h<br>    selectors:<br>    - matchLabels:<br>        app: test-app</pre><p>Once you have created the configuration file, you can apply it to your cluster using the following command:</p><pre>$ kubectl apply -f janitor-config.yaml</pre><p>This will create a kube-janitor instance in the cluster that will automatically clean up resources that have exceeded their TTLs.</p><blockquote>⚠️ Please note that, it’s a best practice to test your configuration in a test cluster before applying it to a production cluster. Also, you should be careful when using kube-janitor as it will delete resources that exceed the specified TTL and it can cause data loss or service disruption if not configured properly.</blockquote><h3>Use Annotations In Kubernetes Resources</h3><p>Kube-janitor allows you to use annotations to specify a custom Time-To-Live (TTL) for a specific resource or set of resources. You can use annotations to override the TTL specified in the kube-janitor configuration file, and set different TTLs for different resources or groups of resources.</p><p>Here’s an example of how to use an annotation to specify a custom TTL for a specific pod:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-pod<br>  annotations:<br>    janitor.k8s.io/ttl: &quot;30m&quot;<br>spec:<br>  ...</pre><p>In this example, the janitor.k8s.io/ttl annotation is used to set the TTL for the my-pod pod to 30 minutes.</p><p>You can also use annotations to specify a custom TTL for a specific resource type or group of resources. For example, you can use an annotation to set a custom TTL for all pods with a specific label:</p><pre>apiVersion: janitor.k8s.io/v1alpha1<br>kind: JanitorConfig<br>metadata:<br>  name: example-config<br>spec:<br>  rules:<br>  - resources:<br>    - apiGroup: &quot;&quot;<br>      resource: pods<br>      ttl: 1h<br>      selectors:<br>      - matchLabels:<br>          app: test-app<br>          env: prod<br>    annotations:<br>      janitor.k8s.io/ttl: &quot;2h&quot;</pre><p>In this example, the janitor.k8s.io/ttl annotation is used to set the TTL for all pods with the app: test-app and env: prod labels to 2 hours.</p><blockquote>💡 Please note that, if you set a custom TTL using an annotation, that value will take precedence over the value specified in the kube-janitor configuration file.</blockquote><p>It’s important to note that, the annotation format is specific to kube-janitor, you should use the correct annotation format in order to kube-janitor to recognize it and use it.</p><h3>Conclusion</h3><p>Kube-janitor is a powerful Kubernetes operator that can help you manage resources in your cluster by automatically cleaning up resources that have exceeded a specified Time-To-Live (TTL). By using a TTL approach, you can ensure that your cluster stays clean and efficient, and that resources are not wasted on resources that are no longer needed.</p><p>Using kube-janitor is relatively straightforward and can be done by installing the operator in your cluster, creating a configuration file, and applying the configuration to your cluster. The configuration file can be used to specify the resources to be cleaned up and the TTL for each resource type. You also use annotations directly in the Kubernetes resources to avoid using global rules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*Oa8Q9umjmmsrRRrF.png" /></figure><h4>👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇</h4><h4>🚀<a href="http://from.faun.to/r/8zxxd">Join FAUN Developer Community &amp; Get Similar Stories in your Inbox Each Week</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea1c6e0c1e73" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/optimize-kubernetes-resource-management-with-time-to-live-ttl-for-cleaner-cluster-ea1c6e0c1e73">Optimize Kubernetes Resource Management with Time-To-Live (TTL) for Cleaner Cluster</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Setup Prometheus and Grafana Monitoring on Kubernetes]]></title>
            <link>https://medium.com/better-programming/how-to-setup-prometheus-and-grafana-monitoring-on-kubernetes-37d76c97dba0?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/37d76c97dba0</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Thu, 17 Mar 2022 08:55:46 GMT</pubDate>
            <atom:updated>2026-06-14T06:22:26.894Z</atom:updated>
            <content:encoded><![CDATA[<h3>How To Setup Prometheus (Operator) and Grafana Monitoring on Kubernetes</h3><h4>This tutorial shows you how to begin a modern monitoring stack with Prometheus (Operator) and Grafana on Kubernetes</h4><figure><img alt="Tourist viewer so they can see the city from afar" src="https://cdn-images-1.medium.com/max/1024/0*EdoF8_GDwK9G89to.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mattvs?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Matthijs van Schuppen</a> on <a href="https://unsplash.com/photos/bwonz6tZpVA?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Monitoring is a key pillar of DevOps best practices. This gives vital information on the performance and health of a platform. In a distributed environment such as Kubernetes and microservices, it is even more true.</p><p>One of the great strengths of Kubernetes is the ability to scale your services and applications. When you reach thousand of applications, monitoring them manually or using scripts is not viable. You need to adopt a monitoring system that scales too! That’s where Prometheus and Grafana come into the scene.</p><p>Prometheus will collect, store and allow you to leverage your platform metrics. On the other hand, Grafana will plug into Prometheus and allow you to create beautiful dashboards and charts.</p><p>Today, we’ll go over what Prometheus is and the best way to deploy it on Kubernetes — using the operator. We’ll see how to set up a monitoring platform with Prometheus and Grafana.</p><p>This tutorial will give you a good starting point with observability and go further!</p><h3>Prometheus</h3><h4>Timeline</h4><p><a href="https://prometheus.io/">Prometheus</a> is a free and open source application for event monitoring and alerting built at <a href="https://soundcloud.com/">SoundCloud</a> in 2012. Soon after, many companies and organizations adopted it and contributed. In 2016, the <a href="https://www.cncf.io/">Cloud Native Computing Foundation</a> (CNCF) incubated the Prometheus project after Kubernetes.</p><p>The following timeline shows the evolution of the Prometheus project:</p><figure><img alt="2012 — built at SoundCloud 2013 — use in SoundCloud’s production May 2016 — CNCF incubated Prometheus July 2016 — Prometheus 1.0 released Nov 2017 — Prometheus 2.0 released Aug 2018 — CNCF graduated Prometheus production-ready" src="https://cdn-images-1.medium.com/max/642/0*80ochMcj3M9VVHs2.png" /><figcaption>The Prometheus project timeline</figcaption></figure><h4>Concepts</h4><p>Prometheus is considered the default monitoring solution for Kubernetes and was inspired by <a href="https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43438.pdf">Borgman</a> from Google. It collects metrics from applications and infrastructure using the HTTP pull requests. Targets are discovered via service discovery or static configuration. Pushing time series is also supported via an intermediary gateway.</p><figure><img alt="Example of a metric exposes to Prometheus over HTTP" src="https://cdn-images-1.medium.com/max/1024/0*OsqziZC3P9HdxaGU.png" /><figcaption>Example of a metric exposes to Prometheus over HTTP</figcaption></figure><p><em>Metrics exposed by a Prometheus target has the following format: </em>&lt;metric name&gt;{&lt;label name&gt;=&lt;label value&gt;, ...}</p><p>Prometheus records real-time metrics in a time-series database (TSDB), which brings dimensional data models, operational simplicity, and scalable data collection. It provides PromQL, a flexible query language to leverage this dimensionality.</p><h4>Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u_rFr08xLJOWCZDO.png" /><figcaption>The Prometheus architecture from <a href="https://prometheus.io/">https://prometheus.io/</a></figcaption></figure><p>The architecture diagram shows Prometheus is a multi-component monitoring system. The following pieces are integrated into the Prometheus deployment:</p><ul><li>Prometheus server scraping and stores time-series data. It also provides a user interface to query the metrics.</li><li><a href="https://prometheus.io/docs/instrumenting/clientlibs/">Client libraries</a> for instrumenting application code.</li><li><a href="https://github.com/prometheus/pushgateway">Pushgateway</a> supports metrics collection from short-lived jobs</li><li><a href="https://prometheus.io/docs/instrumenting/exporters/">Exporters</a> for services that do not instrument Prometheus metrics directly.</li><li><a href="https://github.com/prometheus/alertmanager">Alertmanager</a> handles real-time alerts based on triggers</li></ul><h3>Why Choose the Prometheus Operator?</h3><p>Kubernetes provides a lot of objects to deploy your applications: Pod, Deployment, Service, Ingress, etc… These are native resources, and as they are generic they do not behave like the final application. Kubernetes allows you to create custom resources through Custom Resource Definition (CRD).</p><p>CRD objects implement the behavior of the final application. This allows better maintainability and reduces the deployment effort. When using the <a href="https://github.com/prometheus-operator/prometheus-operator">Prometheus Operator</a>, each component of the architecture comes from a CRD. This makes the Prometheus setup more straightforward than a classical installation.</p><p>In the classic Prometheus installation, adding new metric endpoints requires an update of the server configuration. This allows registering a new endpoint as a target to collect metrics. The Prometheus Operator uses Monitor objects (<a href="https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#podmonitor">PodMonitor</a>, <a href="https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#servicemonitor">ServiceMonitor</a>) to discover dynamically endpoints and scrape metrics.</p><p><em>Using the Prometheus Operator gains you time on the Prometheus installation and maintainability. It provides you Monitor objects to dynamically collect metrics without updating the Prometheus configuration.</em></p><h3>Deploying Prometheus With the Operator</h3><p>The kube-prometheus-stack is a collection of Kubernetes manifests, Grafana dashboards, and Prometheus rules. It provides easy to operate end-to-end Kubernetes cluster monitoring with Prometheus using the Operator.</p><p><a href="https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack">helm-charts/charts/kube-prometheus-stack at main · prometheus-community/helm-charts</a></p><p>This collection is available and deployable through a Helm chart. You can deploy the monitoring stack in a single command line. New to Helm? Have a look at <a href="https://getbetterdevops.io/helm-quickstart-tutorial/">this tutorial</a> to get started!</p><h4>Installing Helm</h4><pre>$ brew install helm</pre><h4>Creating the dedicated monitoring namespace</h4><p>In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster. We create a namespace named monitoring to prepare the new deployment:</p><pre>$ kubectl create namespace monitoring</pre><h4>Installing kube-prometheus-stack with Helm</h4><p>Add the Prometheus chart repository and update the local cache with these commands:</p><pre>$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts <br>$ helm repo update</pre><p>Deploy the kube-stack-prometheus chart in the namespace monitoring with Helm:</p><pre>$ helm upgrade --namespace monitoring --install kube-stack-prometheus prometheus-community/kube-prometheus-stack --set prometheus-node-exporter.hostRootFsMount.enabled=false</pre><p>hostRootFsMount.enabled<em> is set to </em>false<em> to work on Docker Desktop on Mac</em></p><p>CRDs are now installed in the namespace. You can verify this with the following kubectl command:</p><pre>$ kubectl get -n monitoring crds                                                           <br>NAME                                        CREATED AT<br>alertmanagerconfigs.monitoring.coreos.com   2022-03-15T10:54:41Z<br>alertmanagers.monitoring.coreos.com         2022-03-15T10:54:42Z<br>podmonitors.monitoring.coreos.com           2022-03-15T10:54:42Z<br>probes.monitoring.coreos.com                2022-03-15T10:54:42Z<br>prometheuses.monitoring.coreos.com          2022-03-15T10:54:42Z<br>prometheusrules.monitoring.coreos.com       2022-03-15T10:54:42Z<br>servicemonitors.monitoring.coreos.com       2022-03-15T10:54:42Z<br>thanosrulers.monitoring.coreos.com          2022-03-15T10:54:42Z</pre><p>Here is what we have running now in the namespace:</p><pre>$ kubectl get pods -n monitoring                                                                    <br>NAME                                                       READY   STATUS    RESTARTS   AGE<br>alertmanager-kube-stack-prometheus-kube-alertmanager-0     2/2     Running   0          2m36s<br>kube-stack-prometheus-grafana-6994bd6c69-h6s9z             3/3     Running   0          13h<br>kube-stack-prometheus-kube-operator-86667b5cdf-cqndt       1/1     Running   0          13h<br>kube-stack-prometheus-kube-state-metrics-fc9878699-dpgh6   1/1     Running   0          13h<br>kube-stack-prometheus-prometheus-node-exporter-vrjsl       1/1     Running   0          13h<br>prometheus-kube-stack-prometheus-kube-prometheus-0         2/2     Running   0          13h</pre><p>The chart has installed Prometheus components, Operator, Grafana, and the following exporters:</p><ul><li><a href="https://github.com/prometheus/node_exporter">prometheus-node-exporter</a> exposes hardware and OS metrics</li><li><a href="https://github.com/kubernetes/kube-state-metrics">kube-state-metrics</a> listens to the Kubernetes API server and generates metrics about the state of the objects</li></ul><p>Our monitoring stack with Prometheus and Grafana is up and ready!</p><h3>Connecting to Prometheus’s Web Interface</h3><p>The Prometheus web UI is accessible through port-forward with this command:</p><pre>$ kubectl port-forward --namespace monitoring svc/kube-stack-prometheus-kube-prometheus 9090:9090</pre><p>Opening a browser tab on <a href="http://localhost:9090/">http://localhost:9090</a> shows the Prometheus web UI. We can retrieve the metrics collected from exporters:</p><figure><img alt="Dashboard in the Prometheus web UI showing time-series metrics" src="https://cdn-images-1.medium.com/max/1024/0*hv3JV93RRsXmqkRT.png" /><figcaption>Dashboard in the Prometheus web UI showing time-series metrics</figcaption></figure><p>Going to the “Status&gt;Targets” and you can see all the metric endpoints discovered by the Prometheus server:</p><figure><img alt="The metric endpoints discovered by Prometheus are visible in Status&gt;Targets menu" src="https://cdn-images-1.medium.com/max/1024/0*0oN5q_IE5N100Fq-.png" /><figcaption>The metric endpoints discovered by Prometheus are visible in Status&gt;Targets menu</figcaption></figure><figure><img alt="Few target endpoints discovered by Prometheus server" src="https://cdn-images-1.medium.com/max/1024/0*WYMiQqgHEpQDmOjw.png" /><figcaption>Few target endpoints discovered by Prometheus server</figcaption></figure><h3>Connecting To Grafana</h3><p>The credentials to connect to the Grafana web interface are stored in a Kubernetes Secret and encoded in base64. We retrieve the username and password with these two commands:</p><pre>$ kubectl get secret --namespace monitoring kube-stack-prometheus-grafana -o jsonpath=&#39;{.data.admin-user}&#39; | base64 -d <br>$ kubectl get secret --namespace monitoring kube-stack-prometheus-grafana -o jsonpath=&#39;{.data.admin-password}&#39; | base64 -d</pre><p>We create the port-forward to Grafana with the following command:</p><pre>$ kubectl port-forward --namespace monitoring svc/kube-stack-prometheus-grafana 8080:80</pre><p>Open your browser and go to <a href="http://localhost:8080/">http://localhost:8080</a> and fill in previous credentials:</p><figure><img alt="The Grafana login interface" src="https://cdn-images-1.medium.com/max/667/0*hdmGziekrHjYqZFq.png" /><figcaption>The Grafana login interface</figcaption></figure><p>The kube-stack-prometheus deployment has provisioned Grafana dashboards:</p><figure><img alt="List of the provisioned Grafana dashboards" src="https://cdn-images-1.medium.com/max/1024/0*JC3cma4x11hDCI0_.png" /><figcaption>List of the provisioned Grafana dashboards</figcaption></figure><p>Here we can see one of them showing compute resources of Kubernetes pods:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jy2OIoBHt-pplHir.png" /><figcaption>A Grafana dashboard showing Kubernetes pod compute resources</figcaption></figure><p>All right, that’s how you set up Prometheus and Grafana monitoring on Kubernetes</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37d76c97dba0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-setup-prometheus-and-grafana-monitoring-on-kubernetes-37d76c97dba0">How To Setup Prometheus and Grafana Monitoring on Kubernetes</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Is Helm? A Quickstart Tutorial for Kubernetes Beginners]]></title>
            <link>https://medium.com/better-programming/what-is-helm-a-quickstart-tutorial-for-kubernetes-beginners-24fecce1b0eb?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/24fecce1b0eb</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Mon, 14 Mar 2022 16:03:12 GMT</pubDate>
            <atom:updated>2026-06-14T06:17:13.659Z</atom:updated>
            <content:encoded><![CDATA[<h4>To teach you how to handle app packages, upgrades, and rollbacks</h4><figure><img alt="air balloon" src="https://cdn-images-1.medium.com/max/1024/0*4TVr1bmfbZCR6nzA.png" /><figcaption>Photo by <a href="https://unsplash.com/@kajhinkson?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kyle Hinkson</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Today, Kubernetes becomes a must for DevOps practitioners for orchestrating containers. Once you have a Docker image of your application, you have to write YAML manifests to define Kubernetes workloads. Next, you deploy them with the <a href="https://kubernetes.io/docs/reference/kubectl/">kubectl</a> command.</p><p>This deployment way is when you’ve only one application. When you start to have many applications and multiple environments it becomes overwhelmed. Often you define the same YAML files 90% of the time.</p><p>Here, we are going to focus on how to manage applications smartly with Helm.</p><h3>What Is Helm?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/0*cB9_Z3doNnphE2LJ.png" /><figcaption>The Helm logo from <a href="https://helm.sh/">https://helm.sh/</a></figcaption></figure><p><a href="https://helm.sh/">Helm</a> is a package manager for Kubernetes. Helm is an open source project originally created by <a href="https://deislabs.io/">DeisLabs</a> and donated to the <a href="https://www.cncf.io/">Cloud Native Foundation</a> ( <em>CNCF</em>). The CNCF now maintains and has graduated the project. This means that it is mature and not just a fad.</p><p>Package management is not a new concept in the software industry. On Linux distros, you manage software installation and removal with package managers such as <a href="https://www.redhat.com/sysadmin/how-manage-packages">YUM/RPM</a> or <a href="https://ubuntu.com/server/docs/package-management">APT</a>. On Windows, you can use <a href="https://chocolatey.org/">Chocolatey</a> or <a href="https://brew.sh/">Homebrew</a> on Mac.</p><p>Helm lets you package and deploy complete applications in Kubernetes. A package is called a “ <em>Chart.”</em> Helm uses a templating system based on <a href="https://pkg.go.dev/html/template">Go template</a> to render Kubernetes manifests from charts. A chart is a consistent structure separating templates and values.</p><p>As a package, a chart can also manage dependencies with other charts. For example, if your application needs a MySQL database to work you can include the chart as a dependency. When Helm runs at the top level of the chart directory it installs whole dependencies. You have just a single command to render and release your application to Kubernetes.</p><p>Helm charts use versions to track changes in your manifests — thus you can install a specific chart version for specific infrastructure configurations. Helm keeps a release history of all deployed charts in a dedicated workspace. This makes easier application updates and rollbacks if something wrong happens.</p><p>Helm allows you to compress charts. The result of that is an artifact comparable to a Docker image. Then, you can send it to a distant repository for reusability and sharing.</p><h3>What Are the Benefits of Using Helm?</h3><ul><li>Helm provides you the ability to install applications with a single command. A chart can contain other charts as dependencies. You can consequently deploy an entire stack with Helm. You can use Helm like <a href="https://docs.docker.com/compose/">docker-compose</a> but for Kubernetes.</li><li>A chart includes templates for various Kubernetes resources to form a complete application. This reduces the microservices complexity and simplifies their management in Kubernetes.</li><li>Charts can be compressed and sent to a distant repository. This creates an application artifact for Kubernetes. You can also fetch and deploy existing Helm charts from repositories. This is a strong point for reusability and sharing.</li><li>Helm maintains a history of deployed release versions in the Helm workspace. When something goes wrong, rolling back to a previous version is simply — canary release is facilitated with Helm for zero-downtime deployments.</li><li>Helm makes the deployment highly configurable. Applications can be customized on the fly during the deployment. By changing parameters, you can use the same chart for multiple environments such as dev, staging, and production.</li><li>Streamline CI/CD pipelines — Forward GitOps best practices.</li></ul><h3>Quick Look at the Problem Helm Solves</h3><p>Basic Kubernetes practice is to write YAML manifests manually. We’ll create minimum YAML files to deploy NGINX in Kubernetes.</p><p>Here is the deployment that will create Pods:</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx<br>spec:<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  replicas: 1<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      containers:<br>      - name: nginx<br>        image: nginx:1.21.6<br>        ports:<br>        - containerPort: 80</pre><p>The Service exposes NGINX to the outside. The link with pod is done via the selector:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx<br>spec:<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 8080</pre><p>Now we have to create the previous resources with the kubectl command below:</p><pre>$ kubectl create -f deployment.yaml <br>$ kubectl create -f service.yaml</pre><p>We check all resources are up and running:</p><pre>$ kubectl get deployment -l app=nginx<br>NAME    READY   UP-TO-DATE   AVAILABLE   AGE<br>nginx   1/1     1            1           8m29s<br>$ kubectl get pods -l app=nginx                                                                                      <br>NAME                     READY   STATUS    RESTARTS   AGE<br>nginx-65b89996ff-dcfs9   1/1     Running   0          2m26s<br>$ kubectl get svc -l app=nginx <br>NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE<br>nginx   ClusterIP   10.106.79.171   &lt;none&gt;        80/TCP    4m58s</pre><ul><li>Specific values in YAML manifests are hardcoded and not reusable.</li><li>Redundant information to specify such as labels and selectors leads to potential errors.</li><li>Kubectl does not handle potential errors after execution. You’ve to deploy each file one after the other.</li><li>There’s no change traceability.</li></ul><h3>Create a Helm Chart From Scratch</h3><p>Helm can create the chart structure in a single command line:</p><pre>$ helm create nginx</pre><h4>Understand the Helm chart’s structure</h4><figure><img alt="The tree structure of a Helm chart" src="https://cdn-images-1.medium.com/max/1024/0*qsbErzoB3tT9s4GQ.png" /><figcaption>The tree structure of a Helm chart</figcaption></figure><ul><li>Chart.yaml: A YAML file containing information about the chart.</li><li>charts: A directory containing any charts upon which this chart depends on.</li><li>templates: this is where Helm finds the YAML definitions for your Services, Deployments, and other Kubernetes objects. You can add or replace the generated YAML files for your own.</li><li>templates/NOTES.txt: This is a templated, plaintext file that gets printed out after the chart is successfully deployed. This is a useful place to briefly describe the next steps for using the chart.</li><li>templates/_helpers.tpl: That file is the default location for template partials. Files whose name begins with an underscore are assumed to <em>not</em> have a manifest inside. These files are not rendered to Kubernetes object definitions but are available everywhere within other chart templates for use.</li><li>templates/tests: tests that validate that your chart works as expected when it is installed</li><li>values.yaml: The default configuration values for this chart</li></ul><h4>Customize the templates</h4><p>The values.yaml is loaded automatically by default when deploying the chart. Here we set the image tag to 1.21.5:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed663df750219a0e3d5a01102ed666e/href">https://medium.com/media/7ed663df750219a0e3d5a01102ed666e/href</a></iframe><blockquote>You can specify a specific values.yaml file to customize the deployment for environment-specific settings</blockquote><h3>Install The Helm Chart</h3><p>Good advice before deploying a Helm chart is to run the linter if you made an update:</p><pre>$ helm lint nginx<br>==&gt; Linting nginx<br>[INFO] Chart.yaml: icon is recommended<br><br>1 chart(s) linted, 0 chart(s) failed</pre><p>Run Helm to install the chart in dry-run and debug mode to ensure all is OK:</p><pre>$ helm install --debug --dry-run nginx nginx</pre><blockquote>Using helm linter and dry-run install with debug mode will save you precious time in your development.</blockquote><p>To install the chart, remove the --dry-run flag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22b8107180bafe269083b7004d781b4f/href">https://medium.com/media/22b8107180bafe269083b7004d781b4f/href</a></iframe><p>You can see the templated content of the NOTES.txt explaining how to connect to the application.</p><p>Now, you can retrieve the release in the Helm workspace:</p><pre>$ helm list               <br>NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION<br>nginx   default         1               2022-03-14 12:01:46.926038 +0100 CET    deployed        nginx-0.1.0     1.0.0</pre><h3>Upgrade the Helm Release</h3><p>Imagine you want to upgrade the container image to 1.21.6 for testing purposes.</p><p>Instead of creating a new values.yaml, we&#39;ll change the setting from the command line.</p><pre>$ helm upgrade nginx nginx --set image.tag=1.21.6<br>Release &quot;nginx&quot; has been upgraded. Happy Helming!<br>NAME: nginx<br>LAST DEPLOYED: Mon Mar 14 12:04:40 2022<br>NAMESPACE: default<br>STATUS: deployed<br>REVISION: 2<br>NOTES:<br>1. Get the application URL by running these commands:<br>  export POD_NAME=$(kubectl get pods --namespace default -l &quot;app.kubernetes.io/name=nginx,app.kubernetes.io/instance=nginx&quot; -o jsonpath=&quot;{.items[0].metadata.name}&quot;)<br>  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath=&quot;{.spec.containers[0].ports[0].containerPort}&quot;)<br>  echo &quot;Visit <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080</a> to use your application&quot;<br>  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT</pre><p>The pod is using the new container image as well:</p><pre>$ kubectl get pod -l app.kubernetes.io/name=nginx -o jsonpath=&#39;{.items[0].spec.containers[0].image}&#39;<br>nginx:1.21.6</pre><p>The upgrade is visible in the chart history:</p><pre>$ helm history nginx                                                   <br>REVISION        UPDATED                         STATUS          CHART           APP VERSION     DESCRIPTION     <br>1               Mon Mar 14 12:07:33 2022        superseded      nginx-0.1.0     1.0.0           Install complete<br>2               Mon Mar 14 12:08:25 2022        deployed        nginx-0.1.0     1.0.0           Upgrade complete</pre><p>Change is inspectable with helm diff:</p><pre>$ helm diff revision nginx 1 2<br>default, nginx, Deployment (apps) has changed:<br>  # Source: nginx/templates/deployment.yaml<br>  apiVersion: apps/v1<br>  kind: Deployment<br>  metadata:<br>    name: nginx<br>    labels:<br>      helm.sh/chart: nginx-0.1.0<br>      app.kubernetes.io/name: nginx<br>      app.kubernetes.io/instance: nginx<br>      app.kubernetes.io/version: &quot;1.0.0&quot;<br>      app.kubernetes.io/managed-by: Helm<br>  spec:<br>    replicas: 1<br>    selector:<br>      matchLabels:<br>        app.kubernetes.io/name: nginx<br>        app.kubernetes.io/instance: nginx<br>    template:<br>      metadata:<br>        labels:<br>          app.kubernetes.io/name: nginx<br>          app.kubernetes.io/instance: nginx<br>      spec:<br>        serviceAccountName: nginx<br>        securityContext:<br>          {}<br>        containers:<br>          - name: nginx<br>            securityContext:<br>              {}<br>-           image: &quot;nginx:1.21.5&quot;<br>+           image: &quot;nginx:1.21.6&quot;<br>            imagePullPolicy: IfNotPresent<br>            ports:<br>              - name: http<br>                containerPort: 80<br>                protocol: TCP<br>            livenessProbe:<br>              httpGet:<br>                path: /<br>                port: http<br>            readinessProbe:<br>              httpGet:<br>                path: /<br>                port: http<br>            resources:<br>              {}</pre><h3>Rollback the Helm Release</h3><p>The upgrade was not conclusive and you want to go back. As Helm keeps all the changes, rollback is very straightforward:</p><pre>$ helm rollback nginx 1<br>Rollback was a success! Happy Helming!</pre><p>The pod is now back to 1.21.5 container image:</p><pre>$ kubectl get pod -l app.kubernetes.io/name=nginx -o jsonpath=&#39;{.items[0].spec.containers[0].image}&#39;<br>nginx:1.21.5</pre><h3>Uninstall the Helm Chart</h3><p>Uninstalling a Helm chart is as trivial as the installation:</p><pre>$ helm uninstall nginx</pre><h3>Reuse Existing Helm Charts</h3><p>A lot of famous projects provide Helm chart to make the integration more user friendly. They provide the charts through a repository. You have just to add it on your side:</p><pre>$ helm repo add bitnami <a href="https://charts.bitnami.com/bitnami">https://charts.bitnami.com/bitnami</a></pre><p>Once added, update your local cache to synchronize info with remote repositories:</p><pre>$ helm repo update</pre><p>You can now install the chart on your Kubernetes cluster:</p><pre>$ helm install nginx bitnami/nginx</pre><p>Charts are deployed with default values. You can inspire and specify a custom values.yaml to match your needs!</p><pre>$ helm install my-release bitnami/nginx -f values.yaml</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24fecce1b0eb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/what-is-helm-a-quickstart-tutorial-for-kubernetes-beginners-24fecce1b0eb">What Is Helm? A Quickstart Tutorial for Kubernetes Beginners</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 Essential Terraform Tools To Use Everyday]]></title>
            <link>https://medium.com/better-programming/5-essential-terraform-tools-to-use-everyday-e910a96e70d9?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e910a96e70d9</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Wed, 16 Feb 2022 16:57:28 GMT</pubDate>
            <atom:updated>2026-06-14T06:21:01.265Z</atom:updated>
            <content:encoded><![CDATA[<h4>Gain efficiency with Terraform and leverage your code</h4><figure><img alt="The feature image of the post" src="https://cdn-images-1.medium.com/max/1024/1*v42v-9babQQ4OJftLGmbzA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@christopher__burns?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Christopher Burns</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://www.terraform.io/">Terraform</a> is now an essential infrastructure-as-code (IaC) tool to have in your toolbox as a DevOps practitioner. Such as many open-source projects, the community creates and shares many tools to make it more powerful.</p><p>Sometimes there are too many tools and plugins around a project and you may feel lost. That’s why here, I’m going to present you 5 Terraform tools I use on a daily basis.</p><p>They allow you to gain velocity and have even more fun working with Terraform:</p><ul><li>Switching between different Terraform versions in your projects.</li><li>Detect errors and improve your code.</li><li>Generate automatically your code documentation.</li><li>Detecting potential security issues in your code.</li><li>Estimating cloud cost of your changes.</li></ul><h3>1. TFSwitch</h3><p>Terraform is a tool that evolves quite fast and changes versions. You need to download and install the correct version from the <a href="https://www.terraform.io/downloads">HashiCorp website</a>. For each different version, you need the associated binary. When you have several Terraform projects with different versions it can quickly become very heavy to manage.</p><p><a href="https://tfswitch.warrensbox.com/">TFSwitch</a> is a command-line tool and solves this problem. It allows you to select the version of Terraform you want to work with and install it automatically.</p><h4>Installation on Mac</h4><pre>$ brew install warrensbox/tap/tfswitch</pre><h4>Installation on Linux</h4><pre>$ curl -L <a href="https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh">https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh</a> | bash</pre><h4>Quickstart</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*-PfBLFbSeoln3FPQ.gif" /><figcaption>“Select the Terraform version to use with dropdown menu” from <a href="https://tfswitch.warrensbox.com/">https://tfswitch.warrensbox.com/</a></figcaption></figure><p><strong>[1]</strong>: Type tfswitch and press “Enter” on your terminal</p><p><strong>[2]:</strong> Select the version of Terraform you want to use by using the up and down arrow.</p><p><strong>[3]:</strong> Hit enter to select the Terraform version.</p><p>You can also supply the Terraform version on the command line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*vR5Rz9P__1ou7hly.gif" /><figcaption>“Supply the Terraform on command-line” from <a href="https://tfswitch.warrensbox.com/">https://tfswitch.warrensbox.com/</a></figcaption></figure><p><a href="https://tfswitch.warrensbox.com/">TFSwitch</a></p><h3>2. TFLint</h3><p>Syntax errors are sometimes not easy to understand when running your code. Linters provide crucial information to speed up debugging and save time in your development. You can also integrate them into your CI/CD pipeline to implement continuous improvement.</p><p>Terraform is based on the HCL (HashiCorp Language), a language describing the state of your infrastructure. <a href="https://github.com/terraform-linters/tflint">TFLint</a> is a pluggable linter supporting Terraform and its language. In addition to linting, it is also a framework with expandable features through plugins.</p><p>Here is what using TFLint can do for your IaC project:</p><ul><li>Find possible errors for major cloud providers: AWS/Azure/GCP.</li><li>Warn about depreciated syntax and unused declarations.</li><li>Enforce best practices and naming conventions.</li></ul><p><a href="https://github.com/terraform-linters/tflint">GitHub - terraform-linters/tflint: A Pluggable Terraform Linter</a></p><h4>Installation on Mac</h4><pre>$ brew install tflint</pre><h4>Installation on Linux</h4><pre>$ curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash</pre><h4>Installation on Windows</h4><pre>$ choco install tflint</pre><h4>Quickstart</h4><p>TFLint inspects files under the current directory by default. You can change the behavior with the following arguments:</p><pre>$ tflint --help<br>Usage:<br>  tflint [OPTIONS] [FILE or DIR...]</pre><pre>Application Options:<br>  -v, --version                                           Print TFLint version<br>      --init                                              Install plugins<br>      --langserver                                        Start language server<br>  -f, --format=[default|json|checkstyle|junit|compact]    Output format<br>                                                          (default: default)<br>  -c, --config=FILE                                       Config file name<br>                                                          (default: .tflint.hcl)<br>      --ignore-module=SOURCE                              Ignore module sources<br>      --enable-rule=RULE_NAME                             Enable rules from the<br>                                                          command line<br>      --disable-rule=RULE_NAME                            Disable rules from<br>                                                          the command line<br>      --only=RULE_NAME                                    Enable only this<br>                                                          rule, disabling all<br>                                                          other defaults. Can<br>                                                          be specified multiple<br>                                                          times<br>      --enable-plugin=PLUGIN_NAME                         Enable plugins from<br>                                                          the command line<br>      --var-file=FILE                                     Terraform variable<br>                                                          file name<br>      --var=&#39;foo=bar&#39;                                     Set a Terraform<br>                                                          variable<br>      --module                                            Inspect modules<br>      --force                                             Return zero exit<br>                                                          status even if issues<br>                                                          found<br>      --no-color                                          Disable colorized<br>                                                          output<br>      --loglevel=[trace|debug|info|warn|error]            Change the loglevel</pre><pre>Help Options:<br>  -h, --help                                              Show this help message</pre><p>You can also pass a configuration file to TFLint according to this priority:</p><ul><li>Current directory (./.tflint.hcl)</li><li>Home directory (~/.tflint.hcl)</li></ul><p>In the .tflint.hcl , you can make the following actions:</p><ul><li>Define the TFLint configuration</li><li>Declare plugins to use</li><li>Enable/Disable specific TFLint rules</li></ul><p>Here is an example of a TFLint configuration file:</p><pre>config {<br>  plugin_dir = &quot;~/.tflint.d/plugins&quot;<br><br>  module = true<br>  force = false<br>  disabled_by_default = false<br><br>  ignore_module = {<br>    &quot;terraform-aws-modules/vpc/aws&quot;            = true<br>    &quot;terraform-aws-modules/security-group/aws&quot; = true<br>  }<br><br>  varfile = [&quot;example1.tfvars&quot;, &quot;example2.tfvars&quot;]<br>  variables = [&quot;foo=bar&quot;, &quot;bar=[\&quot;baz\&quot;]&quot;]<br>}</pre><pre>plugin &quot;aws&quot; {<br>  enabled = true<br>  version = &quot;0.12.0&quot;<br>  source  = &quot;github.com/terraform-linters/tflint-ruleset-aws&quot;<br>}</pre><pre>rule &quot;aws_instance_invalid_type&quot; {<br>  enabled = false<br>}</pre><p>Run TFLint is simple:</p><pre>$ tflint</pre><p>If you do not get the expected behavior, you can see the detailed logs with debug mode:</p><pre>$ TFLINT_LOG=debug tflint</pre><h4><strong>Use plugins for cloud providers</strong></h4><p>When you are using AWS, Azure, or GCP providers, you can use the corresponding plugin:</p><ul><li><a href="https://github.com/terraform-linters/tflint-ruleset-aws">Amazon Web Services</a></li><li><a href="https://github.com/terraform-linters/tflint-ruleset-azurerm">Microsoft Azure</a></li><li><a href="https://github.com/terraform-linters/tflint-ruleset-google">Google Cloud Platform</a></li></ul><p>The code blocks below indicate to use the AWS plugin:</p><pre>plugin &quot;aws&quot; {<br>  enabled = true<br>  version = &quot;0.12.0&quot;<br>  source  = &quot;github.com/terraform-linters/tflint-ruleset-aws&quot;<br>}</pre><p>The following command installs the plugins:</p><pre>$ tflint --init</pre><p>Terraform does not validate the provider-specific issues. Here is an example with the following configuration file:</p><pre>resource &quot;aws_instance&quot; &quot;foo&quot; {<br>  ami           = &quot;ami-0ff8a91507f77f867&quot;<br>  instance_type = &quot;t1.2xlarge&quot; # invalid type!<br>}</pre><p>The t1.2xlarge is an invalid instance type. An error will occur when you run terraform apply. But the terraform validate and terraform plan commands do not show any error. It is an issue-specific from the AWS provider not from Terraform syntax. TFLint plugin helps to detect this error before the application of the code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6YjdpiK15k_-4iGW.gif" /></figure><h3>3. Terraform-docs</h3><p>Documenting your code is an important point for teamwork and reusability. <a href="https://github.com/terraform-docs/terraform-docs">Terraform-docs</a> is a quick utility to generate docs from Terraform modules in various output formats.</p><p><a href="https://github.com/terraform-docs/terraform-docs">GitHub - terraform-docs/terraform-docs: Generate documentation from Terraform modules in various output formats</a></p><h4>Installation on Mac</h4><pre>$ brew install terraform-docs </pre><h4>Installation on Windows</h4><pre>$ choco install terraform-docs</pre><h4>Installation on Linux</h4><pre>$ curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-$(uname)-amd64.tar.gz<br>$ tar -xzf terraform-docs.tar.gz<br>$ chmod +x terraform-docs<br>$ mv terraform-docs /usr/local/terraform-docs</pre><h4>Quickstart</h4><p>Creating Terraform modules implies using inputs, outputs, resources, and eventually data sources. The following Terraform snippet creates an AWS EC2 instance from an AMI (Amazon Machine Image). Although everything is in one file, it can be considered as a module:</p><pre>terraform {<br>  required_version = &quot;&gt;= 1.0.0&quot;<br>  required_providers {<br>    aws = {<br>      source = &quot;hashicorp/aws&quot;<br>    }<br>  }<br>}</pre><pre>variable &quot;aws_region&quot; {<br>  description = &quot;The AWS region where to deploy the EC2 instance.&quot;<br>  type        = string<br>  default     = &quot;us-east-1&quot;<br>}</pre><pre>variable &quot;tags&quot; {<br>  description = &quot;The tags to associate to the EC2 instance.&quot;<br>  type        = map(string)<br>  default     = {}<br>}</pre><pre>provider &quot;aws&quot; {<br>  region = var.aws_region<br>}</pre><pre>data &quot;aws_ami&quot; &quot;ubuntu&quot; {<br>  most_recent = true</pre><pre>filter {<br>    name   = &quot;name&quot;<br>    values = [&quot;ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*&quot;]<br>  }</pre><pre>filter {<br>    name   = &quot;virtualization-type&quot;<br>    values = [&quot;hvm&quot;]<br>  }</pre><pre>owners = [&quot;099720109477&quot;]<br>}</pre><pre>resource &quot;aws_instance&quot; &quot;default&quot; {<br>  ami           = data.aws_ami.ubuntu.id<br>  instance_type = &quot;t2.micro&quot;</pre><pre>tags   = merge({<br>    Name = &quot;my-ec2-instance&quot;<br>  }, var.tags)<br>}</pre><pre>output &quot;arn&quot; {<br>  description = &quot;EC2 instance ARN.&quot;<br>  value       = aws_instance.default.arn<br>}</pre><p>The command line below will generate the Terraform documentation in markdown and store it in a README file:</p><pre>$ terraform-docs markdown table --output-file README.md .</pre><p>In the README file, we find the different elements used by the module and link to the official documentation for each one:</p><pre>&lt;!-- BEGIN_TF_DOCS --&gt;<br>## Requirements</pre><pre>| Name | Version |<br>|------|---------|<br>| &lt;a name=&quot;requirement_terraform&quot;&gt;&lt;/a&gt; [terraform](#requirement\_terraform) | &gt;= 1.0.0 |</pre><pre>## Providers</pre><pre>| Name | Version |<br>|------|---------|<br>| &lt;a name=&quot;provider_aws&quot;&gt;&lt;/a&gt; [aws](#provider\_aws) | 3.74.0 |</pre><pre>## Modules</pre><pre>No modules.</pre><pre>## Resources</pre><pre>| Name | Type |<br>|------|------|<br>| [aws_instance.default](<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance">https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance</a>) | resource |<br>| [aws_ami.ubuntu](<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami">https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami</a>) | data source |</pre><pre>## Inputs</pre><pre>| Name | Description | Type | Default | Required |<br>|------|-------------|------|---------|:--------:|<br>| &lt;a name=&quot;input_aws_region&quot;&gt;&lt;/a&gt; [aws\_region](#input\_aws\_region) | The AWS region where to deploy the EC2 instance. | `string` | `&quot;us-east-1&quot;` | no |<br>| &lt;a name=&quot;input_tags&quot;&gt;&lt;/a&gt; [tags](#input\_tags) | The tags to associate to the EC2 instance. | `map(string)` | `{}` | no |</pre><pre>## Outputs</pre><pre>| Name | Description |<br>|------|-------------|<br>| &lt;a name=&quot;output_arn&quot;&gt;&lt;/a&gt; [arn](#output\_arn) | EC2 instance ARN. |<br>&lt;!-- END_TF_DOCS --&gt;</pre><h3>4. Checkov</h3><p><a href="https://www.checkov.io/">Checkov</a> is a static code analysis tool for scanning infrastructure as code files including Terraform. It looks for misconfiguration that may lead to security or compliance problems. It has 750 predefined policies to check for common misconfiguration issues.</p><p><a href="https://www.checkov.io/">checkov</a></p><h4>Installation</h4><pre>$ pip install checkov</pre><h4>Quickstart</h4><p>We will take the code of the part presented on the part on terraform-docs. We will generate the Terraform plan and analyze it with Checkov :</p><pre>$ terraform init <br>$ terraform plan -out tf.plan <br>$ terraform show -json tf.plan  &gt; tf.json  <br>$ checkov -f tf.json<br><br>       _               _<br>   ___| |__   ___  ___| | _______   __<br>  / __| &#39;_ \ / _ \/ __| |/ / _ \ \ / /<br> | (__| | | |  __/ (__|   &lt; (_) \ V /<br>  \___|_| |_|\___|\___|_|\_\___/ \_/</pre><pre>By bridgecrew.io | version: 2.0.528<br>Update available 2.0.528 -&gt; 2.0.829<br>Run pip3 install -U checkov to update</pre><pre>cloudformation scan results:</pre><pre>Passed checks: 0, Failed checks: 0, Skipped checks: 0, Parsing errors: 1</pre><pre>Error parsing file tf.json<br>terraform_plan scan results:</pre><pre>Passed checks: 2, Failed checks: 4, Skipped checks: 0</pre><pre>Check: CKV_AWS_88: &quot;EC2 instance should not have public IP.&quot;<br> PASSED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/public_12">https://docs.bridgecrew.io/docs/public_12</a></pre><pre>Check: CKV_AWS_46: &quot;Ensure no hard-coded secrets exist in EC2 user data&quot;<br> PASSED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/bc_aws_secrets_1">https://docs.bridgecrew.io/docs/bc_aws_secrets_1</a></pre><pre>Check: CKV_AWS_79: &quot;Ensure Instance Metadata Service Version 1 is not enabled&quot;<br> FAILED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/bc_aws_general_31">https://docs.bridgecrew.io/docs/bc_aws_general_31</a></pre><pre>Check: CKV_AWS_126: &quot;Ensure that detailed monitoring is enabled for EC2 instances&quot;<br> FAILED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/ensure-that-detailed-monitoring-is-enabled-for-ec2-instances">https://docs.bridgecrew.io/docs/ensure-that-detailed-monitoring-is-enabled-for-ec2-instances</a></pre><pre>Check: CKV_AWS_135: &quot;Ensure that EC2 is EBS optimized&quot;<br> FAILED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/ensure-that-ec2-is-ebs-optimized">https://docs.bridgecrew.io/docs/ensure-that-ec2-is-ebs-optimized</a></pre><pre>Check: CKV_AWS_8: &quot;Ensure all data stored in the Launch configuration EBS is securely encrypted&quot;<br> FAILED for resource: aws_instance.default<br> File: /tf.json:0-0<br> Guide: <a href="https://docs.bridgecrew.io/docs/general_13">https://docs.bridgecrew.io/docs/general_13</a></pre><h3>5. Infracost: Estimate Cloud Cost From Your Code</h3><p>Changes made by Terraform may alter the status of resources hosted by a cloud provider. Depending on these, costs may vary. It is important to keep this dimension in mind when writing IaC code.</p><p>Infracost shows cloud cost estimates for infrastructure-as-code projects such as Terraform. It helps to quickly see a cost breakdown and compare different options up front.</p><p><a href="https://github.com/infracost/infracost">GitHub - infracost/infracost: Cloud cost estimates for Terraform in pull requests💰📉 Love your cloud bill!</a></p><h4>Installation on Mac</h4><pre>$ brew install infracost</pre><h4>Installation on Linux</h4><pre>$ /usr/local/bin<br>curl -fsSL <a href="https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh">https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh</a> | sh</pre><h4>Quickstart</h4><p>Register infracost with your email address to receive an API key:</p><pre>$ infracost register</pre><p>Now your API key is in $HOME/.config/infracost/credentials.yml</p><p>You can use infracost to have the full monthly breakdown of costs:</p><pre>$ infracost breakdown --path .                                                                                                <br>Detected Terraform directory at .<br>  ✔ Checking for cached plan... not found<br>  ✔ Running terraform plan<br>  ✔ Running terraform show<br>  ✔ Extracting only cost-related params from terraform<br>  ✔ Retrieving cloud prices to calculate costs</pre><pre>Project: .</pre><pre>Name                                                 Monthly Qty  Unit   Monthly Cost</pre><pre>aws_instance.default<br> ├─ Instance usage (Linux/UNIX, on-demand, t2.micro)          730  hours         $8.47<br> └─ root_block_device<br>    └─ Storage (general purpose SSD, gp2)                       8  GB            $0.80</pre><pre>OVERALL TOTAL                                                                   $9.27<br>──────────────────────────────────<br>1 cloud resource was detected, rerun with --show-skipped to see details:<br>∙ 1 was estimated, 1 includes usage-based costs, see <a href="https://infracost.io/usage-file">https://infracost.io/usage-file</a></pre><p>You can also detect the change in monthly cost due to a code change:</p><pre>$ infracost diff --path .<br>Detected Terraform directory at .<br>  ✔ Checking for cached plan... change detected<br>  ✔ Running terraform plan<br>  ✔ Running terraform show<br>  ✔ Extracting only cost-related params from terraform<br>  ✔ Retrieving cloud prices to calculate costs</pre><pre>Project: .</pre><pre>~ aws_instance.default<br>  +$21.90 ($9.27 → $31.17)</pre><pre>~ Instance usage (Linux/UNIX, on-demand, t2.micro → t3.medium)<br>      +$21.90 ($8.47 → $30.37)</pre><pre>Monthly cost change for .<br>Amount:  +$21.90 ($9.27 → $31.17)<br>Percent: +236%</pre><pre>──────────────────────────────────<br>Key: ~ changed, + added, - removed</pre><pre>1 cloud resource was detected, rerun with --show-skipped to see details:<br>∙ 1 was estimated, 1 includes usage-based costs, see <a href="https://infracost.io/usage-file">https://infracost.io/usage-file</a></pre><pre>Add cost estimates to your pull requests: <a href="https://infracost.io/cicd">https://infracost.io/cicd</a></pre><p>You can also generate an HTML report:</p><pre>$ infracost breakdown --path . --format html &gt; report.html                                                                  <br>Detected Terraform directory at .<br>  ✔ Checking for cached plan... found<br>  ✔ Extracting only cost-related params from terraform<br>  ✔ Retrieving cloud prices to calculate costs</pre><figure><img alt="Screenshot showing an HTML report generated by Infracost for Terraform" src="https://cdn-images-1.medium.com/max/969/1*XS05TyUHULfCvlPkmssxrw.png" /><figcaption>Infracost HTML report</figcaption></figure><h3>Conclusion</h3><p>The 5 Terraform tools we have seen together in this article will bring you benefits in several aspects:</p><ul><li>Debugging your code faster and anticipate errors with TFLint</li><li>Limiting recurring tasks: version management with TFSwitch and automation of documentation with Terraform-Docs.</li><li>Identify security issues during development with Checkov</li><li>Get visibility on the cost impact of your infrastructure changes with Infracost</li></ul><p>You can also integrate all these tools into your CI/CD pipelines!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e910a96e70d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/5-essential-terraform-tools-to-use-everyday-e910a96e70d9">5 Essential Terraform Tools To Use Everyday</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Secure K8S Nginx Ingress With “Let’s Encrypt” and “Cert Manager”]]></title>
            <link>https://medium.com/better-programming/secure-k8s-nginx-ingress-with-lets-encrypt-46e46abe822b?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/46e46abe822b</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Fri, 12 Nov 2021 17:10:08 GMT</pubDate>
            <atom:updated>2021-11-15T13:31:06.240Z</atom:updated>
            <content:encoded><![CDATA[<h4>Automate the provisioning of Let’s Encrypt certificates and secure your application</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZCKCuHiMIR2Ek60G" /><figcaption>Photo by <a href="https://unsplash.com/@nhippert?utm_source=medium&amp;utm_medium=referral">Nicolas HIPPERT</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Kubernetes ingress exposes a web application or a REST API within a cluster to the outside. However, the application is reachable over HTTP by default which is not secure. The traffic between the application and clients is not encrypted.</p><p>The ingress can be configured to serve the application in HTTPS. It works like a reverse proxy by offloading this functionality from the application. To do this, you need to configure a certificate in the ingress specifying it in the tls field:</p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: wordpress<br>  annotations:<br>    kubernetes.io/ingress.class: nginx<br>spec:<br>  rules:<br>    - http:<br>        paths:<br>          - path: /<br>            pathType: Prefix<br>            backend:<br>              service:<br>                name: wordpress<br>                port:<br>                  number: 80<br>  tls:<br>    - hosts:<br>      - example.com</pre><p>By doing this, the certificate will not be trusted by browsers and visitors will get a warning. This may reduce the user experience and the traffic of an application. To avoid that it will be necessary to generate valid certificates and especially from a certification authority.</p><figure><img alt="Untrusted HTTPS certificate warning in the browser" src="https://cdn-images-1.medium.com/max/642/1*8jQ9Hzm1xyDxcBygIG1SEw.png" /><figcaption>Untrusted HTTPS certificate warning in the browser</figcaption></figure><p>The purpose here is to detail to you how to automate the lifecycle of certificates with <a href="https://cert-manager.io/docs/">Cert-manager</a> and <a href="https://letsencrypt.org/">Let&#39;s Encrypt</a> as a certification authority. Cert-manager automates the provisioning of HTTPS certificates within your Kubernetes cluster. It provides custom resources to simplify the obtain, renewal, and use of those certificates.</p><p>In order we will see how to :</p><ul><li>install cert-manager in your k8s cluster</li><li>create certificate issuers for Let&#39;s Encrypt</li><li>expose a WordPress blog over HTTPS using the Nginx ingress controller</li></ul><h3>Installing Cert-Manager</h3><p>Cert-manager is easy to install using Helm. Helm is a Kubernetes package manager that allows you to add applications to your cluster using repositories with pre-built charts.</p><p>The first step is to add the Jetstack repository:</p><pre>$ helm repo add jetstack https://charts.jetstack.io $ helm repo update</pre><p>Install Cert-Manager with CRDs into your cluster:</p><pre>$ helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true</pre><p>Jetstack also offers a kubectl plugin to easily manage cert-manager resources in your cluster:</p><pre>$ curl -L -o kubectl-cert-manager.tar.gz https://github.com/jetstack/cert-manager/releases/download/v1.6.1/kubectl-cert_manager-darwin-amd64.tar.gz<br>$ tar xzf kubectl-cert-manager.tar.gz<br>$ sudo mv kubectl-cert_manager /usr/local/bin</pre><p>I recommend you install cmctl for a better experience via tab auto-completion:</p><pre>$ curl -L -o cmctl-darwin-amd64.tar.gz https://github.com/jetstack/cert-manager/releases/download/v1.6.1/cmctl-darwin-amd64.tar.gz<br>$ tar xzf cmctl-darwin-amd64.tar.gz<br>$ sudo mv cmctl /usr/local/bin</pre><pre>$ kubectl cert-manager help                                               <br><br>kubectl cert-manager is a CLI tool manage and configure cert-manager resources for Kubernetes<br><br>Usage: kubectl cert-manager [command]<br><br>Available Commands:<br>  approve      Approve a CertificateRequest<br>  check        Check cert-manager components<br>  convert      Convert cert-manager config files between different API versions<br>  create       Create cert-manager resources<br>  deny         Deny a CertificateRequest<br>  experimental Interact with experimental features<br>  help         Help about any command<br>  inspect      Get details on certificate related resources<br>  renew        Mark a Certificate for manual renewal<br>  status       Get details on current status of cert-manager resources<br>  version      Print the cert-manager CLI version and the deployed cert-manager version<br><br>Flags:<br>  -h, --help                           help for kubectl<br>      --log-flush-frequency duration   Maximum number of seconds between log flushes (default 5s)<br><br>Use &quot;kubectl cert-manager [command] --help&quot; for more information about a command.</pre><p>For the rest of this tutorial, you will also need to install the Nginx ingress controller:</p><pre>$ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx<br>$ helm update<br>$ helm install ingress-controller ingress-nginx/ingress-nginx</pre><h3>Configure The Let’s Encrypt Certificate Issuer</h3><p>Issuers and cluster issuers are resources supplying certificates to the cluster. The Cert-Manager installation is unable to issue certificates. You need to configure an issuer for Let’s Encrypt to dynamically acquires new certificates for your services:</p><figure><img alt="cert-manager presentation diagram" src="https://cdn-images-1.medium.com/max/500/1*gE0KWUQyQol2TT7rjR_XjA.jpeg" /><figcaption>cert-manager presentation diagram from <a href="https://getbetterdevops.io/secure-k8s-nginx-ingress-with-lets-encrypt/cert-manager%20presentation%20diagram%20from%20https://cert-manager.io/docs/">https://cert-manager.io/docs/</a></figcaption></figure><blockquote>ClusterIssuer resources are cluster-based while Issuer are namespace-based</blockquote><p>We are going to set up a cluster issuer for staging and production usage. It is good to prefer staging while you configure your integration. It avoids hitting the Let’s Encrypt production rate limit.</p><blockquote>In the following cluster issuer manifests you need to replace the email address with yours</blockquote><h4>Staging</h4><p>Create a YAML file named letsencrypt-staging.yaml:</p><pre>apiVersion: cert-manager.io/v1<br>kind: ClusterIssuer<br>metadata:<br>  name: letsencrypt-staging<br>spec:<br>  acme:<br>    server: https://acme-staging-v02.api.letsencrypt.org/directory<br>    email: example@domain.com<br>    privateKeySecretRef:<br>      name: letsencrypt-staging<br>    solvers:<br>      - http01:<br>          ingress:<br>            class: nginx</pre><p>Apply the manifest:</p><pre>$ kubectl create -f letsencrypt-issuer-staging.yaml</pre><h4>Production</h4><p>Create a YAML file named letsencrypt-production.yaml:</p><pre>apiVersion: cert-manager.io/v1<br>kind: ClusterIssuer<br>metadata:<br>  name: letsencrypt-production<br>spec:<br>  acme:<br>    server: https://acme-v02.api.letsencrypt.org/directory<br>    email: example@domain.com<br>    privateKeySecretRef:<br>      name: letsencrypt-production<br>    solvers:<br>      - http01:<br>          ingress:<br>            class: nginx</pre><p>Apply the manifest:</p><pre>$ kubectl create -f letsencrypt-issuer-staging.yaml</pre><h3>Obtain an HTTPS Certificate</h3><p>The issuer is now in place and ready to retrieve a certificate for the services exposed by an ingress resource. Cert-manager monitors ingress resources and creates certificates based on the tls field.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: wordpress<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: wordpress<br>  template:<br>    metadata:<br>      labels:<br>        app: wordpress<br>    spec:<br>      containers:<br>        - name: wordpress<br>          image: wordpress:latest<br>          ports:<br>            - containerPort: 80<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: wordpress<br>spec:<br>  selector:<br>    app: wordpress<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>---<br>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: wordpress<br>  annotations:<br>    kubernetes.io/ingress.class: nginx<br>    cert-manager.io/cluster-issuer: letsencrypt-staging<br>spec:<br>  rules:<br>    - http:<br>        paths:<br>          - path: /<br>            pathType: Prefix<br>            backend:<br>              service:<br>                name: wordpress<br>                port:<br>                  number: 80<br>  tls:<br>    - hosts:<br>      - example.com</pre><p>The YAML file above defined a deployment creating one pod, a service, and an ingress exposing the service. The ingress uses the Nginx controller deployed previously. The pod runs a WordPress container that will be reachable over HTTPS.</p><p>Cert-manager detects the annotation cert-manager.io/cluster-issuer in the ingress resource. It will use letsencrypt-staging cluster issuer to acquire a certificate for the hostname defined in the tls hosts field.</p><p>Once the acquisition of a certificate is validated with the staging issuer you can proceed to the production issuer. Staging certificates are valid but are not trusted by browsers. You have to replace the value of the ingress resource field cert-manager.io/cluster-issuer with letsencrypt-production.</p><h3>Conclusion</h3><p>We have seen how to expose a web application in a secure with HTTPS and an ingress. It is possible now to set up trusted and valid certificates for the cluster endpoints on the fly. There is just needed to add a simple annotation in the ingress resources.</p><p>Cert-Manager monitors the ingress resources with this annotation and automates the provisioning of a valid Let’s Encrypt certificate. It also deals with certificate renewal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46e46abe822b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/secure-k8s-nginx-ingress-with-lets-encrypt-46e46abe822b">How to Secure K8S Nginx Ingress With “Let’s Encrypt” and “Cert Manager”</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Just 7 Remarkable K8S Tools Boosting Up Your Effectiveness]]></title>
            <link>https://levelup.gitconnected.com/7-essential-tools-to-be-more-efficient-on-kubernetes-874b1b75d878?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/874b1b75d878</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Mon, 01 Nov 2021 15:02:42 GMT</pubDate>
            <atom:updated>2021-11-22T19:58:14.173Z</atom:updated>
            <content:encoded><![CDATA[<h4>Tired of repeating kubectl command lines? Simplify your life and increase your efficiency on Kubernetes</h4><figure><img alt="Photo by Mediamodifier / Unsplash" src="https://cdn-images-1.medium.com/max/1024/0*i0cMfalA9lnhruG-" /><figcaption>Photo by <a href="https://unsplash.com/@mediamodifier?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Mediamodifier</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></figcaption></figure><p>Kubernetes is a great technology to orchestrate containers and microservices. It offers a rich ecosystem with plenty of different objects and concepts to have up and running applications. kubectl is the number 1 you learn to interact with a cluster through the command line.</p><p>Although knowing kubectl is important you might find yourself repeating the same actions and losing time. Here, we will review 7 tools that are tenfold your efficiency when working with Kubernetes. The goal is to make you discover them and I hope after that you will adopt them. In each tool section, you will find references to the official resources to go further.</p><h3>1. Kube-shell</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qYhRgqzUKfHm5F8K.gif" /><figcaption>kubectl auto-completion with kube-shell</figcaption></figure><p>A little help would be nice when typing kubectl commands to reduce errors and boost up your speed. kube-shell is an integrated shell for Kubernetes CLI. It provides easy-of-user kubectl to be more productive:</p><p>A simple line to install it:</p><pre>$ pip install kube-shell</pre><p><a href="https://github.com/cloudnativelabs/kube-shell">GitHub - cloudnativelabs/kube-shell: Kubernetes shell: An integrated shell for working with the Kubernetes</a></p><h3>2. Kubectx — Kubens</h3><p>Are you managing multiple Kubernetes clusters? kubectx helps you switch between clusters back and forth:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ltvT4lb8MgK5DLm1.gif" /><figcaption>Switch between Kubernetes clusters with kubectx</figcaption></figure><p>At each kubectl command we indicate the namespace to interact with the desired resources. kubens allows avoiding having to specify it each time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pKd2AsXk47avdb-C.gif" /><figcaption>Switch between namespaces with kubens</figcaption></figure><p><a href="https://github.com/ahmetb/kubectx">GitHub - ahmetb/kubectx: Faster way to switch between clusters and namespaces in kubectl</a></p><h3>3. Kubetail</h3><p>kubetail aggregates logs from multiple pods into one stream. It is the as running kubectl logs -f but for multiple pods.</p><pre>$ kubectl get pods NAME READY STATUS RESTARTS AGE app1-v1-aba8y 1/1 Running 0 1d app1-v1-gc4st 1/1 Running 0 1d app1-v1-m8acl 1/1 Running 0 6d app1-v1-s20d0 1/1 Running 0 1d app2-v31-9pbpn 1/1 Running 0 1d app2-v31-q74wg 1/1 Running 0 1d my-demo-v5-0fa8o 1/1 Running 0 3h my-demo-v5-yhren 1/1 Running 0 2h $ kubetail app2</pre><p>You can specify the containers in the pod:</p><pre>$ kubetail app2 -c container1 $ kubectail app2 -c container1 -c container2</pre><p><a href="https://github.com/johanhaleby/kubetail">GitHub - johanhaleby/kubetail: Bash script to tail Kubernetes logs from multiple pods at the same time</a></p><h3>4. Kubetree</h3><p>An application can have a lot of different resources and it is always obvious to map them. kubetree is a kubectl plugin to explore ownership relationships between Kubernetes objects:</p><pre>$ kubectl krew install tree</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*obGI4dW_Jcr2S8XF.png" /><figcaption>Exploring NGINX deployment with kubetree</figcaption></figure><p><a href="https://github.com/ahmetb/kubectl-tree">GitHub - ahmetb/kubectl-tree: kubectl plugin to browse Kubernetes object hierarchies as a tree 🎄 (star the repo if you are using)</a></p><h3>5. K9S</h3><p>k9s provides a terminal UI to interact with your Kubernetes cluster. It can be compared to the top command for processes. You can easily navigate, observe and manage your applications with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1-hxRo6J6tlyfP_A.png" /><figcaption>Overview of the pods with k9s</figcaption></figure><p>What’s great is that changes in Kubernetes resources appear live. It’s very useful to debug what’s going on. Here is a video presenting what can be done with k9s :</p><ul><li><a href="https://asciinema.org/a/305944">K9s Tour</a></li><li><a href="https://github.com/derailed/k9s">GitHub - derailed/k9s: 🐶 Kubernetes CLI To Manage Your Clusters In Style!</a></li></ul><h3>6. Kube-Capacity</h3><p>kube-capacity is a simple CLI providing an overview of the resources requests, limits, and usage in a Kubernetes cluster.</p><p>Inspect the cluster nodes:</p><pre>$ kube-capacity</pre><pre>NODE              CPU REQUESTS    CPU LIMITS    MEMORY REQUESTS    MEMORY LIMITS<br>*                 560m (28%)      130m (7%)     572Mi (9%)         770Mi (13%)<br>example-node-1    220m (22%)      10m (1%)      192Mi (6%)         360Mi (12%)<br>example-node-2    340m (34%)      120m (12%)    380Mi (13%)        410Mi (14%)</pre><p>Now includes the pods:</p><pre>$ kube-capacity --pods</pre><pre>NODE              NAMESPACE     POD                   CPU REQUESTS    CPU LIMITS    MEMORY REQUESTS    MEMORY LIMITS<br>*                 *             *                     560m (28%)      780m (38%)    572Mi (9%)         770Mi (13%)</pre><pre>example-node-1    *             *                     220m (22%)      320m (32%)    192Mi (6%)         360Mi (12%)<br>example-node-1    kube-system   metrics-server-lwc6z  100m (10%)      200m (20%)    100Mi (3%)         200Mi (7%)<br>example-node-1    kube-system   coredns-7b5bcb98f8    120m (12%)      120m (12%)    92Mi (3%)          160Mi (5%)</pre><pre>example-node-2    *             *                     340m (34%)      460m (46%)    380Mi (13%)        410Mi (14%)<br>example-node-2    kube-system   kube-proxy-3ki7       200m (20%)      280m (28%)    210Mi (7%)         210Mi (7%)<br>example-node-2    tiller        tiller-deploy         140m (14%)      180m (18%)    170Mi (5%)         200Mi (7%)</pre><h3>7. Lens</h3><p>Lens is the Kubernetes IDE for monitoring and debugging. It is an application that runs on your desktop (Windows, Linux, and Mac). Perfect if you want to develop comfortably on Kubernetes:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FeeDwdVXattc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DeeDwdVXattc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FeeDwdVXattc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/41b20476055e8942a553acfecc0e7a93/href">https://medium.com/media/41b20476055e8942a553acfecc0e7a93/href</a></iframe><p><a href="https://k8slens.dev/">Lens | The Kubernetes IDE</a></p><h3>Conclusion</h3><p>These 7 tools are not exhaustive. I have presented the ones I use the most on a daily basis. I advise you to go to the link below to explore and discover others. <br>You will find your happiness for sure!</p><p><a href="https://github.com/tomhuang12/awesome-k8s-resources">GitHub - tomhuang12/awesome-k8s-resources: A curated list of awesome Kubernetes tools and resources.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=874b1b75d878" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/7-essential-tools-to-be-more-efficient-on-kubernetes-874b1b75d878">Just 7 Remarkable K8S Tools Boosting Up Your Effectiveness</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Create Terraform Multiple Environments]]></title>
            <link>https://guivin.medium.com/how-to-create-terraform-multiple-environments-e5629add231f?source=rss-9c301642b4c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5629add231f</guid>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Guillaume Vincent]]></dc:creator>
            <pubDate>Sun, 03 Oct 2021 17:01:42 GMT</pubDate>
            <atom:updated>2026-06-16T06:43:40.523Z</atom:updated>
            <content:encoded><![CDATA[<h4>Don’t repeat yourself (DRY) with Terraform to create your infrastructure in multiple environments/regions/cloud-providers</h4><figure><img alt="Photo by Gaël Gaborel / Unsplash" src="https://cdn-images-1.medium.com/max/1024/0*OsqgFbtq4AVdzWWQ" /><figcaption>Photo by <a href="https://unsplash.com/@orbisterrae?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Gaël Gaborel</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></figcaption></figure><p>Terraform has simplified the way we provision infrastructure in the cloud and manage it as code. But best practices like separating infrastructure into multiple environments ( <em>staging/QA/production</em>) don’t change. Maybe even depending on your business needs, you need to extend the infrastructure across multiple geographical areas. Maybe you are even thinking about adopting a multi-cloud strategy.</p><p>If you are in this situation, this means that you need to be able to create multiple environments in your code. The challenge here is to factorize the code as much as possible to the DRY (<em>Don’t Repeat Yourself</em>) principle. There are many ways of achieving it using Terraform.</p><p>In this article, we will see two strategies for doing this with Terraform. Each has its strengths and weaknesses, and we’ll compare them at the end. Let’s go!</p><h3>2 Strategies to Create Multiple Environments</h3><p>In both presented strategies, we’re using modules included in projects for convenience. They can be versioned in distinct GIT repositories. Each module is called in a specific layer, and Terraform remote states are stored in a versioned S3 backend. Separated layers improve consistency and make rollback easier.</p><h3>1# Separated Directories</h3><figure><img alt="Multi-environment project hierarchy based on separated directories" src="https://cdn-images-1.medium.com/max/493/0*Mmqjr5SAkaNzuQlN.png" /><figcaption>Multi-environment project hierarchy based on separated directories</figcaption></figure><p>In this project structure, each environment has its own directory that contains layers. You can see below the network configuration of the staging environment :</p><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket = &quot;terraform-remote-states&quot;<br>    # The key definition changes following the environment<br>    key    = &quot;environments/staging/network.tf&quot;<br>    region = &quot;us-east-1&quot;<br>  }<br>}<br><br>module &quot;network&quot; {<br>  source               = &quot;../modules/network&quot;<br>  region               = &quot;us-east-1&quot;<br>  network_cidr         = &quot;10.0.0.0/16&quot;<br>  private_subnet_cidrs = [&quot;10.0.0.0/24&quot;, &quot;10.0.1.0/24&quot;, &quot;10.0.2.0/24&quot;]<br>  public_subnet_cidrs  = [&quot;10.0.3.0/24&quot;, &quot;10.0.4.0/24&quot;, &quot;10.0.5.9/24&quot;]<br>}</pre><p>As mentioned, we use a backend to persist remote states. The key definition, i.e., where the remote state is stored, varies according to the environment and the layer. In the module block, the different values can differ depending on the environment.</p><p>Dependencies between layers can be solved with <a href="https://www.terraform.io/docs/language/data-sources/index.html">data sources</a>. The existing resources in the cloud or outputs from the remote states can be fetched :</p><pre>$ terraform -chdir=&quot;./environments/staging/network&quot; init<br>$ terraform -chdir=&quot;./environments/staging/network&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/database&quot; init<br>$ terraform -chdir=&quot;./environments/staging/database&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/application&quot; init<br>$ terraform -chdir=&quot;./environments/staging/application&quot; apply</pre><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket = &quot;terraform-remote-states&quot;<br>    # The key definition changes following the environment<br>    key    = &quot;environments/staging/network.tf&quot;<br>    region = &quot;us-east-1&quot;<br>  }<br>}<br><br>module &quot;network&quot; {<br>  source               = &quot;../modules/network&quot;<br>  region               = &quot;us-east-1&quot;<br>  network_cidr         = &quot;10.0.0.0/16&quot;<br>  private_subnet_cidrs = [&quot;10.0.0.0/24&quot;, &quot;10.0.1.0/24&quot;, &quot;10.0.2.0/24&quot;]<br>  public_subnet_cidrs  = [&quot;10.0.3.0/24&quot;, &quot;10.0.4.0/24&quot;, &quot;10.0.5.9/24&quot;]<br>}</pre><p>For deployment, you have to perform a terraform init and apply on the layers in order :</p><pre>$ terraform -chdir=&quot;./environments/staging/network&quot; init<br>$ terraform -chdir=&quot;./environments/staging/network&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/database&quot; init<br>$ terraform -chdir=&quot;./environments/staging/database&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/application&quot; init<br>$ terraform -chdir=&quot;./environments/staging/application&quot; apply</pre><h3>2# Workspaces</h3><figure><img alt="Multi-environment project hierarchy based on workspaces" src="https://cdn-images-1.medium.com/max/333/0*ftNeHUF2cd9PyMLt.png" /><figcaption>Multi-environment project hierarchy based on workspaces</figcaption></figure><p>Before we use an S3 backend to store remote states. Initially, there is only one workspace named default and only one associated with the state. Some backends, such as S3, support multiple workspaces. They allow multiple states to be associated with a single Terraform configuration.</p><p>We are going to use this feature to define multiple environments. Each environment will be a workspace. In the backend definition, we add the workspace_key_prefix It is specific to the S3 backend, and it will define the S3 state path as /workspace_key/workspace_key_prefix/workspace_name :</p><pre>$ terraform -chdir=&quot;./environments/staging/network&quot; init<br>$ terraform -chdir=&quot;./environments/staging/network&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/database&quot; init<br>$ terraform -chdir=&quot;./environments/staging/database&quot; apply<br>$ terraform -chdir=&quot;./environments/staging/application&quot; init<br>$ terraform -chdir=&quot;./environments/staging/application&quot; apply</pre><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket               = &quot;terraform-remote-states&quot;<br>    workspace_key_prefix = &quot;environments&quot;<br>    key                  = &quot;network&quot;<br>    region               = &quot;us-east-1&quot;<br>  }<br>}</pre><p>The remote states will look as follow in S3 :</p><figure><img alt="Organization of Terraform remote states in S3 using workspaces" src="https://cdn-images-1.medium.com/max/479/0*STnE0OtprubmIo5N.png" /><figcaption>Organization of Terraform remote states in S3 using workspaces</figcaption></figure><p>The network layer stays the same as before :</p><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket = &quot;terraform-remote-states&quot;<br>    # The key definition changes following the environment<br>    key    = &quot;environments/staging/network.tf&quot;<br>    region = &quot;us-east-1&quot;<br>  }<br>}<br><br>module &quot;network&quot; {<br>  source               = &quot;../modules/network&quot;<br>  region               = &quot;us-east-1&quot;<br>  network_cidr         = &quot;10.0.0.0/16&quot;<br>  private_subnet_cidrs = [&quot;10.0.0.0/24&quot;, &quot;10.0.1.0/24&quot;, &quot;10.0.2.0/24&quot;]<br>  public_subnet_cidrs  = [&quot;10.0.3.0/24&quot;, &quot;10.0.4.0/24&quot;, &quot;10.0.5.9/24&quot;]<br>}</pre><p>The Terraform command manages the workspace. The first step is to create the new workspace:</p><p>$ terraform -chdir=&quot;./network&quot; workspace new staging</p><p>Then you have to select it :</p><p>$ terraform -chdir=&quot;./network&quot; workspace select staging</p><p>The vars directory presented in the diagram above contains variable files to customize the environment. You have to load the good one when you apply the configuration :</p><pre>$ terraform init -chdir=&quot;./network&quot; <br>$ terraform apply -chdir=&quot;./network&quot; -var-file=&quot;./vars/staging.tfvars&quot;</pre><p>Another way is to use locals instead and play with terraform.workspace :</p><pre>terraform {<br>  backend &quot;s3&quot; {<br>    bucket               = &quot;terraform-remote-states&quot;<br>    workspace_key_prefix = &quot;environments&quot;<br>    key                  = &quot;network&quot;<br>    region               = &quot;us-east-1&quot;<br>  }<br>}<br><br>variable &quot;network_cidr&quot; {<br>  type    = list(string)<br>  default = {<br>    staging    = &quot;10.0.0.0/16&quot;<br>    qa         = &quot;10.1.0.0/16&quot;<br>    production = &quot;10.2.0.0/16&quot;<br>  }<br>}<br><br>variable &quot;private_subnet_cidrs&quot; {<br>  type = list(string)<br>  default = {<br>    staging    = [&quot;10.0.0.0/24&quot;, &quot;10.0.1.0/24&quot;, &quot;10.0.2.0/24&quot;]<br>    qa         = [&quot;10.1.0.0/24&quot;, &quot;10.1.1.0/24&quot;, &quot;10.1.2.0/24&quot;]<br>    production = [&quot;10.2.0.0/24&quot;, &quot;10.2.1.0/24&quot;, &quot;10.2.2.0/24&quot;]<br>  }<br>}<br><br>variable &quot;public_subnet_cidrs&quot; {<br>  type = list(string)<br>  default = {<br>    staging    = [&quot;10.0.3.0/24&quot;, &quot;10.0.4.0/24&quot;, &quot;10.0.5.0/24&quot;]<br>    qa         = [&quot;10.1.3.0/24&quot;, &quot;10.1.4.0/24&quot;, &quot;10.1.5.0/24&quot;]<br>    production = [&quot;10.2.3.0/24&quot;, &quot;10.2.4.0/24&quot;, &quot;10.2.5.0/24&quot;]<br>  }<br>}<br><br>locals {<br>  network_cidr          = lookup(var.network_cidr, terraform.workspace, null)<br>  private_subnet_cidrs  = lookup(var.private_subnet_cidrs, terraform.workspace, null)<br>  public_subnet_cidrs   = lookup(var.public_subnet_cidrs, terraform.workspace, null)<br>}<br><br>module &quot;network&quot; {<br>  source                = &quot;../modules/network&quot;<br>  region                = &quot;us-east-1&quot;<br>  network_cidr          = local.network_cidr<br>  private_subnet_cidrs  = local.private_subnet_cidrs<br>  public_subnet_cidrs   = local.public_subnet_cidrs<br>}</pre><h3>Comparison of the Strategies</h3><h4>Separated directories</h4><p>Pros:</p><ul><li>Environments are clearly separated and identifiable</li><li>More granularity: you can customize environment layers</li><li>Less chance of applying a configuration in a bad environment</li></ul><p>Cons:</p><ul><li>Need to duplicate a piece of file structure to create a new environment</li><li>Several directory levels in the project</li></ul><h4>Workspaces</h4><p>Pros:</p><ul><li>Scalability with repeatable environments</li><li>Simplicity</li></ul><p>Cons:</p><ul><li>More possibility to make errors by selecting the wrong workspace</li><li>The customization of an environment layer is less obvious</li></ul><h3>Conclusion</h3><p>There is no single solution for managing multiple environments in a Terraform project. The two approaches we have discussed both have their qualities and their shortcomings. It depends on your project expectations.</p><p>Do you need to scale quickly, or is the pace of environment creation more extended in time? Do you want to have file isolation between environments or rely on the workspace abstraction mechanism?</p><p>In any case, you can compensate for the shortcomings of this choice with other solutions. By using a continuous deployment pipeline, you can greatly reduce the error rate of selecting the wrong workspace. The creation of a new environment in a file structure can be generated on the fly using templating.</p><p>Also, the two methods we have seen are not exhaustive. You can take inspiration from them and create a new one that best fits your use case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5629add231f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>