<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: alessskeno</title>
    <description>The latest articles on DEV Community by alessskeno (@aleskerov).</description>
    <link>https://dev.to/aleskerov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2500115%2F56accb33-64c6-422c-85e7-44e95c865443.jpg</url>
      <title>DEV Community: alessskeno</title>
      <link>https://dev.to/aleskerov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aleskerov"/>
    <language>en</language>
    <item>
      <title>Setting Up a Production-Ready Kubernetes Cluster with RKE2 in vSphere Using Terraform</title>
      <dc:creator>alessskeno</dc:creator>
      <pubDate>Fri, 29 Nov 2024 13:21:19 +0000</pubDate>
      <link>https://dev.to/aleskerov/setting-up-a-production-ready-kubernetes-cluster-with-rke2-in-vsphere-using-terraform-kp9</link>
      <guid>https://dev.to/aleskerov/setting-up-a-production-ready-kubernetes-cluster-with-rke2-in-vsphere-using-terraform-kp9</guid>
      <description>&lt;p&gt;Setting up a robust Kubernetes cluster in a production environment is no small feat. In this article, I’ll walk you through my journey of deploying an RKE2 (Rancher Kubernetes Engine 2) cluster in a vSphere environment using a custom Terraform module. This guide will include configurations, explanations, and a peek into the setup process with screenshots.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why RKE2 on vSphere?
&lt;/h2&gt;

&lt;p&gt;RKE2 provides a lightweight yet powerful Kubernetes distribution ideal for secure production workloads. When combined with vSphere’s virtualization and Terraform’s Infrastructure-as-Code capabilities, you can achieve a flexible, scalable, and automated deployment process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools and Technologies
&lt;/h2&gt;

&lt;p&gt;Terraform: Automates the provisioning of infrastructure.&lt;br&gt;
RKE2: Kubernetes distribution optimized for production environments.&lt;br&gt;
vSphere: Virtualization platform for deploying VMs.&lt;br&gt;
Ansible: Used for post-deployment configuration.&lt;br&gt;
Prerequisites:&lt;br&gt;
Python 3.x&lt;br&gt;
Ansible, Ansible-Core&lt;br&gt;
sshpass and whois (for password management)&lt;/p&gt;
&lt;h2&gt;
  
  
  Terraform Module Overview
&lt;/h2&gt;

&lt;p&gt;I built a reusable Terraform module to standardize and automate the Kubernetes cluster provisioning. Here's an overview of the main.tf configuration file that calls the module:&lt;/p&gt;
&lt;h2&gt;
  
  
  Core Module Features
&lt;/h2&gt;

&lt;p&gt;Multi-AZ Clusters: Enables highly available clusters with master and worker nodes spread across multiple availability zones.&lt;br&gt;
Customizable Resources: Easily configure CPU, memory, and storage for master, worker, and storage nodes.&lt;br&gt;
Built-in RKE2 Installation: Installs RKE2 with a choice of CNI plugins (canal, flannel, etc.).&lt;br&gt;
Networking Configuration: Define Kubernetes service and cluster CIDRs.&lt;br&gt;
Storage Options: Support for local storage and optional NFS integration.&lt;br&gt;
Secure Communication: TLS certificates for domain and API access.&lt;/p&gt;


&lt;h2&gt;
  
  
  main.tf Breakdown
&lt;/h2&gt;

&lt;p&gt;Here are the main aspects of the main.tf file:&lt;/p&gt;
&lt;h2&gt;
  
  
  Module Invocation
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module "rke2_prod_cluster" {
  source       = "./modules/rke2-provisioner"
  env          = "prod" # Environment name
  domain       = var.domain # Domain name
  multi_az     = true # If you want to create multi-az cluster
  install_rke2 = true # Install RKE2
  lh_storage   = true # Local storage for worker nodes
  hashed_pass  = var.hashed_pass # Hashed password for user creation
  cluster_cidr = var.cluster_cidr # Kubernetes cluster CIDR
  service_cidr = var.service_cidr # Kubernetes service CIDR
  nfs_enabled  = false # Change to true if you want to enable nfs server
  update_apt   = false # Update apt packages by changing to true
  rke2_token   = var.rke2_token
  rke2_version = "v1.30.5+rke2r1"
  rke2_cni     = "canal" # Alternatives: flannel, calico, cilium
  kubevip_range_global = join("-", [cidrhost(var.vm_cidr_az1, 50)], [cidrhost(var.vm_cidr_az1, 60)]) # Global IP range for LoadBalancer IPs
  kubevip_alb_cidr          = "${cidrhost(var.vm_cidr_az1, 20)}/32" # IP for Nginx Ingress Controller Service
  rke2_api_endpoint = cidrhost(var.vm_cidr_az1, 10) # API Server IP

  ansible_password  = var.ansible_password # Ansible user password
  domain_crt        = var.domain_crt # Domain certificate
  domain_key        = var.domain_key # Domain key
  domain_root_crt   = var.domain_root_crt # Root certificate
  master_node_count = var.master_node_count_prod
  worker_node_count = var.worker_node_count_prod
  storage_node_count = var.storage_node_count_prod

  # Resources
  worker_node_cpus      = 8
  worker_node_memory    = 8192
  worker_node_disk_size = 100

  master_node_cpus      = 8
  master_node_memory    = 8192
  master_node_disk_size = 50

  storage_node_disk_size = 100

  nfs_node_disk_size = 50

  # AZ1
  master_ip_range_az1       = [for i in range(61, 69) : cidrhost(local.vm_cidr_az1, i)] # Master node IP range
  worker_ip_range_az1       = [for i in range(71, 79) : cidrhost(local.vm_cidr_az1, i)] # Worker node IP range
  vsphere_datacenter_az1    = var.vsphere_datacenter_az1 # vSphere datacenter name
  vsphere_host_az1          = var.vsphere_host_az1 # vSphere host name
  vsphere_resource_pool_az1 = var.vsphere_resource_pool_az1 # vSphere resource pool name
  vsphere_datastore_az1     = var.vsphere_datastore_az1 # vSphere datastore name
  vsphere_network_name_az1  = var.vsphere_network_name_az1 # vSphere network name
  vm_gw_ip_az1              = local.vm_gw_ip_az1 # Gateway IP
  nfs_ip_az1 = cidrhost(local.vm_cidr_az1, 70) # NFS server IP

  # AZ3
  master_ip_range_az3       = [for i in range(81, 89) : cidrhost(local.vm_cidr_az3, i)]
  worker_ip_range_az3       = [for i in range(91, 99) : cidrhost(local.vm_cidr_az3, i)]
  vsphere_datacenter_az3    = var.vsphere_datacenter_az3
  vsphere_host_az3          = var.vsphere_host_az3
  vsphere_resource_pool_az3 = var.vsphere_resource_pool_az3
  vsphere_datastore_az3     = var.vsphere_datastore_az3
  vsphere_network_name_az3  = var.vsphere_network_name_az3
  vm_gw_ip_az3              = local.vm_gw_ip_az3
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deployment Walkthrough
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1: Initialize Terraform
&lt;/h3&gt;

&lt;p&gt;Run the following commands to initialize Terraform and apply the configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;terraform init
terraform plan
terraform apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Verify Resources in vSphere
&lt;/h3&gt;

&lt;p&gt;Confirm the VMs are provisioned in vSphere.&lt;br&gt;
Ensure the network configurations (IP, gateway) match the Terraform parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Validate Kubernetes Cluster
&lt;/h3&gt;

&lt;p&gt;After deployment:&lt;br&gt;
SSH into one of the master nodes.&lt;br&gt;
Run kubectl get nodes to ensure all nodes are registered and ready.&lt;/p&gt;




&lt;h3&gt;
  
  
  Screenshots of the Process
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Terraform Apply Output
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh058m4yp21ntn7j4kmbu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh058m4yp21ntn7j4kmbu.png" alt="Image description" width="656" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  vSphere Dashboard
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i4onnnntfz60cbcldgr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i4onnnntfz60cbcldgr.png" alt="Image description" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhytjqkhsbbxcnp2mqxhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhytjqkhsbbxcnp2mqxhu.png" alt="Image description" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Kubernetes Terminal
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuk565g6e2yyulcu8lqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuk565g6e2yyulcu8lqh.png" alt="Image description" width="800" height="159"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Key Challenges
&lt;/h3&gt;

&lt;p&gt;Configuring multi-AZ setups required precise IP allocation and resource planning.&lt;br&gt;
Ensuring compatibility between Terraform, vSphere, and RKE2 versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips for Success
&lt;/h2&gt;

&lt;p&gt;Automate Certificate Management: Pre-generate and verify certificates for secure communication.&lt;br&gt;
Test Locally: Run initial setups in a test environment to validate module behavior.&lt;br&gt;
Optimize Resource Allocation: Tailor resource parameters to your workload needs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Terraform and vSphere to deploy an RKE2 Kubernetes cluster offers a highly customizable and scalable solution for production environments. By modularizing the Terraform configuration, this setup can be reused and extended for other environments with minimal changes.&lt;br&gt;
If you've followed along or have feedback, share your experience in the comments below. Checkout the &lt;a href="https://github.com/alessskeno/terraform-vsphere-rke2" rel="noopener noreferrer"&gt;code repository&lt;/a&gt; from my GitHub profile. Let's discuss Kubernetes automation at scale!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>vsphere</category>
      <category>infrastructureascode</category>
    </item>
  </channel>
</rss>
