跳转至

通过 Nebular Operator 连接 NebulaGraph 数据库

使用 NebulaGraph Operator 创建 NebulaGraph 集群后,用户可在 NebulaGraph 集群内部访问 NebulaGraph 数据库,也可在集群外访问 NebulaGraph 数据库。

前提条件

使用 NebulaGraph Operator 创建 NebulaGraph 集群。具体步骤参考使用 Kubectl 部署 NebulaGraph 集群 或者使用 Helm 部署 NebulaGraph 集群

在 NebulaGraph 集群内连接 NebulaGraph 数据库

当使用 NebulaGraph Operator 创建 NebulaGraph 集群后,NebulaGraph Operator 会自动在同一命名空间下,创建名为<cluster-name>-graphd-svc、类型为ClusterIP的 Service。通过该 Service 的 IP 和数据库的端口号,用户可连接 NebulaGraph 数据库。

  1. 查看 Service,命令如下:

    $ kubectl get service -l app.kubernetes.io/cluster=<nebula>  #<nebula>为变量值,请用实际集群名称替换。
    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
    

    ClusterIP类型的 Service 只允许在集群内部访问容器应用。更多信息,请参考 ClusterIP

  2. 使用上述<cluster-name>-graphd-svc Service 的 IP 连接 NebulaGraph 数据库:

    kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- <nebula_console_name> -addr <cluster_ip>  -port <service_port> -u <username> -p <password>
    

    示例:

    kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- nebula-console -addr 10.98.213.34  -port 9669 -u root -p vesoft
    
    • --image:为连接 NebulaGraph 的工具 NebulaGraph Console 的镜像。
    • <nebula-console>:自定义的 Pod 名称。
    • -addr:连接 Graphd 服务的 IP 地址,即ClusterIP类型的 Service IP 地址。
    • -port:连接 Graphd 服务的端口。默认端口为 9669。
    • -u:NebulaGraph 账号的用户名。未启用身份认证时,可以使用任意已存在的用户名(默认为 root)。
    • -p:用户名对应的密码。未启用身份认证时,密码可以填写任意字符。

    如果返回以下内容,说明成功连接数据库:

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

用户还可以使用完全限定域名(FQDN)连接数据库,域名格式为<cluster-name>-graphd.<cluster-namespace>.svc.<CLUSTER_DOMAIN>

kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- <nebula_console_name> -addr <cluster_name>-graphd-svc.default.svc.cluster.local -port <service_port> -u <username> -p <password>
CLUSTER_DOMAIN的默认值为cluster.local

通过NodePort在 NebulaGraph 集群外部连接 NebulaGraph 数据库

用户可创建NodePort类型的 Service,通过节点 IP 和暴露的节点端口,从集群外部访问集群内部的服务。用户也可以使用云厂商(例如 Azure、AWS 等)提供的负载均衡服务,设置 Service 的类型为LoadBalancer

NodePort类型的 Service 通过标签选择器spec.selector将前端的请求转发到带有标签app.kubernetes.io/cluster: <cluster-name>app.kubernetes.io/component: graphd的 Graphd pod 中。

操作步骤如下:

  1. 创建名为graphd-nodeport-service.yaml的文件。YAML 文件内容如下:

    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
    
    • NebulaGraph 默认使用9669端口为客户端提供服务。19669为 Graph 服务端口号。
    • targetPort的值为映射至 Pod 的端口,可自定义。
  2. 执行以下命令使 Service 服务在集群中生效。

    kubectl create -f graphd-nodeport-service.yaml
    
  3. 查看 Service 中 NebulaGraph 映射至集群节点的端口。

    kubectl get services
    

    返回:

    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
    

    NodePort 类型的 Service 中,映射至集群节点的端口为32236

  4. 使用节点 IP 和上述映射的节点端口连接 NebulaGraph。

    kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- <nebula_console_name> -addr <node_ip> -port <node_port> -u <username> -p <password>
    

    示例如下:

    kubectl run -ti --image vesoft/nebula-console:v3.2.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:为连接 NebulaGraph 的工具 NebulaGraph Console 的镜像。
    • <nebula-console>:自定义的 Pod 名称。本示例为nebula-console2
    • -addr:NebulaGraph 集群中任一节点 IP 地址。本示例为192.168.8.24
    • -port:NebulaGraph 映射至节点的端口。本示例为32236
    • -u:NebulaGraph 账号的用户名。未启用身份认证时,可以使用任意已存在的用户名(默认为 root)。
    • -p:用户名对应的密码。未启用身份认证时,密码可以填写任意字符。

