Skip to content

Commit 551f016

Browse files
committed
Make the basic example easier to deploy and test
1 parent 9e06935 commit 551f016

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

examples/basic/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
# Basic example
22

3-
A simple example with 3 servers and a load balancer that distributes the traffic between them. The example assumes that each of the servers has some application listening on port 3000.
3+
A simple example with 3 servers and a load balancer that distributes the traffic between them. The example uses an user-data script to launch a HTTP server listening to port 80 on each server. The example does not enable TLS on frontend by default.
44

5-
Once you deploy it, go to your domain settings and add a CNAME record that points to the `lb_url` output variable. After the change gets propagated, all the traffic to your domain (`my.domain.com` in this example) will be distributed among the 3 servers.
5+
## Getting started
6+
7+
To deploy private network, three servers, and load balancer on your UpCloud account, run:
8+
9+
```bash
10+
terraform init
11+
terraform apply
12+
```
13+
14+
Once the deployment is complete and load balancer has reached `running` state you can use `curl` or your browser to send request to the load balancer available in the URL defined by `lb_url` output variable:
15+
16+
```bash
17+
curl $(terraform output -raw lb_url)
18+
```
19+
20+
Note that it might take some time for the DNS name to propagate. During this time the above `curl` command will likely fail with `Could not resolve host: ...` error message.
21+
22+
The output should include hostname of the backend server, for example:
23+
24+
```txt
25+
Hello from lb-module-basic-example-server-1
26+
```
27+
28+
## Enable TLS
29+
30+
The example does not enable TLS on frontend by default. To enable TLS, uncomment `domains` variable in [main.tf](./main.tf) and replace example domain with your domain. Change also the value for `frontend_port` from `80` to `443`.
31+
32+
Deploy the changes with `terraform apply`. When the deployment is completed, go to your domain settings and add a CNAME record that points to the `lb_url` output variable. After the change gets propagated, all the traffic to your domain will be distributed among the 3 servers.

examples/basic/main.tf

+28-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ terraform {
99

1010
provider "upcloud" {}
1111

12+
locals {
13+
prefix = "lb-module-basic-example-"
14+
}
15+
1216
resource "upcloud_network" "network" {
13-
name = "my_network"
17+
name = "${local.prefix}net"
1418
zone = "pl-waw1"
1519

1620
ip_network {
@@ -22,14 +26,24 @@ resource "upcloud_network" "network" {
2226

2327
resource "upcloud_server" "webservers" {
2428
count = 3
25-
hostname = "webserver${count.index}"
26-
title = "webserver_${count.index}"
29+
hostname = "${local.prefix}server-${count.index}"
30+
title = "${local.prefix}server-${count.index}"
2731
zone = "pl-waw1"
28-
plan = "1xCPU-2GB"
32+
plan = "1xCPU-1GB"
33+
34+
# Use user-data script to install and launch http server on start
35+
metadata = true
36+
user_data = file("${path.module}/user-data-script.sh")
2937

3038
template {
3139
storage = "Ubuntu Server 20.04 LTS (Focal Fossa)"
3240
size = 25
41+
title = "${local.prefix}storage-${count.index}"
42+
}
43+
44+
# Required to have internet access when installing httpd on launch
45+
network_interface {
46+
type = "public"
3347
}
3448

3549
network_interface {
@@ -45,12 +59,18 @@ resource "upcloud_server" "webservers" {
4559
module "load_balancer" {
4660
source = "UpCloudLtd/basic-loadbalancer/upcloud"
4761

48-
name = "my_loadbalancer"
62+
name = "${local.prefix}lb"
4963
zone = "pl-waw1"
5064
network = upcloud_network.network.id
51-
backend_servers = [for v in upcloud_server.webservers : v.network_interface.1.ip_address]
52-
backend_server_port = 3000
53-
domains = ["my.domain.net"]
65+
backend_servers = [for v in upcloud_server.webservers : v.network_interface.2.ip_address]
66+
backend_server_port = 80
67+
68+
# To enable TLS on the frontend:
69+
# - Uncomment and replace example.com with your domain
70+
# - Change frontend port from 80 to 443
71+
72+
# domains = ["example.com"]
73+
frontend_port = 80
5474
}
5575

5676
output "lb_url" {

examples/basic/user-data-script.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Install HTTP server and firewall
4+
apt update
5+
apt install -y apache2 ufw
6+
7+
# Add hostname to HTTP response to identify back-end server
8+
echo "Hello from $(hostname)" > /var/www/html/index.html
9+
10+
# Allow incoming traffic only from private network
11+
ufw enable
12+
ufw allow in on eth2

0 commit comments

Comments
 (0)