antonioribeiro / health

Laravel Health Panel
BSD 3-Clause "New" or "Revised" License
1.94k stars 198 forks source link

Compatibility with kubernetes pods #170

Closed san3jaya closed 4 years ago

san3jaya commented 4 years ago

Is this package support kubernetes pods health check? If support can I have small example?

poor-bob commented 4 years ago

I actually can't recall why I did it this way, but here's how I implemented this package for our readinessProbe

A health route:

    // health routes
    Route::get('/health', 'HealthCheckController@index');

A custom HealthCheckController:

<?php

namespace App\Http\Controllers;

use PragmaRX\Health\Http\Controllers\Health;

class HealthCheckController extends Controller {

    public function index() {
        $services = array_map('strtolower', config('health.resources.enabled'));

        foreach($services as $service){
            $serviceHealth = app('pragmarx.health')->checkResource($service)->isHealthy();
            if (!$serviceHealth) {
                return "Service: $service is not healthy";
            }
        }
        return 'Healthy!';
    }
}

a PHP file to call call this health check by-passing laravel (this is where I'm not sure why I did it this way)

<?php

// Start output buffering (Capture STDOUT)
ob_start();

// Check that artisan ran through start.sh
//if (!file_exists('/ready')) { exit(1); }

// Check that php-fpm is running as well
$output = shell_exec('ps aux | grep [p]hp-fpm');
if (empty($output)){
    echo "Can't find php-fpm, exiting with error";
    exit(1);
}

$host = getenv('PLACEHOLDER_API_URL_NO_PROTOCOL');
if (empty($host)){
    $host = 'local.api.REMOVED.com';
}
$_SERVER['REQUEST_URI'] = '/v1.0/health';
$_SERVER['HTTP_ACCEPT'] = '*/*';
$_SERVER['HTTP_USER_AGENT'] = 'curl/7.47.0';
$_SERVER['HTTP_CONNECTION'] = 'close';
$_SERVER['HTTP_HOST'] = $host;

// Start output buffering (Capture STDOUT)
ob_start();

require_once 'server.php';

$content = ob_get_contents();

if (strpos($content, 'Healthy!') !== false) {
    // Success
    exit(0);
} else {
    // Failure
    echo "Content is not Healthy! val: $content";
    exit(1);
}
ob_end_clean();

and lastly the readinessProbe config:

        readinessProbe:
          exec:
            command:
            - php 
            - /var/www/health_check.php

There MIGHT be a reason I decided to do the HTTP request in a "health_check" php file rather than doing it from the readinessProbe, but I couldn't explain why anymore. Sorry, that was a long time ago