7.1 C
Pakistan
Monday, February 26, 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('http://127.0.0.1:8080');
 
// Create a new NGINX service.
$svc = $cluster->service()
    ->setName('nginx')
    ->setNamespace('frontend')
    ->setSelectors(['app' => 'frontend'])
    ->setPorts([
        [
            'protocol' => 'TCP',
            'port' => 80,
            'targetPort' => 80
        ],
    ])
    ->create();

which would translate into the YAML configuration shown below:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: frontend
spec:
  selector:
    app: frontend
  ports:
    - 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles