php用fopen调用kubernetes api持续读取数据流
来源:原创
时间:2023-02-23
作者:脚本小站
分类:PHP
token的方式:
<?php // API 访问令牌和 API Server 地址 $token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $apiServer = 'https://my-apiserver:6443'; // 要读取日志的 Pod 名称和容器名称 $namespace = 'default'; $podName = 'my-pod'; $containerName = 'my-container'; // 打开数据流 $apiPath = "/api/v1/namespaces/$namespace/pods/$podName/log?container=$containerName&follow=true"; $url = $apiServer . $apiPath; $options = [ 'http' => [ 'header' => [ 'Authorization: Bearer ' . $token, ], ], ]; $context = stream_context_create($options); $fp = fopen($url, 'r', false, $context); // 设置超时时间 $timeout = 1; // 持续读取数据流 while (true) { // 使用 stream_select() 函数检查数据流是否可读 $read = [$fp]; $write = null; $except = null; if (stream_select($read, $write, $except, $timeout) === false) { // 发生错误 break; } if (count($read) === 0) { // 没有数据可读 continue; } // 读取数据流 $data = fread($fp, 1024); if ($data === false || strlen($data) === 0) { // 读取出错或已到达流的末尾 break; } echo $data; } // 关闭数据流 fclose($fp);
token方式:非持续输出:
<?php // API 访问令牌和 API Server 地址 $token = 'eyJhb......'; $apiServer = 'https://192.168.1.19:6443'; // 要读取日志的 Pod 名称和容器名称 $namespace = 'default'; $podName = 'nginx-7d64ffcf-4gb2q'; $containerName = 'nginx'; // 打开数据流 $apiPath = "/api/v1/namespaces/$namespace/pods/$podName/log?container=$containerName"; $url = $apiServer . $apiPath; $options = [ 'http' => [ 'header' => [ 'Authorization: Bearer ' . $token, ], ], ]; $context = stream_context_create($options); $fp = fopen($url, 'r', false, $context); // 读取数据流 while (!feof($fp)) { $data = fread($fp, 1024); if ($data === false) { // 读取出错 break; } echo $data; } // 关闭数据流 fclose($fp);
证书秘钥的方式:
<?php function getfile($data){ $file = tempnam(sys_get_temp_dir(), 'connectk8s-'); file_put_contents($file, $data); return $file; } // 设置证书和私钥字符串 $ca = base64_decode('LS0tLS1C......'); // kubeconfig里的 certificate-authority-data 字符串 #var_dump($ca); $cert = base64_decode('LS0tLS1......S0tLQo='); // kubeconfig里的 client-certificate-data 字符串 #var_dump($cert); $key = base64_decode('LS0tLS1......S0tCg=='); // kubeconfig里的 client-key-data 字符串 #var_dump($key); $ca = getfile($ca); $cert = getfile($cert); $key = getfile($key); $apiServer = 'https://192.168.199.80:6443'; // 要读取日志的 Pod 名称和容器名称 $namespace = 'default'; $podName = 'guestbook-ui-85c9c5f9cb-ppmfk'; $containerName = 'guestbook-ui'; // 打开数据流 $apiPath = "/api/v1/namespaces/$namespace/pods/$podName/log?container=$containerName&follow=true"; $url = $apiServer . $apiPath; $options = [ "ssl" => [ "cafile" => $ca, "local_cert" => $cert, "local_pk" => $key, ], ]; $context = stream_context_create($options); $fp = fopen($url, 'r', false, $context); // 设置超时时间 $timeout = 1; // 持续读取数据流 while (true) { // 使用 stream_select() 函数检查数据流是否可读 $read = [$fp]; $write = null; $except = null; if (stream_select($read, $write, $except, $timeout) === false) { // 发生错误 break; } if (count($read) === 0) { // 没有数据可读 continue; } // 读取数据流 $data = fread($fp, 1024); if ($data === false || strlen($data) === 0) { // 读取出错或已到达流的末尾 break; } echo $data; } // 关闭数据流 fclose($fp);
获取pods数据:
// Kubernetes API 地址 $url = "https://<kubernetes_api_address>/api/v1/pods"; // 打开 HTTPS 数据流并使用上下文 $fp = fopen($url, "r", false, $context); // 读取数据流内容 while (!feof($fp)) { echo fgets($fp); } // 关闭数据流 fclose($fp);