Skip to content

Commit 691b873

Browse files
authored
Merge pull request #277 from sspencerwire/fix_formatting_haproxy
Minor HAProxy document adjustments
2 parents d7539d7 + a021fe7 commit 691b873

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed
-3.32 KB
Loading

docs/guides/proxies/haproxy_apache_lxd.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: HAProxy-Apache-LXD
33
author: Steven Spencer
4-
contributors: Ezequiel Bruni
4+
contributors: Ezequiel Bruni, Antoine Le Morvan
55
---
66
# HAProxy Load Balancing Apache using LXD Containers
77

@@ -66,8 +66,10 @@ Next we need to modify the `macvlan` profile. But before we do, we need to know
6666
link/ether a8:5e:45:52:f8:b6 brd ff:ff:ff:ff:ff:ff
6767
inet 192.168.1.141/24 brd 192.168.1.255 scope global dynamic noprefixroute eno1
6868
```
69+
!!! Note
70+
In this case, the interface we are looking for is "eno1" but this could be completely different on your system. Use **your** interface information!
6971

70-
In this case, the interface we are looking for is "eno1" but this could be completely different on your system. Use **your** interface information! Now that we know the LAN interface, we can modify our `macvlan` profile. To do this, at the command line type:
72+
Now that we know the LAN interface, we can modify our `macvlan` profile. To do this, at the command line type:
7173

7274
`lxc profile edit macvlan`
7375

@@ -162,7 +164,7 @@ and then do a search for the `<h1>` tag, which should show this:
162164

163165
Simply change that line to read:
164166

165-
`<h1>WEB1 HTTP Server <strong>Test Page</strong></h1>`
167+
`<h1>SITE1 HTTP Server <strong>Test Page</strong></h1>`
166168

167169
Now repeat the process for web2. Going to these machines by IP in a browser should now return the correct welcome page for each. There's more to do on the web servers, but let's leave them and go on to the proxy server next.
168170

@@ -178,19 +180,19 @@ lxc config device add proxyha http proxy listen=tcp:0.0.0.0:80 connect=tcp:127.0
178180
lxc config device add proxyha https proxy listen=tcp:0.0.0.0:443 connect=tcp:127.0.0.1:443
179181
```
180182

181-
For our testing, we are probably only going to use port 80, or HTTP traffic, but this shows you how you would configure the container to listen on the default web ports for both HTTP and HTTPS. Using this command also ensures that restarting the **proxyha** container will maintain those listening ports.
183+
For our testing, we are only going to use port 80, or HTTP traffic, but this shows you how you would configure the container to listen on the default web ports for both HTTP and HTTPS. Using this command also ensures that restarting the **proxyha** container will maintain those listening ports.
182184

183185
## The HAProxy Configuration
184186

185-
We've already installed HAProxy on the container, but we have done nothing with the configuration. Before we do anything, we need to do something to resolve our hosts. Normally we would be using fully-qualified domain names, but in this lab environment, we are just using IPs. To get some names associated with the machines, we are going to add some hosts file records to the **proxyha** container.
187+
We've already installed HAProxy on the container, but we have done nothing with the configuration. Before we do anything, we need to do something to resolve our hosts. Normally we would be using fully-qualified domain names, but in this lab environment, we are just using IPs. To get some names associated with the machines, we are going to add some host file records to the **proxyha** container.
186188

187189
`lxc exec proxyha vi /etc/hosts`
188190

189191
Add the following records to the bottom of the file:
190192

191193
```
192-
192.168.1.150 web1.testdomain.com web1
193-
192.168.1.101 web2.testdomain.com web2
194+
192.168.1.150 site1.testdomain.com site1
195+
192.168.1.101 site2.testdomain.com site2
194196
```
195197

