github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-mysql/proxy/apecloud-mysql-proxy.md (about)

     1  ---
     2  title: How to use ApeCloud MySQL Proxy Cluster
     3  description: ApeCloud MySQL Proxy Cluster tutorial
     4  keywords: [apecloud mysql proxy, proxy]
     5  sidebar_position: 2
     6  sidebar_label: ApeCloud MySQL Proxy Cluster
     7  ---
     8  
     9  import Tabs from '@theme/Tabs';
    10  import TabItem from '@theme/TabItem';
    11  
    12  # ApeCloud MySQL Proxy
    13  
    14  ## Before you start
    15  
    16  1. [Install kbcli](./../../installation/install-with-kbcli/install-kbcli.md).
    17  2. Install KubeBlocks.
    18  
    19     You can run `kbcli playground init` to install a k3d cluster and KubeBlocks. For details, refer to [Try KubeBlocks on your laptop](./../../try-out-on-playground/try-kubeblocks-on-your-laptop.md) or [Try KubeBlocks on cloud](./../../try-out-on-playground/try-kubeblocks-on-cloud.md).
    20  
    21     ```bash
    22     kbcli playground init
    23  
    24     # Use --version to specify a version
    25     kbcli playground init --version='0.6.0'
    26     ```
    27  
    28     Or if you already have a Kubernetes cluster, you can choose install KubeBlocks by [Helm](./../../installation/install-with-helm/install-kubeblocks-with-helm) or [kbcli](./../../installation/install-with-kbcli/install-kubeblocks-with-kbcli.md).
    29  3. Prepare an ApeCloud MySQL RaftGroup named `mycluster` for demonstrating how to enable the proxy function for an existing cluster. Refer to [Create a MySQL cluster](./../cluster-management/create-and-connect-a-mysql-cluster.md#create-a-mysql-cluster) for details.
    30  
    31  ## Create a Proxy Cluster
    32  
    33  <Tabs>
    34  
    35  <TabItem value="kbcli" label="kbcli" default>
    36  
    37  It is recommended to use kbcli to create an ApeCloud MySQL Proxy Cluster.
    38  
    39  ```bash
    40  kbcli cluster create mysql myproxy --mode raftGroup --availability-policy none --proxy-enabled true
    41  ```
    42  
    43  </TabItem>
    44  
    45  <TabItem value="Helm" label="Helm">
    46  
    47  1. Add the KubeBlocks repository.
    48  
    49     ```bash
    50     helm repo add kubeblocks https://apecloud.github.io/helm-charts
    51     ```
    52  
    53  2. View the repository list to verify whether the KubeBlocks repository is added successfully.
    54  
    55     ```bash
    56     helm repo list
    57     ```
    58  
    59  3. Run the update command to make sure you have added the latest version.
    60  
    61     ```bash
    62     helm repo update
    63     ```
    64  
    65  4. View all versions of ApeCloud MySQL Proxy.
    66  
    67     ```bash
    68     helm search repo kubeblocks/apecloud-mysql --devel --versions
    69     ```
    70  
    71  5. (Optional) If you disable the `apecloud-mysql` add-on when installing KuebBlocks, run the command below to specify a version and install the cluster definition of ApeCloud MySQL. Skip this step if you install KubeBlocks with the default settings.
    72  
    73     ```bash
    74     helm install myproxy kubeblocks/apecloud-mysql --version=v0.6.0
    75     ```
    76  
    77  6. Create an ApeCloud MySQL Proxy Cluster.
    78  
    79     ```bash
    80     helm install myproxy kubeblocks/apecloud-mysql-cluster --version=v0.6.0 --set mode=raftGroup,proxyEnabled=true 
    81     ```
    82  
    83  :::note
    84  
    85  If you only have one node for deploying a RaftGroup, set the `availability-policy` as `none` when creating a RaftGroup.
    86  
    87  ```bash
    88  helm install myproxy kubeblocks/apecloud-mysql-cluster --version=v0.6.0 --set mode=raftGroup,proxyEnabled=true --set extra.availabilityPolicy=none
    89  ```
    90  
    91  :::
    92  
    93  </TabItem>
    94  
    95  </Tabs>
    96  
    97  ## Enable Proxy dynamically
    98  
    99  As its name suggests, ApeCloud MySQL Proxy in nature is a database proxy. An ApeCloud MySQL RaftGroup Cluster can be switched to an ApeCloud MySQL Proxy Cluster by setting `proxyEnabled=true`.
   100  
   101  <Tabs>
   102  <TabItem value="kbcli" label="kbcli" default>
   103  
   104  Coming soon...
   105  
   106  </TabItem>
   107  
   108  <TabItem value="kubectl" label="kubectl">
   109  
   110  ```bash
   111  helm upgrade mycluster kubeblocks/apecloud-mysql-cluster --set mode=raftGroup,proxyEnabled=true
   112  ```
   113  
   114  </TabItem>
   115  
   116  </Tabs>
   117  
   118  ## Connect Proxy Cluster
   119  
   120  ApeCloud MySQL Proxy is routed through the `vtgate` component, and the way the MySQL Server accesses `vtgate` is similar to the way of accessing `mysqld`. The external SQL access address provided by ApeCloud MySQL Proxy is the `vtgate` address and port. The `vtgate` address created by KubeBlocks by default is `myproxy-cluster-vtgate-headless`, and the port number is `15306`. You can visit ApeCloud MySQL Proxy through the MySQL Server in any pod under the same namespace as ApeCloud MySQL Proxy.
   121  
   122  ### Connect Proxy Cluster by VTGate
   123  
   124  <Tabs>
   125  <TabItem value="kbcli" label="kbcli" default>
   126  
   127  Run the command below to connect to the Proxy Cluster.
   128  
   129  ```bash
   130  kbcli cluster connect myproxy --component vtgate
   131  ```
   132  
   133  </TabItem>
   134  
   135  <TabItem value="port-forward" label="port-forward">
   136  
   137  1. Expose the port of VTGate to the localhost so that the localhost can access the Proxy.
   138  
   139     ```bash
   140     kubectl port-forward svc/vt-vtgate-headless 15306:15306
   141     ```
   142  
   143  2. Connect to the cluster.
   144  
   145     ```bash
   146     mysql -h 127.0.0.1 -P 15306
   147     ```
   148  
   149  </TabItem>
   150  </Tabs>
   151  
   152  ### Connect Proxy Cluster by MySQL Server
   153  
   154  <Tabs>
   155  
   156  <TabItem value="kbcli" label="kbcli" default>
   157  
   158  Run the command below to connect to the MySQL Server.
   159  
   160  ```bash
   161  kbcli cluster connect myproxy
   162  ```
   163  
   164  </TabItem>
   165  
   166  <TabItem value="port-forward" label="port-forward">
   167  
   168  1. Expose the port of the MySQL Server to the localhost so that the localhost can access the MySQL Server.
   169  
   170     ```bash
   171     kubectl port-forward svc/vt-mysql 3306:3306
   172     ```
   173  
   174  2. Connect to the cluster.
   175  
   176     ```bash
   177     mysql -h 127.0.0.1 -P 3306
   178     ```
   179  
   180  </TabItem>
   181  </Tabs>
   182  
   183  :::note
   184  
   185  If you need to test the failover of MySQL, you need to delete the Pod first and continue to port-forward the port, and you can also write a shell script. Here are examples.
   186  
   187  For VTGate,
   188  
   189  ```bash
   190  while true; do date; kubectl port-forward svc/vt-vtgate-headless 15306:15306; sleep 0.5; done
   191  ```
   192  
   193  For the MySQL Server,
   194  
   195  ```bash
   196  while true; do date; kubectl port-forward svc/vt-mysql 3306:3306; sleep 0.5; done
   197  ```
   198  
   199  :::
   200  
   201  ## Configure Proxy Cluster parameters
   202  
   203  VTGate, VTConsensus, and VTTablet support parameter configuration. You can configure VTGate and VTConsensus by using `--component` to specify a component and configure VTTablet by using `--component=mysql --config-specs=vttablet-config` to specify both a component and a configuration file template since VTTablet is the sidecar of the MySQL component.
   204  
   205  ### View parameter details
   206  
   207  * View the details of the current configuration file.
   208  
   209     ```bash
   210     # vtgate
   211     kbcli cluster describe-config myproxy --component vtgate --show-detai
   212     
   213     # vtcontroller
   214     kbcli cluster describe-config myproxy --component vtcontroller --show-detail
   215     
   216     # vttablet
   217     kbcli cluster describe-config myproxy --component mysql --show-detail --config-specs vttablet-config
   218     ```
   219  
   220  * View the parameter descriptions.
   221  
   222     ```bash
   223     # vtgate
   224     kbcli cluster explain-config myproxy --component vtgate
   225  
   226     # vttablet
   227     kbcli cluster explain-config myproxy --component mysql --config-specs=vttablet-config
   228     ```
   229  
   230  * View the definition of a specified parameter.
   231  
   232     ```bash
   233     kbcli cluster explain-config myproxy --component vtgate --param=healthcheck_timeout
   234     ```
   235  
   236  ### Reconfigure parameters
   237  
   238  1. View the current values in the MySQL Server.
   239  
   240     ```bash
   241     kbcli cluster connect myproxy --component=vtgate
   242     ```
   243  
   244     ```bash
   245     mysql> show variables like '%healthcheck_timeout%';
   246     ```
   247  
   248     ```bash
   249     mysql> show variables like '%health_check_interval%';
   250     ```
   251  
   252  2. Configure the `healthcheck_timeout` for VTGate and the `health_check_interval` for VTTablet.
   253  
   254     You can use `--set` flag or edit the parameter configuration file to edit values.
   255  
   256     * By using `--set` flag
   257  
   258        ```bash
   259        # vtgate
   260        kbcli cluster configure myproxy --component vtgate --set=healthcheck_timeout=2s
   261  
   262        # vttablet
   263        kbcli cluster configure myproxy --set=health_check_interval=4s --component=mysql --config-spec=vttablet-config
   264        ```
   265  
   266     * By editing the parameter configuration file
   267  
   268        ```bash
   269        kbcli cluster edit-config myproxy --component vtgate
   270        ```
   271  
   272  :::note
   273  
   274  After the `vtgate` parameter values configuration command is executed, a new vtgate Pod is started and the old vtgate Pod is terminated. You can run the command below to monitor whether the old Pod is terminated.
   275  
   276  ```bash
   277  kubectl get pod <vtgate-pod-name> -w
   278  ```
   279  
   280  :::
   281  
   282  3. Use the output command to view the configuration status. For example,
   283  
   284     ```bash
   285     kbcli cluster describe-ops myproxy -reconfiguring-lth8d -n default
   286     ```
   287  
   288  :::note
   289  
   290  For more information about parameter configuration, refer to [Configuration](./../configuration/configuration.md).
   291  
   292  :::
   293  
   294  ## Log
   295  
   296  You can view the log files of components, Pods, and containers by both kbcli and kubectl.
   297  
   298  <Tabs>
   299  
   300  <TabItem value="kbcli" label="kbcli" default>
   301  
   302  View the log of different components.
   303  
   304  ```bash
   305  kbcli cluster list-logs myproxy
   306  kbcli cluster list-logs myproxy --component vtgate
   307  kbcli cluster list-logs myproxy --component vtcontroller
   308  kbcli cluster list-logs myproxy --component mysql
   309  ```
   310  
   311  View the log of a Pod.
   312  
   313  ```bash
   314  kbcli cluster logs myproxy --instance myproxy-vtgate-85bdcf99df-wbmnl
   315  ```
   316  
   317  View the log of a container in a Pod.
   318  
   319  ```bash
   320  kbcli cluster logs myproxy --instance myproxy-mysql-0 -c vttablet
   321  ```
   322  
   323  </TabItem>
   324  
   325  <TabItem value="kubectl" label="kubectl" default>
   326  
   327  View the log of VTGate.
   328  
   329  ```bash
   330  kubectl logs myproxy-cluster-vtgate-8659d5db95-4dzt5
   331  ```
   332  
   333  View the log of VTTable and `-c` is required.
   334  
   335  ```bash
   336  kubectl logs myproxy-cluster-mysql-0 -c vttablet
   337  ```
   338  
   339  Enter the container and view more logs of VTGate.
   340  
   341  ```bash
   342  kubectl exec -it myproxy-cluster-vtgate-8659d5db95-4dzt5 -- bash
   343  ls /vtdataroot
   344  ```
   345  
   346  Enter the container and view more logs of VTTable.
   347  
   348  ```bash
   349  kubectl exec -it myproxy-cluster-mysql-0  -c vttablet -- bash
   350  ls /vtdataroot
   351  ```
   352  
   353  </TabItem>
   354  
   355  </Tabs>
   356  
   357  ## Monitoring
   358  
   359  :::note
   360  
   361  In the production environment, all monitoring add-ons are disabled by default when installing KubeBlocks. You can enable these add-ons but it is highly recommended to build your monitoring system or purchase a third-party monitoring service. For details, refer to [Monitoring](./../../observability/monitor-database.md).
   362  
   363  :::
   364  
   365  1. Enable the monitoring function.
   366  
   367     ```bash
   368     kbcli cluster update myproxy --monitor=true
   369     ```
   370  
   371  2. View the add-on list and enable the Grafana add-on.
   372  
   373     ```bash
   374     kbcli addon list 
   375     
   376     kbcli addon enable grafana
   377     ```
   378  
   379  3. View the dashboard list.
   380  
   381     ```bash
   382     kbcli dashboard list
   383     ```
   384  
   385  4. Open the Grafana dashboard.
   386  
   387     ```bash
   388     kbcli dashboard open kubeblocks-grafana
   389     ```
   390  
   391  ## Read-write splitting
   392  
   393  You can enable the read-write splitting function.
   394  
   395  ```bash
   396  kbcli cluster configure myproxy --component vtgate --set=read_write_splitting_policy=random
   397  ```
   398  
   399  You can also set the ratio for read-write splitting and here is an example of directing 70% flow to the read-only node.
   400  
   401  ```bash
   402  kbcli cluster configure myproxy --component vtgate --set=read_write_splitting_ratio=70
   403  ```
   404  
   405  Moreover, you can [use Grafana](#monitoring) or run `show workload` to view the flow distribution.
   406  
   407  ```bash
   408  show workload;
   409  ```
   410  
   411  ## Transparent failover
   412  
   413  Run the command below to implement transparent failover.
   414  
   415  ```bash
   416  kbcli cluster configure myproxy --component vtgate --set=enable_buffer=true
   417  ```