运行 kubectl in php shell_exec returns 和空列表

Running kubectl in php shell_exec returns and empty list

在我的 DO 服务器上,我试图在 shell_exec 中 运行 kubectl get deployments -o=json,但它要么是 returns null 要么是一个空列表:

{
    "apiVersion": "v1",
    "items": [],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

但是当我在本地 运行 它时,它起作用了:

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "apps/v1",
            "kind": "Deployment",
            "metadata": {
                "annotations": {
                    "deployment.kubernetes.io/revision": "1",
                    "kubectl.kubernetes.io/last-applied-configuration": "..."
                },
                "creationTimestamp": "2021-09-23T11:41:11Z",
                "generation": 1,
                "name": "database-1911797883",
                "namespace": "default",
                "resourceVersion": "...",
                "uid": "..."
            },
            "spec": {
                ...
                "replicas": 1,
                "revisionHistoryLimit": 10,
                "selector": {
                    "matchLabels": {
                        "app": "database-1911797883"
                    }
                },
                "template": {
                    "metadata": {
                        "creationTimestamp": null,
                        "labels": {
                            "app": "database-1911797883"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "image": "...",
                                "imagePullPolicy": "Always",
                                "name": "database-1911797883",
                                "resources": {},
                                "terminationMessagePath": "/dev/termination-log",
                                "terminationMessagePolicy": "File"
                            }
                        ],
                        "dnsPolicy": "ClusterFirst",
                        "restartPolicy": "Always",
                        "schedulerName": "default-scheduler",
                        "securityContext": {},
                        "terminationGracePeriodSeconds": 30
                    }
                }
            },
            "status": {
                "availableReplicas": 1,
                "conditions": [
                    ...
                ],
                "observedGeneration": 1,
                "readyReplicas": 1,
                "replicas": 1,
                "updatedReplicas": 1
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

我知道 shell_exec 在 php-fpm 中没有被禁用并且 kubectl config viewshell_exec 和 cli 中打印相同的值。

我配置为 运行 php-fpm 作为通过 cli 执行命令的同一用户。

打开您的 /etc/php-fpm.d/www.conf(或您在网站上使用的任何 conf 文件)并取消注释 clear_env = no

From the docs:

 clear_env bool

    Clear environment in FPM workers. Prevents arbitrary environment
variables from reaching FPM worker processes by clearing the environment
in workers before env vars specified in this pool configuration are added.
Default value: Yes.

因此,将其设置为 no 将允许工作人员使用导致预期输出的环境信息。