This page shows how to set up a Service Directory namespace, register a service in the namespace, and add endpoints to a service. You can create a namespace when you're registering a service or before registering a service.
Before running the commands listed on this page, familiarize yourself with the concepts in the Service Directory overview and key terms related to Service Directory.
Set up your project
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Service Directory API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Service Directory API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Configure Service Directory resources
To populate Service Directory with services, create a namespace and register your service in the namespace.
Configure a namespace
For your project, create a namespace in your region. This region doesn't have to be where all your services and endpoints are running but must be close, if possible.
You can register your services in any Service Directory region; they are still globally resolvable. Within a region, projects can have multiple namespaces, and those namespaces can be in different regions. A single namespace can't span multiple regions.
Console
In the Google Cloud console, go to the Service Directory namespaces page.
- Click Create namespace.
- In the Region list, select a region for your namespace.
- In the Namespace name field, give the namespace a name.
- Click Create.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
Create a namespace
gcloud service-directory namespaces create NAMESPACE \ --location REGION
Replace the following:
NAMESPACE: the name of the namespace that you are creating.REGION: the Google Cloud region that contains the namespace.
Optional: Set an IAM policy on your namespace. This gives the specified user or group the specified role for this namespace and all services that belong to the namespace.
gcloud service-directory namespaces add-iam-policy-binding NAMESPACE \ --member user:someone@example.com \ --role ROLE \ --location REGION
Replace the following:
NAMESPACE: the name of the namespace that you created.ROLE: the role that you are granting.REGION: the Google Cloud region that contains the namespace.
C#
To run this code, first set up a C# development environment and install the Service Directory C# SDK.
Go
To run this code, first set up a Go development environment and install the Service Directory Go SDK.
Java
To run this code, first set up a Java development environment and install the Service Directory Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Service Directory Node.js SDK.
PHP
To run this code, first set up a PHP development environment and install the Service Directory PHP SDK.
Python
To run this code, first set up a Python development environment and install the Service Directory Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Service Directory Ruby SDK.
Configure a service
Create a service in the namespace. A service consists of a name and optional service-related annotations. There are some restrictions on the format of service names:
- Service names must be unique within a namespace.
- Service names must follow the naming convention for DNS labels.
To create a Private Service Connect forwarding rule and register it as a service with Service Directory, see Register a Private Service Connect endpoint with Service Directory.
To register a standard service with Service Directory, follow these steps:
Console
In the Google Cloud console, go to the Service Directory page.
- Click Register Service.
- Click Standard, and then click Next.
- Choose a Region to register your service in.
- Choose a Namespace to register your service in.
If you don't have a namespace, follow these steps to create one:
- In the Namespace box, click Create namespace.
- Enter a Namespace name.
- Click Create.
- Enter a Service name.
- Optional: If you want to add annotations to the service, do the following:
- Click Add annotation.
- Add a Key and Value.
- To add more annotations, click Add annotation again.
- Click Create.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
Create a service in a namespace.
gcloud service-directory services create SERVICE \ --annotations KEY_1=VALUE_1,KEY_2=VALUE_2 \ --namespace NAMESPACE \ --location REGION
Replace the following:
SERVICE: the name of the service that you are creating.NAMESPACE: the name of the namespace that contains your service.REGION: the Google Cloud region that contains the namespace.KEY_1,VALUE_1,KEY_2,VALUE_2: key and value string set in pairs.
Optional: Set an IAM policy on your service. This gives the specified user or group the specified role for this service and all endpoints that belong to the service.
gcloud service-directory services add-iam-policy-binding SERVICE \ --member user:someone@example.com \ --role ROLE \ --namespace NAMESPACE \ --location REGION
Replace the following:
SERVICE: the name that you gave your service.NAMESPACE: the name of the namespace that contains your serviceROLE: the role that you are granting.REGION: the Google Cloud region that contains the namespace.
C#
To run this code, first set up a C# development environment and install the Service Directory C# SDK.
Go
To run this code, first set up a Go development environment and install the Service Directory Go SDK.
Java
To run this code, first set up a Java development environment and install the Service Directory Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Service Directory Node.js SDK.
PHP
To run this code, first set up a PHP development environment and install the Service Directory PHP SDK.
Python
To run this code, first set up a Python development environment and install the Service Directory Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Service Directory Ruby SDK.
Configure an endpoint
After you have registered the service, add some endpoints. An endpoint consists of a unique name, an optional IP address and a port, and key-value annotations. The IP address, if specified, must be a valid IPv4 or IPv6 address.
Console
In the Google Cloud console, go to the Service Directory page.
- Click a service.
- Click Add endpoint.
- Provide an Endpoint name.
- Enter an IPv4 or IPv6 IP address.
- Enter a Port number.
- Optional: If you want to add annotations to the endpoint, do the following:
- Under Annotations, click Add annotation.
- Add a Key and Value.
- To add more annotations, click Add annotation again.
- Click