0

I'm not sure if this is the correct place to ask the question, but since this is specific to deploying WordPress, I thought I'd ask here first. What is the correct way to implement wp-cron server-side in Kubernetes?

I'm in the process of optimizing a website which is deployed to a Kubernetes cluster, and I'm interested in disabling the built-in web-based cron scheduler for WordPress by adding:

define('DISABLE_WP_CRON', true);

to my wp-config.php.

In a normal scenario, I know that I would simply add a cronjob to my cron daemon to call something similar to:

wget -q http://example.com/wp-cron.php -O /dev/null

However since I'm running WordPress in a container, and there may be multiple instances of the container running at the same time, I wasn't sure what the ideal deployment method for a server-side cron would be. I'm concerned that calling wp-cron from multiple instances at exactly the same time could potentially cause issues (since there may also not be enough time for the MySQL operations which would lock the crons to a single "runner" to propagate.). I've searched but was not able to find any concrete examples of WordPress websites deployed to Kubernetes clusters which disable the wp_cron functionality.

The options that I'm aware of:

  1. Using the Kubernetes built-in cron service to call wget -q http://example.com/wp-cron.php

  2. Using an additional "service" runs a cron daemon and which either calls: wget -q http://example.com/wp-cron.php -O /dev/null or wp cron event run --due-now

  3. Since I'm using a custom-built container, I know that I can add a build argument to enable/disable the cron and deploy a single "main" instance which runs the cron, with additional child instances which don't run the cron.

  4. Letting wp_cron run normally without any interference / changes.

Any insight into best practices would be very helpful. I'm ideally looking for a solution which will work in a situation where the WordPress service is running multiple instances at the same time (a minimum of 2).

8
  • WP CLI is superior to wget, but it sounds like the missing pieces you need is the question "how do I set a cron job on a kubernetes cluster that runs on one machine?" which is not WordPress specific. Otherwise you're asking if you should trigger cron via WP CLI or via a URL to which the answer depends on what you're able to do and your technical level. I'm not sure how someone could write an answer that you would be able to mark as not just the best answer but as the factually correct answer.
    – Tom J Nowell
    Commented May 15, 2023 at 12:14
  • and you're right that you should trigger cron on a single machine not every machine, wether that's a physical, virtual, or containerised machine
    – Tom J Nowell
    Commented May 15, 2023 at 12:15
  • Well, if i go with the WP CLI method, that means that whatever instance is performing the cronjob should have access to the entire codebase (from my understanding) meaning that I have to "share" the site root between instances, or do I have an incorrect understanding of how WP CLI works?
    – JakGuru
    Commented May 15, 2023 at 12:19
  • WP CLI requires a WordPress install, sharing the site root between instances is a kubernetes/host thing, WP CLI is unaware of that. Note again that you need to ask a question in a way that a "correct" answer can be written, but i'm not seeing that here, can you edit your question to be specific and phrased that way? The site isn't a discussion forum and if we try to discuss things in comments it will start auto-flagging for moderation and suggesting we move to chat. Comments are for clarifying the question so an answer can be written
    – Tom J Nowell
    Commented May 15, 2023 at 12:23
  • Sorry, i'm not sure what you're asking me to change. Are you asking me to change the question to something along the line of "What is the correct way to implement wp-cron server-side in Kubernetes?"
    – JakGuru
    Commented May 15, 2023 at 12:42

1 Answer 1

0

I was hoping that I'd get more community feedback, but overall based on my research, the best option was to create a separate image of my application using the same code based, specifically tailored to run wp-cli, using the built-in crond service to ensure that the crons were running according to a proper cron schedule. I then deployed this image as a separate deployment to my Kubernetes cluster, using previously configured persistent volume claims to ensure that directories such as the uploads were synchronized correctly between all instances (pods) of the web application AND the cron instance.

It is working quite well, but I do not know if there is a smarter / better approach which is more inline with best practices and standards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.