简体   繁体   中英

Using Wordpress CLI image on Kubernetes

We have a custom docker image based on the official wordpress image that holds a custom theme that we are developing. We have CI/CD on this project using Gitlab CI and deploying branches on a bare-metal Kubernetes Cluster v1.6 for review. It works fine, however we are trying to enhance the process by automating the required manual actions when a new instance is deployed:

  • Installing Wordpress
    • Setting admin credential
    • Setting site name and url
  • Activating theme
  • Activating plugins
  • Importing data
  • etc.

wp-cli has all the commands needed. But how to use it with containers and K8S? We are aware that there are two options:

  • Installing the tool in our WordPress based image.
  • Using a second container that contains only wp-cli and communicates with the main container.

There are WordPress images with preinstalled wp-cli (eg tutum-docker-wordpress ), but we don't feel this is the correct way. At some point we would like to use a CronJob resource with the cli image to export data each day and we are trying to make the process as generic as possible and we would like to stick with the official images so the second option is preferred. Based on our research in order to implement the second option we will need to achieve two things:

  • Access the files of the WordPress installation from the CLI container.
  • Do not retry after successful execution.

We researched couple of option with no complete success:

  • Second container in the same pod - It looks possible to share files between both containers using EmptyDir , however even if the container succeeds it will be restarted.
  • InitContainer - This one sounds very tempting because database migrations are done this way. However it seems impossible to get the files unless you use a custom image.
  • Job - It is designed exactly to handle one-time tasks, however accessing the files of a container in a pod does not seem to be possible.

This seems like a pretty common feature when you deploy review deployments of Wordpress on Kubernetes and someone should have already done that. But we could not find any specific info about this use case.

Please advice on what is the best way to achieve the desired implementation according to you and how?

You can use an NFS based file system and mount your wordpress content into any type of workload.

If you need something to help you get started check out https://matthewdavis.io/highly-available-wordpress-on-kubernetes/ .

Based on @kotsov's hint above, we got this working using init containers in the deployment.

Steps:

  • The first init container starts the wordpress image, but with args overridden to ["apache2-foreground","-version"]. This will cause it to run the entry-point, installing/setting up wordpress if necessary, but then exiting with the version.
  • The second init container is the wordpress cli with its commands.
  • After these, the wordpress container is let run again.

We also set these up on a persistent volume for /var/www/html so if it's started again it wont have to do all this. The init steps will skip initialisation steps based on directory contents.

In the deployment template:

initContainers:
- name: Initialise Wordpress
  image: wordpress: <version>
  args: ["apache2-foreground","-version"]
  env: 
    <your wordpress env>
  volumes:
    <shared volume info>
- name: Wordpress CLI commands
  image: wordpress-cli: <version>
  env: 
    <your wordpress cli env>
  volumes:
    <shared volume info>
containers:
- name: Wordpress
  image: wordpress: <version>
  env: 
    <your wordpress env>
  volumes:
    <shared volume info>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM