In this challenge, we'll configure Terraform to manage Kubernetes resources using the specified provider and create a Kubernetes service and deployment.
Task Overview:
Configure Terraform to use the HashiCorp Kubernetes provider with version 2.11.0.
Specify the path to the kubeconfig file.
Create a Kubernetes service named
webapp-service
with the specified specifications.Create a Kubernetes deployment named
frontend
with the specified specifications.
Solution:
Step 1: Install Terraform 1.1.5:
Ensure that Terraform version 1.1.5 is installed on the control plane.
Step 2: Create provider.tf
Configuration File:
Create a file named provider.tf
and add the following configuration:
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.11.0"
}
}
}
provider "kubernetes" {
config_path = "/root/.kube/config"
}
This configuration sets up the HashiCorp Kubernetes provider with the specified version and points to the kubeconfig file.
Step 3: Create main.tf
Configuration File
Create a file named main.tf
and add the following configuration:
resource "kubernetes_service" "webapp-service" {
metadata {
name = "webapp-service"
}
spec {
type = "NodePort"
port {
port = 8080
node_port = 30080
}
}
}
resource "kubernetes_deployment" "frontend" {
metadata {
name = "frontend"
labels = {
name = "frontend"
}
}
spec {
replicas = 4
selector {
match_labels = {
name = "webapp"
}
}
template {
metadata {
labels = {
name = "webapp"
}
}
spec {
container {
image = "kodekloud/webapp-color:v1"
name = "simple-webapp"
port {
container_port = 8080
}
}
}
}
}
}
This configuration defines a Kubernetes service named webapp-service
and a deployment named frontend
with the specified specifications.
Step 4: Initialize, Plan, and Apply Terraform:
Run the following commands in the terminal:
terraform init
terraform plan
terraform apply
This will initialize Terraform, generate an execution plan, and apply the changes to create the Kubernetes resources.
Conclusion:
In this tutorial, we configured Terraform to manage Kubernetes resources using the specified provider and created a Kubernetes service and deployment according to the given specifications. By following the provided steps, you can efficiently manage Kubernetes infrastructure using Terraform, ensuring consistency and reproducibility in your environment.