Search code examples
phpkuberneteskubectlfpm

Running kubectl in php shell_exec returns and empty list


On my DO server I am trying to run kubectl get deployments -o=json in shell_exec, but it either returns null or an empty list:

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

But when I run it locally, it works:

{
    "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": ""
    }
}

I know that shell_exec is not disabled in php-fpm and kubectl config view prints the same values in both shell_exec and cli.

I configured to run php-fpm as the same user that executes the command through cli.


Solution

  • Open your /etc/php-fpm.d/www.conf (or whatever conf file you have there that you are using for your web) and uncomment 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.
    

    Hence, setting it to no will allow workers to use the environment information leading to expected output.