Skip to content

Connect to Nebula Graph databases with Nebular Operator

After creating a Nebula Graph cluster with Nebula Operator on Kubernetes, you can connect to Nebula Graph databases from within the cluster and outside the cluster.

Prerequisites

Create a Nebula Graph cluster with Nebula Operator on Kubernetes. For more information, see Deploy Nebula Graph clusters with Kubectl or Deploy Nebula Graph clusters with Helm.

Connect to Nebula Graph databases from within a Nebula Graph cluster

When a Nebula Graph cluster is created, Nebula Operator automatically creates a Service named <cluster-name>-graphd-svc with the type ClusterIP under the same namespace. With the IP of the Service and the port number of the Nebula Graph database, you can connect to the Nebula Graph database.

  1. Run the following command to check the IP of the Service:

    $ kubectl get service -l app.kubernetes.io/cluster=<nebula>  #<nebula> is a variable value. Replace it with the desired name.
    NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                                          AGE
    nebula-graphd-svc          ClusterIP   10.98.213.34   <none>        9669/TCP,19669/TCP,19670/TCP                     23h
    nebula-metad-headless      ClusterIP   None           <none>        9559/TCP,19559/TCP,19560/TCP                     23h
    nebula-storaged-headless   ClusterIP   None           <none>        9779/TCP,19779/TCP,19780/TCP,9778/TCP            23h
    

    Services of the ClusterIP type only can be accessed by other applications in a cluster. For more information, see ClusterIP.

  2. Run the following command to connect to the Nebula Graph database using the IP of the <cluster-name>-graphd-svc Service above:

    kubectl run -ti --image vesoft/nebula-console:v2.5.0 --restart=Never -- <nebula-console> -addr <10.98.213.34>  -port 9669 -u <root> -p <vesoft>
    
    • --image: The image for the tool Nebula Console used to connect to Nebula Graph databases.
    • <nebula-console>: The custom Pod name.
    • -addr: The IP of the ClusterIP Service, used to connect to Graphd services.
    • -port: The port to connect to Graphd services, the default port of which is 9669.
    • -u: The username of your Nebula Graph account. Before enabling authentication, you can use any existing username. The default username is root.
    • -p: The password of your Nebula Graph account. Before enabling authentication, you can use any characters as the password.

    A successful connection to the database is indicated if the following is returned:

    If you don't see a command prompt, try pressing enter.
    
    (root@nebula) [(none)]>
    

You can also connect to Nebula Graph databases with Fully Qualified Domain Name (FQDN). The domain format is <cluster-name>-graphd.<cluster-namespace>.svc.<CLUSTER_DOMAIN>:

kubectl run -ti --image vesoft/nebula-console:v2.5.0 --restart=Never -- <nebula-console> -addr <cluster_name>-graphd-svc.default.svc.cluster.local -port 9669 -u root -p vesoft

The default value of CLUSTER_DOMAIN is cluster.local.

Connect to Nebula Graph databases from outside a Nebula Graph cluster

You can create a Service of type NodePort to connect to Nebula Graph databases from outside a Nebula Graph cluster with a node IP and an exposed node port. You can also use load balancing software provided by cloud providers and set the Service of type LoadBalancer.

The Service of type NodePort forwards the front-end requests via the label selector spec.selector to Graphd pods with labels app.kubernetes.io/cluster: <cluster-name> and app.kubernetes.io/component: graphd.

Steps:

  1. Create a YAML file named graphd-nodeport-service.yaml. The file contents are as follows:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app.kubernetes.io/cluster: nebula
        app.kubernetes.io/component: graphd
        app.kubernetes.io/managed-by: nebula-operator
        app.kubernetes.io/name: nebula-graph
      name: nebula-graphd-svc-nodeport
      namespace: default
    spec:
      externalTrafficPolicy: Local
      ports:
      - name: thrift
        port: 9669
        protocol: TCP
        targetPort: 9669
      - name: http
        port: 19669
        protocol: TCP
        targetPort: 19669
      selector:
        app.kubernetes.io/cluster: nebula
        app.kubernetes.io/component: graphd
        app.kubernetes.io/managed-by: nebula-operator
        app.kubernetes.io/name: nebula-graph
      type: NodePort
    
    • Nebula Graph uses port 9669 by default. 19669 is the port of the Graph service in a Nebula Graph cluster.
    • The value of targetPort is the port mapped to the database Pods, which can be customized.
  2. Run the following command to create a NodePort Service.

    kubectl create -f graphd-nodeport-service.yaml
    
  3. Check the port mapped on all of your cluster nodes.

    kubectl get services
    

    Output:

    NAME                           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                                          AGE
    nebula-graphd-svc              ClusterIP   10.98.213.34   <none>        9669/TCP,19669/TCP,19670/TCP                     23h
    nebula-graphd-svc-nodeport     NodePort    10.107.153.129 <none>        9669:32236/TCP,19669:31674/TCP,19670:31057/TCP   24h
    nebula-metad-headless          ClusterIP   None           <none>        9559/TCP,19559/TCP,19560/TCP                     23h
    nebula-storaged-headless       ClusterIP   None           <none>        9779/TCP,19779/TCP,19780/TCP,9778/TCP            23h
    

    As you see, the mapped port of Nebula Graph databases on all cluster nodes is 32236.

  4. Connect to Nebula Graph databases with your node IP and the node port above.

    kubectl run -ti --image vesoft/nebula-console:v2.5.0 --restart=Never -- <nebula-console> -addr <node_ip> -port <node_port> -u root -p vesoft
    

    Example:

    [root@k8s4 ~]# kubectl run -ti --image vesoft/nebula-console:v2.5.0 --restart=Never -- nebula-console2 -addr 192.168.8.24 -port 32236 -u root -p vesoft
    If you don't see a command prompt, try pressing enter.
    
    (root@nebula) [(none)]>
    
    • --image: The image for the tool Nebula Console used to connect to Nebula Graph databases.
    • <nebula-console>: The custom Pod name. The above example uses nebula-console2.
    • -addr: The IP of any node in a Nebula Graph cluster. The above example uses 192.168.8.24.
    • -port: The mapped port of Nebula Graph databases on all cluster nodes. The above example uses 32236.
    • -u: The username of your Nebula Graph account. Before enabling authentication, you can use any existing username. The default username is root.
    • -p: The password of your Nebula Graph account. Before enabling authentication, you can use any characters as the password.

Last update: September 29, 2021
Back to top