❯ terraform -v
Terraform v0.13.1
+ provider registry.terraform.io/digitalocean/digitalocean v1.22.2
+ provider registry.terraform.io/hashicorp/kubernetes v1.12.0
resource "kubernetes_service" "app" {
metadata {
name = "app"
namespace = kubernetes_namespace.app.metadata.0.name
}
spec {
selector = {
app = kubernetes_deployment.app.metadata.0.labels.app
}
# session_affinity = "ClientIP" # << This has no effect on the error
port {
port = 80
target_port = 8000
}
# type = "LoadBalancer" # << Error message appears when this is commented, everything is fine if it is set
}
}
Terraform either applies the configuration or outputs a helpful error message that provides suggestions on what is causing the problem and how to fix it.
Terraform complains:
Error: Failed to update service: Service "app" is invalid: spec.ports[0].nodePort: Forbidden: may not be used when `type` is 'ClusterIP'
terraform applyCluster is set up using the DigitalOcean provider.
There is a StackOverflow question by someone else, asking about the same problem: https://stackoverflow.com/questions/59461825/how-to-define-a-kubernetes-service-with-terraform-and-clusterip
I had to manually set spec.port.node_port=0 and terraform apply once. That worked around the problem permanently, even after removing the node_port setting again.
Most helpful comment
I had to manually set
spec.port.node_port=0andterraform applyonce. That worked around the problem permanently, even after removing thenode_portsetting again.