Setting Up Laravel Supervisor for Queue Jobs in Ubuntu, Docker, and Kubernetes Cluster 

Laravel queue with supervisor

Index: 

  1. On BareMetal Servers
  • Installing Supervisor
  • Creating a Supervisor Configuration File
  • Reloading Supervisor
  • Accessing Supervisor Web Interface
  • Restarting Supervisor
  • Accessing Supervisor Web Interface
  1. On the Containerized Qjob solution in Docker 
  • Create a Dockerfile for Your Laravel Application
  • Create a Supervisor Configuration File
  • Build the  Docker Image
  • Create a Docker Compose File (Optional)
  • Run Your Containers with composer 
  1. On Kubernetes Cluster 
  • Create a Dockerfile for the Supervisor
  • Create Supervisor Configuration (Optional)
  •  Build the Docker Image
  • Push the Docker Image (Optional)
  • Use the Docker Image
  • Update Kubernetes Deployment YAML
  • Deploy to Kubernetes
  • Accessing Supervisor Web Interface (Optional)
  • Monitoring and Logging

Supervisord is an invaluable tool for managing processes in Ubuntu systems, particularly useful for running long-running tasks like Laravel queue workers. In this comprehensive guide, we’ll walk you through the steps to set up Supervisord for Laravel queue jobs on Ubuntu, along with accessing Supervisor’s web interface for easier management.

  1. On BareMetal Servers

Step 1: Installing SupervisorD

Firstly, let’s install SupervisorD on your Ubuntu system. Open your terminal and run the following commands:

sudo apt-get update
sudo apt-get install supervisor

Step 2: Creating a Supervisor Configuration File

Now, let’s create a configuration file for the Supervisor. We’ll create a default configuration for a Laravel queue worker. Run the following command to create the configuration file:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Paste the following configuration into the file:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/laravel/project/artisan queue:work –sleep=3 –tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log

Make sure to replace /path/to/your/laravel/project with the actual path to your Laravel project. This configuration sets up a Laravel queue worker process with specified options.

Save and exit the editor by pressing Ctrl + X, then Y, and finally Enter.

Configuration Description:

[program:laravel-worker]

– process_name=%(program_name)s_%(process_num)02d

 #This line defines the naming pattern for the worker processes. It uses the format `%(program_name)s_%(process_num)02d`, where `%(program_name)s` represents the program name (`laravel-worker` in this case), and `%(process_num)02d` pads the process number with leading zeros to ensure consistent naming.

– command=php /path/to/your/laravel/project/artisan queue:work –sleep=3 –tries=3

  #This line specifies the command that Supervisor should execute to start the worker process. It runs the `queue:work` Artisan command for your Laravel application, with specific options such as `–sleep` (sleep time between jobs) and `–tries` (maximum number of attempts per job).

– autostart=true

 #This line indicates that the program should be started automatically when the Supervisor starts. Setting it to `true` ensures that the Laravel worker process is automatically started upon system boot or Supervisor restart.

– autorestart=true

 #This line indicates that the program should be automatically restarted if it exits unexpectedly. Setting it to `true` ensures that Supervisor will monitor and automatically restart the Laravel worker process if it terminates unexpectedly.

– user=www-data

# This line specifies the user account under which the Laravel worker process should run. In this case, it’s set to `www-data`, which is a common user for web server processes in Ubuntu.

– numprocs=1

# This line specifies the number of processes that the Supervisor should spawn for this program. It’s set to `1`, indicating that only one instance of the Laravel worker process will be managed by the Supervisor.

– redirect_stderr=true

 #This line tells the Supervisor to redirect standard error (stderr) output to the specified log file. By setting it to `true`, any error messages generated by the Laravel worker process will be logged.

– stdout_logfile=/var/log/laravel-worker.log

# This line specifies the file where standard output (stdout) should be logged. It sets the path to `/var/log/laravel-worker.log`, meaning any output produced by the Laravel worker process will be logged to this file.

These descriptions provide a clear understanding of each line’s purpose and functionality within the Supervisor configuration for the Laravel worker process.

