Skip to content

Commit

Permalink
chore: Added Init Container example
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Feb 3, 2019
1 parent b830ef6 commit 4430ef9
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 17 deletions.
5 changes: 5 additions & 0 deletions INSTALL.adoc
Expand Up @@ -124,6 +124,11 @@ function pod {
kubectl get pod -o name | grep -v "Terminating" | grep $1 | sed -e "s/^pods\///"
}
# Get the nodePort of first port mapping of a service
function svc_node_port {
kubectl get svc $1 -o jsonpath={.spec.ports[0].nodePort}
}
# Change the current context (e.g. 'k8s_context minikube')
function k8s_context {
kubectl config use-context $1
Expand Down
12 changes: 6 additions & 6 deletions README.adoc
Expand Up @@ -46,23 +46,23 @@ link:behavorial/SelfAwareness/README.adoc[Self Awareness]::

=== Structural Patterns

link:structural/InitContainer/README.adoc[Init Container]::
Initialize a HTTP server's HTML source from a remote git repository [*]
link:structural/Sidecar/README.adoc[Sidecar]::
Git polling example for a sidecar
link:structural/InitContainer/README.adoc[Init Container]::
_TBD_
link:structural/Ambassador/README.adoc[Ambassador]::
_TBD_
link:structural/Adapter/README.adoc[Adapter]::
Adapter for exporting timing information from the sample random-generator application in a Prometheus format [*]

=== Configuration Patterns

link:configuration/EnvvarConfiguration/README.adoc[Envvar Configuration]::
link:configuration/EnvvarConfiguration/README.adoc[EnvVar Configuration]::
_TBD_
link:configuration/ConfigurationResource/README.adoc[Configuration Resource]::
_TBD_
link:configuration/ConfigurationTemplate/README.adoc[Configuration Template]::
Example how to use a template configuration `standalone.xml` which is processed with a template processed and filled with data from `ConfigMap` before a Wildfly server is started.
Example how to use a template configuration `standalone.xml` which is processed with a template processed and filled with data from ConfigMap before a Wildfly server is started.
link:configuration/ImmutableConfiguration/README.adoc[Immutable Configuration]::
Several examples how to use immutable configuration containers for application configuration. This includes examples for the plain Docker case and for Kubernetes.

Expand All @@ -71,9 +71,9 @@ link:configuration/ImmutableConfiguration/README.adoc[Immutable Configuration]::
link:advanced/StatefulService/README.adoc[Stateful Service]::
_TBD_
link:advanced/Controller/README.adoc[Controller]::
Simple, pure shell based controller which watches `ConfigMap` resources for changes and restarts `Pods` by using a label selector provided as annotation. An additional link:advanced/Controller/expose-controller/README.adoc[example controller] exposes an `Ingress` route when it detects an `exposeService` label attached to the service.
Simple, pure shell based controller which watches ConfigMap resources for changes and restarts `Pods` by using a label selector provided as annotation. An additional link:advanced/Controller/expose-controller/README.adoc[example controller] exposes an Ingress route when it detects an `exposeService` label attached to the service.
link:advanced/Operator/README.adoc[Operator]::
Operator based on the `ConfigMap` watch link:advanced/Controller/README.adoc[controller] and introduces a CRD `ConfigWatcher` which connects a `ConfigMap` with a set of `Pods` to restart in case of a config change.
Operator based on the ConfigMap watch link:advanced/Controller/README.adoc[controller] and introduces a CRD ConfigWatcher which connects a ConfigMap with a set of Pods to restart in case of a config change.
link:advanced/ImageBuilder/README.adoc[Image Builder]::
Setup a link:advanced/ImageBuilder/openshift/README.adoc[chained build] on OpenShift and use link:advanced/ImageBuilder/knative/README.adoc[Knative build] for doing builds within the cluster.
link:advanced/ElasticScale/README.adoc[ElasticScale]::
Expand Down
3 changes: 1 addition & 2 deletions behavorial/BatchJob/README.adoc
Expand Up @@ -26,14 +26,13 @@ kubectl create -f pv-and-pvc.yml

The job can be started with


[source, bash]
----
kubectl create -f job.yml
----

This Job will create multiple Pods to create the number.
Each Pod will run for creating 10000 entries into `logs/random-generator.log`.
Each Pod will run for creating 10000 entries into `logs/random.log`.
The Job is confiured to run the Pod 5 times, with 3 max running in parallel.
That will take some minutes, but at the end you should have 50000 entries in the log file.

Expand Down
2 changes: 1 addition & 1 deletion behavorial/BatchJob/job.yml
Expand Up @@ -28,7 +28,7 @@ spec:
# Class running batch job
- RandomRunner
# 1. Arg: File to store data (on a PV)
- /logs/random-generator.log
- /logs/random.log
# 2. How many iterations
- "10000"
volumeMounts:
Expand Down
4 changes: 2 additions & 2 deletions behavorial/ScheduledJob/README.adoc
Expand Up @@ -13,7 +13,7 @@ Then create the PersistenVolume and PersistenVolumeClaim with
kubectl create -f pv-and-pvc.yml
----

but you don't have to do it, if you already have your Minikube still running from the _Batch Job_example footnote:[If you reuse the Batch Job example, don't forget that the `random-generator.log` is generated in the `logs/` directory over there.]
but you don't have to do it, if you already have your Minikube still running from the _Batch Job_example footnote:[If you reuse the Batch Job example, don't forget that the `random.log` is generated in the `logs/` directory over there.]
Now create the CronJob which fires every three minutes:
Expand All @@ -24,7 +24,7 @@ kubectl create -f cron-jobs.yml
In this example we only use only one completion per Job (i.e. the default for a Job).
You can check the `logs/random-generator.log` for the numbers generated, and also of course the Pods that are run on behalf of the job.
You can check the `logs/random.log` for the numbers generated, and also of course the Pods that are run on behalf of the job.
=== More Information
Expand Down
2 changes: 1 addition & 1 deletion behavorial/ScheduledJob/cron-job.yml
Expand Up @@ -21,7 +21,7 @@ spec:
# Class running batch job
- RandomRunner
# 1. Arg: File to store data (on a PV)
- /logs/random-generator.log
- /logs/random.log
# 2. How many iterations
- "10000"
volumeMounts:
Expand Down
7 changes: 6 additions & 1 deletion structural/Adapter/README.adoc
@@ -1,3 +1,8 @@
== Adapter

