Skip to content

Instantly share code, notes, and snippets.

@byjg
Created September 3, 2022 02:19
Show Gist options
  • Save byjg/e125e478a0562190176d69ea795fd3d4 to your computer and use it in GitHub Desktop.
Save byjg/e125e478a0562190176d69ea795fd3d4 to your computer and use it in GitHub Desktop.
Code applied in the EasyHAProxy screencasts

EasyHAProxy Docker

Terminal 1

docker network create easyhaproxy

docker run -d \
      --name easy-haproxy \
      --rm \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -e EASYHAPROXY_DISCOVER="docker" \
      -e EASYHAPROXY_LETSENCRYPT_EMAIL=change@please.me \
      -p 80:80 \
      -p 443:443 \
      -p 1936:1936 \
      --network easyhaproxy \
byjg/easy-haproxy:4.2.0


docker logs easy-haproxy --follow

Terminal 2

docker run -e TITLE="Container 1"  \
    --name container1 \
    --label easyhaproxy.http.host=test-docker.byjg.com \
    --label easyhaproxy.http.port=80 \
    --label easyhaproxy.http.localport=8080 \
    --network easyhaproxy \
    -d \
    --rm \
byjg/static-httpserver


docker stop container1

docker run -e TITLE="Container 2 Test"  \
    --name container2 \
    --label easyhaproxy.http.host=test-docker-2.byjg.com \
    --label easyhaproxy.http.port=80 \
    --label easyhaproxy.http.localport=8080 \
    --network easyhaproxy \
    -d \
    --rm \
byjg/static-httpserver

docker stop container2

EasyHAProxy Docker and Letsencrypt

Terminal 2

docker run -e TITLE="Container 1 Test"  \
    --name container1 \
    --label easyhaproxy.http.host=test-docker.byjg.com \
    --label easyhaproxy.http.port=80 \
    --label easyhaproxy.http.localport=8080 \
    --network easyhaproxy \
    -d \
    --rm \
byjg/static-httpserver

docker stop container1

docker run -e TITLE="Container 2 Letsencrypt"  \
    --name container2 \
    --label easyhaproxy.http.host=test-docker-2.byjg.com \
    --label easyhaproxy.http.port=80 \
    --label easyhaproxy.http.localport=8080 \
    --label easyhaproxy.http.letsencrypt=true \
    --network easyhaproxy \
    -d \
    --rm \
byjg/static-httpserver

docker stop container2

EasyHAProxy Static File

Preparation

This will create the service "outside" docker, because the ports are exposed.

docker network create --driver=bridge --subnet="172.19.0.0/16" --gateway="172.19.0.1" easyhaproxy

docker run -e TITLE="HTTP Service 1"  \
    --name container1 \
    -p 11350:8080 \
    -d \
    --rm \
byjg/static-httpserver


docker run -e TITLE="HTTP Service 2"  \
    --name container2 \
    -p 11360:8080 \
    -d \
    --rm \
byjg/static-httpserver

Create $PWD/static/config.yml

customerrors: true   # Optional (default false)

ssl_mode: default

easymapping:
  - port: 80
    hosts:
      test-static.byjg.com: 
        containers:
          - 172.19.0.1:11350
      test-static-2.byjg.com: 
        containers:
          - 172.19.0.1:11360

Run EasyHAProxy

docker run -d \
      --name easy-haproxy \
      --rm \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v $PWD/static:/etc/haproxy/static/ \
      -e EASYHAPROXY_DISCOVER="static" \
      -p 80:80 \
      -p 443:443 \
      -p 1936:1936 \
      --network easyhaproxy \
    byjg/easy-haproxy:4.2.0

EasyHAProxy - TCP + MySQL

Preparation

CREATE DATABASE demo;

use demo;

CREATE TABLE list (
   id int auto_increment key,
   name varchar(30)
);

insert into list (name) values ('List 1');
insert into list (name) values ('List 2');
insert into list (name) values ('List 3');
docker run -it -d \
    --network easyhaproxy \
    -e MYSQL_ALLOW_EMPTY_PASSWORD=true \
    --label easyhaproxy.mysql.mode=tcp \
    --label easyhaproxy.mysql.localport=3306 \
    --label easyhaproxy.mysql.port=3306 \
    --label easyhaproxy.mysql.host=mysql1.haproxy \
    --name mysql1 \
mysql:8.0

docker exec -it mysql1 mysql -u root # Run the SQL Here

docker run -it -d \
    --network easyhaproxy \
    -e MYSQL_ALLOW_EMPTY_PASSWORD=true \
    --label easyhaproxy.mysql.mode=tcp \
    --label easyhaproxy.mysql.localport=3306 \
    --label easyhaproxy.mysql.port=3306 \
    --label easyhaproxy.mysql.host=mysql2.haproxy \
    --name mysql2 \
mysql:8.0

docker exec -it mysql1.local mysql -u root # Run the SQL Here

docker stop mysql1    # You can stop and start to see EasyHAProxy find them 
docker stop mysql2    # You can stop and start to see EasyHAProxy find them

Running

docker run -d \
      --name easy-haproxy \
      --rm \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v $PWD/static:/etc/haproxy/static/ \
      -e EASYHAPROXY_DISCOVER="docker" \
      -p 3306:3306 \
      -p 1936:1936 \
      --network easyhaproxy \
    byjg/easy-haproxy:4.2.0
    
docker logs easy-haproxy --follow

EasyHAProxy as Ingress on Kubernetes

Preparation

helm repo add byjg https://opensource.byjg.com/helm
helm repo update byjg
helm search repo byjg

kubectl create namespace easyhaproxy

kubectl get nodes

kubectl label nodes easyhaproxy-demo  "easyhaproxy/node=master"

Install EasyHAProxy

helm upgrade --install ingress byjg/easyhaproxy \
    --namespace easyhaproxy \
    --set "easyhaproxy.letsencrypt.email=please@change.me" \
    --set resources.requests.cpu=100m \
    --set resources.requests.memory=128Mi

Install Services

kubectl create namespace demo

helm upgrade --install k8s-demo byjg/static-httpserver \
    --namespace demo \
    --set "ingress.hosts={test-k8s.byjg.com,test-k8s-2.byjg.com}" \
    --set parameters.title="Domain Parked" \
    --set parameters.htmlTitle="Domain Parked"

Kubernetes and Letsencrypt

It will patch the kubernetes example:

kubectl get ingress -n demo
kubectl annotate ingress k8s-demo-static-httpserver -n demo easyhaproxy.letsencrypt=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment