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 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 HY559 Services Tutorial
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
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, outside 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 � option httpchk � option 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
Recommend
More recommend