Here is the step by step instructions to host a highly available website.
- Create an account in dockerhub
- Machine with Jenkins up and running
- An agent with docker running
- Install the
git
andjava
in Agent - Add this Agent to Jenkins with the label called
docker
- Edit the Agent's Security Group Inbound rules by allowing traffic on 8080, 8081, 8082 custom ports.
Execute the following on agent
- Create a directory called
nginx
and change into it. - Create a file called
nginx.conf
with following contents
upstream backend {
server GATEWAYIP:8081;
server GATEWAYIP:8082;
}
server {
location / {
proxy_pass http://backend;
}
}
- Get the Docker default gateway IP by running below command on docker agent.
GATEWAY=$(docker network inspect bridge | grep -i gateway | awk '{print $2}' | sed -e 's/,$//' -e 's/^"//' -e 's/"$//')
- Replace the GATEWAYIP with the gateway ip.
sed -i 's/GATEWAYIP/'"$GATEWAY"'/g' nginx.conf
- Confirm the GATEWAYIP is replaced with the actual ip address in nginx.conf.
- Create a Dockerfile with following content
FROM nginx
LABEL owner=loki
LABEL desc="Nginx LoadBalancer"
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
- Create the docker image. Pls replace the registry with your registry
docker build -t lokeshkamalay/nginx:latest .
- Store the image in
Dockerhub
docker push lokeshkamalay/nginx:latest
- Setup a Jenkins Pipeline job with
Pipeline Script from SCM
- Choose repo as
https://github.com/lokeshkamalay/tomcat_maven_app.git
- Ensure the agent running docker is configured with the label
docker
- Create a credential with the name of
docker-hub
and input docker hub username and password. - Run the Job