<?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 Yuru Bolshakou on Medium]]></title>
        <description><![CDATA[Stories by Yuru Bolshakou on Medium]]></description>
        <link>https://medium.com/@xaaaazme?source=rss-d46739ea2270------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*027e1UU2nuBMsdTEXEv2UA.png</url>
            <title>Stories by Yuru Bolshakou on Medium</title>
            <link>https://medium.com/@xaaaazme?source=rss-d46739ea2270------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:24:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xaaaazme/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[How to set up a High Availability HashiCorp Vault cluster in Docker]]></title>
            <link>https://medium.com/@xaaaazme/how-to-set-up-high-availability-hashicorp-vault-cluster-in-docker-655bbc8ad9ad?source=rss-d46739ea2270------2</link>
            <guid isPermaLink="false">https://medium.com/p/655bbc8ad9ad</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[hashicorp-vault]]></category>
            <category><![CDATA[tlm]]></category>
            <category><![CDATA[high-availability]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Yuru Bolshakou]]></dc:creator>
            <pubDate>Fri, 23 Oct 2026 10:56:21 GMT</pubDate>
            <atom:updated>2026-11-19T18:00:45.232Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aqe0u_3xJRjqxaB1" /></figure><h3>What you will learn</h3><p>You will learn how to set up a robust, high-availability HashiCorp Vault cluster in Docker. We will use Terraform for automation and provisioning, and secure the cluster communication using mTLS. By the end of this guide, you will have a production-like Vault setup running locally.</p><h3>Disclaimer</h3><p>All actions initiated by a jobless junior. Use at your own risk and peril. Always test security configurations in an isolated environment before deploying to production.</p><h3>Why you need that</h3><p>Implementing a self-hosted Vault cluster isn’t just for fun; it solves critical infrastructure challenges:</p><ul><li><strong>For security of your credentials:</strong> Stop storing secrets, API keys, and passwords in plaintext .env files or hardcoded in your repositories.</li><li><strong>For Public Key Infrastructure:</strong> Vault can act as your internal Certificate Authority to issue and manage TLS certificates dynamically.</li><li><strong>For ZeroTrust Architecture:</strong> Ensure that every service authenticates and authorizes itself before accessing any secrets.</li><li><strong>For High Availability (HA):</strong> A single point of failure for secrets can bring down your entire infrastructure. A multi-node Vault cluster ensures that if one node goes down, the others seamlessly take over the workload.</li></ul><h3>What you need</h3><p>You should be familiar with the following concepts and tools:</p><ul><li><strong>Core principles of PKI:</strong> Understanding how Certificate Authorities, public/private keys, chain of trust and mTLS work.</li><li><strong>Docker &amp; Docker Compose:</strong> For containerizing and orchestrating our Vault nodes.</li><li><strong>Terraform:</strong> We will use it as Infrastructure as Code (IaC) to generate certificates and bootstrap our configuration.</li><li><strong>HashiCorp Vault Operator knowledge:</strong> Basic understanding of Vault’s architecture (storage backends, seal/unseal process, etc).</li></ul><h3>Architecture</h3><pre>deployment/<br>├── deploy.sh<br>├── base/<br>│   ├── infrastructure/<br>│   │   └── vault/<br>│   │       ├── Dockerfile<br>│   │       ├── vault-common.yaml<br>│   │       ├── vault.network.yaml<br>│   │       └── vault.volume.yaml<br>│   ├── microservices/<br>│   └── observability/<br>├── overlays/<br>│   ├── dev/<br>│   │   ├── docker-compose.yaml<br>│   │   └── infrastructure/<br>│   │       └── vault/<br>│   │           ├── docker-compose.yaml<br>│   │           ├── vault.config.yaml<br>│   │           ├── vault.volume.yaml<br>│   │           ├── certs/<br>│   │           │   ├── vault-1/<br>│   │           │   ├── vault-2/<br>│   │           │   └── vault-3/<br>│   │           └── node-configs/<br>│   │               ├── vault-1.hcl<br>│   │               ├── vault-2.hcl<br>│   │               └── vault-3.hcl<br>│   └── prod/<br>└── terraform/<br>    ├── environments/<br>    │   ├── dev/<br>    │   │   ├── terraform.tfvars<br>    │   │   └── variables.tf<br>    │   └── prod/<br>    └── modules/<br>        └── vault/<br>            ├── root_ca_generation/<br>            │   ├── providers.tf<br>            │   ├── root_ca.tf<br>            │   ├── variables.tf<br>            │   └── outputs.tf<br>            ├── intermediate_ca_generation/<br>            │   ├── data.tf<br>            │   ├── providers.tf<br>            │   ├── intermediate_ca.tf<br>            │   ├── variables.tf<br>            │   └── outputs.tf<br>            └── bootstrap/<br>                ├── data.tf<br>                ├── providers.tf<br>                ├── variables.tf<br>                └── vault.tf</pre><h3>Root CA Generation</h3><p>First, we must generate our Root Certificate Authority. While you could certainly use OpenSSL for this, managing CLI commands and bash scripts can get messy and hard to reproduce. Terraform allows us to define our PKI declaratively and keep it in version control.</p><h3>Providers</h3><pre>terraform {<br>  required_providers {<br>    tls = {<br>      source  = &quot;hashicorp/tls&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>  }<br>}</pre><h3>Variables</h3><pre>variable &quot;root_ca_subject&quot; {<br>  description = &quot;Distinguished Name (DN) subject attributes for the Root CA&quot;<br>  type = object({<br>    common_name         = string<br>    organization        = string<br>    organizational_unit = optional(string)<br>    country             = optional(string)<br>    province            = optional(string)<br>    locality            = optional(string)<br>  })<br>}<br>variable &quot;root_ca_validity_period_hours&quot; {<br>  description = &quot;Validity period in hours for the Root CA certificate&quot;<br>  type        = number<br>}<br>variable &quot;root_ca_key_algorithm&quot; {<br>  description = &quot;Cryptographic algorithm for the Root CA private key&quot;<br>  type        = string<br>}<br>variable &quot;root_ca_key_rsa_bits&quot; {<br>  description = &quot;Key size in bits for RSA algorithm&quot;<br>  type        = number<br>}</pre><h3>Outputs</h3><pre>output &quot;root_ca_cert&quot; {<br>  value       = tls_self_signed_cert.root_ca.cert_pem<br>}<br>output &quot;root_ca_private_key&quot; {<br>  value       = tls_private_key.root_ca.private_key_pem<br>  sensitive   = true<br>}</pre><blockquote><em>The </em><em>sensitive = true flag in Terraform redacts confidential values (such as passwords, API keys, and connection strings) from the command-line interface (CLI) output, console logs, and plan/apply diffs, replacing them with </em><em>(sensitive value) or </em><em>&lt;sensitive&gt;.</em></blockquote><h3>Root CA Resources</h3><p>Here is the complete configuration for generating the Root CA:</p><pre>resource &quot;tls_private_key&quot; &quot;root_ca&quot; {<br>  algorithm = var.root_ca_key_algorithm<br>  rsa_bits  = var.root_ca_key_rsa_bits<br>}<br>resource &quot;tls_self_signed_cert&quot; &quot;root_ca&quot; {<br>  private_key_pem = tls_private_key.root_ca.private_key_pem<br>  subject {<br>    country             = var.root_ca_subject.country<br>    province            = var.root_ca_subject.province<br>    locality            = var.root_ca_subject.locality<br>    organization        = var.root_ca_subject.organization<br>    organizational_unit = var.root_ca_subject.organizational_unit<br>    common_name         = var.root_ca_subject.common_name<br>  }<br>  validity_period_hours = var.root_ca_validity_period_hours<br>  is_ca_certificate     = true<br>  max_path_length       = 1<br>  allowed_uses = [<br>    &quot;cert_signing&quot;,<br>    &quot;crl_signing&quot;<br>  ]<br>}</pre><ol><li>Generating the Private Key<br>The resource named tls_private_key is used to generate the cryptographic private and public keys.<br>We must provide it an algorithm (e.g. RSA) and a bit count (1024, 2048, 4096). The bit count directly impacts security: a higher number makes the key more robust against brute force attacks, but cryptographic operations become slower. I recommend using at least 2048 bits for standard use, though 4096 is preferred for a Root CA.</li><li>Generating the Certificate<br>The second resource is used to generate the self-signed certificate. For this to work, we provide the private key we generated in the previous step, along with the necessary information for the certificate.<br>Most importantly, we set the is_ca_certificate flag to true, set max_path_length = 1 meaning this CA can only sign one level of sub-CAs), and define the allowed_uses specifically for signing other certificates (cert_signing) and revocation lists (crl_signing).</li></ol><p><strong>Danger Zone</strong></p><p>After you run terraform apply, Terraform generates a .tfstate file. Be extremely careful, because this state file stores the generated private key in plaintext.<br>If you lose this file, or worse, if a hacker gets access to it, the consequences will be serious — they will be able to sign forged certificates for your entire infrastructure.</p><h3>Intermediate CA Generation</h3><p>In a production environment, you should <strong>NEVER</strong> use your Root CA to sign end-entity certificates directly. It is simply too risky. The best practice is to keep the Root CA offline (ideally its private key is stored securely in an HSM — Hardware Security Module, but price of this thing is horible).</p><p>Instead, the Root CA is used exactly once: to sign an <strong>Intermediate CA</strong>. We will use this Intermediate CA to sign everything else in our cluster.</p><h3>Providers</h3><pre>terraform {<br>  required_providers {<br>    tls = {<br>      source  = &quot;hashicorp/tls&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>  }<br>}</pre><h3>Variables</h3><pre>variable &quot;intermediate_ca_subject&quot; {<br>  description = &quot;Distinguished Name (DN) subject attributes for the Intermediate CA&quot;<br>  type = object({<br>    common_name         = string<br>    organization        = string<br>    organizational_unit = optional(string)<br>    country             = optional(string)<br>    province            = optional(string)<br>    locality            = optional(string)<br>  })<br>}<br>variable &quot;intermediate_ca_validity_period_hours&quot; {<br>  description = &quot;Validity period in hours for the Intermediate CA certificate&quot;<br>  type        = number<br>}<br>variable &quot;intermediate_ca_key_algorithm&quot; {<br>  description = &quot;Cryptographic algorithm for the Intermediate CA private key&quot;<br>  type        = string<br>}<br>variable &quot;intermediate_ca_key_rsa_bits&quot; {<br>  description = &quot;Key size in bits for RSA algorithm&quot;<br>  type        = number<br>}</pre><h3>Data</h3><p>In this step we take the Root CA private key from previous module.</p><pre>data &quot;terraform_remote_state&quot; &quot;root_ca_generation&quot; {<br>  backend = &quot;local&quot;<br>  config = {<br>    path = &quot;../root_ca_generation/terraform.tfstate&quot;<br>  }<br>}</pre><h3>Intermediate CA Resources</h3><pre>resource &quot;tls_private_key&quot; &quot;intermediate_ca&quot; {<br>  algorithm = var.intermediate_ca_key_algorithm<br>  rsa_bits  = var.intermediate_ca_key_rsa_bits<br>}<br>resource &quot;tls_cert_request&quot; &quot;intermediate_ca&quot; {<br>  private_key_pem = tls_private_key.intermediate_ca.private_key_pem<br>  subject {<br>    common_name         = var.intermediate_ca_subject.common_name<br>    organization        = var.intermediate_ca_subject.organization<br>    organizational_unit = var.intermediate_ca_subject.organizational_unit<br>    country             = var.intermediate_ca_subject.country<br>    province            = var.intermediate_ca_subject.province<br>    locality            = var.intermediate_ca_subject.locality<br>  }<br>}<br>resource &quot;tls_locally_signed_cert&quot; &quot;intermediate_ca&quot; {<br>  cert_request_pem   = tls_cert_request.intermediate_ca.cert_request_pem<br>  ca_private_key_pem = data.terraform_remote_state.root_ca_generation.outputs.root_ca_private_key<br>  ca_cert_pem        = data.terraform_remote_state.root_ca_generation.outputs.root_ca_cert<br>  validity_period_hours = var.intermediate_ca_validity_period_hours<br>  is_ca_certificate     = true<br>  max_path_length       = 0<br>  allowed_uses = [<br>    &quot;cert_signing&quot;,<br>    &quot;crl_signing&quot;,<br>    &quot;digital_signature&quot;<br>  ]<br>}</pre><p>There is nothing entirely special here if you know PKI, but in a nutshell:</p><ol><li>We create a new key pair for the Intermediate CA.</li><li>We create a CSR (Certificate Signing Request).</li><li>We sign the CSR using the Root CA’s private key.</li></ol><blockquote>Note the max_path_length = 0 - this explicitly prevents our Intermediate CA from signing other CAs).</blockquote><h3>Outputs</h3><pre>output &quot;intermediate_ca_cert&quot; {<br>  value       = tls_locally_signed_cert.intermediate_ca.cert_pem<br>}<br>output &quot;intermediate_ca_private_key&quot; {<br>  value       = tls_private_key.intermediate_ca.private_key_pem<br>  sensitive   = true<br>}</pre><h3>Bootstrap</h3><p>In this step, we are going to generate the TLS certificates for each HashiCorp Vault instance. When configuring mTLS for a cluster, there are generally two ways to handle it:</p><ul><li><strong>The lazy way:</strong> Generate a single certificate and private key pair, and give them to every instance.</li><li><strong>The robust way:</strong> Generate unique certificates, private keys, and public keys for <em>each</em> specific instance of HashiCorp Vault.</li></ul><p>Of course, the second method is much more robust and aligns with Zero Trust principles, so we will stick to it. If an attacker compromises one node and steals its private key, the other instances remain perfectly safe, and you can revoke the compromised certificate without rebuilding the whole cluster.</p><p>More importantly, following our enterprise architecture, we will <strong>not</strong> use the Root CA to sign these certificates. We will use the <strong>Intermediate CA</strong> we generated in the previous step.</p><h3>Providers</h3><p>We introduce the local provider here because we need Terraform to save the generated certificates to our local filesystem so that Docker Compose can mount them as volumes later.</p><pre>terraform {<br>  required_providers {<br>    tls = {<br>      source  = &quot;hashicorp/tls&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>    local = {<br>      source  = &quot;hashicorp/local&quot;<br>      version = &quot;~&gt; 2.5&quot;<br>    }<br>  }<br>}</pre><h3>Data</h3><pre>data &quot;terraform_remote_state&quot; &quot;intermediate_ca_generation&quot; {<br>  backend = &quot;local&quot;<br><br>  config = {<br>    path = &quot;../intermediate_ca_generation/terraform.tfstate&quot;<br>  }<br>}<br><br>data &quot;terraform_remote_state&quot; &quot;root_ca_generation&quot; {<br>  backend = &quot;local&quot;<br><br>  config = {<br>    path = &quot;../root_ca_generation/terraform.tfstate&quot;<br>  }<br>}</pre><h3>Variables</h3><pre>variable &quot;vault_cert_subject&quot; {<br>  type = object({<br>    country             = string<br>    province            = string<br>    locality            = string<br>    organization        = string<br>    organizational_unit = string<br>  })<br>}<br>variable &quot;vault_nodes_config&quot; {<br>  type = map(object({<br>    common_name  = string<br>    dns_names    = list(string)<br>    ip_addresses = list(string)<br>  }))<br>}<br>variable &quot;vault_cert_validity_period_hours&quot; {<br>  type    = number<br>  default = 43800 # 5 years<br>}<br>variable &quot;vault_key_algorithm&quot; {<br>  type    = string<br>  default = &quot;RSA&quot;<br>}<br>variable &quot;vault_key_rsa_bits&quot; {<br>  type    = number<br>  default = 2048<br>}<br>variable &quot;vault_certs_output_dir&quot; {<br>  type = string<br>}</pre><h3>Vault Cert Generation Resource</h3><p>Here is where the magic happens. Terraform iterates over our vault_nodes_config map. For every node, it generates a unique private key, creates a Certificate Signing Request (CSR) with specific IP addresses and DNS names (SANs), and signs it using our <strong>Intermediate CA</strong>.</p><pre>resource &quot;tls_private_key&quot; &quot;vault&quot; {<br>  for_each  = var.vault_nodes_config<br>  algorithm = var.vault_key_algorithm<br>  rsa_bits  = var.vault_key_rsa_bits<br>}<br><br>resource &quot;tls_cert_request&quot; &quot;vault&quot; {<br>  for_each        = var.vault_nodes_config<br>  private_key_pem = tls_private_key.vault[each.key].private_key_pem<br><br>  subject {<br>    country             = var.vault_cert_subject.country<br>    province            = var.vault_cert_subject.province<br>    locality            = var.vault_cert_subject.locality<br>    organization        = var.vault_cert_subject.organization<br>    organizational_unit = var.vault_cert_subject.organizational_unit<br>    common_name         = each.value.common_name<br>  }<br><br>  dns_names    = each.value.dns_names<br>  ip_addresses = each.value.ip_addresses<br>}<br><br>resource &quot;tls_locally_signed_cert&quot; &quot;vault&quot; {<br>  for_each           = var.vault_nodes_config<br>  cert_request_pem   = tls_cert_request.vault[each.key].cert_request_pem<br>  ca_private_key_pem = data.terraform_remote_state.intermediate_ca_generation.outputs.intermediate_ca_private_key<br>  ca_cert_pem        = data.terraform_remote_state.intermediate_ca_generation.outputs.intermediate_ca_cert<br><br>  validity_period_hours = var.vault_cert_validity_period_hours<br><br>  allowed_uses = [<br>    &quot;key_encipherment&quot;,<br>    &quot;digital_signature&quot;,<br>    &quot;server_auth&quot;,<br>    &quot;client_auth&quot;<br>  ]<br>}<br><br>resource &quot;local_file&quot; &quot;vault_key_file&quot; {<br>  for_each = var.vault_nodes_config<br>  content  = tls_private_key.vault[each.key].private_key_pem<br>  filename = &quot;${path.cwd}/${var.vault_certs_output_dir}/vault-${each.key}/vault.key&quot;<br><br>  file_permission      = &quot;0600&quot;<br>  directory_permission = &quot;0700&quot;<br>}<br><br>resource &quot;local_file&quot; &quot;vault_cert_file&quot; {<br>  for_each = var.vault_nodes_config<br><br>  content = join(&quot;\n&quot;, [<br>    tls_locally_signed_cert.vault[each.key].cert_pem,<br>    data.terraform_remote_state.intermediate_ca_generation.outputs.intermediate_ca_cert,<br>  ])<br><br>  filename = &quot;${path.cwd}/${var.vault_certs_output_dir}/vault-${each.key}/vault.pem&quot;<br><br>  file_permission      = &quot;0644&quot;<br>  directory_permission = &quot;0755&quot;<br>}<br><br>resource &quot;local_file&quot; &quot;ca_file&quot; {<br>  for_each = var.vault_nodes_config<br><br>  content  = data.terraform_remote_state.root_ca_generation.outputs.root_ca_cert<br>  filename = &quot;${path.cwd}/${var.vault_certs_output_dir}/vault-${each.key}/ca.pem&quot;<br><br>  file_permission      = &quot;0644&quot;<br>  directory_permission = &quot;0755&quot;<br>}</pre><p>In the local_file.vault_cert_file resource, we are <strong>building a standard Full Chain certificate</strong> for each Vault node by combining the node&#39;s individual certificate and the Intermediate CA certificate into a single file.</p><ul><li><strong>join(&quot;\n&quot;, [...]) function:</strong> Concatenates the end-entity certificate with the Intermediate CA certificate, separated by a newline character.</li><li><strong>Why it matters:</strong> This allows the Vault server to automatically provide the intermediate certificate to connecting clients during the TLS handshake, establishing an unbroken chain of trust back to the Root CA.</li></ul><h3>TF Variables</h3><pre>### ROOT CA ###<br>root_ca_subject = {<br>  country             = &quot;BY&quot;<br>  province            = &quot;Grodno Region&quot;<br>  locality            = &quot;Grodno&quot;<br>  organization        = &quot;Cool B2B AI SaaS&quot;<br>  organizational_unit = &quot;Infrastructure &amp; Security&quot;<br>  common_name         = &quot;Cool B2B AI SaaS Root CA (Dev)&quot;<br>}<br><br>root_ca_validity_period_hours = 87600 # 10 years (365 * 24 * 10)<br>root_ca_key_algorithm         = &quot;RSA&quot;<br>root_ca_key_rsa_bits          = 4096<br><br>### INTERMEDIATE CA ###<br>intermediate_ca_subject = {<br>  country             = &quot;BY&quot;<br>  province            = &quot;Grodno Region&quot;<br>  locality            = &quot;Grodno&quot;<br>  organization        = &quot;Cool B2B AI SaaS &quot;<br>  organizational_unit = &quot;Infrastructure &amp; Security&quot;<br>  common_name         = &quot;Cool B2B AI SaaS Intermediate CA (Dev)&quot;<br>}<br><br>intermediate_ca_validity_period_hours = 43800 # 5 years (365 * 24 * 5)<br>intermediate_ca_key_algorithm         = &quot;RSA&quot;<br>intermediate_ca_key_rsa_bits          = 4096<br><br>### VAULT CLUSTER ###<br>vault_cert_subject = {<br>  country             = &quot;BY&quot;<br>  province            = &quot;Grodno Region&quot;<br>  locality            = &quot;Grodno&quot;<br>  organization        = &quot;Cool B2B AI SaaS &quot;<br>  organizational_unit = &quot;Infrastructure &amp; Security Vault&quot;<br>}<br>vault_nodes_config = {<br>  &quot;1&quot; = {<br>    common_name  = &quot;vault-1.dev.internal&quot;<br>    dns_names    = [&quot;localhost&quot;, &quot;vault&quot;, &quot;vault-1&quot;, &quot;vault-1.dev.internal&quot;]<br>    ip_addresses = [&quot;127.0.0.1&quot;]<br>  },<br>  &quot;2&quot; = {<br>    common_name  = &quot;vault-2.dev.internal&quot;<br>    dns_names    = [&quot;localhost&quot;, &quot;vault&quot;, &quot;vault-2&quot;, &quot;vault-2.dev.internal&quot;]<br>    ip_addresses = [&quot;127.0.0.1&quot;]<br>  },<br>  &quot;3&quot; = {<br>    common_name  = &quot;vault-3.dev.internal&quot;<br>    dns_names    = [&quot;localhost&quot;, &quot;vault&quot;, &quot;vault-3&quot;, &quot;vault-3.dev.internal&quot;]<br>    ip_addresses = [&quot;127.0.0.1&quot;]<br>  }<br>}<br>vault_cert_validity_period_hours = 35040 # 4 years (365 * 24 * 4)<br>vault_key_algorithm              = &quot;RSA&quot;<br>vault_key_rsa_bits               = 2048<br>vault_certs_output_dir           = &quot;overlays/dev/infrastructure/vault/certs&quot;</pre><h3>Docker Base Configuration</h3><p>Since we are sticking to a GitOps approach, we want to keep our infrastructure code DRY (Don’t Repeat Yourself). Instead of writing the same Docker Compose configuration for every single Vault node, we create a base template (vault-common) that all other nodes will inherit from.</p><h3>Base Vault Service</h3><p>base/infrastructure/vault/vault-common.yaml</p><pre>services:<br>  vault-common:<br>    build: .<br>    restart: unless-stopped<br>    command: [ &quot;vault&quot;, &quot;server&quot;, &quot;-config=/vault/config/vault.hcl&quot; ]<br>    cap_add:<br>      - IPC_LOCK<br>    environment:<br>      - SKIP_SETCAP=1<br>      - VAULT_CACERT=/vault/config/certs/ca.pem<br>      - VAULT_CLIENT_CERT=/vault/config/certs/vault.pem<br>      - VAULT_CLIENT_KEY=/vault/config/certs/vault.key<br>    networks:<br>      - vault-cluster<br>    volumes:<br>      - ./config/policies:/vault/config/policies</pre><p><em>Note on </em><em>IPC_LOCK:</em> This is a critical Linux capability used to prevent RAM from swapping to disk. Since Vault stores sensitive credentials and encryption keys in memory, allowing the OS to swap memory to disk could potentially leak secrets into the swapfile. IPC_LOCK ensures memory is locked and stays out of persistent storage.</p><h3>Common Volumes</h3><p>base/infrastructure/vault/vault.volume.yaml</p><pre>volumes:<br>  vault-config:<br>    external: false<br>    name: vault-config</pre><h3>Network Configuration</h3><p>base/infrastructure/vault/vault.network.yaml</p><pre>networks:<br>  vault-cluster:<br>    name: vault-network<br>    driver: bridge<br>    ipam:<br>      config:<br>        - subnet: &quot;192.168.42.128/25&quot;<br>          ip_range: &quot;192.168.42.128/25&quot;<br>          gateway: &quot;192.168.42.129&quot;</pre><p>This strict network configuration is used to fix the IP addresses of the HashiCorp Vault instances. In a Raft HA cluster, nodes need reliable, unchanging endpoints to communicate and maintain quorum. If IPs change every time containers restart, Raft retry_join mechanisms might fail</p><h3>Custom Dockerfile</h3><p>One of the most painful issues when running Vault in Docker with TLS is dealing with file permissions. The official Vault image runs as a non-root user (vault, usually UID 100). When you mount certificates from the host machine into the container, they often inherit the host&#39;s root permissions. Vault then crashes on startup with a Permission denied error when trying to read vault.key.</p><p>To solve this, we create a custom Dockerfile with a wrapper script:</p><pre>FROM hashicorp/vault:2.1.0<br>USER root<br>RUN mkdir -p /vault/data /vault/plugins/tmp \<br>    &amp;&amp; chown -R vault:vault /vault<br>RUN printf &#39;#!/bin/sh\n\<br>chown -R root:vault /vault/config/certs 2&gt;/dev/null || true\n\<br>chmod 0640 /vault/config/certs/vault.key 2&gt;/dev/null || true\n\<br>chmod 0644 /vault/config/certs/vault.pem /vault/config/certs/root.pem 2&gt;/dev/null || true\n\<br>exec /usr/local/bin/docker-entrypoint.sh &quot;$@&quot;\n&#39; &gt; /vault-chmod-entrypoint.sh \<br>    &amp;&amp; chmod +x /vault-chmod-entrypoint.sh<br>ENTRYPOINT [&quot;/vault-chmod-entrypoint.sh&quot;]<br>CMD [&quot;vault&quot;, &quot;server&quot;, &quot;-config=/vault/config/hcl/vault.hcl&quot;]</pre><p>This Dockerfile automatically fixes file permissions for TLS certificates <em>before</em> the Vault process starts. It boots as the root user to apply strict permissions securely, and then hands over execution to the original docker-entrypoint.sh, ensuring the actual Vault server runs safely under its intended user.</p><h3>Docker Overlays: Node Configurations</h3><p>Now we move to the environment-specific configurations (our dev overlay). This is where we define the exact behavior of each Vault node, configure the Raft storage backend, and enforce mutual TLS (mTLS).</p><h3>Node 1 Configuration</h3><p>overlays/dev/infrastructure/vault/node-configs/vault-1.hcl</p><pre>ui            = true<br>disable_mlock = true<br>license_path  = &quot;/vault/config/vault-license.hclic&quot;<br>api_addr     = &quot;&lt;https://vault-1.dev.internal&gt;&quot;<br>cluster_addr = &quot;&lt;https://vault-1.dev.internal:8201&gt;&quot;<br>cluster_name = &quot;dev-vault-cluster&quot;<br>plugin_directory = &quot;/vault/plugins/&quot;<br>plugin_tmpdir    = &quot;/vault/plugins/tmp&quot;<br>listener &quot;tcp&quot; {<br>  address                            = &quot;[::]:8200&quot;<br>  cluster_address                    = &quot;[::]:8201&quot;<br>  tls_cert_file                      = &quot;/vault/config/certs/vault.pem&quot;<br>  tls_key_file                       = &quot;/vault/config/certs/vault.key&quot;<br>  tls_client_ca_file                 = &quot;/vault/config/certs/ca.pem&quot;<br>  tls_require_and_verify_client_cert = &quot;true&quot;<br>}<br>storage &quot;raft&quot; {<br>  path    = &quot;/vault/data&quot;<br>  node_id = &quot;vault-1&quot;<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-2.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-3.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>}</pre><h3>Node 2 Configuration</h3><p>overlays/dev/infrastructure/vault/node-configs/vault-2.hcl</p><pre>ui            = true<br>disable_mlock = true<br>license_path  = &quot;/vault/config/vault-license.hclic&quot;<br>api_addr     = &quot;&lt;https://vault-2.dev.internal&gt;&quot;<br>cluster_addr = &quot;&lt;https://vault-2.dev.internal:8201&gt;&quot;<br>cluster_name = &quot;dev-vault-cluster&quot;<br>plugin_directory = &quot;/vault/plugins/&quot;<br>plugin_tmpdir    = &quot;/vault/plugins/tmp&quot;<br>listener &quot;tcp&quot; {<br>  address                            = &quot;[::]:8200&quot;<br>  cluster_address                    = &quot;[::]:8201&quot;<br>  tls_cert_file                      = &quot;/vault/config/certs/vault.pem&quot;<br>  tls_key_file                       = &quot;/vault/config/certs/vault.key&quot;<br>  tls_client_ca_file                 = &quot;/vault/config/certs/ca.pem&quot;<br>  tls_require_and_verify_client_cert = &quot;true&quot;<br>}<br>storage &quot;raft&quot; {<br>  path    = &quot;/vault/data&quot;<br>  node_id = &quot;vault-2&quot;<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-1.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-3.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>}</pre><h3>Node 3 Configuration</h3><p>overlays/dev/infrastructure/vault/node-configs/vault-3.hcl</p><pre>ui            = true<br>disable_mlock = true<br>license_path  = &quot;/vault/config/vault-license.hclic&quot;<br>api_addr     = &quot;&lt;https://vault-3.dev.internal&gt;&quot;<br>cluster_addr = &quot;&lt;https://vault-3.dev.internal:8201&gt;&quot;<br>cluster_name = &quot;dev-vault-cluster&quot;<br>plugin_directory = &quot;/vault/plugins/&quot;<br>plugin_tmpdir    = &quot;/vault/plugins/tmp&quot;<br>listener &quot;tcp&quot; {<br>  address                            = &quot;[::]:8200&quot;<br>  cluster_address                    = &quot;[::]:8201&quot;<br>  tls_cert_file                      = &quot;/vault/config/certs/vault.pem&quot;<br>  tls_key_file                       = &quot;/vault/config/certs/vault.key&quot;<br>  tls_client_ca_file                 = &quot;/vault/config/certs/ca.pem&quot;<br>  tls_require_and_verify_client_cert = &quot;true&quot;<br>}<br>storage &quot;raft&quot; {<br>  path    = &quot;/vault/data&quot;<br>  node_id = &quot;vault-3&quot;<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-1.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>  retry_join {<br>    leader_api_addr         = &quot;&lt;https://vault-2.dev.internal:8200&gt;&quot;<br>    leader_client_cert_file = &quot;/vault/config/certs/vault.pem&quot;<br>    leader_client_key_file  = &quot;/vault/config/certs/vault.key&quot;<br>    leader_ca_cert_file     = &quot;/vault/config/certs/ca.pem&quot;<br>  }<br>}</pre><h3>Configs</h3><p>overlays/dev/infrastructure/vault/vault.config.yaml</p><pre>configs:<br>  node_1_config:<br>    file: ./node-configs/vault-1.hcl<br>  node_2_config:<br>    file: ./node-configs/vault-2.hcl<br>  node_3_config:<br>    file: ./node-configs/vault-3.hcl</pre><h3>Volumes</h3><p>overlays/dev/infrastructure/vault/vault.volume.yaml</p><pre>volumes:<br>  vault-data-1:<br>    external: true<br>    name: vault-data-1<br>vault-data-2:<br>    external: true<br>    name: vault-data-2<br>  vault-data-3:<br>    external: true<br>    name: vault-data-3</pre><h3>The final</h3><p>overlays/dev/infrastructure/vault/docker-compose.yaml</p><p>Now we assemble the master docker-compose.yaml for our dev environment. We use the include and extends features of Docker Compose to pull in the base configurations, networks, volumes, and node-specific configs we created earlier. This keeps the final file incredibly clean and readable.</p><pre>name: dev-vault-cluster<br>include:<br>  - path: ${PWD}/base/infrastructure/vault/vault.network.yaml<br>  - path: ${PWD}/base/infrastructure/vault/vault.volume.yaml<br>  - path: ${PWD}/overlays/dev/infrastructure/vault/vault.config.yaml<br>  - path: ${PWD}/overlays/dev/infrastructure/vault/vault.volume.yaml<br>services:<br>  vault-1:<br>    extends:<br>      file: ${PWD}/base/infrastructure/vault/vault-common.yaml<br>      service: vault-common<br>    container_name: vault-1<br>    hostname: vault-1<br>    ports:<br>      - &quot;8200:8200&quot;<br>    networks:<br>      vault-cluster:<br>        aliases:<br>          - vault-1.dev.internal<br>    configs:<br>      - source: node_1_config<br>        target: /vault/config/vault.hcl<br>    volumes:<br>      - ./certs/vault-1/ca.pem:/vault/config/certs/ca.pem:ro<br>      - ./certs/vault-1/vault.key:/vault/config/certs/vault.key:ro<br>      - ./certs/vault-1/vault.pem:/vault/config/certs/vault.pem:ro<br>      - vault-data-1:/vault/data<br>  vault-2:<br>    extends:<br>      file: ${PWD}/base/infrastructure/vault/vault-common.yaml<br>      service: vault-common<br>    container_name: vault-2<br>    hostname: vault-2<br>    ports:<br>      - &quot;8201:8200&quot;<br>    networks:<br>      vault-cluster:<br>        aliases:<br>          - vault-2.dev.internal<br>    configs:<br>      - source: node_2_config<br>        target: /vault/config/vault.hcl<br>    volumes:<br>      - ./certs/vault-2:/vault/config/certs<br>      - vault-data-2:/vault/data<br>  vault-3:<br>    extends:<br>      file: ${PWD}/base/infrastructure/vault/vault-common.yaml<br>      service: vault-common<br>    container_name: vault-3<br>    hostname: vault-3<br>    ports:<br>      - &quot;8202:8200&quot;<br>    networks:<br>      vault-cluster:<br>        aliases:<br>          - vault-3.dev.internal<br>    configs:<br>      - source: node_3_config<br>        target: /vault/config/vault.hcl<br>    volumes:<br>      - ./certs/vault-3/ca.pem:/vault/config/certs/ca.pem:ro<br>      - ./certs/vault-3/vault.key:/vault/config/certs/vault.key:ro<br>      - ./certs/vault-3/vault.pem:/vault/config/certs/vault.pem:ro<br>      - vault-data-3:/vault/data</pre><h3>Deployment Script</h3><p>To deploy this entire architecture from scratch, you need to execute the Terraform modules sequentially (first the Root CA, then the node certificates), ensure Docker volumes exist, and finally spin up the cluster.</p><p>Here is the complete sequence of commands. You can save this as deploy.sh in the root of your project:</p><pre>#!/bin/bash<br>set -e<br><br>echo &quot;==&gt; Creating persistent Docker volumes...&quot;<br>docker volume create vault-data-1<br>docker volume create vault-data-2<br>docker volume create vault-data-3<br><br>echo &quot;==&gt; Generating Root CA...&quot;<br>terraform -chdir=terraform/modules/vault/root_ca_generation init<br>terraform -chdir=terraform/modules/vault/root_ca_generation apply -auto-approve \<br>  -var-file=&quot;$(pwd)/terraform/environments/dev/terraform.tfvars&quot;<br><br>echo &quot;==&gt; Generating Intermediate CA...&quot;<br>terraform -chdir=terraform/modules/vault/intermediate_ca_generation init<br>terraform -chdir=terraform/modules/vault/intermediate_ca_generation apply -auto-approve \<br>  -var-file=&quot;$(pwd)/terraform/environments/dev/terraform.tfvars&quot;<br><br>echo &quot;==&gt; Generating Node Certificates...&quot;<br>terraform -chdir=terraform/modules/vault/bootstrap init<br>terraform -chdir=terraform/modules/vault/bootstrap apply -auto-approve \<br>  -var-file=&quot;$(pwd)/terraform/environments/dev/terraform.tfvars&quot;<br><br>echo &quot;==&gt; Starting Vault-1 (Leader)...&quot;<br>docker compose -f overlays/dev/infrastructure/vault/docker-compose.yaml up vault-1 --build --detach<br><br>echo &quot;  -&gt; Initializing Vault-1 and saving keys...&quot;<br>mkdir -p ./hashicorp-vault-keys<br>docker exec vault-1 vault operator init -format=json &gt; ./hashicorp-vault-keys/vault-init.json<br><br>echo &quot;  -&gt; Unsealing Vault-1...&quot;<br>for token in $(cat ./hashicorp-vault-keys/vault-init.json | jq -r &#39;.unseal_keys_b64[0:3] | join(&quot; &quot;)&#39;) ; do<br>  docker exec vault-1 vault operator unseal $token<br>done<br><br>echo &quot;==&gt; Starting Vault-2...&quot;<br>docker compose -f overlays/dev/infrastructure/vault/docker-compose.yaml up vault-2 --build --detach<br><br>echo &quot;  -&gt; Unsealing Vault-2...&quot;<br>for token in $(cat ./hashicorp-vault-keys/vault-init.json | jq -r &#39;.unseal_keys_b64[0:3] | join(&quot; &quot;)&#39;) ; do<br>  docker exec vault-2 vault operator unseal $token<br>done<br><br>echo &quot;==&gt; Starting Vault-3...&quot;<br>docker compose -f overlays/dev/infrastructure/vault/docker-compose.yaml up vault-3 --build --detach<br><br>echo &quot;  -&gt; Unsealing Vault-3...&quot;<br>for token in $(cat ./hashicorp-vault-keys/vault-init.json | jq -r &#39;.unseal_keys_b64[0:3] | join(&quot; &quot;)&#39;) ; do<br>    docker exec vault-3 vault operator unseal $token<br>done<br><br>echo &quot;==&gt; Cluster deployment and unseal completed successfully!&quot;</pre><h3>Recommendations</h3><ol><li>Configure a KMS . If an instance drop down there is a way to automatically unseal it</li><li>Configure S3. Don&#39;t store.tfstate file locally</li></ol><h3>GitHub Repository</h3><p><a href="https://github.com/xazme/hashicorp-vault-template/tree/high-availability-cluster">https://github.com/xazme/hashicorp-vault-template/tree/high-availability-cluster</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=655bbc8ad9ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>