Skip to content

Commit

Permalink
docs: adjust format
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle-0808 authored and weicao committed Sep 10, 2024
1 parent d99768e commit 1c69b7e
Showing 1 changed file with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,45 +50,52 @@ The password does not include -p.
</TabItem>
<TabItem value="kubectl" label="kubectl">


## Step 1. Retrieve Database Credentials

Before connecting to the MySQL database running inside your Kubernetes cluster, you may need to retrieve the username and password from a Kubernetes Secret. Secrets in Kubernetes are typically base64-encoded, so you will need to decode them to obtain the actual credentials. Here’s how you can do this with kubectl.

1. Retrieve the `username`:
Use the kubectl get secrets command to extract the username from the secret named `mycluster-conn-credential` in the demo namespace.

Use the kubectl get secrets command to extract the username from the secret named `mycluster-conn-credential` in the demo namespace.

```bash
kubectl get secrets -n demo mycluster-conn-credential -o jsonpath='{.data.\username}' | base64 -d
>
root
```
- Replace "mycluster" with the actual name of your database cluster.
- Replace "demo" with the actual namespace of your database cluster.

- Replace "mycluster" with the actual name of your database cluster.
- Replace "demo" with the actual namespace of your database cluster.

2. Retrieve the `password`:

```bash
kubectl get secrets -n demo mycluster-conn-credential -o jsonpath='{.data.\password}' | base64 -d
>
2gvztbvz
```
- Replace "mycluster" with the actual name of your database cluster.
- Replace "demo" with the actual namespace of your database cluster.

- Replace "mycluster" with the actual name of your database cluster.
- Replace "demo" with the actual namespace of your database cluster.

:::note
In most KubeBlocks v0.9 addons, the secret that contains the connection credentials follows the naming pattern {cluster.name}-conn-credential. However, in some newer versions of KubeBlocks addons, the naming of the secret may have changed to {cluster.name}-{component.name}-account-{account.name}. To ensure you are using the correct secret name, run the following command to list all secrets in the namespace and search for the ones related to your database cluster.

In most KubeBlocks v0.9 addons, the secret that contains the connection credentials follows the naming pattern `{cluster.name}-conn-credential`. However, in some newer versions of KubeBlocks addons, the naming of the secret may have changed to `{cluster.name}-{component.name}-account-{account.name}`. To ensure you are using the correct secret name, run the following command to list all secrets in the namespace and search for the ones related to your database cluster.

Example:

```bash
kubectl get secrets -n demo | grep mycluster
```
:::

:::

## Step 2. Connect to the cluster
## Step 2. Connect to the cluster

After retrieving the credentials, you can connect to the MySQL database running in your Kubernetes cluster using one of two methods:
- Using kubectl exec to connect directly within the Pod.
- Using kubectl port-forward to access the database from your local machine.

- Using `kubectl exec` to connect directly within the Pod.
- Using `kubectl port-forward` to access the database from your local machine.

### Option 1: Use kubectl exec command in pod

Expand All @@ -101,14 +108,15 @@ In some cases, you may need to connect directly to a MySQL database running insi
```bash
kubectl exec -ti -n demo mycluster-mysql-0 -- bash
```
- -ti: Opens an interactive terminal session (-t allocates a pseudo-TTY, and -i keeps the session open).
- -n demo: Specifies the demo namespace where your Pod is running.
- mycluster-mysql-0: The name of the MySQL Pod. Make sure to replace this with the actual Pod name if it's different.
- -- bash: Opens a Bash shell inside the Pod. You can also use sh if Bash is not available in the container.
- `-ti`: Opens an interactive terminal session (-t allocates a pseudo-TTY, and -i keeps the session open).
- `-n demo`: Specifies the demo namespace where your Pod is running.
- `mycluster-mysql-0`: The name of the MySQL Pod. Make sure to replace this with the actual Pod name if it's different.
- `-- bash`: Opens a Bash shell inside the Pod. You can also use sh if Bash is not available in the container.

2. Connect to the cluster.

Once inside the Pod, you can use the MySQL client to connect to the database service running within the same Pod or cluster. Since you're already inside the Pod, you don't need to specify an external host or address.
Once inside the Pod, you can use the MySQL client to connect to the database service running within the same Pod or cluster. Since you're already inside the Pod, you don't need to specify an external host or address.

```bash
mysql -u root -p2gvztbvz
Expand All @@ -122,22 +130,23 @@ Here is an example of using CLI to connect to the cluster on the local host.

1. Forward the Port Using `kubectl port-forward`.

First, you'll need to forward a port from your local machine to the MySQL service running in Kubernetes. This command forwards your local port 3306 (the default MySQL port) to the same port on the MySQL service inside the cluster.
First, you'll need to forward a port from your local machine to the MySQL service running in Kubernetes. This command forwards your local port 3306 (the default MySQL port) to the same port on the MySQL service inside the cluster.
```bash
```bash
kubectl port-forward svc/mycluster-mysql 3306:3306 -n demo
```
- svc/mycluster-mysql: Refers to the MySQL service in your Kubernetes cluster.
- 3306:3306: Binds the local port 3306 to the service's port 3306.
- -n demo: Specifies the namespace demo where the MySQL service is running.
```
- `svc/mycluster-mysql`: Refers to the MySQL service in your Kubernetes cluster.
- `3306:3306`: Binds the local port 3306 to the service's port 3306.
- `-n demo`: Specifies the namespace demo where the MySQL service is running.

2. Connect to the Database Locally.

Once the port is forwarded, you can connect to the MySQL database using a standard MySQL client as if it were running locally on 127.0.0.1 (localhost). The connection will be securely tunneled to the service inside the cluster.
Once the port is forwarded, you can connect to the MySQL database using a standard MySQL client as if it were running locally on 127.0.0.1 (localhost). The connection will be securely tunneled to the service inside the cluster.

```bash
```bash
mysql -h 127.0.0.1 -P 3306 -u root -p2gvztbvz
```
```

</TabItem>
</Tabs>
Expand Down

0 comments on commit 1c69b7e

Please sign in to comment.