Vitess(MySQL 프로토콜)를 사용한 데이터베이스 접근
Teleport can provide secure access to Vitess (MySQL) via the Teleport Database Service. The Teleport Database Service proxies traffic from database clients to self-hosted databases in your infrastructure.
Teleport can provide secure access to Vitess (MySQL) via the Teleport Database Service. This allows for fine-grained access control through the Teleport RBAC system.
The Teleport Database Service proxies traffic from database clients to self-hosted databases in your infrastructure. Teleport maintains a certificate authority (CA) for database clients. You configure your database to trust the Teleport database client CA, and the Teleport Database Service presents certificates signed by this CA when proxying user traffic. With this setup, there is no need to store long-lived credentials for self-hosted databases.
Meanwhile, the Teleport Database Service verifies self-hosted databases by checking their TLS certificates against either the Teleport database CA or a custom CA used with the database.
In this guide, you will:
- Configure your Vitess (MySQL) database for Teleport access.
- Add the database to your Teleport cluster.
- Connect to the database via Teleport.
동작 원리#
The Teleport Database Service authenticates to your self-hosted Vitess database using mutual TLS. Vitess trusts the Teleport certificate authority for database clients, and presents a certificate signed by either the Teleport database CA or a custom CA. When a user initiates a database session, the Teleport Database Service presents a certificate signed by Teleport. The authenticated connection then proxies client traffic from the user.