通过Ingress在 NebulaGraph 集群外部连接 NebulaGraph 数据库

Nginx Ingress 是 Kubernetes Ingress 的一个实现。Nginx Ingress 通过 Watch 机制感知 Kubernetes 集群的 Ingress 资源,将 Ingress 规则生成 Nginx 配置,使 Nginx 能够转发 7 层流量。

用户可以通过 HostNetwork 和 DaemonSet 组合的模式使用 Nginx Ingress 从集群外部连接 NebulaGraph 集群。

由于使用 HostNetwork,Nginx Ingress 的 Pod 就不能被调度在同一个节点上。为了避免监听端口冲突,可以事先选择一些节点并将其标记为边缘节点,专门用于部署 Nginx Ingress。然后 Nginx Ingress 以 DaemonSet 模式部署在这些节点上。

由于 Ingress 不支持 TCP 或 UDP 服务,为此 nginx-ingress-controller 使用--tcp-services-configmap--udp-services-configmap参数指向一个 ConfigMap,该 ConfigMap 中的键指需要使用的外部端口,值指要公开的服务的格式,值的格式为<命名空间/服务名称>:<服务端口>

例如指向名为tcp-services的 ConfigMap 的配置如下:

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: nginx-ingress
data:
  # update 
  9769: "default/nebula-graphd-svc:9669"

操作步骤如下:

  1. 创建名为nginx-ingress-daemonset-hostnetwork.yaml的文件。

    单击 nginx-ingress-daemonset-hostnetwork.yaml 查看完整的 YAML 示例内容。

    Note

    上述 YAML 中的资源对象均使用nginx-ingress命名空间。用户可执行kubectl create namesapce nginx-ingress创建命名空间,或者自定义其他命名空间。

  2. 为任一节点(本示例使用的节点名为worker2,IP 为192.168.8.160)打上标签,以运行上述 YAML 文件中名为nginx-ingress-controller的 DaemonSet。

    kubectl label node worker2 nginx-ingress=true
    
  3. 执行以下命令使 Nginx Ingress 在集群中生效。

    kubectl create -f nginx-ingress-daemonset-hostnetwork.yaml
    

    返回:

    configmap/nginx-ingress-controller created
    configmap/tcp-services created
    serviceaccount/nginx-ingress created
    serviceaccount/nginx-ingress-backend created
    clusterrole.rbac.authorization.k8s.io/nginx-ingress created
    clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress created
    role.rbac.authorization.k8s.io/nginx-ingress created
    rolebinding.rbac.authorization.k8s.io/nginx-ingress created
    service/nginx-ingress-controller-metrics created
    service/nginx-ingress-default-backend created
    service/nginx-ingress-proxy-tcp created
    daemonset.apps/nginx-ingress-controller created
    

    成功部署 Nginx Ingress 后,由于 Nginx Ingress 中配置的网络类型为hostNetwork,因此用户可通过部署了 Nginx Ingress 的节点的 IP(192.168.8.160)和外部端口(9769)访问 NebulaGraph 服务。

  4. 执行以下命令部署连接 NebulaGraph 服务的 Console 并通过宿主机 IP(本示例为192.168.8.160)和上述配置的外部端口访问 NebulaGraph 服务。

    kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- <nebula_console_name> -addr <host_ip> -port <external_port> -u <username> -p <password>
    

    示例:

    kubectl run -ti --image vesoft/nebula-console:v3.2.0 --restart=Never -- nebula-console -addr 192.168.8.160 -port 9769 -u root -p vesoft
    
    • --image:为连接 NebulaGraph 的工具 NebulaGraph Console 的镜像。
    • <nebula-console>:自定义的 Pod 名称。本示例为nebula-console
    • -addr:部署 Nginx Ingress 的节点 IP,本示例为192.168.8.160
    • -port:外网访问使用的的端口。本示例设置为9769
    • -u:NebulaGraph 账号的用户名。未启用身份认证时,可以使用任意已存在的用户名(默认为 root)。
    • -p:用户名对应的密码。未启用身份认证时,密码可以填写任意字符。

    如果返回以下内容,说明成功连接数据库:

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

最后更新: February 23, 2023