Enable TLS for DM

This document describes how to enable TLS between components of the DM cluster in Kubernetes and how to use DM to migrate data between MySQL/TiDB databases that enable TLS for the MySQL client.

Enable TLS between DM components

Starting from v1.2, TiDB Operator supports enabling TLS between components of the DM cluster in Kubernetes.

To enable TLS between components of the DM cluster, perform the following steps:

  1. Generate certificates for each component of the DM cluster to be created:

    • A set of server-side certificates for the DM-master/DM-worker component, saved as the Kubernetes Secret objects: ${cluster_name}-${component_name}-cluster-secret
    • A set of shared client-side certificates for the various clients of each component, saved as the Kubernetes Secret objects: ${cluster_name}-dm-client-secret.

    Enable TLS for DM - 图1Note

    The Secret objects you created must follow the above naming convention. Otherwise, the deployment of the DM cluster will fail.

  2. Deploy the cluster, and set .spec.tlsCluster.enabled to true.

    Enable TLS for DM - 图2Note

    After the cluster is created, do not modify this field; otherwise, the cluster will fail to upgrade.

  3. Configure dmctl to connect to the cluster.

Certificates can be issued in multiple methods. This document describes two methods. You can choose either of them to issue certificates for the DM cluster:

If you need to renew the existing TLS certificate, refer to Renew and Replace the TLS Certificate.

Generate certificates for components of the DM cluster

This section describes how to issue certificates using two methods: cfssl and cert-manager.