Step 3: Reloading Supervisor

After creating the Supervisor configuration file, you need to reload Supervisor to apply the changes. Run the following commands:

sudo supervisorctl reread
sudo supervisorctl update

Step 4: Accessing Supervisor Web Interface

Now, let’s set up access to the Supervisor’s web interface for easier management. First, you need to enable the web interface by editing the Supervisor configuration file:

sudo nano /etc/supervisor/supervisord.conf

Find the [inet_http_server] section and make sure it looks like this:

[inet_http_server]
port = 127.0.0.1:9001
username = user
password = pass

Replace the user and pass with your desired username and password. Save and exit the editor.

Step 5: Restarting Supervisor

Restart the Supervisor to apply the changes:

sudo service supervisor restart

Step 6: Accessing Supervisor Web Interface

Finally, open your web browser and navigate to http://127.0.0.1:9001. You’ll be prompted to enter the username and password you specified earlier. Once logged in, you’ll have access to the Supervisor web interface, where you can manage processes, view logs, and more.

That’s it! You’ve successfully set up Laravel Supervisor for queue jobs in Ubuntu and accessed Supervisor’s web interface for easier management. Enjoy the seamless execution of your Laravel queue workers.

  1. On Container solution Docker 

If you use Docker then follow the below step.

Here’s how you can set up a Docker container that runs both your Laravel application and Supervisor to manage Laravel queue workers:

Step 1: Create a Dockerfile for Your Laravel Application

Ex Dockerfile

# Use the official PHP image as the base image
FROM php:8.0-apache

# Install dependencies
RUN apt-get update && apt-get install -y \
supervisor \
&& rm -rf /var/lib/apt/lists/*

# Copy your Laravel application files into the container
COPY . /var/www/html

# Set up Apache configurations (if needed)
# COPY apache.conf /etc/apache2/sites-available/000-default.conf

# Expose port 80
EXPOSE 80

# Start Apache server
CMD [“apache2-foreground”]

Step 2: Create a Supervisor Configuration File

Create a Supervisor configuration file named laravel-worker.conf:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work –sleep=3 –tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log

Step 3: Build Your Docker Image

Build your Docker image using the Dockerfile:

docker build -t your-laravel-image .

Step 4: Create a Docker Compose File (Optional)

If you prefer using Docker Compose for managing your containers, you can create a docker-compose.yml file:

version: ‘3’
services:
laravel-app:
build: .
ports:
– “8080:80”
volumes:
– .:/var/www/html
depends_on:
– supervisor

supervisor:
image: your-laravel-image
command: supervisord -c /etc/supervisor/conf.d/supervisord.conf
volumes:
– ./laravel-worker.conf:/etc/supervisor/conf.d/laravel-worker.conf

Step 5: Run Your Containers

Run your containers using either docker run or docker-compose up:

docker run -d –name laravel-container your-laravel-image
docker run -d –name supervisor-container –volumes-from laravel-container your-laravel-image supervisord -c /etc/supervisor/conf.d/supervisord.conf

Or with Docker Compose:

docker-compose up -d

This setup will create a Docker container running your Laravel application and another container running Supervisor to manage Laravel queue workers. The Supervisor configuration file (laravel-worker.conf) is mounted into the Supervisor container so that it can manage the Laravel queue worker process.

  1. On-Kubernetes Cluster 

Let’s integrate the Supervisor Docker image into the Kubernetes deployment YAML file for managing Laravel queue workers. Below is an example of how you can achieve this:

To create a Docker image for Supervisor, you can create a Dockerfile with the necessary configurations to install Supervisor and any additional dependencies. Here’s a basic example of how you can create a Docker image for Supervisor:

Step 1: Create a Dockerfile for the Supervisor 

Create a file named Dockerfile with the following content:

Dockerfile

# Use an appropriate base image
FROM ubuntu:latest

# Install Supervisor
RUN apt-get update && apt-get install -y supervisor

# Copy Supervisor configuration files
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Expose the port for Supervisor web interface (optional)
EXPOSE 9001

# Start Supervisor
CMD [“supervisord”, “-c”, “/etc/supervisor/conf.d/supervisord.conf”]

Step 2: Create Supervisor Configuration (Optional)

If you have custom configurations for supervisors, such as managing Laravel queue workers, you can create a SupervisorD.conf file and place it in the same directory as your Dockerfile. Here’s a basic example:


[supervisord]
nodaemon=true

[program:laravel-worker]
command=php /path/to/your/laravel/project/artisan queue:work –sleep=3 –tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log

Replace /path/to/your/laravel/project with the actual path to your Laravel project.

Step 3: Build the Docker Image

Navigate to the directory containing your Dockerfile and Supervisor configuration file, then build the Docker image:

docker build -t your-supervisor-image .

Step 4: Push the Docker Image (Optional)

If you want to use this Docker image in other environments or share it with others, you can push it to a Docker registry:

docker push your-supervisor-image

Step 5: Use the Docker Image

Now you can use this Docker image to run Supervisor in your Kubernetes cluster or any other Docker environment. You can deploy it as a separate container or include it in your existing Docker-compose setup.

Note:

  • Make sure to customize the Dockerfile and Supervisor configuration according to your specific requirements and environment.
  • Ensure that any necessary dependencies for the Supervisor and your application are installed in the Docker image.
  • You may need to adjust security settings and user permissions based on your application’s requirements and best practices.

Step 5: Update Kubernetes Deployment YAML

Modify your Kubernetes Deployment YAML file to include the Supervisor container alongside your Laravel application container:

apiVersion: apps/v1
kind: Deployment
metadata:
name: laravel-app
spec:
replicas: 1
selector:
matchLabels:
app: laravel-app
template:
metadata:
labels:
app: laravel-app
spec:
containers:
– name: laravel-app
image: your-laravel-image
ports:
– containerPort: 80
# Add other settings like environment variables, volumes, etc.
– name: supervisor
image: your-supervisor-image
command: [“supervisord”, “-c”, “/etc/supervisor/conf.d/supervisord.conf”]
ports:
– containerPort: 9001
# Add other settings like volumes, environment variables, etc.

Step 6: Deploy to Kubernetes

Apply the updated Kubernetes Deployment YAML file to your Kubernetes cluster:

kubectl apply -f your-deployment.yaml

Step 7: Accessing Supervisor Web Interface (Optional)

If you want to access the Supervisor web interface from outside the Kubernetes cluster, you need to expose it using a Kubernetes Service and possibly an Ingress resource for external access. Below is an example of how you can expose the Supervisor web interface:

apiVersion: v1
kind: Service
metadata:
name: supervisor-service
spec:
selector:
app: supervisor
ports:
– protocol: TCP
port: 9001
targetPort: 9001

Apply the Service YAML file:

kubectl apply -f your-service.yaml

Step 8: Monitoring and Logging

Configure monitoring and logging solutions in your Kubernetes cluster to monitor the performance and health of your Laravel application and Supervisor, including the Laravel queue workers managed by the Supervisor.

With these steps, you can deploy both your Laravel application and Supervisor for managing Laravel queue workers in a Kubernetes environment. Adjust the configurations and settings as needed to fit your specific requirements and environment.

8 thoughts on “Setting Up Laravel Supervisor for Queue Jobs in Ubuntu, Docker, and Kubernetes Cluster 

  1. Thanks a bunch for sharing this with all people you actually know what you’re speaking approximately! Bookmarked. Kindly also talk over with my website =). We may have a link alternate arrangement among us!

  2. Thank you for the good writeup. It in truth was once a entertainment account it. Glance advanced to far introduced agreeable from you! By the way, how could we keep in touch?

  3. Thanks a lot for the helpful posting. It is also my belief that mesothelioma has an incredibly long latency time period, which means that warning signs of the disease might not exactly emerge right until 30 to 50 years after the primary exposure to asbestos. Pleural mesothelioma, that’s the most common type and affects the area about the lungs, may cause shortness of breath, chest muscles pains, plus a persistent coughing, which may bring about coughing up our blood.

Leave a Reply

Your email address will not be published. Required fields are marked *