HY559 Services Tutorial
HY559 Services Tutorial Dimokritos Stamatakis dstamat@ics.forth.gr - - PowerPoint PPT Presentation
HY559 Services Tutorial Dimokritos Stamatakis dstamat@ics.forth.gr - - PowerPoint PPT Presentation
HY559 Services Tutorial Dimokritos Stamatakis dstamat@ics.forth.gr HY559 Services Tutorial VirtualBox network configuration Internal network configuration limits visibility to selected VMs only For wider visibility change network
HY559 Services Tutorial
VirtualBox network configuration
- Internal network configuration limits visibility to
selected VMs only
- For wider visibility change network configuration to
NAT, bridged, or host-only depending on your needs
HY559 Services Tutorial
NAT network configuration
- the simplest way of accessing an external network from
a VM
- (+) VMs can access the internet without any host
modifications
- (-) VMs are invisible from the internet and the host
– only port forwarding could fix that problem – all VMs get the same IP address and different ports, just like every NAT configuration
HY559 Services Tutorial
Bridged network configuration
- for more advanced networking needs - VirtualBox
connects to one of your installed network cards and exchanges network packets directly
- (+) each VM has a distinct IP address which potentially
remains the same on every boot
- (-) for this to work, VirtualBox needs a device driver on
your host system
HY559 Services Tutorial
Host-only network configuration
- Hybrid between the bridged and internal networking
- modes. Visibility is limited to VMs and host
- (+) each VM has a distinct IP address which potentially
remains the same on every boot
- (-) no internet access
HY559 Services Tutorial
VirtualBox network configuration options
- 1) Your application requires internet access all the time
– choose the NAT configuration through the Network menu – set up port forwarding, or (preferred) add a second network adapter in Host-Only mode through the Network menu
- enable network adapter and choose the Host-only method
– in case of adding a second network adapter consider the following
- append these two lines to the VM’s /etc/network/interfaces:
auto eth1 iface eth1 inet dhcp
- run /etc/init.d/networking restart
- reboot the VM
- you should see both eth0 and eth1 if you run ifconfig
HY559 Services Tutorial
VirtualBox network configuration options
- 2) Your application requires mainly VM/host access and
sometimes internet access
– choose host-only configuration through the Network menu
- when choosing this mode a distinct IP address is assigned to this
VM via DHCP and potentially remains the same on every boot
- when you need internet access, just terminate this VM and choose
NAT from the Network menu
– alternatively add two network adapters: a NAT and a Host-Only and don’t forget to consider the steps described before!
- 3) Your application requires internal (between VMs)
access
– follow the instructions of the first tutorial
http://www.csd.uoc.gr/~hy559/docs/spring12/slides/hy559-20120302-tutorial1.pdf
– assign the IP addresses as you like
VirtualBox network configuration
- recommended solution:
– install your preferred linux system with NAT network configuration selected – OR if you installed with internal network configuration place the following two lines to VM’s /etc/network/interfaces if they are not there:
auto eth0 iface eth0 inet dhcp
– run /etc/init.d/networking restart – use apt-get to install any packages you need
- you can also enable more than one network adapters
– don’t forget to consider the steps described before!
HY559 Services Tutorial
HY559 Services Tutorial
VirtualBox network configuration final steps
- finally make sure to install the Guest Additions if you
haven’t done so
- choose one of the suggested network configurations
described before, depending on your application
- you can now clone your VM (OR after the java
installation) and create VMs with preconfigured network
- don’t forget to edit the /etc/udev/rules.d/70-persistent-
net.rules file as described at the first tutorial about Virtualization on every new clone
HY559 Services Tutorial
Java - Apache tomcat installation
- install java [ sun java is recommended (not openjdk) ]
– Sun Java rather than openjdk especially for Apache Cassandra – in ubuntu there is no Sun Java6 anymore
- install openjdk OR install Oracle java7 OR install Sun java6 manually
- add the following to the end of each line at /etc/apt/
sources.list file:
non-free
- apt-get update
- apt-get install sun-java6-jdk
- apt-get install tomcat6
HY559 Services Tutorial
Java - Apache tomcat installation
- if you have issues finding those packages update your /
etc/apt/sources.list file by inserting those lines:
– deb http://ftp.gr.debian.org/debian/ squeeze main non-free – deb-src http://ftp.gr.debian.org/debian/ squeeze main non-free – deb http://security.debian.org/ squeeze/updates main non-free – deb-src http://security.debian.org/ squeeze/updates main non- free – # squeeze-updates, previously known as 'volatile' – deb http://ftp.gr.debian.org/debian/ squeeze-updates main non-free – deb-src http://ftp.gr.debian.org/debian/ squeeze-updates main non-free
HY559 Services Tutorial
Servlet deployment
- let’s create our first servlet
- create a folder named servlets to /var/lib/tomcat6/
webapps
- go to servlets folder, mkdir WEB-INF and cd there
– create a folder named lib
- place the jars needed by the application
– create a folder named classes
- place the .class files produced after javac AFile.java
– touch a web.xml file (it’s content will be described later)
HY559 Services Tutorial
Servlet deployment
- a Java servlet is a class that extends HTTPServlet
- must implement doGet() and doPost() methods
– those methods are called when a GET/POST request occurs
- receives client requests and responds with some output
through the response.getWriter()/ response.getOutputStream() stream
- In this demo we use the HelloServlet
– serves HTTP GET requests – responds back with the IP address of interface eth0
HY559 Services Tutorial
Servlet deployment– HelloServlet.java
- The HelloServlet source code should look like this:
HY559 Services Tutorial
Servlet deployment– HelloServlet.java
- javac HelloServlet.java
- if there is a problem with the javax.servlet classes:
– download the javax.servlet.jar from here:
– http://www.java2s.com/Code/JarDownload/javax/javax.servlet.jar.zip
– unzip it and place it to lib folder – add the absolute path of this jar to the CLASSPATH variable
- export CLASSPATH=$CLASSPATH:/var/lib/tomcat6/webapps/
servlets/WEB-INF/lib/javax.servlet.jar
- move the HelloServlet.class to classes folder
HY559 Services Tutorial
Servlet deployment– web.xml
- Add those lines to web.xml created before:
HY559 Services Tutorial
Servlet deployment – web.xml
- …where
– servlet-name is the servlet desired name – servlet-class is the name of the .class file (before the “.”)
- In case of packages you have to include them:
ServletA.java package package1; ... public class ServletA extends HTTPServlet {... web.xml ... <servlet-class> package1.ServletA </servlet-class> ...
– url-pattern is an alias for accessing the servlet
- note the “/” before the name
- this name could be different than the actual servlet name
HY559 Services Tutorial
Servlet deployment – accessing the servlet
- accessing the servlet
– Open a browser, type the VM IP address on port 8080, slash the folder created in webapps (in this demo it is servlets), slash the servlet url pattern we defined in web.xml
HY559 Services Tutorial
JSP deployment
- Java Server Pages (JSPs)
- dynamically generated web pages that are translated
into Servlets once (on first request)
- a high-level abstraction of Java Servlets
- basically used for the View role of MVC
HY559 Services Tutorial
JSP deployment
- Make a jsps folder at /var/lib/tomcat6/webapps
- create a new jsp file, say hello_jsp.jsp
HY559 Services Tutorial
JSP deployment– hello_jsp.jsp
- The hello_jsp.jsp file should look like this:
HY559 Services Tutorial
JSP deployment – accessing the JSP
- <% Java code %>
– this code is injected into the servlet’s _jspService method (analogous to doGet/doPost)
- <%! Java code %>
– this code is inserted into the body of the servlet class,
- utside of any existing methods
- <%= Java expression %>
– evaluated and inserted into the servlet’s output (analogous to out.print(expression’s result) )
HY559 Services Tutorial
JSP deployment – accessing the JSP
- accessing the jsp
– Open a browser, type the VM IP address on port 8080, slash the folder created in webapps (in this demo it is jsps), slash the jsp name
HY559 Services Tutorial
Load Balancer deployment
- use HAProxy : http://haproxy.1wt.eu/
- very fast and reliable solution for:
– High Availability – Load Balancing – Proxying
- for TCP and HTTP-based applications
- based on Level-7 load balancing
- can support tens of thousands of connections
- easy integration into existing architectures
- riskless: servers optionally not exposed to internet
HY559 Services Tutorial
Load Balancer deployment
- apt-get install haproxy
- open /etc/haproxy/haproxy.cfg and replace all the “listen”
entries with those lines:
listen http_proxy LISTEN-IP:8080 balance roundrobin
- ption httpchk
- ption forwardfor # This sets X-Forwarded-For
# # Define your servers to balance server SRV1-HOSTNAME SRV1-IP:8080 weight 1 maxconn 512 check server SRV2-HOSTNAME SRV2-IP:8080 weight 1 maxconn 512 check
HY559 Services Tutorial
Load Balancer deployment
- where:
– LISTEN-IP is the IP of the server that acts as a load balancer – then the port bound by the application – SRV{1,2}-HOSTNAME are the hostnames of the servers to balance – SRV{1,2}-IP are the IPs of the servers to balance – and finally the port bound by the application again
- weight sets server weight relative to other servers{0-256}
- maxconn sets the maximum per-process number of
concurrent connections
- check enables health checking. By default each server is
considered available
HY559 Services Tutorial
Load Balancer deployment
- if you want the load balancer to start with the init script
– open /etc/default/haproxy and set ENABLED=1
- run /etc/init.d/haproxy start
– /etc/init.d/haproxy status to check whether haproxy is running
- OR if you want to view debug messages start it like this
– haproxy –f /etc/haproxy/haproxy.cfg -d
HY559 Services Tutorial
Load Balancer deployment - test
- assuming you have accomplished the tomcat deployment...
- start two VMs running tomcat and add their hostnames and
IPs to /etc/haconf/haconf.cfg in the appropriate fields
- the VM that acts as load balancer does not have to run
tomcat, it should just have the port 8080 free
- open up a browser and type the URL consisting of the Load
Balancer VM IP (LISTEN-IP field to haconf.cfg), the port (8080) and the servlet/jsp location
- you should expect to get a response from one of those two
servers running tomcat
HY559 Services Tutorial
Load Balancer deployment - test
HY559 Services Tutorial
Load Balancer deployment - test
- notice that the load balancer successfully forwards the
entire client HTTP request to one of the backend servers
- the backend server that was chosen responds to the load
balancer and the latter forwards this to the client
- let’s take down the devian-vm2 VM and see what happens
HY559 Services Tutorial
Load Balancer deployment - test
HY559 Services Tutorial
Load Balancer deployment - test
- the load balancer now notices the debian-vm2 failure and
erases it from the backend servers list
- will not forward HTTP requests to that server anymore
- if a client’s request arrives before the server deletion then
a small delay is observed
- figure out an issue that this load balancer has
HY559 Services Tutorial
Load Balancer deployment - test
- an issue is that this load balancer is a single point of failure
– once the load balancer VM crashes, the whole system crashes
- client knows just the load balancer’s IP
- cannot reach the backend servers directly
- solution:
– replicate the load balancer to a number of replicas – only one will be active at a time – if the current active crashes, then another one undertakes it’s role – see corosync and pacemaker http://www.clusterlabs.org/
HY559 Services Tutorial
Load Balancer deployment - test
- changing backend servers list or other configs dynamically
- it is possible to do so from the command line
- haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy-
private.pid -sf $(</var/run/haproxy-private.pid)
- where the haproxy.cfg file contains the new server list
HY559 Services Tutorial
Managing VMs
- You can start/terminate/describe VirtualBox VMs via
command line
- VBoxManage startvm <VM name>
– starts VM with name VM name
- VBoxManage startvm <UUID>
– starts VM with id UUID
- VBoxManage controlvm <VM name> acpipowerbutton
– terminates VM with name VM name
- VBoxManage controlvm <UUID> acpipowerbutton
– terminates VM with id UUID
HY559 Services Tutorial
Managing VMs
- VBoxManage list runningvms
– returns a list with the running VMs (VM name, UUID)
- VBoxManage guestproperty get <VM name> "/
VirtualBox/GuestInfo/Net/0/V4/IP"
– returns the IP v4 of VM with name VM name
- VBoxManage guestproperty get <UUID> "/VirtualBox/