Tải bản đầy đủ (.pdf) (453 trang)

Docker and kubernetes for java developers

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.21 MB, 453 trang )


Docker and Kubernetes for Java Developers

Scale, deploy, and monitor multi-container applications


Jaroslaw Krochmalski

BIRMINGHAM - MUMBAI


Docker and Kubernetes for Java
Developers
Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a
retrieval system, or transmitted in any form or by any means, without the
prior written permission of the publisher, except in the case of brief
quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the
accuracy of the information presented. However, the information contained in
this book is sold without warranty, either express or implied. Neither the
author(s), nor Packt Publishing, and its dealers and distributors will be held
liable for any damages caused or alleged to be caused directly or indirectly by
this book.
Packt Publishing has endeavored to provide trademark information about all
of the companies and products mentioned in this book by the appropriate use
of capitals. However, Packt Publishing cannot guarantee the accuracy of this
information.

First published: August 2017



Production reference: 1240817


Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78646-839-0

www.packtpub.com


Credits

Author

Copy Editor

Jaroslaw Krochmalski

Safis Editing

Reviewer

Project Coordinator

Pierre Mavro


Kinjal Bari

Commissioning Editor

Proofreader

Vijin Boricha

Safis Editing


Acquisition Editor

Indexer

Prachi Bisht

Mariammal Chettiyar

Content Development Editor

Graphics

Trusha Shriyan

Kirk D'Penha

Technical Editor

Production Coordinator


Varsha Shivhare

Shantanu Zagade



About the Author
Jaroslaw Krochmalski is a passionate software designer and developer who
specializes in the financial domain. He has over 12 years of experience in
software development. He is a clean-code and software craftsmanship
enthusiast. He is a certified scrum master and a fan of Agile. His professional
interests include new technologies in web application development, design
patterns, enterprise architectures, and integration patterns.
He has been designing and developing software professionally since 2000
and has been using Java as his primary programming language since 2002. In
the past, he has worked for companies such as Kredyt Bank (KBC) and
Bank BPS on many large-scale projects, such as international money orders,
express payments, and collection systems. He currently works as a consultant
at Danish company 7N as an infrastructure architect for the Nykredit bank.
You can reach him via Twitter at @jkroch or by email at


About the Reviewer
Pierre Mavro lives in a suburb of Paris. He's an open source software lover
and has been working with Linux for more than 10 years now. Currently, he
is working as a lead SRE at Criteo, where he manages distributed systems
and NoSQL technologies. During the last few years, he has been designing
high-availability infrastructures, public and private cloud infrastructures, and
worked for a high-frequency trading company. He also wrote a book on

MariaDB named MariaDB High Performance. He’s also one of the cofounders of Nousmotards, an application for riders.


www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub
.com. Did you know that Packt offers eBook versions of every book published,
with PDF and ePub files available? You can upgrade to the eBook version at
www.PacktPub.com and as a print book customer, you are entitled to a discount
on the eBook copy. Get in touch with us at for more
details. At www.PacktPub.com, you can also read a collection of free technical
articles, sign up for a range of free newsletters and receive exclusive
discounts and offers on Packt books and eBooks.

/>
Get the most in-demand software skills with Mapt. Mapt gives you full
access to all Packt books and video courses, as well as industry-leading tools
to help you plan your personal development and advance your career.


Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser


Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our
editorial process. To help us improve, please leave us an honest review on
this book's Amazon page at />If you'd like to join our team of regular reviewers, you can e-mail us at
We award our regular reviewers with free eBooks

and videos in exchange for their valuable feedback. Help us be relentless in
improving our products!


Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions

1.

Introduction to Docker
The idea behind Docker
Virtualization and containerization compared
Benefits from using Docker
Docker concepts - images and containers
Images
Layers
Containers
Docker registry, repository, and index
Additional tools

Installing Docker
Installing on macOS
Installing on Linux
Installing on Windows
Summary

2.

Networking and Persistent Storage
Networking
Docker network types
Bridge
Host
None
Networking commands


Creating and inspecting a network
Connecting a container to the network
Exposing ports and mapping ports
Persistent storage
Volume-related commands
Creating a volume
Removing a volume
Volume drivers
Summary

3.

Working with Microservices

An introduction to microservices
Monolithic versus microservices
The monolithic architecture
The microservices architecture
Maintaining data consistency
The Docker role
Kubernetes' role
When to use the microservice architecture
Summary

4.

Creating Java Microservices
Introduction to REST
HTTP methods
REST in Java
Java EE7 - JAX-RS with Jersey
JAX-RS annotations
Spring Boot
Coding the Spring Boot microservice
Maven build file
Application entry point
Domain model and a repository
REST controller
Documenting the API
Running the application
Making calls
Spring RestTemplate
HTTPie
Postman



Paw for Mac
Spring Initializr
Summary

5.

Creating Images with Java Applications
Dockerfile
Dockerfile instructions
FROM
MAINTAINER
WORKDIR
ADD
COPY
RUN
CMD
The ENTRYPOINT
EXPOSE
VOLUME
LABEL
ENV
USER
ARG
ONBUILD
STOPSIGNAL
HEALTHCHECK
Creating an image using Maven
Building the image

Creating and removing volumes
Summary

6.

Running Containers with Java Applications
Starting and stopping containers
Starting
Stopping
Listing the running containers
Removing the containers
Container running modes
Foreground
Detached


