Create a LoadBalancer for nginx.
This commit is contained in:
parent
0179bb0828
commit
f5b4a7d323
29
README.md
29
README.md
@ -126,6 +126,8 @@ nginx NodePort 172.20.10.182 <none> 80:31234/TCP 6m8s app=Nginx
|
||||
|
||||
### Reaching the app.
|
||||
|
||||
#### Using the NodePort
|
||||
|
||||
It is not possible with terraform output to retrieve the configured nodes. However, it is possible to retrieve IPs for our nodes using aws cli:
|
||||
|
||||
```sh
|
||||
@ -146,6 +148,31 @@ $ curl http://$CLUSTER_IP:31234/
|
||||
```
|
||||
|
||||
|
||||
#### Using the LoadBalancer
|
||||
|
||||
This approach is simpler, as it is just needed to retrieve the created the LoadBalancer external address, either by using `kubectl` or `terraform output`:
|
||||
|
||||
```sh
|
||||
$ kubectl get svc -n testaroo nginx-lb
|
||||
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
||||
nginx-lb LoadBalancer 172.20.149.132 a34059e68106b41a292730b5defe734b-581837320.eu-west-3.elb.amazonaws.com 80:31698/TCP 3m50s
|
||||
|
||||
$ terraform output 09:59:47
|
||||
lb-address = "a34059e68106b41a292730b5defe734b-581837320.eu-west-3.elb.amazonaws.com"
|
||||
```
|
||||
|
||||
The service should be reachable directly using it:
|
||||
|
||||
```sh
|
||||
$ curl http://$(terraform output -raw lb-address):80/
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Welcome to nginx!</title>
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
### Reaching the/a node ssh port:
|
||||
|
||||
Still using the AWS CLI to retrieve nodes, just:
|
||||
@ -182,7 +209,7 @@ PID USER TIME COMMAND
|
||||
$ kubectl exec -ti -n testaroo alpine -- sh
|
||||
/ # echo "hello world"
|
||||
hello world
|
||||
/ #
|
||||
/ #
|
||||
```
|
||||
|
||||
## Todo:
|
||||
|
@ -20,10 +20,11 @@ resource "aws_subnet" "subnets" {
|
||||
tags = {
|
||||
Name = "${each.key}-${var.environment}"
|
||||
Env = var.environment
|
||||
"kubernetes.io/cluster/eks-cluster-${var.environment}" = "shared"
|
||||
"kubernetes.io/cluster/eks-cluster-${var.environment}" = "owned"
|
||||
}
|
||||
}
|
||||
|
||||
// The internet gateway is required so nodes can connect to the control plane
|
||||
resource "aws_internet_gateway" "nat_gateway" {
|
||||
vpc_id = aws_vpc.main.id
|
||||
}
|
||||
|
23
k8s/nginx.tf
23
k8s/nginx.tf
@ -59,7 +59,7 @@ resource "kubernetes_service" "testaroo" {
|
||||
count = var.enable_nginx
|
||||
|
||||
metadata {
|
||||
name = "nginx"
|
||||
name = "nginx-np"
|
||||
namespace = kubernetes_namespace.testaroo.metadata.0.name
|
||||
}
|
||||
|
||||
@ -76,3 +76,24 @@ resource "kubernetes_service" "testaroo" {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_service" "testaroo-lb" {
|
||||
count = var.enable_nginx
|
||||
|
||||
metadata {
|
||||
name = "nginx-lb"
|
||||
namespace = kubernetes_namespace.testaroo.metadata.0.name
|
||||
}
|
||||
|
||||
spec {
|
||||
selector = {
|
||||
app = kubernetes_deployment.testaroo[0].spec.0.template.0.metadata.0.labels.app
|
||||
}
|
||||
|
||||
type = "LoadBalancer"
|
||||
port {
|
||||
port = 80
|
||||
target_port = 80
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
k8s/output.tf
Normal file
4
k8s/output.tf
Normal file
@ -0,0 +1,4 @@
|
||||
output "lb-address" {
|
||||
description = "load balancer hosntame"
|
||||
value = flatten(kubernetes_service.testaroo-lb[0].status[*].load_balancer[*].ingress[*].hostname)[0]
|
||||
}
|
Reference in New Issue
Block a user