In the distributed application, it’s quite common to use load balancers so that your application isn’t dependent on database topologies, to easily load balance the workload, and to do connection failover. ProxySQL, a high-performance MySQL proxy, allows users to send different queries to multiple servers to distribute the load more efficiently. ProxySQL has several benefits, including intelligent load balancing across different databases and the ability to determine if a database instance is running so that read traffic can be redirected accordingly.
This blog post takes an example of Azure Kubernetes Service (AKS) to deploy ProxySQL as a service to split the read and write workloads to Azure Database for MySQL.
Important: Azure Database for MySQL supports ProxySQL version 2.0.6 and later.
Note:
ProxySQL is an open source community tool. It is supported by Microsoft on a best effort basis. In order to get production support with authoritative guidance, you can evaluate and reach out to ProxySQL Product support.
The basic setup for running ProxySQL as a service in AKS using Azure Database for MySQL is shown in Figure 1.
To prepare for setting up ProxySQL as a service in AKS, you need:
sudo az aks install-cli
ProxySQL will be configured to direct all the write workload to this server.
ProxySQL will be configured to direct all the read workload to this server.
Important: Enforcement of SSL connections is enabled by default in Azure Database for MySQL. You should avoid disabling the SSL option and instead configure ProxySQL to use SSL as outlined below.
Setting up ProxySQL as a service in AKS using Azure Database for MySQL involves the following tasks:
With ProxySQL, the user connects to ProxySQL and in turn ProxySQL passes the connection to the MySQL node. To allow ProxySQL to access to the MySQL database, you need to create a user on the MySQL database with the same credentials as on the ProxySQL server
mysql -hmydemomaster.mysql.database.azure.com -umydemo@mydemomaster -p
CREATE USER 'mydemouser'@'%' IDENTIFIED BY 'secretpassword';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'mydemouser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
For the purposes of this post, we use the configMap to deploy the configuration to the ProxySQL container. Begin with the sample ProxySQL configMap file provided below.
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin"
mysql_ifaces="0.0.0.0:6032"
}
mysql_variables=
{
threads=4
max_connections=2048
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:3306;/tmp/proxysql.sock"
default_schema="information_schema"
stacksize=1048576
server_version="5.1.30"
connect_timeout_server=10000
monitor_history=60000
monitor_connect_interval=200000
monitor_ping_interval=200000
ping_interval_server_msec=10000
ping_timeout_server=200
commands_stats=true
sessions_sort=true
monitor_username="proxysql"
monitor_password="proxysqlsecretpassword"
}
mysql_servers =
(
{ hostgroup_id=1, hostname="mydemomaster.mysql.database.azure.com", port=3306 , weight=1, comment="write Group", use_ssl=1 },
{ hostgroup_id=2, hostname="mydemoreplica.mysql.database.azure.com", port=3306 , weight=1, comment="Read Group", use_ssl=1 }
)
mysql_users =
(
{ username = "mydemouser" , password = "secretpassword" , default_hostgroup = 1 , active = 1 }
)
mysql_query_rules =
(
{
rule_id=1
active=1
match_digest="^SELECT .* FOR UPDATE"
destination_hostgroup=1
apply=1
},
{
rule_id=2
active=1
match_digest="^SELECT .*"
destination_hostgroup=2
apply=1
},
)
Begin with the sample ProxySQL deployment file below. This template uses the latest ProxySQL docker image. You can update the number of replicas, restart policy and container port for ProxySQL.
apiVersion: apps/v1
kind: Deployment
metadata:
name: proxysql
spec:
replicas: 1
selector:
matchLabels:
app: proxysql
tier: frontend
template:
metadata:
labels:
app: proxysql
tier: frontend
spec:
restartPolicy: Always
containers:
- name: proxysql
image: proxysql/proxysql:latest
volumeMounts:
- name: "proxysql-config"
mountPath: "/etc/proxysql.cnf"
subPath: "proxysql.cnf"
ports:
- containerPort: 3306
name: "proxysql-mysqldb"
- containerPort: 6032
name: "proxysql-admin"
volumes:
- name: "proxysql-config"
configMap:
name: "proxysql-configmap"
---
apiVersion: v1
kind: Service
metadata:
name: proxysql
labels:
app: proxysql
tier: frontend
spec:
type: LoadBalancer
ports:
- port: 3306
name: "proxysql-mysqldb"
- port: 6032
name: "proxysql-admin"
selector:
app: proxysql
tier: frontend
Next, you’ll deploy ProxySQL on AKS.
az aks get-credentials --name aks-cluster-name --resource-group "mydemo"
kubectl create configmap proxysql-configmap --from-file=proxysql.cnf
kubectl apply -f ./proxysqlaks.yml
kubectl get service proxysql -w
[Use CTRL + C to break out]kubectl get pods,services -o wide
mysql -h[External_IP] -umydemouser -psecretpassword -P3306
To learn how to scale your WordPress application using ProxySQL deployed as service in AKS to perform transparent read/write split across Azure Database for MySQL replicas, see the blog post scaling your Azure Database for MySQL workload running on Kubernetes with read replicas and ProxySQL.
If you have trouble setting up ProxySQL as a service using Azure Database for MySQL, please contact the Azure Database for MySQL team at AskAzureDBforMySQL@service.microsoft.com.
Thank you!
Amol Bhatnagar
Program Manager - Microsoft
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.