diff --git a/README.md b/README.md index e4df68a..7c6ac07 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,12 @@ microk8s.kubectl get pv,pvc microk8s.kubectl apply -f db.yml microk8s.kubectl logs -f pod/dbapp ``` -5. Test the database connection +5. Init and test the database connection ```bash microk8s.kubectl get all -mysql -u root -p -h dbservice.ip.address golf +echo "show tables;" | mysql -u root -p -h dbservice.ip.address golf +mysql -u root -p -h dbservice.ip.address gold < database/golf.sql +echo "show tables;" | mysql -u root -p -h dbservice.ip.address golf ``` 6. Fine-tune the ```DBHOST``` environment variable with the IP of the dbservvice and deploy your web application ```bash diff --git a/haproxy/Dockerfile b/haproxy/Dockerfile new file mode 100644 index 0000000..4b3afe3 --- /dev/null +++ b/haproxy/Dockerfile @@ -0,0 +1,17 @@ +FROM haproxy + +RUN apt-get update && apt-get install -y unzip wget rsyslog + +RUN wget https://releases.hashicorp.com/consul/1.6.1/consul_1.6.1_linux_amd64.zip && \ + unzip consul_1.6.1_linux_amd64.zip +RUN wget https://releases.hashicorp.com/consul-template/0.22.0/consul-template_0.22.0_linux_amd64.zip &&\ + unzip consul-template_0.22.0_linux_amd64.zip + +ADD service.json / +ADD docker-entrypoint.sh / +RUN chmod +x /docker-entrypoint.sh + +ADD rsyslog.conf /etc/ + +ADD haproxy.conf.ctmpl /tmp/haproxy.conf.ctmpl + diff --git a/haproxy/docker-entrypoint.sh b/haproxy/docker-entrypoint.sh new file mode 100644 index 0000000..a8ce610 --- /dev/null +++ b/haproxy/docker-entrypoint.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -e + +# first arg is `-f` or `--some-option` +if [ "${1#-}" != "$1" ]; then + set -- haproxy "$@" +fi + +if [ "$1" = 'haproxy' ]; then + shift # "haproxy" + # if the user wants "haproxy", let's add a couple useful flags + # -W -- "master-worker mode" (similar to the old "haproxy-systemd-wrapper"; allows for reload via "SIGUSR2") + # -db -- disables background mode + set -- haproxy -W -db "$@" +fi + +#/consul agent -join=consul --data-dir=/tmp/x --config-file=/service.json &> /dev/stdout + +/consul-template -consul-addr=consul:8500 -template="/tmp/haproxy.conf.ctmpl:/usr/local/etc/haproxy/haproxy.cfg:bash -c 'killall -9 haproxy; haproxy -f /usr/local/etc/haproxy/haproxy.cfg || true'" + +exec "$@" + diff --git a/haproxy/haproxy.conf.ctmpl b/haproxy/haproxy.conf.ctmpl new file mode 100644 index 0000000..c1a7bb8 --- /dev/null +++ b/haproxy/haproxy.conf.ctmpl @@ -0,0 +1,22 @@ +global + daemon + user nobody + group nogroup + maxconn 4096 + +defaults + mode http + timeout client 10s + timeout connect 10s + timeout server 10s + +listen http-in + bind *:9999 + stats enable + stats auth admin:devops + stats uri /haproxy + balance roundrobin + option httpchk HEAD /pi/3 HTTP/1.0 + + {{range service "pi"}} + server {{.Node}} {{.Address}}:{{.Port}} check inter 10s fastinter 1s rise 2 fall 2 weight 8{{end}} diff --git a/haproxy/rsyslog.conf b/haproxy/rsyslog.conf new file mode 100644 index 0000000..6cd0766 --- /dev/null +++ b/haproxy/rsyslog.conf @@ -0,0 +1,22 @@ +# Loads the imudp into rsyslog address space +# and activates it. +# IMUDP provides the ability to receive syslog +# messages via UDP. +$ModLoad imudp + +# Address to listen for syslog messages to be +# received. +$UDPServerAddress 0.0.0.0 + +# Port to listen for the messages +$UDPServerRun 514 + +# Take the messages of any priority sent to the +# local0 facility (which we reference in the haproxy +# configuration) and send to the haproxy.log +# file. +local0.* -/var/log/haproxy.log + +# Discard the rest +& ~ + diff --git a/haproxy/service.json b/haproxy/service.json new file mode 100644 index 0000000..860b6fd --- /dev/null +++ b/haproxy/service.json @@ -0,0 +1,6 @@ +{ + "service": { + "name": "haproxy", + "port": 9999 + } +} diff --git a/kubernetes/db.yml b/kubernetes/db.yml index 326659c..70a63dc 100644 --- a/kubernetes/db.yml +++ b/kubernetes/db.yml @@ -21,7 +21,7 @@ spec: - name: mysql-db image: mysql ports: - - name: mysql + - name: mysql:5.7 containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD