If you happen to have access to a running OpenShift installation, this blog post can help you to install a WordPress in a quick and convenient way. In other cases, you can either check out one of our “HowTo install Openshift” tutorials (locally on Vagrant, or on any Fedora system). Alternatively, you can start from scratch and follow my blog post Install Wordpress via Docker Compose.

As with the Docker Compose based installation, the solution consists of a MySQL container and a WordPress container.

Install WordPress via OpenShift: Architecture with two Containers and two persisten Volumes

Ths blog post is closely following https://blog.openshift.com/running-wordpress-easy-way/.

Step 1: Log into OpenShift

Let us log in to OpenShift (OKD):

 

# oc login
...
# oc project wordpress

Full log:

# oc login
Authentication required for https://console.<myip>.nip.io:8443 (openshift)
Username: myuser
Password:
Login successful.

You have access to the following projects and can switch between them with 'oc project ':

  * default
    kube-public
    kube-service-catalog
    kube-system
    management-infra
    openshift
    openshift-infra
    openshift-logging
    openshift-node
    openshift-sdn
    openshift-template-service-broker
    openshift-web-console
    wordpress

Using project "default".
# oc project wordpress
Now using project "wordpress" on server "https://console.<myip>.nip.io:8443".

Step 2: Create a Project

This step can be skipped if you install WordPress on an existing project. However, placing all resources in a separate project will help you clean the system from all created resources once you decide to get rid of the installation.

First, click on ‘okd’ logo on the upper left. Then you will see the list of projects in the right pane. Click the “create project” button on the upper right:

Then you can add the project details:

Click “Create”.

Alternatively, you can add the project on the command line:

# oc new-project wordpress
Now using project "wordpress" on server "https://console.159.69.198.32.nip.io:8443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

to build a new example application in Ruby.

Step 3: Add the WordPress Template

Structure of an OpenShift Template

In OpenShift, you can define templates that control, which parameters the user is prompted for (with defaults), and which objects are created from it. Therefore, you will find a parameters section, in which we will define parameters we will make use of in the objects section.

{
    "kind": "Template",
    "apiVersion": "v1",
    "metadata": { # name, annotations, ... },
    "parameters": [ { # name, description, value, from, required ... }, {...}, ... ],
    "objects": [ { # kind, apiVersion, metadata, spec, ... }, {...}, ... ]
}

For a typical solution, we will need the following kinds of objects:

  • ImageStream
  • BuildConfig
  • DeploymentConfig
  • Service
  • Route
  • PersistentVolumeClaim

In our case, there are two independent containers in a single template: a database container and the application container. Both containers will persist their data in persistent volumes. This is, why you will find two DeploymentConfigs as well as two PersistentVolumeClaims.

WordPress OpenShift Template

The latest WordPress standalone template can be downloaded from GIT on https://raw.githubusercontent.com/openshift-evangelists/wordpress-quickstart/master/templates/classic-standalone.json:

{
    "kind": "Template",
    "apiVersion": "v1",
    "metadata": {
        "name": "wordpress-classic-standalone",
	"annotations": {
            "openshift.io/display-name": "WordPress (Classic / Standalone)",
	    "description": "Creates a WordPress installation with separate MySQL database instance. Requires that two persistent volumes be available. If a ReadWriteMany persistent volume type is available and used, WordPress can be scaled to multiple replicas and the deployment strategy switched to Rolling to permit rolling deployments on restarts.",
	    "tags": "quickstart,php,wordpress",
            "iconClass": "icon-php"
	}
    },
    "parameters": [
        {
            "name": "APPLICATION_NAME",
            "description": "The name of the WordPress instance.",
            "value": "my-wordpress-site",
            "from": "[a-zA-Z0-9]",
            "required": true
        },
        {
            "name": "QUICKSTART_REPOSITORY_URL",
            "description": "The URL of the quickstart Git repository.",
            "value": "https://github.com/openshift-evangelists/wordpress-quickstart",
            "required": true
        },
        {
            "name": "WORDPRESS_VOLUME_SIZE",
            "description": "Size of the persistent volume for WordPress.",
            "value": "1Gi",
            "required": true
        },
        {
            "name": "WORDPRESS_VOLUME_TYPE",
            "description": "Type of the persistent volume for WordPress.",
            "value": "ReadWriteOnce",
            "required": true
        },
        {
            "name": "WORDPRESS_DEPLOYMENT_STRATEGY",
            "description": "Type of the deployment strategy for WordPress.",
            "value": "Recreate",
            "required": true
        },
        {
            "name": "WORDPRESS_MEMORY_LIMIT",
            "description": "Amount of memory available to WordPress.",
            "value": "512Mi",
            "required": true
        },
        {
            "name": "DATABASE_VOLUME_SIZE",
            "description": "Size of the persistent volume for the database.",
            "value": "1Gi",
            "required": true
        },
        {
            "name": "DATABASE_MEMORY_LIMIT",
            "description": "Amount of memory available to the database.",
            "value": "512Mi",
            "required": true
        },
	{
	    "description": "The name of the database user.",
	    "name": "DATABASE_USERNAME",
	    "from": "user[a-f0-9]{8}",
	    "generate": "expression"
	},
	{
	    "description": "The password for the database user.",
	    "name": "DATABASE_PASSWORD",
	    "from": "[a-zA-Z0-9]{12}",
	    "generate": "expression"
	},
        {
            "name": "MYSQL_VERSION",
            "description": "The version of the MySQL database.",
            "value": "5.7",
            "required": true
        },
        {
            "name": "PHP_VERSION",
            "description": "The version of the PHP builder.",
            "value": "7.0",
            "required": true
        }
    ],
    "objects": [
        {
            "kind": "ImageStream",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}-img",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "kind": "BuildConfig",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "triggers": [
                    {
                        "type": "ConfigChange"
                    },
                    {
                        "type": "ImageChange"
                    }
                ],
                "source": {
                    "type": "Git",
                    "git": {
                        "uri": "${QUICKSTART_REPOSITORY_URL}"
                    }
                },
                "strategy": {
                    "type": "Source",
                    "sourceStrategy": {
                        "from": {
                            "kind": "ImageStreamTag",
                            "namespace": "openshift",
                            "name": "php:${PHP_VERSION}"
                        }
                    }
                },
                "output": {
                    "to": {
                        "kind": "ImageStreamTag",
                        "name": "${APPLICATION_NAME}-img:latest"
                    },
                    "imageLabels": [
                        {
                            "name": "io.k8s.display-name",
                            "value": "WordPress"
                        },
                        {
                            "name": "io.k8s.description",
                            "value": "WordPress application and S2I builder."
                        },
                        {
                            "name": "io.openshift.s2i.scripts-url",
                            "value": "image:///opt/app-root/s2i"
                        },
                        {
                            "name": "io.s2i.scripts-url",
                            "value": "image:///opt/app-root/s2i"
                        },
                        {
                            "name": "io.openshift.tags",
                            "value": "builder,php,wordpress"
                        }
                    ]
                }
            }
        },
        {
            "kind": "DeploymentConfig",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "strategy": {
                    "type": "${WORDPRESS_DEPLOYMENT_STRATEGY}"
                },
                "triggers": [
                    {
                        "type": "ConfigChange"
                    },
                    {
                        "type": "ImageChange",
                        "imageChangeParams": {
                            "automatic": true,
                            "containerNames": [
                                "wordpress"
                            ],
                            "from": {
                                "kind": "ImageStreamTag",
                                "name": "${APPLICATION_NAME}-img:latest"
                            }
                        }
                    }
                ],
                "replicas": 1,
                "selector": {
                    "app": "${APPLICATION_NAME}",
                    "deploymentconfig": "${APPLICATION_NAME}"
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "app": "${APPLICATION_NAME}",
                            "deploymentconfig": "${APPLICATION_NAME}"
                        }
                    },
                    "spec": {
                        "volumes": [
                            {
                                "name": "data",
                                "persistentVolumeClaim": {
                                    "claimName": "${APPLICATION_NAME}-wordpress-data"
                                }
                            }
                        ],
                        "containers": [
                            {
                                "name": "wordpress",
                                "image": "${APPLICATION_NAME}-img",
                                "ports": [
                                    {
                                        "containerPort": 8080,
                                        "protocol": "TCP"
                                    }
                                ],
				"resources": {
                                    "limits": {
                                        "memory": "${WORDPRESS_MEMORY_LIMIT}"
				    }
                                },
				"readinessProbe": {
				    "failureThreshold": 3,
				    "httpGet": {
					"path": "/wp-admin/install.php",
					"port": 8080,
					"scheme": "HTTP",
					"httpHeaders": [
					    {
						"name": "X-Forwarded-Proto",
						"value": "https"
					    }
					]
				    },
				    "periodSeconds": 10,
				    "successThreshold": 1,
				    "timeoutSeconds": 1
				},
				"livenessProbe": {
				    "failureThreshold": 3,
				    "httpGet": {
					"path": "/wp-admin/install.php",
					"port": 8080,
					"scheme": "HTTP",
					"httpHeaders": [
					    {
						"name": "X-Forwarded-Proto",
						"value": "https"
					    }
					]
				    },
				    "periodSeconds": 10,
				    "successThreshold": 1,
				    "timeoutSeconds": 1
				},
                                "env": [
                                    {
                                        "name": "MYSQL_DATABASE",
                                        "value": "wordpress"
                                    },
                                    {
                                        "name": "MYSQL_USER",
                                        "value": "${DATABASE_USERNAME}"
                                    },
                                    {
                                        "name": "MYSQL_PASSWORD",
                                        "value": "${DATABASE_PASSWORD}"
                                    },
                                    {
                                        "name": "MYSQL_HOST",
                                        "value": "${APPLICATION_NAME}-db"
                                    },
                                    {
                                        "name": "MYSQL_TABLE_PREFIX",
                                        "value": "wp_"
                                    }
                                ],
                                "volumeMounts": [
                                    {
                                        "name": "data",
                                        "mountPath": "/opt/app-root/src"
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        },
        {
            "kind": "DeploymentConfig",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}-db",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "strategy": {
                    "type": "Recreate"
                },
                "triggers": [
                    {
                        "type": "ConfigChange"
                    },
                    {
                        "type": "ImageChange",
                        "imageChangeParams": {
                            "automatic": true,
                            "containerNames": [
                                "mysql"
                            ],
                            "from": {
                                "kind": "ImageStreamTag",
                                "namespace": "openshift",
                                "name": "mysql:${MYSQL_VERSION}"
                            }
                        }
                    }
                ],
                "replicas": 1,
                "selector": {
                    "app": "${APPLICATION_NAME}",
                    "deploymentconfig": "${APPLICATION_NAME}-db"
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "app": "${APPLICATION_NAME}",
                            "deploymentconfig": "${APPLICATION_NAME}-db"
                        }
                    },
                    "spec": {
                        "volumes": [
                            {
                                "name": "data",
                                "persistentVolumeClaim": {
                                    "claimName": "${APPLICATION_NAME}-mysql-data"
                                }
                            }
                        ],
                        "containers": [
                            {
                                "name": "mysql",
                                "image": "mysql",
                                "ports": [
                                    {
                                        "containerPort": 3306,
                                        "protocol": "TCP"
                                    }
                                ],
				"resources": {
                                    "limits": {
                                        "memory": "${DATABASE_MEMORY_LIMIT}"
				    }
                                },
				"readinessProbe": {
				  "timeoutSeconds": 1,
				  "initialDelaySeconds": 5,
				  "exec": {
				    "command": [ "/bin/sh", "-i", "-c",
				      "MYSQL_PWD=\"$MYSQL_PASSWORD\" mysql -h 127.0.0.1 -u $MYSQL_USER -D $MYSQL_DATABASE -e 'SELECT 1'"]
				  }
				},
				"livenessProbe": {
				  "timeoutSeconds": 1,
				  "initialDelaySeconds": 30,
				  "tcpSocket": {
				    "port": 3306
				  }
				},
                                "env": [
                                    {
                                        "name": "MYSQL_DATABASE",
                                        "value": "wordpress"
                                    },
                                    {
                                        "name": "MYSQL_USER",
                                        "value": "${DATABASE_USERNAME}"
                                    },
                                    {
                                        "name": "MYSQL_PASSWORD",
                                        "value": "${DATABASE_PASSWORD}"
                                    }
                                ],
                                "volumeMounts": [
                                    {
                                        "name": "data",
                                        "mountPath": "/var/lib/mysql/data"
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        },
        {
            "kind": "Service",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "ports": [
                    {
                        "name": "8080-tcp",
                        "protocol": "TCP",
                        "port": 8080,
                        "targetPort": 8080
                    }
                ],
                "selector": {
                    "app": "${APPLICATION_NAME}",
                    "deploymentconfig": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "kind": "Service",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}-db",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "ports": [
                    {
                        "name": "3306-tcp",
                        "protocol": "TCP",
                        "port": 3306,
                        "targetPort": 3306
                    }
                ],
                "selector": {
                    "app": "${APPLICATION_NAME}",
                    "deploymentconfig": "${APPLICATION_NAME}-db"
                }
            }
        },
        {
            "kind": "Route",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "host": "",
                "to": {
                    "kind": "Service",
                    "name": "${APPLICATION_NAME}",
                    "weight": 100
                },
                "port": {
                    "targetPort": 8080
                },
                "tls": {
                    "termination": "edge",
                    "insecureEdgeTerminationPolicy": "Allow"
                }
            }
        },
        {
            "kind": "PersistentVolumeClaim",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}-mysql-data",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "accessModes": [
                    "ReadWriteOnce"
                ],
                "resources": {
                    "requests": {
                        "storage": "${DATABASE_VOLUME_SIZE}"
                    }
                }
            }
        },
        {
            "kind": "PersistentVolumeClaim",
            "apiVersion": "v1",
            "metadata": {
                "name": "${APPLICATION_NAME}-wordpress-data",
                "labels": {
                    "app": "${APPLICATION_NAME}"
                }
            },
            "spec": {
                "accessModes": [
                    "${WORDPRESS_VOLUME_TYPE}"
                ],
                "resources": {
                    "requests": {
                        "storage": "${WORDPRESS_VOLUME_SIZE}"
                    }
                }
            }
        }
    ]
}

Here, we have defined two Docker containers: a WordPress container and a database container. The latter persists his data in a Docker volume:

Install WordPress via OpenShift: Architecture with two Containers and two persisten Volumes

Step 3.1: Add OpenShift Template via GUI

We can add the OpenShift Template via the web graphical user interface (Web GUI) by clicking on
–> View All

–> Choosing the project

and clicking on the ‘Import YAML / JSON’ button and pasting the above template into the field:

–> Create

Here, we have chosen to only “save” the template for later use. We will process the template in the next step.

Step 3.1: Add OpenShift Template via CLI

Adding the OpenShift template via OpenShift CLI is as simple as typing following commands to the command line:

# oc login
# oc project <project>
# oc create -f https://raw.githubusercontent.com/openshift-evangelists/wordpress-quickstart/master/templates/classic-standalone.json

Here, you can skip the first two commands, if you already are logged in to the correct project. The third command will download the above JSON content from GIT.

Step 4: Spin up the WordPress Containers

Now is the time to start the WordPress installation: let us look for the just imported WordPress template by Typing “WordPress” into the search box of the project:

Note: if the template is not found, try again after reloading the page (e.g. by pressing F5).

Now, you will be prompted for all the parameters that are defined in the OpenShift template. In the chosen template, all parameters have defaults that can be kept, if you wish, so you can just press the “Create” button:

Step 5: Open the WordPress URL in a Browser

OpenShift will now transfer you to your WordPress application:

Wait for the pod to be up and running before clicking on the link below the application name.

If your OpenShift installation uses self-signed certificates, you will need to accept the security warning, before you reach your application.

Do not forget to write down the password and username, so you can log in as administrator in the next step. The newly created WordPress installation has no way of sending you an email, if you have lost the password.

Since this is a test site I intend to use for performance-testing, I have chosen to discourage search engines from indexing the site. Note, that the username and the email are mandatory parameters.

After clicking “Install WordPress” and logging in with the chosen user and password, OpenShift will finally direct you to the WordPress administration page:

The URL without /wp-admin at the end will lead you to the default WordPress page:

Step 6 (optional): Shut down the WordPress Containers

Finally, you also can save resources and shut down the WordPress containers by scaling down the applications to zero. You can do so by clicking on the “>” on the project’s dashboard and scaling down to zero pods:

Alternatively, head to the deployments of the project:

Now you need to click on the most recent deployment:

And now you can scale down the deployment:

 

Step 7 (optional): Clean OpenShift from all created Resources

Cleaning OpenShift from all created resources will be a breeze if you have created all resources in a separate project, as recommended above. You can just delete the project as follows: click on ‘okd’ on the upper left and choose “Delete Project” in the drop-down list of your project:

Alternatively, type the following command on the CLI:

oc delete project <your-project>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.