Attaching to running containers
Monitoring containers
Viewing logs
Inspecting a container
Statistics
Container events
Restart policies
no
always
on-failure
unless-stopped
Updating a restart policy on a running container
Runtime constraints on resources
Memory

Processors
Updating constraints on a running container
Running with Maven
Plugin configuration
Starting and stopping containers
Summary

7.

Introduction to Kubernetes
Why do we need Kubernetes?
Basic Kubernetes concepts
Pods
ReplicaSets
Deployment
Services
kube-dns
Namespace
Nodes
Kubelet
Proxy
Docker
The Master node
etcd
The API server
The scheduler
Available tools


kubectl

Dashboard
Minikube
Summary

8.

Using Kubernetes with Java
Installing Minikube
Installing on Mac
Installing on Windows
Installing on Linux
Starting up the local Kubernetes cluster
Installing kubectl
Installing on Mac
Installing on Windows
Installing on Linux
Deploying on the Kubernetes cluster
Creating a service
Creating a deployment
Interacting with containers and viewing logs
Scaling manually
Autoscaling
Viewing cluster events
Using the Kubernetes dashboard
Minikube addons
Cleaning up
Summary

9.


Working with the Kubernetes API
API versioning
Alpha
Beta
Stable
Authentication
HTTP basic auth
Static token file
Client certificates
OpenID
Authorization
Attribute-based access control


Role-based access control (RBAC)
WebHook
AlwaysDeny
AlwaysAllow
Admission control
Using the API
API operations
Example calls
Creating a service using the API
Creating a deployment using the API
Deleting a service and deployment
Swagger docs
Summary

10.


Deploying Java on Kubernetes in the Cloud
Benefits of using the cloud, Docker, and Kubernetes
Installing the tools
Python and PIP
AWS command-line tools
Kops
jq
Configuring Amazon AWS
Creating an administrative user
Creating a user for kops
Creating the cluster
DNS settings
Root domain on AWS hosted domain
The subdomain of the domain hosted on AWS
Route 53 for a domain purchased with another registrar
Subdomain for cluster in AWS Route 53, the domain elsewhere
Checking the zones' availability
Creating the storage
Creating a cluster
Starting up clusters
Updating a cluster
Installing the dashboard
Summary

11.

More Resources


Docker

Awesome Docker
Blogs
Interactive tutorials
Kubernetes
Awesome Kubernetes
Tutorials
Blogs
Extensions
Tools
Rancher
Helm and charts
Kompose
Kubetop
Kube-applier


Preface
Imagine creating and testing Java EE applications on Apache Tomcat or
Wildfly in minutes, along with deploying and managing Java applications
swiftly. Sounds too good to be true? You have a reason to cheer, because
such scenarios are possible by leveraging Docker and Kubernetes.
This book will start by introducing Docker and delve deep into its networking
and persistent storage concepts. You will be then introduced to the concept of
microservices and learn how to deploy and run Java microservices as Docker
containers. Moving on, the book will focus on Kubernetes and its features.
You will start by running the local cluster using Minikube. The next step will
be to deploy your Java service in the real cloud, on Kubernetes running on
top of Amazon AWS. At the end of the book, you will get hands-on
experience of some more advanced topics to further extend your knowledge
of Docker and Kubernetes.



What this book covers
Introduction to Docker, introduces the reasoning behind Docker and
presents the differences between Docker and traditional virtualization. The
chapter also explains basic Docker concepts, such as images, containers, and
Dockerfiles.
Chapter 1,

Networking and Persistent Storage, explains how networking and
persistent storage work in Docker containers.
Chapter 2,

Working with Microservices, presents an overview of what
microservices are and explains their advantages in comparison to monolithic
architectures.
Chapter 3,

Creating Java Microservices, explores a recipe for quickly
constructing Java microservice, by utilizing either Java EE7 or the Spring
Boot.
Chapter 4,

Creating Images with Java Applications, teaches how to package the
Java microservices into Docker images, either manually or from the Maven
build file.
Chapter 5,

Running Containers with Java Applications, shows how to run a
containerized Java application using Docker.

Chapter 6,

Introduction to Kubernetes, introduces the core concepts of
Kubernetes, such as Pods, nodes, services, and deployments.
Chapter 7,

Using Kubernetes with Java, shows how to deploy Java
microservices, packaged as a Docker image, on the local Kubernetes cluster.
Chapter 8,

Working with Kubernetes API, shows how the Kubernetes API can
be used to automate the creation of Kubernetes objects such as services or
deployments. This chapter gives examples of how to use the API to get
information about the cluster's state.
Chapter 9,


Deploying Java on Kubernetes in the Cloud, shows the reader how
to configure Amazon AWS EC2 instances to make them suitable to run a
Kubernetes cluster. This chapter also gives precise instructions on how to
create a Kubernetes cluster on the Amazon AWS cloud.
Chapter 10,

More Resources, explores how Java and Kubernetes point the
reader to additional resources available on the internet that are of high
quality, to further extend knowledge about Docker and Kubernetes.
Chapter 11,


What you need for this book

For this book, you will need any decent PC or Mac, capable of running a
modern version of Linux, Windows 10 64-bit, or macOS.


Who this book is for
This book is for Java developers, who would like to get into the world of
containerization. The reader will learn how Docker and Kubernetes can help
with deployment and management of Java applications on clusters, either on
their own infrastructure or in the cloud.


×