Using cfssl

  1. Download cfssl and initialize the certificate issuer:

    1. mkdir -p ~/bin
    2. curl -s -L -o ~/bin/cfssl https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
    3. curl -s -L -o ~/bin/cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
    4. chmod +x ~/bin/{cfssl,cfssljson}
    5. export PATH=$PATH:~/bin
    6. mkdir -p cfssl
    7. cd cfssl
  2. Generate the ca-config.json configuration file:

    1. cat << EOF > ca-config.json
    2. {
    3. "signing": {
    4. "default": {
    5. "expiry": "8760h"
    6. },
    7. "profiles": {
    8. "internal": {
    9. "expiry": "8760h",
    10. "usages": [
    11. "signing",
    12. "key encipherment",
    13. "server auth",
    14. "client auth"
    15. ]
    16. },
    17. "client": {
    18. "expiry": "8760h",
    19. "usages": [
    20. "signing",
    21. "key encipherment",
    22. "client auth"
    23. ]
    24. }
    25. }
    26. }
    27. }
    28. EOF
  3. Generate the ca-csr.json configuration file:

    1. cat << EOF > ca-csr.json
    2. {
    3. "CN": "TiDB",
    4. "CA": {
    5. "expiry": "87600h"
    6. },
    7. "key": {
    8. "algo": "rsa",
    9. "size": 2048
    10. },
    11. "names": [
    12. {
    13. "C": "US",
    14. "L": "CA",
    15. "O": "PingCAP",
    16. "ST": "Beijing",
    17. "OU": "TiDB"
    18. }
    19. ]
    20. }
    21. EOF
  4. Generate CA by the configured option:

    1. cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
  5. Generate the server-side certificates:

    In this step, a set of server-side certificate is created for each component of the DM cluster.

    • DM-master

      First, generate the default dm-master-server.json file:

      1. cfssl print-defaults csr > dm-master-server.json

      Then, edit this file to change the CN and hosts attributes:

      1. ...
      2. "CN": "TiDB",
      3. "hosts": [
      4. "127.0.0.1",
      5. "::1",
      6. "${cluster_name}-dm-master",
      7. "${cluster_name}-dm-master.${namespace}",
      8. "${cluster_name}-dm-master.${namespace}.svc",
      9. "${cluster_name}-dm-master-peer",
      10. "${cluster_name}-dm-master-peer.${namespace}",
      11. "${cluster_name}-dm-master-peer.${namespace}.svc",
      12. "*.${cluster_name}-dm-master-peer",
      13. "*.${cluster_name}-dm-master-peer.${namespace}",
      14. "*.${cluster_name}-dm-master-peer.${namespace}.svc"
      15. ],
      16. ...

      ${cluster_name} is the name of the DM cluster. ${namespace} is the namespace in which the DM cluster is deployed. You can also add your customized hosts.

      Finally, generate the DM-master server-side certificate:

      1. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=internal dm-master-server.json | cfssljson -bare dm-master-server
    • DM-worker

      First, generate the default dm-worker-server.json file:

      1. cfssl print-defaults csr > dm-worker-server.json

      Then, edit this file to change the CN and hosts attributes:

      1. ...
      2. "CN": "TiDB",
      3. "hosts": [
      4. "127.0.0.1",
      5. "::1",
      6. "${cluster_name}-dm-worker",
      7. "${cluster_name}-dm-worker.${namespace}",
      8. "${cluster_name}-dm-worker.${namespace}.svc",
      9. "${cluster_name}-dm-worker-peer",
      10. "${cluster_name}-dm-worker-peer.${namespace}",
      11. "${cluster_name}-dm-worker-peer.${namespace}.svc",
      12. "*.${cluster_name}-dm-worker-peer",
      13. "*.${cluster_name}-dm-worker-peer.${namespace}",
      14. "*.${cluster_name}-dm-worker-peer.${namespace}.svc"
      15. ],
      16. ...

      ${cluster_name} is the name of the cluster. ${namespace} is the namespace in which the DM cluster is deployed. You can also add your customized hosts.

      Finally, generate the DM-worker server-side certificate:

      1. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=internal dm-worker-server.json | cfssljson -bare dm-worker-server
  6. Generate the client-side certificates:

    First, generate the default client.json file:

    1. cfssl print-defaults csr > client.json

    Then, edit this file to change the CN, hosts attributes. You can leave the hosts empty:

    1. ...
    2. "CN": "TiDB",
    3. "hosts": [],
    4. ...

    Finally, generate the client-side certificate:

    1. cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client.json | cfssljson -bare client
  7. Create the Kubernetes Secret object:

    If you have already generated a set of certificates for each component and a set of client-side certificate for each client as described in the above steps, create the Secret objects for the DM cluster by executing the following command:

    • The DM-master cluster certificate Secret:

      1. kubectl create secret generic ${cluster_name}-dm-master-cluster-secret --namespace=${namespace} --from-file=tls.crt=dm-master-server.pem --from-file=tls.key=dm-master-server-key.pem --from-file=ca.crt=ca.pem
    • The DM-worker cluster certificate Secret:

      1. kubectl create secret generic ${cluster_name}-dm-worker-cluster-secret --namespace=${namespace} --from-file=tls.crt=dm-worker-server.pem --from-file=tls.key=dm-worker-server-key.pem --from-file=ca.crt=ca.pem
    • Client certificate Secret:

      1. kubectl create secret generic ${cluster_name}-dm-client-secret --namespace=${namespace} --from-file=tls.crt=client.pem --from-file=tls.key=client-key.pem --from-file=ca.crt=ca.pem

    You have created two Secret objects:

    • One Secret object for each DM-master/DM-worker server-side certificate to load when the server is started;
    • One Secret object for their clients to connect.