* Prometheus adapter with `nc` for the random-generator service, transforming the log to a Prometheus related format
IMPORTANT: The instructions have not been written/finished, but the resource file has been verified. Instructions will be added soon.


=== More Information

* https://www.youtube.com/watch?v=Ph3t8jIt894[Container patterns for modular distributed system design]
6 changes: 3 additions & 3 deletions structural/Adapter/deployment.yml
Expand Up @@ -28,7 +28,7 @@ spec:
protocol: TCP
volumeMounts:
- mountPath: /logs
name: logs
name: log-volume
# ------------------------------------------------
# Prometheus adapter
- image: k8spatterns/random-generator-exporter
Expand All @@ -44,8 +44,8 @@ spec:
# Mount shared volume for accessing the logs
volumeMounts:
- mountPath: /logs
name: logs
name: log-volume
volumes:
# New empty directory volume for sharing the log file between container
- name: logs
- name: log-volume
emptyDir: {}
28 changes: 27 additions & 1 deletion structural/InitContainer/README.adoc
@@ -1 +1,27 @@
## Init Container
== Init Container

IMPORTANT: The instructions have not been written/finished, but the resource file has been verified. Instructions will be added soon.

[source, bash]
----
kubctl create -f pod.yml
kubectl get pods -w
NAME READY STATUS RESTARTS AGE
www 0/1 Init:0/1 0 8s
www 0/1 PodInitializing 0 10s
www 1/1 Running 0 12s
kubectl create -f service.yml
port=$(kubectl get svc www -o jsonpath={.spec.ports[0].nodePort})
curl -s http://$(minikube ip):$port
----

