docker for java developers
TRANSCRIPT
Docker for Java Developers4th Anniversary Java Colombo Meetup
Image Credit: Jordan Novet/VentureBeat
Agenda - Session 1● Introduction to Docker
● Why Docker is Important?
● Let’s Run Docker
● Docker Demo
● Why Need of Docker Cluster?
● Brief Introduction to K8S
● Kubernetes DemoBy Lakmal Warusawithana Director Cloud Architecture ,WSO2 IncVice President - Apache Stratoshttp://twitter.com/lakwarus
Why Docker?● Achieve agility and control for Development and IT Operations
teams to build, ship, and run any app, anywhere
https://www.docker.com
How to setup Docker on Linux● curl -sSL https://get.docker.com/ | sh
● Docker client, the Docker daemon, and
any containers run directly on your
localhost
● Can address ports on a Docker
container using standard localhost
addressing such as localhost:PORT or
Container-IP:PORT
How to setup Docker on Mac● Goto https://docs.docker.
com/engine/installation/mac/
● Docker daemon is running inside a
Linux VM called default
● The default is a lightweight Linux VM
(~24MB download, and boots in
approximately 5s)
How to setup Docker on Windows● Goto https://docs.docker.
com/engine/installation/windows/
● Docker daemon is running inside a
Linux VM called default
● The default is a lightweight Linux VM
(~24MB download, and boots in
approximately 5s)
Why Need of Docker Cluster?● Avoid single point of failure
● Make horizontally scalable
● Have more granular management for distributed applications
(microservices)
● Self healing systems
What is Kubernetes?● Manage a cluster of Linux containers as a single system to
accelerate Dev and simplify Ops.
● Initiated by Google
● Massive community support
Kubernetes Architecture
Node1 Node2 Node n
Physical Network
Master
Overlay Network (Flannel/OpenVSwitch/Weave)A
PI S
erve
rSc
hedu
ler
Con
trol
ler M
anag
eret
cd
Key Concepts of Kubernetes● Pod - A group of Containers
● Labels - Labels for identifying pods
● Proxy/Service - A load balancer for Pods
● etcd - A metadata service
● cAdvisor - Container Advisor provides resource
usage/performance statistics
● Replication Controller - Manages replication of pods
● Scheduler - Schedules pods in worker nodes
● API Server - Kubernetes API server
Pet Store Sample - Deployment View
https://github.com/wso2/msf4j/tree/master/samples/petstore/deployment
Agenda - Session 2● Running a Microservice JAR in a Docker
container● Building a Docker image with Maven● Running a Java server application in a
Docker container● Talking to the Docker API in Java● Next generation continuous delivery
with Jenkins & Docker● Deploying Java server applications in
production
Imesh GunaratneSenior Technical Lead, WSO2
Evolution of SOA
http://www.pwc.com/us/en/technology-forecast/2014/cloud-computing/features/microservices.html
What is a Microservice?● “Microservice is an architectural style for developing a single
application as a suite of small services, each running in its
own process and communicating with lightweight mechanisms,
often an HTTP resource API” - Martin Fowler
http://martinfowler.com/articles/microservices.html
WSO2 MSF4J Hello World Samplepackage org.wso2.msf4j.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
/**
* Hello service resource class.
*/
@Path("/hello")
public class HelloService {
@GET
@Path("/{name}")
public String hello(@PathParam("name") String name) {
return "Hello " + name;
}
}
WSO2 MSF4J Hello World Sample
git clone https://github.com/wso2/msf4j.git
# Build msf4j, this requires JDK 1.8
cd msf4j/
mvn clean install
# Build hello world sample
cd samples/helloworld/
mvn clean install
# Run self contained JAR
java -jar target/helloworld-*.jar
curl http://localhost:8080/hello/java-colombo
How to run a Microservice JAR in a Docker Container>docker run \
# Mount local folder to container
-v ~/dev/wso2/msf4j/samples/helloworld/target/:/tmp/ \
# Ports to be exposed
-p 8080:8080 \
# Docker image tag
-t java \
# Command to be run
java -jar /tmp/helloworld-1.0.0-SNAPSHOT.jar
Docker Maven Plugins● Spotify Docker Maven Plugin
○ https://github.com/spotify/docker-maven-plugin
● Fabric8io Docker Maven Plugin
○ https://github.com/fabric8io/docker-maven-plugin
● Alex Collins Docker Maven Plugin
○ https://github.com/alexec/docker-maven-plugin
● Wouter Danes Docker Maven Plugin
○ https://github.com/wouterd/docker-maven-plugin
Using Spotify Docker Maven PluginAdd Docker Maven plugin to the pom.xml file:<build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.0</version> <configuration> <imageName>helloworld</imageName> <baseImage>java</baseImage> <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint> <!-- copy the service's jar file from target into the root directory of the image --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins></build>
Using Spotify Docker Maven Plugin
1. Build Hello World Docker image using Maven:>mvn clean package docker:build
2. List Docker images:>docker images
3. Run Hello World Docker container:>docker run -p 8080:8080 helloworld
Running Tomcat in DockerFROM java:8-jre
ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME
...
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.0.32
ENV TOMCAT_TGZ_URL https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-
tomcat-$TOMCAT_VERSION.tar.gz
RUN set -x \
&& curl -fSL "$TOMCAT_TGZ_URL" -o tomcat.tar.gz \
&& curl -fSL "$TOMCAT_TGZ_URL.asc" -o tomcat.tar.gz.asc \
&& gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz \
&& tar -xvf tomcat.tar.gz --strip-components=1 \
&& rm bin/*.bat \
&& rm tomcat.tar.gz*
EXPOSE 8080
CMD ["catalina.sh", "run"]
Running Tomcat in Docker● Dockerfile:
○ https://github.com/docker-library/tomcat
● Run Tomcat in Docker container:○ docker run -it --rm -p 8888:8080 tomcat:8.0
Docker API Clients for Java● Docker Java API
○ https://github.com/docker-java/docker-java
● Docker Client○ https://github.com/spotify/docker-client
● jClouds Docker API Client○ https://github.com/jclouds/jclouds-labs/tree/master/docker
● Other API Clients:● https://docs.docker.com/engine/reference/api/remote_api_client_libraries/
Docker Java Client Sample [docker images cmd]
https://github.com/docker-java/docker-java/wiki
DockerClientConfig config = DockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://192.168.99.100:2376").build();DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
ListImagesCmd cmd = dockerClient.listImagesCmd();System.out.println("Executing docker images command...");List<Image> images = cmd.exec();for(Image image : images) { System.out.println(new Date(image.getCreated() * 1000) + " - " + image.getId() + " - " + image.getRepoTags()[0] + " - " + image.getVirtualSize());}
Docker Java Client Sample [docker run cmd]
https://github.com/docker-java/docker-java/wiki
DockerClientConfig config = DockerClientConfig.createDefaultConfigBuilder() .withDockerHost("tcp://192.168.99.100:2376").build();DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
String imageTag = "tomcat:8.0";System.out.println("Starting container: " + imageTag);CreateContainerResponse container = dockerClient.createContainerCmd(imageTag).exec();dockerClient.startContainerCmd(container.getId()).exec();
Jenkins Pipeline Plugin
http://devops.com/2016/01/21/12670/
Formerly known as the Workflow plugin. Originally inspired by the Build Flow Plugin.
Problems with Standalone Docker
● Limited scalability● Potential risk of Single Point of
Failure (SPOF)
Apache Mesos Architecture
https://www.digitalocean.com/community/tutorials/an-introduction-to-mesosphere
Deploying Tomcat in Kubernetes [1]
https://github.com/imesh/docker-for-java/tree/master/kubernetes
Node IP: 172.17.8.102
Port: 8080
Domain Name: tomcatIP: 10.2.10.20
Port: 8080NodePort: 32001
Protocol: TCP
Pod 1 Pod 2 Pod n
Tomcat Service
L1
L1 L1 L1
Node
Tomcat Replication Controller
Deploying Tomcat in Kubernetes [2]
https://github.com/imesh/docker-for-java/tree/master/kubernetes
apiVersion: v1kind: ReplicationControllermetadata: name: tomcat labels: name: tomcatspec: replicas: 1 selector: name: tomcat template: metadata: labels: name: tomcat spec: containers: - name: tomcat image: tomcat:8.0 ports: - containerPort: 8080 protocol: "TCP"
apiVersion: v1kind: Servicemetadata: labels: name: tomcat name: tomcatspec: type: NodePort sessionAffinity: ClientIP ports: # ports that this service should serve on - name: 'http' port: 8080 targetPort: 8080 nodePort: 32001 # label keys and values that must match in order to receive traffic for this service selector: name: tomcat
Tomcat ServiceTomcat Replication Controller