Using cert-manager

  1. Install cert-manager.

    Refer to cert-manager installation in Kubernetes for details.

  2. Create an Issuer to issue certificates to the DM cluster.

    To configure cert-manager, create the Issuer resources.

    First, create a directory which saves the files that cert-manager needs to create certificates:

    1. mkdir -p cert-manager
    2. cd cert-manager

    Then, create a dm-cluster-issuer.yaml file with the following content:

    1. apiVersion: cert-manager.io/v1
    2. kind: Issuer
    3. metadata:
    4. name: ${cluster_name}-selfsigned-ca-issuer
    5. namespace: ${namespace}
    6. spec:
    7. selfSigned: {}
    8. ---
    9. apiVersion: cert-manager.io/v1
    10. kind: Certificate
    11. metadata:
    12. name: ${cluster_name}-ca
    13. namespace: ${namespace}
    14. spec:
    15. secretName: ${cluster_name}-ca-secret
    16. commonName: "TiDB"
    17. isCA: true
    18. duration: 87600h # 10yrs
    19. renewBefore: 720h # 30d
    20. issuerRef:
    21. name: ${cluster_name}-selfsigned-ca-issuer
    22. kind: Issuer
    23. ---
    24. apiVersion: cert-manager.io/v1
    25. kind: Issuer
    26. metadata:
    27. name: ${cluster_name}-dm-issuer
    28. namespace: ${namespace}
    29. spec:
    30. ca:
    31. secretName: ${cluster_name}-ca-secret

    ${cluster_name} is the name of the cluster. The above YAML file creates three objects:

    • An Issuer object of the SelfSigned type, used to generate the CA certificate needed by Issuer of the CA type;
    • A Certificate object, whose isCa is set to true.
    • An Issuer, used to issue TLS certificates between components of the DM cluster.

    Finally, execute the following command to create an Issuer:

    1. kubectl apply -f dm-cluster-issuer.yaml
  3. Generate the server-side certificate.

    In cert-manager, the Certificate resource represents the certificate interface. This certificate is issued and updated by the Issuer created in Step 2.

    Each component needs a server-side certificate, and all components need a shared client-side certificate for their clients.

    • The DM-master server-side certificate

      1. apiVersion: cert-manager.io/v1
      2. kind: Certificate
      3. metadata:
      4. name: ${cluster_name}-dm-master-cluster-secret
      5. namespace: ${namespace}
      6. spec:
      7. secretName: ${cluster_name}-dm-master-cluster-secret
      8. duration: 8760h # 365d
      9. renewBefore: 360h # 15d
      10. subject:
      11. organizations:
      12. - PingCAP
      13. commonName: "TiDB"
      14. usages:
      15. - server auth
      16. - client auth
      17. dnsNames:
      18. - "${cluster_name}-dm-master"
      19. - "${cluster_name}-dm-master.${namespace}"
      20. - "${cluster_name}-dm-master.${namespace}.svc"
      21. - "${cluster_name}-dm-master-peer"
      22. - "${cluster_name}-dm-master-peer.${namespace}"
      23. - "${cluster_name}-dm-master-peer.${namespace}.svc"
      24. - "*.${cluster_name}-dm-master-peer"
      25. - "*.${cluster_name}-dm-master-peer.${namespace}"
      26. - "*.${cluster_name}-dm-master-peer.${namespace}.svc"
      27. ipAddresses:
      28. - 127.0.0.1
      29. - ::1
      30. issuerRef:
      31. name: ${cluster_name}-dm-issuer
      32. kind: Issuer
      33. group: cert-manager.io

      ${cluster_name} is the name of the cluster. Configure the items as follows:

      • Set spec.secretName to ${cluster_name}-dm-master-cluster-secret.

      • Add server auth and client auth in usages.

      • Add the following DNSs in dnsNames. You can also add other DNSs according to your needs:

        • “${cluster_name}-dm-master”
        • “${cluster_name}-dm-master.${namespace}”
        • “${cluster_name}-dm-master.${namespace}.svc”
        • “${cluster_name}-dm-master-peer”
        • “${cluster_name}-dm-master-peer.${namespace}”
        • “${cluster_name}-dm-master-peer.${namespace}.svc”
        • “*.${cluster_name}-dm-master-peer”
        • “*.${cluster_name}-dm-master-peer.${namespace}”
        • “*.${cluster_name}-dm-master-peer.${namespace}.svc”
      • Add the following two IPs in ipAddresses. You can also add other IPs according to your needs:

        • 127.0.0.1
        • ::1
      • Add the Issuer created above in issuerRef.

      • For other attributes, refer to cert-manager API.

        After the object is created, cert-manager generates a ${cluster_name}-dm-master-cluster-secret Secret object to be used by the DM-master component of the DM cluster.

    • The DM-worker server-side certificate

      1. apiVersion: cert-manager.io/v1
      2. kind: Certificate
      3. metadata:
      4. name: ${cluster_name}-dm-worker-cluster-secret
      5. namespace: ${namespace}
      6. spec:
      7. secretName: ${cluster_name}-dm-worker-cluster-secret
      8. duration: 8760h # 365d
      9. renewBefore: 360h # 15d
      10. subject:
      11. organizations:
      12. - PingCAP
      13. commonName: "TiDB"
      14. usages:
      15. - server auth
      16. - client auth
      17. dnsNames:
      18. - "${cluster_name}-dm-worker"
      19. - "${cluster_name}-dm-worker.${namespace}"
      20. - "${cluster_name}-dm-worker.${namespace}.svc"
      21. - "${cluster_name}-dm-worker-peer"
      22. - "${cluster_name}-dm-worker-peer.${namespace}"
      23. - "${cluster_name}-dm-worker-peer.${namespace}.svc"
      24. - "*.${cluster_name}-dm-worker-peer"
      25. - "*.${cluster_name}-dm-worker-peer.${namespace}"
      26. - "*.${cluster_name}-dm-worker-peer.${namespace}.svc"
      27. ipAddresses:
      28. - 127.0.0.1
      29. - ::1
      30. issuerRef:
      31. name: ${cluster_name}-dm-issuer
      32. kind: Issuer
      33. group: cert-manager.io

      ${cluster_name} is the name of the cluster. Configure the items as follows:

      • Set spec.secretName to ${cluster_name}-dm-worker-cluster-secret.

      • Add server auth and client auth in usages.

      • Add the following DNSs in dnsNames. You can also add other DNSs according to your needs:

        • “${cluster_name}-dm-worker”
        • “${cluster_name}-dm-worker.${namespace}”
        • “${cluster_name}-dm-worker.${namespace}.svc”
        • “${cluster_name}-dm-worker-peer”
        • “${cluster_name}-dm-worker-peer.${namespace}”
        • “${cluster_name}-dm-worker-peer.${namespace}.svc”
        • “*.${cluster_name}-dm-worker-peer”
        • “*.${cluster_name}-dm-worker-peer.${namespace}”
        • “*.${cluster_name}-dm-worker-peer.${namespace}.svc”
      • Add the following two IPs in ipAddresses. You can also add other IPs according to your needs:

        • 127.0.0.1
        • ::1
      • Add the Issuer created above in issuerRef.

      • For other attributes, refer to cert-manager API.

        After the object is created, cert-manager generates a ${cluster_name}-dm-cluster-secret Secret object to be used by the DM-worker component of the DM cluster.

    • A set of client-side certificates of DM cluster components.

      1. apiVersion: cert-manager.io/v1
      2. kind: Certificate
      3. metadata:
      4. name: ${cluster_name}-dm-client-secret
      5. namespace: ${namespace}
      6. spec:
      7. secretName: ${cluster_name}-dm-client-secret
      8. duration: 8760h # 365d
      9. renewBefore: 360h # 15d
      10. subject:
      11. organizations:
      12. - PingCAP
      13. commonName: "TiDB"
      14. usages:
      15. - client auth
      16. issuerRef:
      17. name: ${cluster_name}-dm-issuer
      18. kind: Issuer
      19. group: cert-manager.io

      ${cluster_name} is the name of the cluster. The above YAML file creates three objects:

      • Set spec.secretName to ${cluster_name}-dm-master-cluster-secret.

      • Add server auth and client auth in usages.

      • dnsNames and ipAddresses are not required.

      • Add the Issuer created above in the issuerRef

      • For other attributes, refer to cert-manager API

        After the object is created, cert-manager generates a ${cluster_name}-cluster-client-secret Secret object to be used by the clients of the DM components.

Deploy the DM cluster

When you deploy a DM cluster, you can enable TLS between DM components, and set the cert-allowed-cn configuration item to verify the CN (Common Name) of each component’s certificate.

Enable TLS for DM - 图3Note

Currently, you can set only one value for the cert-allowed-cn configuration item of DM-master. Therefore, the commonName of all Certificate objects must be the same.

  • Create the dm-cluster.yaml file:
  1. apiVersion: pingcap.com/v1alpha1
  2. kind: DMCluster
  3. metadata:
  4. name: ${cluster_name}
  5. namespace: ${namespace}
  6. spec:
  7. tlsCluster:
  8. enabled: true
  9. version: v5.4.0
  10. pvReclaimPolicy: Retain
  11. discovery: {}
  12. master:
  13. baseImage: pingcap/dm
  14. maxFailoverCount: 0
  15. replicas: 1
  16. storageSize: "1Gi"
  17. config:
  18. cert-allowed-cn:
  19. - TiDB
  20. worker:
  21. baseImage: pingcap/dm
  22. maxFailoverCount: 0
  23. replicas: 1
  24. storageSize: "1Gi"
  25. config:
  26. cert-allowed-cn:
  27. - TiDB

Use the kubectl apply -f dm-cluster.yaml file to create a DM cluster.

