Inside the infrastructure MySQL access and ProxySQL rules
MySQL access and ProxySQL rules
Configuration management: a Salt master keeps the desired state of the servers, states and the pillar data they use, and its minions apply it on the hosts. Here the state is who may do what in MySQL, and which hostgroup ProxySQL sends a query to.
My work
The MySQL grants and the ProxySQL rules for writes and reads are kept in Salt.
How it is built
An example of the mechanism, not the configuration of the real servers: a generalised layout from the documentation of Salt 3006 LTS and ProxySQL, with one master, a minion on the MySQL host and a minion on the ProxySQL host. In my work Salt ran with masters and applied the ProxySQL rules; the figure draws one master.
While the master is down
unavailable
- No new job reaches the minions through this master.
- What was applied keeps working: MySQL keeps its grants in its own tables, and ProxySQL routes by its runtime layer, which on startup is filled from its disk.
- The minions keep trying to reconnect: by default each wait is twice the one before, up to recon_max, ten seconds, and then starts again from recon_default, one second. 4
12 15
Components
Press a component to read what it does.
Salt master states, pillar
Keeps the state tree, with its top file, and the pillar; sends jobs to the minions and collects what they return.
Minion MySQL host
The Salt minion on the MySQL host: it applies the states meant for this host, among them the grants.
Minion ProxySQL host
The Salt minion on the ProxySQL host: it applies the states meant for this host, here the users and query rules of ProxySQL.
MySQL users, grants
The database server. Its accounts and their privileges are kept by MySQL itself, in its grant tables.
Memory admin tables
ProxySQL’s main database in memory: the tables edited through the admin interface, mysql_users and mysql_query_rules among them. Not persistent.
Runtime what routes
The data structures the worker threads of ProxySQL use: the only layer that affects what the proxy does, for example where it routes a query.
Disk proxysql.db
A SQLite database on the host’s disk. On startup ProxySQL fills its memory layer from it.
-
The job
Minions stay connected to the master’s publisher and get their jobs from it. Applying the states with no names, a highstate, a minion takes the top file and applies what it maps to this host.
In detail
All minions are always connected to the publisher on port 4505 and connect to the return port 4506 only when they need to. A top file maps groups of machines to the configuration roles that should be applied to them; it is named top.sls by default and lies at the top of the state tree. state.apply invoked without any names triggers a highstate: the minion matches the expressions of the top file, and the states listed for those it matches are downloaded, compiled and run. 1
2 3 14 Where it breaks Ports 4505 and 4506 have to be open on the master only; closed there, no minion gets a job or returns a result.
-
Pillar
Pillar is data kept apart from the states: compiled on the master, and given to a minion only if the minion is targeted. That is why secrets, passwords among them, are kept there.
My part The MySQL grants and the ProxySQL rules for writes and reads are kept in Salt.
In detail
Pillar data is compiled on the master, and the pillar data for a minion is accessible only to the minion for which it is targeted in the pillar configuration: that makes pillar useful for sensitive data specific to a minion. Its top file is laid out as the state top file is. The minion keeps an in-memory copy, generated when it starts and refreshed with saltutil.refresh_pillar; running states compiles the pillar afresh, unless the pillar cache is on. 5
6 Where it breaks Pillar files placed under the file roots, where the states lie, can be read by any minion: the targeting of the pillar no longer protects them.
-
Grants
A state of grants makes MySQL give or take away a privilege on a level of objects to a user at a host. The user and the password are a state of their own.
In detail
The mysql_grants state module grants and revokes MySQL permissions: a grant names the privileges, the level (*.*, db.*, db.table) and the user at a host. It cannot set a password: users are managed with the mysql_user state, which in turn cannot grant permissions. Both need the MySQLdb Python module on the minion. 7
8 Where it breaks MySQL does nothing when it is asked to grant privileges narrower than those already in place: a grant cannot be made narrower without revoking first, which the state does only with revoke_first.
-
Users and rules
ProxySQL lets in only the users in its own table, and sends a query by its rules, in the order of their numbers: a query no rule matches goes to its user’s default hostgroup.
In detail
mysql_users holds the users that may log into ProxySQL, with the credentials it uses for the backends. Active rules of mysql_query_rules are evaluated in ascending rule_id order: match_digest matches the digest of a query, match_pattern its text, destination_hostgroup says where a matching query goes, unless a transaction has already started and the user has transaction_persistent=1, and a matching rule with apply=1 ends the evaluation. default_hostgroup is where a user’s queries go when no rule matches them. 9
10 11 Where it breaks A user that exists in MySQL but not in ProxySQL’s table cannot connect through ProxySQL, which authenticates the clients itself. An earlier rule that matches and ends the evaluation leaves the rules after it unread for that query.
-
Loading to runtime
What is written in the admin tables does not route a single query yet: ProxySQL works by its runtime layer, and a change reaches it with LOAD … TO RUNTIME.
My part Salt applied the ProxySQL rules.
In detail
The runtime layer is the internal data structures used by ProxySQL’s worker threads, and the only layer that actively affects the behaviour of the proxy: when a thread needs a variable or a routing rule, it looks there. It is changed only with LOAD … TO RUNTIME commands, for example LOAD MYSQL QUERY RULES TO RUNTIME or LOAD MYSQL USERS TO RUNTIME. 9
12 Where it breaks A new user, or a new rule, works only once it has been loaded to runtime: in the memory layer it changes nothing.
-
Saving to disk
The memory layer is lost on a restart unless it was saved: SAVE … TO DISK writes it to a SQLite file, from which ProxySQL fills its memory when it starts.
My part Every change that needed a restart of a service was done by hand.
In detail
The memory layer, the main database, is read and written through the admin interface with ordinary SQL, and is not persistent: changes there are lost on restart unless saved to disk. The disk layer is a SQLite database on the local file system, typically proxysql.db, changed with SAVE … TO DISK (or by editing the SQLite file directly, which is not recommended). On startup ProxySQL loads the persistent state from it into memory and pushes it to the runtime structures. 12
16 Where it breaks A change loaded to runtime but not saved to disk works until the next restart and is then gone. And three variables cannot be changed at runtime at all: mysql-interfaces, mysql-threads and mysql-stacksize take effect only after the proxy is restarted.
-
A dry run
Before anything is changed, the states can be run with test=True: Salt shows what it would change and changes nothing.
In detail
The test run is asked for by adding test=True to the state functions, for example salt '*' state.apply test=True: the return shows in yellow the states that would be applied, with the result reported as None. With test set to True in the minion’s configuration, states run in test mode by default. 13
Where it breaks A dry run tells what the states would do on the host as it is now; it does not check that the change is right.
Sources
- Salt 3006: Introduction to Salt at scale
- Salt 3006: The top file
- Salt 3006: States tutorial, part 1
- Salt 3006: Minion configuration
- Salt 3006: Pillar
- Salt 3006: Pillar walkthrough
- Salt 3006: salt.states.mysql_grants
- Salt 3006: salt.states.mysql_user
- ProxySQL: MySQL users management
- ProxySQL: MySQL tables
- ProxySQL: Configuration
- ProxySQL: Multi-layer configuration
- Salt 3006: Running states in test mode
- Salt 3006: Opening the firewall up for Salt
- MySQL 8.4: Grant Tables
- ProxySQL: Global variables
Next case
Infrastructure on GCP
Infrastructure as code: Terraform compares a configuration with the state of what it manages, plans the changes and applies them through the provider’s API; Terragrunt cuts the configuration into units and runs them in the order of their dependencies.