How to develop microservices with a Vagrant, Ansible and Nginx
https://github.com/Vorago/painless-microservice-development
Painless microservicedevelopment
View Slide
Andrew Naydyonockandrew.naydyonockvoragovoragoSoftware EngineerTeam LeadTeacherJavaDevOps
Microservice developmentIssuesSolutionsAdvices
Usual problems
Not enough RAM
Selenium test won’t runwithout a tweak
Functional tests takeforever to finish
Infrastructure requiresmaintenance
Possible resolutions• Buy more RAM• Don’t run selenium on a local machine• Infrastructure maintenance on Friday morning• Give up
We actually bought RAM
But it did not solve ourproblems
Core conceptPainless microservice development
Service AService BFront endAll localLocal
Service AService BFront endVMAll in a VM
NginxService AService BFront endVMAll in a VM + Nginx
Where we are now
NginxService AService BFront endService AService BFront endVMOne service running from IDELocal
NginxService AService BMockFront endService AService BFront endVMHow about a mock?Local
NginxService AMockService BFront endService AService BFront endVMDeveloping Service BLocal
NginxService AMockService BMockFront endService AService BFront endVMFront endLocal
NginxService AService BFront endVMSeleniumLocal
Summary
Not enough RAMMock irrelevant services
Selenium requirestweaksProduction-like environment
Functional tests are slowPersonal Selenium Grid
InfrastructuremaintenanceAutomated provisioning
How to make it real?
ToolsAnsibleVagrantNginxWiremock
Ansible• Agentless• Idempotent• YAML-configuredBuild production-like VMsfor development
For both production and local environments--- - hosts: vagrant become: true become_user: root tasks: - name: Install nginx apt: name: nginx state: present - name: Autostart nginx service: name: nginx enabled: yes state: started
Build production-like VMsfor development• Easy to use• Light configuration• Works seamlessly with AnsibleVagrant
ansible-provision.shVagrantfileVagrant.configure('2') do |config| config.vm.box = 'ubuntu/trusty64' config.vm.synced_folder '.', '/vagrant' config.vm.network 'forwarded_port', guest: 80, host: 8080config.vm.provision :shell, path: 'bootstrap.sh' config.vm.provision :shell, path: 'ansible-provision.sh' end cd /vagrant/ansible ansible-playbook setup.yml -i inventory/local
• Reverse proxy• Reload config on the flyNginxWebserver
• Fluent API• LightweightWireMockJava framework for HTTPstubbingnew WireMockServer(8888).start(); WireMock.configureFor("localhost", 8888); stubFor(get(urlEqualTo("/hello")) .willReturn(aResponse() .withHeader("Content-Type", "text/plain") .withBody("Hello world!")));
A piece of cakegit pullvagrant up
Important bits
Service discoveryetcd + confd
Nginx configurationGo for configuration file per serviceBunch of includes in nginx.conf is easier to handle
DatabasesDeploy all databases inside a Vagrant
Databases & VagrantVagrant.configure('2') do |config| config.vm.box = 'ubuntu/xenial64' config.vm.synced_folder '.', '/vagrant' config.vm.provision 'shell', path: 'bootstrap.sh' config.vm.network 'forwarded_port', guest: 80, host: 8080 config.vm.network 'forwarded_port', guest: 5432, host: 5432 config.vm.provision :shell, path: 'ansible-provision.sh' end Don’t forget to forward your ports
VirtualBox dynamic portforwardingVBoxManage controlvm "VM name" natpf1 “postgres,tcp,,5432,,5432”VBoxManage controlvm "VM name" natpf1 delete postgres
Selenium gridHaving a selenium grid and a fistful of nodes willspeed up your testing
DockerUsing docker inside a Vagrant makes your Opstasks a breeze
Q & AProof of Conceptvorago/painless-microservice-development