Configure dmctl and connect to the cluster

Get into the DM-master Pod:

  1. kubectl exec -it ${cluster_name}-dm-master-0 -n ${namespace} sh

Use dmctl:

  1. cd /var/lib/dm-master-tls
  2. /dmctl --ssl-ca=ca.crt --ssl-cert=tls.crt --ssl-key=tls.key --master-addr 127.0.0.1:8261 list-member

Use DM to migrate data between MySQL/TiDB databases that enable TLS for the MySQL client

This section describes how to configure DM to migrate data between MySQL/TiDB databases that enable TLS for the MySQL client.

To learn how to enable TLS for the MySQL client of TiDB, refer to Enable TLS for the MySQL Client.

Step 1: Create the Kubernetes Secret object for each TLS-enabled MySQL

Suppose you have deployed a MySQL/TiDB database with TLS-enabled for the MySQL client. To create Secret objects for the TiDB cluster, execute the following command:

  1. kubectl create secret generic ${mysql_secret_name1} --namespace=${namespace} --from-file=tls.crt=client.pem --from-file=tls.key=client-key.pem --from-file=ca.crt=ca.pem
  2. kubectl create secret generic ${tidb_secret_name} --namespace=${namespace} --from-file=tls.crt=client.pem --from-file=tls.key=client-key.pem --from-file=ca.crt=ca.pem

Step 2: Mount the Secret objects to the DM cluster

After creating the Kubernetes Secret objects for the upstream and downstream databases, you need to set spec.tlsClientSecretNames so that you can mount the Secret objects to the Pod of DM-master/DM-worker.

  1. apiVersion: pingcap.com/v1alpha1
  2. kind: DMCluster
  3. metadata:
  4. name: ${cluster_name}
  5. namespace: ${namespace}
  6. spec:
  7. version: v5.4.0
  8. pvReclaimPolicy: Retain
  9. discovery: {}
  10. tlsClientSecretNames:
  11. - ${mysql_secret_name1}
  12. - ${tidb_secret_name}
  13. master:
  14. ...

Step 3: Modify the data source and migration task configuration

After configuring spec.tlsClientSecretNames, TiDB Operator will mount the Secret objects ${secret_name} to the path /var/lib/source-tls/${secret_name}.

  1. Configure from.security in the source1.yaml file as described in the data source configuration:

    1. source-id: mysql-replica-01
    2. relay-dir: /var/lib/dm-worker/relay
    3. from:
    4. host: ${mysql_host1}
    5. user: dm
    6. password: ""
    7. port: 3306
    8. security:
    9. ssl-ca: /var/lib/source-tls/${mysql_secret_name1}/ca.crt
    10. ssl-cert: /var/lib/source-tls/${mysql_secret_name1}/tls.crt
    11. ssl-key: /var/lib/source-tls/${mysql_secret_name1}/tls.key
  2. Configure target-database.security in the task.yaml file as described in the Configure Migration Tasks:

    1. name: test
    2. task-mode: all
    3. is-sharding: false
    4. target-database:
    5. host: ${tidb_host}
    6. port: 4000
    7. user: "root"
    8. password: ""
    9. security:
    10. ssl-ca: /var/lib/source-tls/${tidb_secret_name}/ca.crt
    11. ssl-cert: /var/lib/source-tls/${tidb_secret_name}/tls.crt
    12. ssl-key: /var/lib/source-tls/${tidb_secret_name}/tls.key
    13. mysql-instances:
    14. - source-id: "replica-01"
    15. loader-config-name: "global"
    16. loaders:
    17. global:
    18. dir: "/var/lib/dm-worker/dumped_data"

Step 4: Start the migration tasks

Refer to Start the migration tasks.