gradle build
docker build -t sample_api_img .
docker run --rm -p 8080:8080 sample_api_img
docker login
docker tag sample_api_img $DOCKER_USER/sample_api:dev
docker push $DOCKER_USER/sample_api:dev
docker run --rm -p 8080:8080 $DOCKER_USER/sample_api:dev
Here we will configure AWS to scale out server automaticaly when load is increassed and scale in when load in descresed. In order to achive it we will create instance image which will be used and base image to setup new instances. Then setup loadbalancer to balance traffic between all instances.
Create two groups one to controll load balancer traffic and another to controll web tire traffic.
- Create group for Load Balancer
- Click
Create Security Groupand set name for load balancer group Add Rulefor inbound trafficAdd Rulefor outbound traffic- Click
Create.
- Click
- Create group for Web Tire
- Click Click
Create Security Groupand set name for web-tire Add Rulefor inbound traffic (http)Add Rulefor ssh port 22- Input load balance security
Group Namein fieldSourceto restrict traffic for balancer only
- Click Click
Private public key pair is used to access AWS linux based instances. And created under Services > EC2 > Key Pairs and click "Create Key Pair".
First lets create instance which will be be used to create image. Then this image is used to created other instances.
Navigate to Services > EC2 > Instances and click Launch Instance.
- Select server. For this example we are going to use
Ubuntu Serverand instabce typet2.microwhich is available in trial account. - Click
Configure Instance Details - Select availability zone as subnet aattribute (e.g. zone a)
- Left all other parameters with default values
- Clicke "Next" here we left default paramaters.
- Click "Next" and add tag to mark this instance (e.g. 'sample-ami')
- Click "Next" and choose sequrity group created earlier.
- Click
Review and Launch - Select creates earlier
key pairandLaunchinstance - Open instance and give name
- ssh to created instance
$ sudo ssh -i "./sample-key.pem" ubuntu@instance_public_dns
- Install Docker by commanda below or follw instraction on
https://www.docker.com/
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
apt-cache policy docker-ce
sudo apt-get install -y docker-ce
- Login to your docker account
docker login
After that docker file with credentials will be stored in ~/.docker/config.json.
sudo vim /etc/rc.local
- Add run commands just befor
exit 0
su - ubuntu
export DOCKER_CONFIG=/home/ubuntu/.docker/
export SAMPLE_APP_ENV=dev
export DOCKER_CONFIG=/home/ubuntu/.docker/
docker pull maksymenko/sample_api:$SAMPLE_APP_ENV
docker run -d --rm -p 8080:8080 $DOCKER_USER/sample_api:$SAMPLE_APP_ENV
Just left this file here so that all instances will have this file shen new instance created to scake uot application.
Navigate to Services > EC2 > Instances select instance created above.
- Click
Actionand selectImage>Create Image - Give name of image (e.g.:
sample-image) - Click
Create Image - Go yo
AMIsection to see that image creation is in progress, it take some time for new AMI to show up.
- Go to
AMIsection \ - Choose
AMIcreated above - Click
Launch - Click
Configure Instance Details - Select availability zone as subnet aattribute (e.g. zone a)
- Left all other parameters with default values
- Go to
Advanced Detailssection and specify bash command which will be executes when instance created. Here we are going to specify custom bash commands tspecific for instance.
#!/bin/bash
- Click
Next - Choose
Security Groupassample-web-tire - Click
Launch - Use log file to debug script behavir
/var/log/cloud-init-output.log/var/log/syslog
- Find public DNS of created instance and point browser to port 8080
http://{instance_public_dns}:8080
- Navigate to
Services > EC2 > Load Balancersand clickCreate Load Balancer - Choose
Application Load Balancerand clickCreate - Give name (e.g. sample-app-loadbalancer)
- Set port 8080 for http as our application uses port 8080
- Add
httpsis recommended for production usage (here we can skip it) - Select availability zones, lets choose at least to zones
aand zonebfor system reliability. - Define tags to mark balancer.
- Click
Nextand agailNextto chooseSecurity Groupcreated above. - Selected existing security group created to controll loadbalancer traffic.
- Click
Nextto configure routing and set port 8080 - Create
new target groupto which load balancer will send traffic. - Give name to group and click
Nexttoregister targetsinstances - Select instances and
add to registeredon port 8080. - Click
Nextto review andCreate
In order to setup autoscaling we have to define two components "Launch Configuration" and "Autoscaling Group".
- Open section
Services > EC2 > AUTO SCALING > Launch Configurationsand clickCreate Auto Scaling groupthenCreate launch configuration. - Choose base image. As we created earlier our preconfigured instance, click "My AMIs" and select image.
- Select instance type and click "Next".
- Give name to launch configuration e.g. "Sample web-app launch configuration".
- Click "Next" then again "Next"
- Select existing security group (created above for web application) and click
Review. - Click
Create launch configurationand choose key for ssh access.
- Give name for group
- Select group size (put 2 to have one instance in two different availability zones)
- Choose Subnet (availability zones) let choos
aandbzones which were selected for load balancer. - Go to
Advanced Detailsand select that group will select traffic from one or more load balancer. - Then select name of target group created for load balancer, and click
Next - Define scaling policy (when and how to scale application), and click
Next - Configure notification as needed and click
Next - Add tegs to ad some metadate to created resource, and click
Review. - Click
Create Autoscaling Groupfor complete process andCloseto come back to autoscaling home page.
As now we have aouto scaling group we can remove instances created earlier.
TODO: complete aoute scaling configuration step
- Load balancer Security group http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-groups.html#elb-vpc-security-groups
- health check troubleshoot http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/ts-elb-healthcheck.html#ts-elb-healthcheck-failed-vpc
- http://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-application-load-balancer.html#alb-configure-routing
- https://aws.amazon.com/premiumsupport/knowledge-center/troubleshoot-unhealthy-checks-ecs/
- https://docs.oseems.com/general/operatingsystem/linux/automatically-run-program-on-startup
- http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
- http://docs.aws.amazon.com/AmazonECS/latest/developerguide/private-auth.html
- https://docs.docker.com/engine/reference/commandline/login/#login-to-a-self-hosted-registry