=== More Information

* https://github.com/k8spatterns/examples/tree/master/structural/InitContainer[Init Container example]
* https://kubernetes.io/docs/concepts/workloads/pods/init-containers/[Init Containers]
* https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container[Configuring Pod Initialization]
* http://blog.javascriptroom.com/2013/01/21/the-initializer-pattern/[The Initializer Pattern in JavaScript]
* https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/Initialization/Initialization.html[Object Initialization in Swift]
33 changes: 33 additions & 0 deletions structural/InitContainer/pod.yml
@@ -0,0 +1,33 @@
apiVersion: v1
kind: Pod
metadata:
name: www
labels:
app: www
spec:
initContainers:
- name: download
image: axeclbr/git
# Clone an HTML page to be served
command:
- git
- clone
- https://github.com/mdn/beginner-html-site-scripted
- /var/lib/data
# Shared volume with main container
volumeMounts:
- mountPath: /var/lib/data
name: source
containers:
# Simple static HTTP server for serving these pages
- name: run
image: docker.io/centos/httpd
ports:
- containerPort: 80
# Shared volume with main container
volumeMounts:
- mountPath: /var/www/html
name: source
volumes:
- emptyDir: {}
name: source
19 changes: 19 additions & 0 deletions structural/InitContainer/random-generator/README.adoc
@@ -0,0 +1,19 @@
== Init Container (random-genertor example)

[source, bash]
----
kubctl create -f deployment.yml
k get pods -w
NAME READY STATUS RESTARTS AGE
random-generator-6c8f6bb79c-jc6nf 0/1 Init:0/1 0 4s
random-generator-6c8f6bb79c-jc6nf 0/1 PodInitializing 0 61s
random-generator-6c8f6bb79c-jc6nf 1/1 Running 0 62s
kubectl create -f service.yml
kubectl get service
port=$(kubectl get svc random-generator -o jsonpath={.spec.ports[0].nodePort})
curl -s http://$(minikube ip):$port/logs | head -10
----
54 changes: 54 additions & 0 deletions structural/InitContainer/random-generator/deployment.yml
@@ -0,0 +1,54 @@
# DeploymentConfig for starting up the random-generator
apiVersion: apps/v1
kind: Deployment
metadata:
name: random-generator
spec:
replicas: 1
selector:
matchLabels:
app: random-generator
template:
metadata:
labels:
app: random-generator
spec:
initContainers:
# Same image as main container, but used for calling the batch CLI
- image: k8spatterns/random-generator:1.0
name: init
command:
- java
# Use / as classpath to pick up the class file
- -classpath
- /
# Class running batch job
- RandomRunner
# 1. Arg: File to store data (on a PV)
- /logs/random.log
# 2. How many iterations
- "100000"
# Shared volume with main container. Use for initializing the batch file
volumeMounts:
- mountPath: /logs
name: log-volume
containers:
# ------------------------------------------------
# Main container sharing the /logs dir with the init container
- image: k8spatterns/random-generator:1.0
name: random-generator
env:
# The log file that we want to export
- name: LOG_FILE
value: /logs/random.log
ports:
# Application running on port 8080
- containerPort: 8080
protocol: TCP
volumeMounts:
- mountPath: /logs
name: log-volume
volumes:
# New empty directory volume for sharing the log file between container
- name: log-volume
emptyDir: {}
13 changes: 13 additions & 0 deletions structural/InitContainer/random-generator/service.yml
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: random-generator
spec:
selector:
app: random-generator
ports:
- name: random
port: 8080
protocol: TCP
targetPort: 8080
type: NodePort
13 changes: 13 additions & 0 deletions structural/InitContainer/service.yml
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: www
spec:
selector:
app: www
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 80
type: NodePort

0 comments on commit 4430ef9

Please sign in to comment.