31.6 C
Monday, June 24, 2024

Handle Kubernetes Clusters Using Laravel and PHP

Laravel Access to features provided by the outstanding renoki-co/php-k8s package in Laravel is made possible by the PHP K8s package. A PHP handler for the Kubernetes Cluster API is included in the underlying PHP K8s package. It allows you to use PHP to automate the creation, deletion, updating, and other management of individual Kubernetes resources.

Here’s an illustration of how PHP K8s offers an object-oriented method for dynamically generating Kubernetes resources and configuration:

use RenokiCo\PhpK8s\KubernetesCluster;
// Create a new instance of KubernetesCluster
$cluster = new KubernetesCluster('');
// Create a new NGINX service.
$svc = $cluster->service()
    ->setSelectors(['app' => 'frontend'])
            'protocol' => 'TCP',
            'port' => 80,
            'targetPort' => 80

which would translate into the YAML configuration shown below:

apiVersion: v1
kind: Service
  name: nginx
  namespace: frontend
    app: frontend
    - protocol: TCP
      port: 80
      targetPort: 80

You can access the Kubernetes cluster and handle connection configuration with the help of the Laravel PHP package.

use RenokiCo\LaravelK8s\LaravelK8sFacade;
foreach (LaravelK8sFacade::getAllConfigMaps() as $cm) {
    // $cm->getName();

Since there are several ways to connect to the cluster instance, you can specify which connection type to use and configure them from the k8s.php config:

// Specify the cluster connection type and get the cluster
$cluster = LaravelK8sFacade::connection('http')->getCluster();
// Get the cluster using the default connection type
$cluster = LaravelK8sFacade::getCluster();

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles