Skip to content

Instantly share code, notes, and snippets.

@ivaravko
Last active November 10, 2024 03:55
Show Gist options
  • Save ivaravko/501b4e8e5f1bc1bf5fecd149186b99b9 to your computer and use it in GitHub Desktop.
Save ivaravko/501b4e8e5f1bc1bf5fecd149186b99b9 to your computer and use it in GitHub Desktop.
The simple Terraform and Kubernetes with Docker on macOS

If you'd like to experiment with Terraform and Kubernetes on macOS locally, a great provider for doing so is the Kubernetes provider. You can get set up in a few simple steps, like so:

1. Install Docker

Install Docker for Mac if you have not already.

2. Enable Kubernetes

Enable Kubernetes

Screenshot 2022-12-03 at 12 54 48

3. Install Terraform

Grab the latest Terraform for macOS from releases.hashicorp.com and place the terafform binary somewhere in your PATH or you can install with Homebrew:

brew install terraform

3. Configure, Plan & Apply!

Start with a basic NGINX Kubernetes pod definition in a minimal Terraform configuration — create a main.tf file, and add this to it:

# Configure Kubernetes provider and connect to the Kubernetes API server
provider "kubernetes" {
  config_path    = "~/.kube/config"
  config_context = "docker-desktop"
}

# Create an Nginx pod
resource "kubernetes_pod" "nginx" {
  metadata {
    name = "terraform-example"
    labels = {
      app = "nginx"
    }
  }

  spec {
    container {
      image = "nginx:1.23.2"
      name  = "example"
    }
  }
}

# Create an service
resource "kubernetes_service" "nginx" {
  metadata {
    name = "terraform-example"
  }
  spec {
    selector = {
      app = kubernetes_pod.nginx.metadata.0.labels.app
    }
    port {
      port        = 80
    }

    type = "NodePort"
  }

  depends_on = [
    kubernetes_pod.nginx
  ]
}

Save the file, then apply the configuration:

terraform init
terraform plan

If the plan is good and without error, apply it:

terraform apply

Check to see that the pod is running:

kubectl get pod

The output should have something like this:

NAME                READY     STATUS    RESTARTS   AGE
terraform-example   1/1       Running   1          11m

Find the NodePort

kubectl get svc terraform-example -o jsonpath='{.spec.ports[0].nodePort}'

The output

31761

Now visit http://localhost:31761 in your browser and you should see the Welcome to nginx! default page!

@ngocson952006
Copy link

Thanks, this is what I need for starting learning Terraform

@mitchallen
Copy link

I'm getting this:

% terraform plan
╷
│ Error: Unsupported block type
│ 
│   on main.tf line 28, in resource "kubernetes_service" "nginx":
│   28:     selector {
│ 
│ Blocks of type "selector" are not expected here. Did you mean to define argument "selector"? If so, use the equals
│ sign to assign it a value.

@ivaravko
Copy link
Author

ivaravko commented Dec 3, 2022

Hi @mitchallen

Thank you for reporting an issue. I have updated docs to use the latest Docker for Mac, Terraform and k8s. Please find the entire diff for change by link https://gist.github.com/ivaravko/501b4e8e5f1bc1bf5fecd149186b99b9/revisions.

@ltackmann
Copy link

I am getting this when doing terraform apply

Error: Post "https://127.0.0.1:6443/api/v1/namespaces/default/pods": EOF
│
│   with kubernetes_pod.nginx,
│   on main.tf line 8, in resource "kubernetes_pod" "nginx":
│    8: resource "kubernetes_pod" "nginx" {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment