Настройка health check эндпоинтов
Health check эндпоинты проверяют работоспособность приложения. Балансировщики нагрузки, Kubernetes, мониторинговые системы опрашивают их, чтобы исключать нездоровые инстансы из ротации.
Два типа проверок
Liveness — жив ли процесс. Если нет, контейнер перезапускается. Должен отвечать даже при деградации зависимостей.
Readiness — готов ли принимать трафик. Если нет, балансировщик не направляет запросы. Проверяет БД, кэш, внешние сервисы.
Laravel: health check эндпоинты
// routes/api.php
Route::get('/health/live', fn() => response()->json(['status' => 'ok']));
Route::get('/health/ready', function () {
$checks = [];
// Database
try {
DB::connection()->getPdo();
$checks['database'] = 'ok';
} catch (\Throwable $e) {
$checks['database'] = 'error: ' . $e->getMessage();
}
// Redis
try {
Cache::store('redis')->set('health-check', 1, 5);
$checks['cache'] = 'ok';
} catch (\Throwable $e) {
$checks['cache'] = 'error: ' . $e->getMessage();
}
$healthy = !str_contains(implode('', $checks), 'error');
$status = $healthy ? 200 : 503;
return response()->json([
'status' => $healthy ? 'healthy' : 'unhealthy',
'checks' => $checks,
], $status);
});
Node.js Express
app.get('/health/live', (_req, res) => {
res.json({ status: 'ok', uptime: process.uptime() });
});
app.get('/health/ready', async (_req, res) => {
const checks: Record<string, string> = {};
try {
await db.query('SELECT 1');
checks.database = 'ok';
} catch (e) {
checks.database = `error: ${e}`;
}
try {
await redis.ping();
checks.redis = 'ok';
} catch (e) {
checks.redis = `error: ${e}`;
}
const healthy = Object.values(checks).every(v => v === 'ok');
res.status(healthy ? 200 : 503).json({ status: healthy ? 'healthy' : 'unhealthy', checks });
});
Kubernetes probes
containers:
- name: app
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
failureThreshold: 3
Срок реализации
Базовые liveness + readiness эндпоинты с проверкой БД и Redis: 0.5–1 день. С интеграцией в Kubernetes и мониторинговую систему: 1–2 дня.







