GOAL:
I’m using terraform to quickly and easily setup a Kubernetes cluster. I have a LoadBalancer service that should expose my blog-backend
service with a custom domain (I have already added DNS NS entries in my Cloudflare that redirects base domain to DigitalOcean’s servers).
resource "kubernetes_service" "blog-backend-lb" {
metadata {
name = "blog-backend-lb"
labels = {
app = "blog-backend"
}
annotations = {
"service.beta.kubernetes.io/do-loadbalancer-name" = "blog.mydomain.com"
"service.beta.kubernetes.io/do-loadbalancer-hostname" = "blog.mydomain.com"
}
}
spec {
type = "LoadBalancer"
selector = {
app = "blog-backend"
}
port {
port = 8080
target_port = 8080
}
}
}
PROBLEM: The service is being create properly and I can see Kubernetes picked up a domain.
However, I cannot access it unless I manually create a domain entry in the DigitalOcean dashboard: Networking -> Domains -> Add Domain -> Create Record for my subdomain redirecting to a LoadBalancer.
This is a bit cumbersome, because I want to clean everything up using terraform without touching anything by hand.
QUESTION: How can I automate it using a Terraform?
I thought about using a domain resource, but I don’t know where to get the ip address from.
resource "digitalocean_domain" "blog-backend-domain" {
name = "blog.mydomain.com"
ip_address = "<WHERE TO GET THE IP FROM?>"
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Ok, problem solved. When I used LoadBalancer as a service I didn’t have access to its IP from within terraform. However, when using
ingress-nginx
helm chart I have the IP. Yay!So the solution is:
The latest Terraform version has modified the way we get IP from ingress
So use this block