Real Life Anwendungen in Openshift
In meinem letzten Beitrag https://blog.doubleslash.de/standardization-through-containerrization/ habe ich kurz beschrieben was Openshift ist und wie man es mit Vagrant ausprobieren kann.
Nun möchte ich an einem kurzen „Real Life“ Beispiel zeigen wie schnell man die Grundlage für eine neue Anwendung schaffen kann.
Wie im Komponentendiagramm dargestellt soll ein Wildfly 10 mit einer MySQL Datenbank kommunizieren.
Openshift bietet für den WildFly und MySQL bereits Unterstützung und bieten sich für andere Projekte als solide Basis an.
Ich verwende im Beispiel fertige Templates für „wildfly:latest“ (s2i Builder) und „mysql-ephemeral“.
Bevor gestartet werden kann wird ein neue Projekt angelegt. Hierzu führe ich mit dem oc tool folgende Schritte durch:
# Benutzer admin mit PW admin (nur für diesen Test)
oc login https://10.2.2.2:8443
# neue Projekt anlegen
oc new-project real-life-app
# unter Umständen in den Namespace wechseln
oc project real-life-app
Als nächsten Schritt legen wir die Datenbank an. Wenn man jetzt nicht genau weiss, was das Template so an Einstellungsmöglichkeiten hat, kann man sich mit dem oc Tool diese einfach anzeigen lassen. Hierfür gibt es die Befehle
# Verfügbare Templates aus dem Namespace openshift anzeigen lassen
oc get template -n openshift
NAME DESCRIPTION PARAMETERS OBJECTS
datagrid65-basic Application template for JDG 6.5 applications. 11 (6 blank) 5
datagrid65-https Application template for JDG 6.5 applications. 20 (11 blank) 7
datagrid65-mysql Application template for JDG 6.5 and MySQL applications. 32 (19 blank) 9
datagrid65-mysql-persistent Application template for JDG 6.5 and MySQL applications with persistent storage. 33 (19 blank) 10
datagrid65-postgresql Application template for JDG 6.5 and PostgreSQL applications built using. 29 (16 blank) 9
datagrid65-postgresql-persistent Application template for JDG 6.5 and PostgreSQL applications with persistent... 30 (16 blank) 10
decisionserver62-amq-s2i Application template for BRMS Realtime Decision Server 6 A-MQ applications bu... 26 (3 blank) 9
decisionserver62-basic-s2i Application template for BRMS Realtime Decision Server 6 applications built u... 14 (3 blank) 5
decisionserver62-https-s2i Application template for BRMS Realtime Decision Server 6 HTTPS applications b... 21 (4 blank) 7
eap64-amq-persistent-s2i Application template for EAP 6 A-MQ applications with persistent storage buil... 27 (6 blank) 10
eap64-amq-s2i Application template for EAP 6 A-MQ applications built using S2I. 26 (6 blank) 9
eap64-basic-s2i Application template for EAP 6 applications built using S2I. 12 (3 blank) 5
eap64-https-s2i Application template for EAP 6 applications built using S2I. 21 (8 blank) 7
eap64-mongodb-persistent-s2i Application template for EAP 6 MongDB applications with persistent storage bu... 33 (15 blank) 10
eap64-mongodb-s2i Application template for EAP 6 MongDB applications built using S2I. 32 (15 blank) 9
eap64-mysql-persistent-s2i Application template for EAP 6 MySQL applications with persistent storage bui... 34 (16 blank) 10
eap64-mysql-s2i Application template for EAP 6 MySQL applications built using S2I. 33 (16 blank) 9
eap64-postgresql-persistent-s2i Application template for EAP 6 PostgreSQL applications with persistent storag... 31 (13 blank) 10
eap64-postgresql-s2i Application template for EAP 6 PostgreSQL applications built using S2I. 30 (13 blank) 9
jenkins-ephemeral Jenkins service, without persistent storage. WARNING: Any data stored will be... 4 (all set) 3
jenkins-persistent Jenkins service, with persistent storage. You must have persistent volumes a... 5 (all set) 4
mongodb-ephemeral MongoDB database service, without persistent storage. WARNING: Any data store... 7 (3 generated) 2
mongodb-persistent MongoDB database service, with persistent storage. Scaling to more than one... 8 (3 generated) 3
mysql-ephemeral MySQL database service, without persistent storage. WARNING: Any data stored... 6 (2 generated) 2
mysql-persistent MySQL database service, with persistent storage. Scaling to more than one re... 7 (2 generated) 3
nodejs-example An example Node.js application with no database 14 (8 blank) 5
nodejs-mongodb-example An example Node.js application with a MongoDB database 15 (3 blank) 7
postgresql-ephemeral PostgreSQL database service, without persistent storage. WARNING: Any data st... 6 (2 generated) 2
postgresql-persistent PostgreSQL database service, with persistent storage. Scaling to more than o... 7 (2 generated) 3
warpdrive-python27 Python 2.7 by Graham Dumpleton (www.getwarped.org) 13 (7 blank) 5
warpdrive-python33 Python 3.3 by Graham Dumpleton (www.getwarped.org) 13 (7 blank) 5
warpdrive-python34 Python 3.4 by Graham Dumpleton (www.getwarped.org) 13 (7 blank) 5
Nützliche Informationen sind PARAMETERS und OBJECTS
PARAMETERS gibt die Einstellungsmöglichkeiten wieder (z.B. Benutzer/Passwort).
OBJECTS sind die Openshift Konfigurationen die Angelegt werden (z.B. Labels, Service, Buildconfig, Deploymentconfig …).
Diese Infos lassen sich wie folgt auslesen.
# Beschreibung des Templates ausgeben lassen
oc describe template mysql-ephemeral -n openshift
Name: mysql-ephemeral
Created: 5 weeks ago
Labels: <none>
Description: MySQL database service, without persistent storage.
for testing
Annotations: iconClass=icon-mysql-database
tags=database,mysql
Parameters:
Name: MEMORY_LIMIT
Display Name: Memory Limit
Description: Maximum amount of memory the container can use.
Required: false
Value: 512Mi
Name: NAMESPACE
Display Name: Namespace
Description: The OpenShift Namespace where the ImageStream resides.
Required: false
Value: openshift
Name: DATABASE_SERVICE_NAME
Display Name: Database Service Name
Description: The name of the OpenShift Service exposed for the database.
Required: true
Value: mysql
Name: MYSQL_USER
Display Name: MySQL User
Description: Username for MySQL user that will be used for accessing the database.
Required: true
Generated: expression
From: user[A-Z0-9]{3}
Name: MYSQL_PASSWORD
Display Name: MySQL Password
Description: Password for the MySQL user.
Required: true
Generated: expression
From: [a-zA-Z0-9]{16}
Name: MYSQL_DATABASE
Display Name: MySQL Database Name
Description: Name of the MySQL database accessed.
Required: true
Value: sampledb
Object Labels: template=mysql-ephemeral-template
Objects:
Service ${DATABASE_SERVICE_NAME}
Die MySQL Datenbank wird wie folgt erstellt:
# Erstellt die App mysql
oc new-app --template=mysql-ephemeral -p DATABASE_SERVICE_NAME=mysql,MYSQL_USER=admin,MYSQL_PASSWORD=password,MYSQL_DATABASE=reallifedb
--> Deploying template "mysql-ephemeral" in project "openshift" for "mysql-ephemeral"
With parameters:
Memory Limit=512Mi
Namespace=openshift
Database Service Name=mysql
MySQL User=admin
MySQL Password=password
MySQL Database Name=reallifedb
--> Creating resources ...
service "mysql" created
deploymentconfig "mysql" created
--> Success
Run 'oc status' to view your app.
Die Ausgabe spiegelt wieder, was für Paramter gesetzt wurde und was für Ressourcen in Openshift erstellt werden. Hier einen „Service und eine DeploymentConfig“.
Beim WildFly ist das identisch. Hier benutze ich die Openshift Webconsole (die ist hier etwas einfacher :)).
Nach der Anmeldung an der Openshift Webconsole hierzu einfach das Projekt „real-life-app“ auswählen. Danach mit „add to project“ eine neue App erstellen (siehe Screenshot).
Nach der Auswahl müssen ein paar Parameter angegeben werden.
- Name: real-life-app
- Git Repsoitory Url: https://github.com/doubleSlashde/reallifeapp.git (Beispielanwendung auf Basis von Kitchensink)
Mit dem Click auf „Create“ wird die Anwendung erstellt.
Dieser Vorgang dauert etwas. Im Hintergrund wird jetzt das Git Repo gecloned. Das Baseimage geladen und der s2i Build angestoßen. Das Ergebnis ist dann ein Docker Image, welches gestartet wird.
Der Fortschritt kann in der Oberfläche oder mit dem OC Tool (oc status oder oc log real-life-app) verfolgt werden.
In der Übersicht werden jetzt zwei Pods als „Running“ angezeigt.
Der WildFly ist auch bereits erreichbar unter http://real-life-app-real-life-app.apps.10.2.2.2.xip.io/
Jetzt muss der WildFly noch konfiguriert werden. Das generierte Image kann über sogenannte ENVIRONMENT Variablen kurz ENV auf die Verwendung von MySQL (neben H2 wird auch Postgre per default unterstützt) konfiguriert werden.
Die Konfiguration wird über die sogenannte DeploymentConfig gemacht. Dies ist sinnvoll, da die Verwendung von Datenbank,Passwort,Benutzer etc. parametrisiert werden kann.
Toll beim WildFly Image ist, dass die Service IP für die mysql Datenbank automatisch eingesetzt wird. Dies erspart manuelle Konfiguration. Der Schlüssel liegt hier im Service namen der MySQL Datenbank. Dieser muss einfach „mysql“ lauten.
Für das Beispiel reichen folgende Befehle über das OC Tool:
# Enabled die MYSQL Datasource und bestimmt gleichzeitig die verwendete Datenbank.
oc env dc/real-life-app MYSQL_DATABASE=reallifedb
# Bestimmt den Datenbank Benutzer.
oc env dc/real-life-app MYSQL_USER=admin
# Bestimmt das Datenbank Passwort.
oc env dc/real-life-app MYSQL_PASSWORD=password
Nachdem die Parameter gesetzt wurden wird automatisch ein neues Deployment mit den geänderten Variablen getriggert.
Jetzt kann die Anwendung unter http://xip.io/ aufgerufen werden.
War das Deployment erfolgreich ist diese Seite zu sehen und sollte funktionieren.
Viel Spaß beim Ausprobieren!
Referenzen:
Sehr guter Artikel, danke dafür!