Skip to content

Commands for learning Docker and Kubernetes for Java applications

Notifications You must be signed in to change notification settings

dawand/docker_kubernetes_java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docker

Running a containerized Java application

docker run milkyway/java-hello-world

Network and ports

List network types

docker network ls

Create a new network

docker network create myNetwork

inspect a network

docker network inspect myNetwork

Run Apache Tomcat for the newly created network

docker run -it --name myTomcat --net=myNetwork tomcat

Run a busybox with the same network as myTomcat container

docker run -it --net container:myTomcat busybox

Check if we can reach the tomcat webserver from the busybox

wget localhost:8080

Bind your host port to the container

docker run -it --name myTomcat2 --net=myNetwork -p 8080:8080 tomcat

Map a random port to the exposed container port

docker run -it --name myTomcat3 --net=myNetwork -P tomcat

Find out the randomly allocated port

docker port myTomcat3 OR check in: docker inspect myTomcat3

Volumes

Specify a local path for container volume

Windows: docker run -v d:/docker_volumes/volume1:/volume -it busybox

Mac:docker run -v ~/volume1:/volume -it busybox

Create a standalone named volume

docker volume create --name myVolume

Map the volume to containers

docker run -it -v myVolume:/volume --name myBusybox3 busybox

docker run -it -v myVolume:/volume --name myBusybox4 busybox

OR use -volumes-from

docker run -it -volumes-from myBusybox4 --name myBusybox5 busybox

Remove a volume

docker volume rm myVolume

Dockerfile

Build the docker with the Dockerfile provided in this repository

docker build . -t rest-example

Run the newly built image (rest-example)

docker run -p 8080:8080 -it rest-example

Build the ping-example container from Dockerfile

cd ping-example

docker build -t ping-example .

docker run ping-example

for sending ping to localhost

docker run ping-example www.google.com

to ping Google

Run container in detached mode

docker run -d -p 8080:8080 rest-example

Attach to the detached container

docker attach rest-example

View logs of the running container

docker logs -f rest-example

Specify the log driver to write logs to

docker run log-driver=syslog rest-example

You can use journald, splunk, fluentd, awslogs, etc

Inspect a container

docker inspect rest-example

Inspect containers using Go template engine

docker inspect -f '{{.State.ExitCode}}' jboss.wildfly

Fetch the IP address from the metadata of the container

docker inspect rest-example | jq -r '.[0].NetworkSettings.IPAddress'

Get stats of all (not only running) the containers

docker stats -a

Fetch container events

docker events

Restart a container after it is shut down (in every case)

docker run --restart=always rest-example

Restart a container after it is shut down (with a non-zero exit status)

docker run --restart=on-failure:5 rest-example

Get the number of restarts of a container

docker inspect -f "{{ .RestartCount }}" rest-example

Discover the last time the container was started again

docker inspect -f "{{ .State.StartedAt }}" rest-example

Updating a restart policy on a running container

docker run -d -t rest-example

docker update --restart=always rest-example

Restrict the memory to 512 MB for the container

docker run -it -m 512m ubuntu

Set memory reservation without setting the hard memory limit

docker run -it --memory-reservation 1G ubuntu /bin/bash

Set the Kernel memory limit

docker run -it --kernel-memory 100M ubuntu /bin/bash

Setting user memory and kernel memory for a container

docker run -it -m 1G --kernel-memory 100M ubuntu /bin/bash

Set memory swappiness constraint for a container (0 to disable swaps, 100 all anonymous pages to swap out)

docker run -it --memory-swappiness=0 ubuntu /bin/bash

Set the CPU usage limit for a container to 50% of the CPU resource

docker run -it --cpu-quota=50000 ubuntu /bin/bash

Constraint the container to get 50% of the CPU usage every 50ms

docker run -it --cpu-quota=25000 --cpu-period=50000 ubuntu /bin/bash

Set the number of CPU cores to be used by the container

docker run -it --cpuset 2 ubuntu

Updating constrains of a running container

docker update --cpu-shares 512 -m 500M rest-example

Java Microservices

Maven Automation

Using Maven to generate Docker images (without Dockerfile)

cd dokuja

mvn clean package docker:build

Using Maven to start a container

mvn clean package docker:start

Or to stop the container

mvn docker:stop

Using Maven to run a container

mvn clean package docker:run

Kubernetes

Minikube

Install Minikube to use Kubernetes locally

Mac: brew cask install minikube

Linux: curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \ && chmod +x minikube sudo cp minikube /usr/local/bin && rm minikube

Windows: choco install minikube kubernetes-cli

Start minikube

minikube start

Show Kubernetes dashboard

minikube dashboard

List the available minikube addons

minikube addons list

disable an addon

minikube addons disable dashboard

enable an addon

minikube addons enable heapster

Kubectl

Install kubectl on your platform

Mac: brew install kubernetes-cli Linux: sudo snap install kubectl --classic Windows: curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/windows/amd64/kubectl.exe

Test if minikube and kubectl are correctly installed

kubectl get nodes

Services

Create a service for the rest-example app

kubectl create -f service.yml

Display the services

kubectl get services

Display the services of all the namespaces

kubectl get services --all-namespaces

Display the details of a particular service

kubectl describe service rest-example

Deployments

Create a deployment for the rest-example app

kubectl create -f deployment.yml

Display all the eployments

kubectl get depployments

Get the service address

minikube service rest-example --url

Test if you can get the list of books

curl $(minikube service dokuja --url)/books -H {"Content-Type: application/json"}

Read the logs of the running container

kubectl logs $(kubectl get pods | sed -n 2p | awk '{print $1;}')

Get the shell of the running container

kubectl exec -it $(kubectl get pods | sed -n 2p | awk '{print $1;}') -- /bin/bash

ReplicaSets

Scaling deployments manually

kubectl scale deployment dokuja --replicas=3

Automatically scaling up and down the deployment

kubectl autoscale deployment dokuja --cpu-percent=50 --min=1 --max=5

Events

Viewing cluster events

kubectl get events

Cleaning up

Delete a deployment

kubectl delete deployment dokuja

Delete a service

kubectl delete service dokuja

API-Server

Setup a proxy for kubectl to communicate with minikube

kubectl proxy --port=8080

In a separate bash session:

Check if our api-server is running

curl http://localhost:8080/api/

Create a service using api-server

curl -s http://localhost:8080/api/v1/namespaces/default/services \

-XPOST -H 'Content-Type: application/json' -d@service.json

Create a deployment using api-server

curl -s http://localhost:8080/apis/extensions/v1beta1/namespaces/default/deployments \

-XPOST -H 'Content-Type: application/json' -d@deployment.json

Check the running pod

curl -X GET http://localhost:8080/api/v1/namespaces/default/pods

Delete a deployment using curl

curl http://localhost:8080/apis/extensions/v1beta1/namespaces/default/deployments -XDELETE

Delete the service using curl

curl http://localhost:8080/api/v1/namespaces/default/services/dokuja -XDELETE

About

Commands for learning Docker and Kubernetes for Java applications

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published