In this lab we introduce how to orchestrate a multi-container application in OpenShift.
This lab should be performed on YOUR ASSIGNED AWS VM as ec2-user unless otherwise instructed.
Expected completion: 40-60 minutes
Let's start with a little experimentation. I am sure you are all excited about your new blog site! And, now that it is getting super popular with 1,000s of views per day, you are starting to worry about uptime.
So, let's see what will happen. Launch the site:
$ sudo podman run -d -p 8080:8080 -v ~/workspace/pv/uploads:/var/www/html/wp-content/uploads:z -e DB_ENV_DBUSER=user -e DB_ENV_DBPASS=mypassword -e DB_ENV_DBNAME=mydb -e DB_HOST=0.0.0.0 -e DB_PORT=3306 --name wordpress wordpress
$ sudo podman run -d --network=container:wordpress -v ~/workspace/pv/mysql:/var/lib/mysql:z -e DBUSER=user -e DBPASS=mypassword -e DBNAME=mydb --name mariadb mariadbTake a look at the site in your web browser on your machine using
http://<YOUR AWS VM PUBLIC DNS NAME HERE>:8080. As you learned before, you can confirm the port that your server is running on by executing:
$ sudo podman ps
$ sudo podman port wordpress
8080/udp -> 0.0.0.0:8080
8080/tcp -> 0.0.0.0:8080Now, let's see what happens when we kick over the database. However, for a later experiment, let's grab the container-id right before you do it.
$ OLD_CONTAINER_ID=$(sudo podman inspect --format '{{ .ID }}' mariadb)
$ sudo podman stop mariadbTake a look at the site in your web browser or using curl now. And, imagine explosions! (making sound effects will be much appreciated by your lab mates.)
- web browser ->
http://<YOUR AWS VM PUBLIC DNS NAME HERE>:8080OR
$ curl -L http://localhost:8080Now, what is neat about a container system, assuming your web application can handle it, is we can bring it right back up, with no loss of data.
$ sudo podman start mariadbOK, now, let's compare the old container id and the new one.
$ NEW_CONTAINER_ID=$(sudo podman inspect --format '{{ .ID }}' mariadb)
$ echo -e "$OLD_CONTAINER_ID\n$NEW_CONTAINER_ID"Hmmm. Well, that is cool, they are exactly the same. OK, so all in all, about what you would expect for a web server and a database running on VMs, but a whole lot faster (well, the starting is). Let's take a look at the site now.
- web browser ->
http://<YOUR AWS VM PUBLIC DNS NAME HERE>:8080OR
$ curl -L http://localhost:8080And.. Your site is back! Fortunately wordpress seems to be designed such that it does not need a restart if its database goes away temporarily.
Finally, let's kill off these containers to prepare for the next section.
$ sudo podman rm -f mariadb wordpressStarting and stopping is definitely easy, and fast. However, it is still pretty manual. What if we could automate the recovery? Or, in buzzword terms, "ensure the service remains available"? Enter Kubernetes/OpenShift.
Now login to our local OpenShift & create a new project:
$ oc login -u developer
You have one project on this server: "myproject"
$ oc new-project devel
Now using project "devel" on server "https://127.0.0.1:8443".You are now logged in to OpenShift and are using the devel project. You can also view the OpenShift web console by using the same credentials to log in to https://<YOUR AWS VM PUBLIC DNS NAME HERE>:8443 in a browser.
Let's get started by talking about a pod. A pod is a set of containers that provide one "service." How do you know what to put in a particular pod? Well, a pod's containers need to be co-located on a host and need to be spawned and re-spawned together. So, if the containers always need to be running on the same container host, well, then they should be a pod.
Note: We will be putting this file together in steps to make it easier to explain what the different parts do. We will be identifying the part of the file to modify by looking for an "empty element" that we inserted earlier and then replacing that with a populated element.
Let's make a pod for mariadb. Open a file called mariadb-pod.yaml.
$ mkdir -p ~/workspace/mariadb/openshift
$ vi ~/workspace/mariadb/openshift/mariadb-pod.yamlIn that file, let's put in the pod identification information:
apiVersion: v1
kind: Pod
metadata:
name: mariadb
labels:
name: mariadb
spec:
containers:We specified the version of the Kubernetes API, the name of this pod (aka name), the kind of Kubernetes thing this is, and a label which lets other Kubernetes things find this one.
Generally speaking, this is the content you can copy and paste between pods, aside from the names and labels.
Now, let's add the custom information regarding this particular container. To start, we will add the most basic information. Please replace the containers: line with:
containers:
- name: mariadb
image: localhost:5000/mariadb
ports:
- containerPort: 3306
env:Here we set the name of the container; remember we can have more than
one in a pod. We also set the image to pull, in other words, the container
image that should be used and the registry to get it from.
Lastly, we need to configure the environment variables that need to be fed from
the host environment to the container. Replace env: with:
env:
- name: DBUSER
value: user
- name: DBPASS
value: mypassword
- name: DBNAME
value: mydbOK, now we are all done, and should have a file that looks like:
apiVersion: v1
kind: Pod
metadata:
name: mariadb
labels:
name: mariadb
spec:
containers:
- name: mariadb
image: localhost:5000/mariadb
ports:
- containerPort: 3306
env:
- name: DBUSER
value: user
- name: DBPASS
value: mypassword
- name: DBNAME
value: mydbOur wordpress container is much less complex, so let's do that pod next.
$ mkdir -p ~/workspace/wordpress/openshift
$ vi ~/workspace/wordpress/openshift/wordpress-pod.yamlapiVersion: v1
kind: Pod
metadata:
name: wordpress
labels:
name: wordpress
spec:
containers:
- name: wordpress
image: localhost:5000/wordpress
ports:
- containerPort: 8080
env:
- name: DB_ENV_DBUSER
value: user
- name: DB_ENV_DBPASS
value: mypassword
- name: DB_ENV_DBNAME
value: mydbA couple things to notice about this file. Obviously, we change all the appropriate names to reflect "wordpress" but, largely, it is the same as the mariadb pod file. We also use the environment variables that are specified by the wordpress container, although they need to get the same values as the ones in the mariadb pod.
Ok, so, let's launch our pods and make sure they come up correctly. In order to do this, we need to introduce the oc command which is what drives OpenShift. Generally, speaking, the format of oc commands is oc <operation> <kind>. Where <operation> is something like create, get, remove, etc. and kind is the kind from the pod files.
$ oc create -f ~/workspace/mariadb/openshift/mariadb-pod.yaml
$ oc create -f ~/workspace/wordpress/openshift/wordpress-pod.yamlNow, I know i just said, kind is a parameter, but, as this is a create statement, it looks in the -f file for the kind.
Ok, let's see if they came up:
$ oc get podsWhich should output two pods, one called mariadb and one called wordpress . You can also check the OpenShift web console if you already have it pulled up and verify the pods show up there as well.
If you have any issues with the pods transistioning from a "Pending" state, you can check out the logs from the OpenShift containers in multiple ways. Here are a couple of options:
$ oc logs mariadb
$ oc describe pod mariadb
$ oc logs wordpress
$ oc describe pod wordpressOk, now let's kill them off so we can introduce the services that will let them more dynamically find each other.
$ oc delete pod/mariadb pod/wordpressVerify they are terminating or are gone:
$ oc get podsNote you used the "singular" form here on the kind, which, for delete, is required and requires a "name". However, you can, usually, use them interchangeably depending on the kind of information you want.
Now we want to create Kubernetes Services for our pods so that OpenShift can introduce a layer of indirection between the pods.
Let's start with mariadb. Open up a service file:
$ vi ~/workspace/mariadb/openshift/mariadb-service.yamland insert the following content:
apiVersion: v1
kind: Service
metadata:
name: mariadb
labels:
name: mariadb
spec:
ports:
- port: 3306
selector:
name: mariadbAs you can probably tell, there isn't really anything new here. However, you need to make sure the kind is of type Service and that the selector matches at least one of the labels from the pod file. The selector is how the service finds the pod that provides its functionality.
OK, now let's move on to the wordpress service. Open up a new service file:
$ vi ~/workspace/wordpress/openshift/wordpress-service.yamland insert:
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
name: wordpress
spec:
ports:
- port: 8080
selector:
name: wordpressHere you may notice there is no reference to the wordpress pod at all. Any pod that provides "wordpress capabilities" can be targeted by this service. Pods can claim to provide "wordpress capabilities" through their labels. This service is programmed to target pods with a label of name: wordpress.
Another example of this might have been if we had made the mariadb-service just a "db" service and then, the pod could be mariadb, mysql, sqlite, anything really, that can support SQL the way wordpress expects it to. In order to do that, we would just have to add a label to the mariadb-pod.yaml called "db" and a selector in the mariadb-service.yaml (although, an even better name might be db-service.yaml) called db. Feel free to experiment
with that at the end of this lab if you have time.
Now let's get things going. Start mariadb:
$ oc create -f ~/workspace/mariadb/openshift/mariadb-pod.yaml -f ~/workspace/mariadb/openshift/mariadb-service.yamlNow let's start wordpress.
$ oc create -f ~/workspace/wordpress/openshift/wordpress-pod.yaml -f ~/workspace/wordpress/openshift/wordpress-service.yamlOK, now let's make sure everything came up correctly:
$ oc get pods
$ oc get servicesNote these may take a while to get to a RUNNING state as it pulls the image from the registry, spins up the containers, etc.
Eventually, you should see:
$ oc get pods
NAME READY STATUS RESTARTS AGE
mariadb 1/1 Running 0 45s
wordpress 1/1 Running 0 42s$ oc get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mariadb ClusterIP 172.30.xx.xx <none> 3306/TCP 1m
wordpress ClusterIP 172.30.xx.xx <none> 8080/TCP 1mNow let's expose the wordpress service by creating a route
$ oc expose svc/wordpressAnd you should be able to see the service's accessible URL by viewing the routes:
$ oc get routes
NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
wordpress wordpress-devel.<YOUR AWS VM PUBLIC IP>.nip.io wordpress 8080 NoneCheck and make sure you can access the wordpress service through the route:
$ curl -L wordpress-devel.<YOUR AWS VM PUBLIC IP>.nip.io- OR open the URL in a browser to view the UI
Seemed awfully manual and ordered up there, didn't it? In our next lab we'll demonstrate how simple deployments can be with OpenShift templates.