0
votes

I'm currently going through the terraform tutorial from hashicorp. I have a K8S cluster to which I access from my machine (~/.kube/config).

I tried to create a small tf file with

  provider "kubernetes" {}

But when I run

terraform apply

I get this error

Error: Error applying plan:

1 error(s) occurred:

* kubernetes_pod.nginx: 1 error(s) occurred:

* kubernetes_pod.nginx: Post "http://localhost/api/v1/namespaces/default/pods": dial tcp 127.0.0.1:80: connect: connection refused

I've also tried to specify the path to my k8s config

load_config_file = "true"
config_path = "~/.kube/config"

but I have the same issue

I would also like to connect by specifying the certs:

provider "kubernetes" {
  host = "https://104.196.242.174"

  client_certificate     = file("~/.kube/client-cert.pem")
  client_key             = file("~/.kube/client-key.pem")
  cluster_ca_certificate = file("~/.kube/cluster-ca-cert.pem")

  load_config_file = false # when you wish not to load the local config file
}

But I can't find how to generate those certs from the .kube/config or directly with kubectl.

1

1 Answers

0
votes

From the error log I can see the problem seems to be that your cluster is unreachable : dial tcp 127.0.0.1:80: connect: connection refused.
Could be either because

  • you didn't specify the host param in the provider configuration (see here) which feel a reasonable assumption as on the second example you gave your cluster is located at 104.196.242.174, not at 127.0.0.1
  • There is some network issue that prevent you from accessing the cluster (which might not be the case if you can properly issue command via kubectl)

About the authentication with certificate, I guess it depend on how your cluster was built/configured. I would have started with the following doc about that last problem.