Pebble, workload container and environment variables

Hello,

I am migrating mysql-operator to pebble, and the image we are using needs the environment variable MYSQL_ROOT_PASSWORD to be set.

For this I have the following layer, where I define this env var:

    def _mysql_layer(self):
        """Construct the pebble layer"""
        logger.debug("Building pebble layer")
        layer = {
            "summary": "MySQL layer",
            "description": "Pebble layer configuration for MySQL",
            "services": {
                "mysql": {
                    "override": "merge",
                    "summary": "mysql daemon",
                    "command": "docker-entrypoint.sh mysqld",
                    "startup": "enabled",
                    "environment": {
                        "MYSQL_ROOT_PASSWORD": "SuperSecurePassword",
                    },
                },
            },
        }

        return layer

But this variable is not set in the workload container:

âžś  mysql-operator git:(pebble) âś— microk8s.kubectl exec -it --namespace=mysql mysql-0 -c mysql -- bash
root@mysql-0:/# printenv | grep MYSQL
MYSQL_PORT=tcp://10.152.183.214:65535
MYSQL_PORT_65535_TCP_ADDR=10.152.183.214
MYSQL_PORT_65535_TCP=tcp://10.152.183.214:65535
MYSQL_PORT_65535_TCP_PORT=65535
MYSQL_PORT_65535_TCP_PROTO=tcp
MYSQL_SERVICE_PORT=65535
MYSQL_SERVICE_PORT_PLACEHOLDER=65535
MYSQL_SERVICE_HOST=10.152.183.214

What am I missing??

It’s a little hard to tell without seeing a bit more of the code. This function returns a dictionary that could be used as a pebble layer but not sure if/how/when that’s being applied. Do you have a link to the full charm code available?

So I think we got to the bottom of this in chat, but for the sake of people who run into this in the future:

The environment variables set in pebble are only set for those processes that pebble starts.

In this case, bash is being run outside of Pebble and this doesn’t get the variable :slight_smile:

(Also it looks like mysqld was failing to start due to being run as root, Jose has put in an issue against pebble to support running processes as other users :slight_smile: )

Hello Jon,

Just to fully understand the behavior.

When you say:

the environment variables set in pebble are only set for those processes that pebble starts.

that means if I have the following layer:

            "services": {
                "mysql": {
                    "override": "merge",
                    "summary": "mysql daemon",
                    "command": "docker-entrypoint.sh mysqld",
                    "startup": "enabled",
                    "environment": {
                        "MYSQL_ROOT_PASSWORD": self.mysql_root_password,
                    },
                },
            },

Is Pebble starting docker-entrypoint.sh mysqld?

If so, shouldn’t the command "docker-entrypoint.sh mysqld" get the environment variable?

My issue here is that I need that docker-entrypoint.sh gets the variable, so if I can set the variable in the container is ok for me.

Hello Tom,

I am working in this Draft PR

Correct, the entrypoint should be getting the environment variable, which can then chose how it does/doesn’t want to pass it onto mysqld.

2 Likes