196198
Which should allow the **proxyha** container to resolve those names.
@@ -245,17 +247,17 @@ errorfile 504 /etc/haproxy/errors/504.http
245247
# bind *:443 ssl crt /etc/letsencrypt/live/example.com/example.com.pem
246248
# reqadd X-Forwarded-Proto:\ https
247249
248-
# acl host_web1 hdr(host) -i web1.testdomain.com
249-
# acl host_web2 hdr(host) -i web2.testdomain.com
250+
# acl host_web1 hdr(host) -i site1.testdomain.com
251+
# acl host_web2 hdr(host) -i site2.testdomain.com
250252
251253
# use_backend subdomain1 if host_web1
252254
# use_backend subdomain2 if host_web2
253255
254256
frontend http_frontend
255257
bind *:80
256258
257-
acl web_host1 hdr(host) -i web1.testdomain.com
258-
acl web_host2 hdr(host) -i web2.testdomain.com
259+
acl web_host1 hdr(host) -i site1.testdomain.com
260+
acl web_host2 hdr(host) -i site2.testdomain.com
259261
260262
use_backend subdomain1 if web_host1
261263
use_backend subdomain2 if web_host2
@@ -265,21 +267,21 @@ backend subdomain1
265267
balance roundrobin
266268
http-request set-header X-Client-IP %[src]
267269
# redirect scheme https if !{ ssl_fc }
268-
server web1 web1.testdomain.com:80 check
269-
server web2 web2.testdomain.com:80 check
270+
server site1 site1.testdomain.com:80 check
271+
server site2 web2.testdomain.com:80 check
270272
271273
backend subdomain2
272274
# balance leastconn
273275
balance roundrobin
274276
http-request set-header X-Client-IP %[src]
275277
# redirect scheme https if !{ ssl_fc }
276-
server web2 web2.testdomain.com:80 check
277-
server web1 web1.testdomain.com:80 check
278+
server site2 site2.testdomain.com:80 check
279+
server site1 site1.testdomain.com:80 check
278280
```
279281

280282
A little explanation of what's going on above. You should see this in your testing, when you get to the testing section of this guide (below):
281283

282-
Both **web1** and **web2** are definded in the "acl" section. Then both **web1** and **web2** are included in each other's "roundrobin" for their respective back ends. What happens when you go to web1.testdomain.com in the test, the URL does not change, but the page inside will switch each time you access the page from the web1 to the web2 test pages. Same goes for web2.testdomain.com.
284+
Both **site1** and **site2** are defined in the "acl" section. Then both **site1** and **site2** are included in each other's "roundrobin" for their respective back ends. What happens when you go to site1.testdomain.com in the test, the URL does not change, but the page inside will switch each time you access the page from the **site1** to the **site2** test pages. Same goes for site2.testdomain.com.
283285

284286
This is done to show you the switch is occurring, but in reality, your website content will look exactly the same regardless of which server you are hitting. Keep in mind that we are showing how you might want to distribute traffic between multiple hosts. You can also use "leastcon" in the balance line, and instead of switching based on the previous hit, it will load the site with the least number of connections.
285287

@@ -403,7 +405,7 @@ If everything starts and runs without issue, we are ready to move on to testing.
403405

404406
## Testing The Proxy
405407

406-
As with the hosts (`/etc/hosts`) setup that we used so that our **proxyha** container can resolve the web servers, and since in our lab environment we don't have a local DNS server running, we need to set the hostname values on our local machine for both the **web1** and **web2** containers. But, instead of using their IP addresses, we need to use the **proxyha** IP address for both.
408+
As with the hosts (`/etc/hosts`) setup that we used so that our **proxyha** container can resolve the web servers, and since in our lab environment we don't have a local DNS server running, we need to set the IP values on our local machine for both the site1 and site2 websites, to correspond to our haproxy container.
407409

408410
To do this, we need to modify our `/etc/hosts` file on our local machine. Consider this method of domain resolution a "poor man's DNS."
409411

@@ -412,19 +414,20 @@ To do this, we need to modify our `/etc/hosts` file on our local machine. Consid
412414
Then just add these two lines:
413415

414416
```
415-
192.168.1.149 web1.testdomain.com web1
416-
192.168.1.149 web2.testdomain.com web2
417+
192.168.1.149 site1.testdomain.com site1
418+
192.168.1.149 site2.testdomain.com site2
417419
```
418420

419-
If you ping either **web1** or **web2** on your local machine now, you should get a response from **proxyha**:
421+
If you ping either **site1** or **site2** on your local machine now, you should get a response from **proxyha**:
420422

421423
```
422-
PING web1.testdomain.com (192.168.1.149) 56(84) bytes of data.
423-
64 bytes from web1.testdomain.com (192.168.1.149): icmp_seq=1 ttl=64 time=0.427 ms
424-
64 bytes from web1.testdomain.com (192.168.1.149): icmp_seq=2 ttl=64 time=0.430 ms
424+
PING site1.testdomain.com (192.168.1.149) 56(84) bytes of data.
425+
64 bytes from site1.testdomain.com (192.168.1.149): icmp_seq=1 ttl=64 time=0.427 ms
426+
64 bytes from site1.testdomain.com (192.168.1.149): icmp_seq=2 ttl=64 time=0.430 ms
425427
```
426428

427-
Now open your web browser and type web1.testdomain.com (or web2.testdomain.com) as the URL in the address bar. You should get a response back from one of the two test pages and if you load the page again, you should get the next server's test page. Note that the URL does not change, but the returned page will change alternately between servers.
429+
Now open your web browser and type site1.testdomain.com (or site2.testdomain.com) as the URL in the address bar. You should get a response back from one of the two test pages and if you load the page again, you should get the next server's test page. Note that the URL does not change, but the returned page will change alternately between servers.
430+
428431

429432
![screenshot of web1 being loaded and showing the second server test message](../images/haproxy_apache_lxd.png)
430433

0 commit comments

Comments
 (0)