gRPC 프로토콜을 사용하는 Vitess 접근은 현재 Teleport에서 지원되지 않습니다.
사전 요구사항#
-
A running Teleport cluster. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.
-
The
tctlandtshclients.Installing `tctl` and `tsh` clients
-
Determine the version of your Teleport cluster. The
tctlandtshclients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at/v1/webapi/findand use a JSON query tool to obtain your cluster version. Replace with the web address of your Teleport Proxy Service:$ TELEPORT_DOMAIN= $ TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')" -
Follow the instructions for your platform to install
tctlandtshclients:
-
- 셀프 호스팅 Vitess 인스턴스.
- Teleport Database Service를 실행할 호스트(예: Amazon EC2 인스턴스).
- 선택 사항: 셀프 호스팅 데이터베이스에 인증서를 발급하는 인증 기관.
To check that you can connect to your Teleport cluster, sign in with tsh login, then
verify that you can run tctl commands using your current credentials.
For example, run the following command, assigning to the domain name of the Teleport Proxy Service in your cluster and to your Teleport username:
$ tsh login --proxy= --user=
$ tctl status
# Cluster (=teleport.url=)
# Version (=teleport.version=)
# CA pin (=presets.ca_pin=)
If you can connect to the cluster and run the tctl status command, you can use your
current credentials to run subsequent tctl commands from your workstation.
If you host your own Teleport cluster, you can also run tctl commands on the computer that
hosts the Teleport Auth Service for full permissions.
1/4단계. Teleport Database 토큰 생성#
The Database Service requires a valid join token to join your Teleport cluster.
Run the following tctl command and save the token output in /tmp/token
on the server that will run the Database Service:
$ tctl tokens add --type=db --format=text
(=presets.tokens.first=)
2/4단계. 인증서/키 쌍 생성#
Teleport uses mutual TLS authentication with self-hosted databases. These databases must be able to verify certificates presented by the Teleport Database Service. Self-hosted databases also need a certificate/key pair that Teleport can verify.
By default, the Teleport Database Service trusts certificates issued by a certificate authority managed by the Teleport Auth Service. You can either:
- Configure your self-hosted database to trust this CA, and instruct Teleport to issue a certificate for the database to present to the Teleport Database Service.
- Configure the Database Service to trust a custom CA.
3/4단계. Vitess 구성#
Kubernetes용 Vitess Operator를 사용하여 Vitess 클러스터를 실행하는 경우,
먼저 server.cas, server.crt, server.key 파일의 내용을 클러스터 구성이 포함된 시크릿에 복사합니다.
---
apiVersion: v1
kind: Secret
metadata:
name: example-cluster-config
type: Opaque
stringData:
server.cas: |
-----BEGIN CERTIFICATE-----
MIIDlDCCAnygAwIBAgIQcCge3zdTWnA7isWitaG5yzANBgkqhkiG9w0BAQsFADBk
...
jtOP8B0/0xc=
-----END CERTIFICATE-----
server.crt: |
-----BEGIN CERTIFICATE-----
MIIDqjCCApKgAwIBAgIRAKq0OQqYIx3pbkSVpIgMooowDQYJKoZIhvcNAQELBQAw
...
aRWuAdb7KYfHgZgC+k5jiFS9MYPbOc3qMK6KwGAU
-----END CERTIFICATE-----
server.key: |
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAsiejNWoNPPgcjjNZvG0pA+eADXxPyiGf6Or7oiy2ZmkblC4I
...
hr6KW+m+bBx0ABXrJVZ4dfv7ppP173vhavmSG3dvo2D5savAay6L/bE=
-----END RSA PRIVATE KEY-----
다음으로 게이트웨이 구성을 업데이트합니다:
mysql_auth_server_impl: clientcert항목이 있는 새extraFlags섹션을 추가합니다.- 추가된 파일을 참조하는 새
secureTransport섹션을 추가합니다.
cells:
- name: zone1
gateway:
extraFlags:
mysql_auth_server_impl: clientcert
secureTransport:
required: true
tls:
clientCACertSecret:
name: teleport-cluster-config
key: server.cas
certSecret:
name: teleport-cluster-config
key: server.crt
keySecret:
name: teleport-cluster-config
key: server.key
사용자 정의 배포를 사용하여 Vitess 클러스터를 실행하는 경우 vtgate 서비스의 플래그를 업데이트해야 합니다. 다음 플래그를 추가해야 합니다:
vtgate ... \
--mysql_server_ssl_ca=/path/to/server.cas \
--mysql_server_ssl_cert=/path/to/server.crt \
--mysql_server_ssl_key=/path/to/server.key \
--mysql_auth_server_impl=clientcert
server.cas, server.crt, server.key 파일은 vtgate 서비스에서 접근 가능한 위치에 있어야 합니다. 이 플래그를 적용하기 위해 Vitess 서비스를 재시작합니다.
vtgate 및 Vitess 구성에 대한 자세한 내용은 문서를 참조하십시오.
Teleport 사용자 생성#
| Flag | Description |
|---|---|
--roles |
List of roles to assign to the user. The builtin access role allows them to connect to any database server registered with Teleport. |
--db-users |
List of database usernames the user will be allowed to use when connecting to the databases. A wildcard allows any user. |
--db-names |
List of logical databases (aka schemas) the user will be allowed to connect to within a database server. A wildcard allows any database. |
For more detailed information about database access controls and how to restrict access see RBAC documentation.
Database Service 구성 및 시작#
Teleport Database Service를 실행할 위치에 Teleport를 설치하고 구성합니다:
To install a Teleport Agent on your Linux server:
The recommended installation method is the cluster install script. It will select the correct version, edition, and installation mode for your cluster.
-
Assign to your Teleport cluster hostname and port, but not the scheme (https://).
-
Run your cluster's install script:
$ curl "https:///scripts/install.sh" | sudo bash
On the host where you will run the Teleport Database Service, start Teleport with the appropriate configuration.
Note that a single Teleport process can run multiple different services, for
example multiple Database Service agents as well as the SSH Service or Application
Service. The step below will overwrite an existing configuration file, so if
you're running multiple services add --output=stdout to print the config in
your terminal, and manually adjust /etc/teleport.yaml.
Run the following command to generate a configuration file at
/etc/teleport.yaml for the Database Service. Update
to use the host and port of the Teleport Proxy
Service:
$ sudo teleport db configure create \
-o file \
--token=/tmp/token \
--proxy=:443 \
--name=example-vitess \
--protocol=mysql \
--uri=db.example.com:3306 \
--labels=env=dev
To configure the Teleport Database Service to trust a custom CA:
-
Export a CA certificate for the custom CA and make it available at
/var/lib/teleport/db.caon the Teleport Database Service host. -
Run a variation of the command above that uses the
--ca-cert-fileflag. This configures the Teleport Database Service to use the CA certificate atdb.cato verify traffic from the database:$ sudo teleport db configure create \ -o file \ --token=/tmp/token \ --proxy=:443 \ --name=example-vitess \ --protocol=mysql \ --uri=db.example.com:3306 \ --ca-cert-file="/var/lib/teleport/db.ca" \ --labels=env=dev
If your database servers use certificates that are signed by a public CA such
as ComodoCA or DigiCert, you can use the trust-system-cert-pool option
without exporting the CA:
$ sudo teleport db configure create \
-o file \
--token=/tmp/token \
--proxy=:443 \
--name=example-vitess \
--protocol=mysql \
--uri=db.example.com:3306 \
--trust-system-cert-pool \
--labels=env=dev
(!docs/pages/includes/start-teleport.mdx service="the Teleport Database Service"!)
Teleport는 Kubernetes 클러스터에 Teleport Database Service를 설치하기 위한 Helm 차트를 제공합니다.
Configure Helm to fetch Teleport charts from the Teleport Helm repository:
$ helm repo add teleport (=teleport.helm_repo_url=)
Refresh the local Helm cache by fetching the latest charts:
$ helm repo update
Install a Teleport Agent into your Kubernetes Cluster with the Teleport Database Service configuration.
Create a file called values.yaml with the following content. Update to use the host and port of the Teleport Proxy
Service and to the join token you created earlier:
roles: db
proxyAddr: name="example.teleport.sh" />
# Set to false if using Teleport Community Edition
enterprise: true
authToken: "JOIN_TOKEN" />"
databases:
- name: example-vitess
uri: db.example.com:3306
protocol: mysql
static_labels:
env: dev
To configure the Teleport Database Service to trust a custom CA:
-
Export a CA certificate for the custom CA and make it available at
db.caon your workstation. -
Create a secret containing the database CA certificate in the same namespace as Teleport using the following command:
$ kubectl create secret generic db-ca --from-file=ca.pem=/path/to/db.ca -
Add the following to
values.yaml:roles: db proxyAddr: example.teleport.sh # Set to false if using Teleport Community Edition enterprise: true authToken: JOIN_TOKEN databases: - name: example-vitess uri: db.example.com:3306 protocol: mysql + tls: + ca_cert_file: "/etc/teleport-tls-db/db-ca/ca.pem" static_labels: env: dev + extraVolumes: + - name: db-ca + secret: + secretName: db-ca + extraVolumeMounts: + - name: db-ca + mountPath: /etc/teleport-tls-db/db-ca + readOnly: true -
Install the chart:
$ helm install teleport-kube-agent teleport/teleport-kube-agent \ --create-namespace \ --namespace teleport-agent \ --version (=teleport.version=) \ -f values.yaml -
Make sure that the Teleport Agent pod is running. You should see one
teleport-kube-agentpod with a single ready container:$ kubectl -n teleport-agent get pods NAME READY STATUS RESTARTS AGE teleport-kube-agent-0 1/1 Running 0 32s
(!docs/pages/includes/database-access/multiple-instances-tip.mdx !)
4/4단계. 연결#
Database Service가 클러스터에 참여하면 로그인하여 사용 가능한 데이터베이스를 확인합니다:
$ tsh login --proxy=teleport.example.com --user=alice
$ tsh db ls
# Name Description Labels
# -------------- -------------- --------
# example-vitess Example Vitess env=dev
$ tsh login --proxy=mytenant.teleport.sh --user=alice
$ tsh db ls
# Name Description Labels
# -------------- -------------- --------
# example-vitess Example Vitess env=dev
역할이 접근할 수 있는 데이터베이스만 볼 수 있습니다. 자세한 내용은 RBAC 가이드를 참조하십시오.
데이터베이스에 대한 자격 증명을 검색하고 연결하려면:
$ tsh db connect example-vitess
데이터베이스 인스턴스에 연결할 때 기본적으로 사용할 데이터베이스 이름과 사용자를 선택적으로 지정할 수 있습니다:
$ tsh db connect --db-user=root --db-name=mysql example-vitess
연결할 수 있으려면 mysql 또는 mariadb 명령줄 클라이언트가 PATH에 있어야 합니다. mariadb는 MySQL 및 MariaDB의 기본 명령줄 클라이언트입니다.
데이터베이스에서 로그아웃하고 자격 증명을 제거하려면:
# 특정 데이터베이스 인스턴스에 대한 자격 증명 제거.
$ tsh db logout example-vitess
# 모든 데이터베이스 인스턴스에 대한 자격 증명 제거.
$ tsh db logout
