Simulation of K8s / OpenShift init container within Docker Compose.
Note that some support from docker images is required - they should not use "complex" entrypoints,
preventing overridden docker container command to intercept start of container with helpers.
Idea is to use the same images for init container and pod container b/w K8s / OpenShift and
Docker Compose. Docker Compose project uses additional image sharing helpers which are used
to intercept end of init container work and start of pod container to implement awaiting
of pod container for completion of work of init container.
Building
Building Requirements
JDK 1.8+
Docker 1.12+
If remote Docker instance is used then DOCKER_HOST environment variable should point to that
engine and include schema, like tcp://docker-host:2375 instead of docker-host:2375.
Current directory is directory where this repository is cloned
Docker Compose is installed and has access to Docker
Project is built (refer to "Building" section) using the same Docker instance
which Docker Compose connects
Subdomain name of application FQDN is defined by app_subdomain environment variable
Current directory is directory where this repository is cloned
Name of Docker Compose project is defined by compose_project environment variable
e.g.
app_subdomain='app'&& \
compose_project='dcic'
Docker Compose Testing Steps
Refer to docker-compose directory for Docker Compose project.
Create and start containers
docker-compose -p "${compose_project}" -f docker-compose/docker-compose.yml up -d
If there is a need to deploy with JaCoCo agent turned on,
then use this command instead (adds JAVA_OPTIONS environment variable which is picked and used
by Docker Compose project)
If deployed with JaCoCo agent turned on then check coverage using this command
(note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in ${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html file is report entry point).
Stop and remove containers
docker-compose -p "${compose_project}" -f docker-compose/docker-compose.yml down -v -t 0
Testing with OpenShift
All commands were tested using Bash on CentOS 7.7.
Curl is required for testing outside OpenShift.
Commands for other OS and shells - like determining public IP address of host - may differ.
In case of need in OpenShift instance one can use OKD to setup local OpenShift instance easily
Configure Docker insecure registry - add 172.30.0.0/16 subnet into insecure-registries list of
Docker daemon configuration, e.g. into /etc/docker/daemon.json file.
Like this
{
"insecure-registries": ["172.30.0.0/16"]
}
Restart Docker daemon to apply changes
Determine & decide what address (existing domain name or IP address) will be used to access OpenShift,
e.g. localhost or IP address of VM.
Let's assume that OpenShift address is defined in openshift_address environment variable, e.g.
openshift_address="$(ip address show \| sed -r 's/^[[:space:]]*inet (192(\.[0-9]{1,3}){3})\/[0-9]+ brd (([0-9]{1,3}\.){3}[0-9]{1,3}) scope global .*$/\1/;t;d' \| head -n 1)"
If there is a need to deploy with JaCoCo agent turned on,
then use this command instead (overrides app.extraJvmOptions Helm chart value comparing to
previous command)
Login successful.
You have one project on this server: "myproject"
Using project "myproject".
Release "dcic" does not exist. Installing it now.
NAME: dcic
LAST DEPLOYED: Tue Nov 9 03:59:42 2021
NAMESPACE: myproject
STATUS: deployed
REVISION: 1
NOTES:
1. Application URL: https://app.docker-compose-init-container.local/
If deployed with JaCoCo agent turned on then check coverage using this command
(note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in ${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html file is report entry point).
Configure Docker insecure registry for Minikube registry - add subnet of Minikube registry into
insecure-registries list of Docker daemon configuration, e.g. into /etc/docker/daemon.json file.
Minikube registry IP address can be retrieved using this command
minikube ip
If command returns 192.168.49.2, then the daemon.json file should look like this
{
"insecure-registries": ["192.168.49.0/24"]
}
Stop Minikube
minikube stop
Restart Docker daemon to apply changes
Start Minikube back
minikube start
Start proxy if need to access outside host where Minikube runs
If accessing outside of host where Minikube runs then replace localhost with external address of host where Minikube runs, e.g.
k8s_address="$(ip address show \| sed -r 's/^[[:space:]]*inet (192(\.[0-9]{1,3}){3})\/[0-9]+ brd (([0-9]{1,3}\.){3}[0-9]{1,3}) scope global .*$/\1/;t;d' \| head -n 1)"
If deployed with JaCoCo agent turned on then check coverage using this command
(note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in ${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html file is report entry point).
Stop and remove K8s application, remove temporary images from local Docker registry
mabrarov/docker-compose-init-container
Docker Compose Init Container
Simulation of K8s / OpenShift init container within Docker Compose.
Note that some support from docker images is required - they should not use "complex" entrypoints, preventing overridden docker container command to intercept start of container with helpers.
Idea is to use the same images for init container and pod container b/w K8s / OpenShift and Docker Compose. Docker Compose project uses additional image sharing helpers which are used to intercept end of init container work and start of pod container to implement awaiting of pod container for completion of work of init container.
Building
Building Requirements
DOCKER_HOST
environment variable should point to that engine and include schema, liketcp://docker-host:2375
instead ofdocker-host:2375
.Building Steps
Building with Maven Wrapper:
or on Windows:
Testing with Docker Compose
Docker Compose Testing Assumptions
app_subdomain
environment variablecompose_project
environment variablee.g.
Docker Compose Testing Steps
Refer to docker-compose directory for Docker Compose project.
Create and start containers
docker-compose -p "${compose_project}" -f docker-compose/docker-compose.yml up -d
If there is a need to deploy with JaCoCo agent turned on, then use this command instead (adds
JAVA_OPTIONS
environment variable which is picked and used by Docker Compose project)Wait till application starts
Check
https://${app_subdomain}.docker-compose-init-container.local
URL, e.g. with curl:Expected output is
If deployed with JaCoCo agent turned on then check coverage using this command (note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in
${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html
file is report entry point).Stop and remove containers
docker-compose -p "${compose_project}" -f docker-compose/docker-compose.yml down -v -t 0
Testing with OpenShift
All commands were tested using Bash on CentOS 7.7. Curl is required for testing outside OpenShift. Commands for other OS and shells - like determining public IP address of host - may differ.
oc Client Tools can be used to
Setup of oc commandline tool from oc Client Tools can be done using following command
OKD Setup
In case of need in OpenShift instance one can use OKD to setup local OpenShift instance easily
Configure Docker insecure registry - add 172.30.0.0/16 subnet into insecure-registries list of Docker daemon configuration, e.g. into /etc/docker/daemon.json file.
Like this
Restart Docker daemon to apply changes
Determine & decide what address (existing domain name or IP address) will be used to access OpenShift, e.g. localhost or IP address of VM.
Let's assume that OpenShift address is defined in
openshift_address
environment variable, e.g.Create & start OKD instance
OpenShift Testing Assumptions
openshift_address
environment variableopenshift_user
environment variableopenshift_password
environment variableopenshift_project
environment variableopenshift_app
environment variablehelm_release
environment variablee.g.
OpenShift Testing Steps
Deploy application using openshift/app Helm chart and wait for completion of rollout
If there is a need to deploy with JaCoCo agent turned on, then use this command instead (overrides
app.extraJvmOptions
Helm chart value comparing to previous command)Expected output looks like:
Test OpenShift service and pod
Expected output ends with
Check
https://${openshift_app}.docker-compose-init-container.local
, e.g. with curl:Expected output is
If deployed with JaCoCo agent turned on then check coverage using this command (note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in
${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html
file is report entry point).Stop and remove OpenShift application
OKD Removal
Stop and remove OKD containers
Remove OKD mounts
Remove OKD configuration
sudo rm -rf "${HOME}/openshift.local.clusterup"
Testing with Kubernetes
All commands were tested using Bash on Ubuntu Server 18.04. Curl is required for testing outside Kubernetes.
kubectl Setup
Helm Setup
Minikube Setup
In case of need in Kubernetes (K8s) instance one can use Minikube to setup local K8s instance easily
Download Minikube executable (minikube)
Create & start K8s instance
Configure Docker insecure registry for Minikube registry - add subnet of Minikube registry into insecure-registries list of Docker daemon configuration, e.g. into /etc/docker/daemon.json file.
Minikube registry IP address can be retrieved using this command
If command returns
192.168.49.2
, then the daemon.json file should look like thisStop Minikube
Restart Docker daemon to apply changes
Start Minikube back
Start proxy if need to access outside host where Minikube runs
kubectl proxy --address='0.0.0.0' --disable-filter=true --port=8080
Check K8s dashboard using http://localhost:8080/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
If accessing outside of host where Minikube runs then replace localhost with external address of host where Minikube runs, e.g.
and use http://${k8s_address}:8080/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
Minikube Testing Assumptions
k8s_namespace
environment variablek8s_app
environment variablehelm_release
environment variablee.g.
Minikube Testing Steps
Push built docker images into Minikube registry
Deploy application using kubernetes/app Helm chart and wait for completion of rollout
If there is a need to deploy with JaCoCo agent turned on, then use this command instead
Expected output looks like:
Test K8s service and pod
Expected output ends with
Check
https://${k8s_app}.docker-compose-init-container.local
, e.g. with curl:Expected output is
If deployed with JaCoCo agent turned on then check coverage using this command (note that project has to be built and not cleaned at the time of execution of this command)
After successful execution of command JaCoCo HTML report can be found in
${jacoco_report_dir}
directory (${jacoco_report_dir}/index.html
file is report entry point).Stop and remove K8s application, remove temporary images from local Docker registry
Minikube Removal
Delete minikube instance
Optionally remove K8s configuration files
rm -rf ~/.kube