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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Service Directory API.
- Install the Google Cloud CLI.
-
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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Service Directory API.
- Install the Google Cloud CLI.
-
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 Create.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
Once the service is registered, add some endpoints.
gcloud service-directory endpoints create ENDPOINT \ --address IP_ADDRESS \ --port PORT_NUMBER \ --annotations KEY_1=VALUE_1,KEY_2=VALUE_2 \ --service SERVICE \ --namespace NAMESPACE \ --location REGION
gcloud service-directory endpoints create ENDPOINT2 \ --address IP_ADDRESS2 \ --port PORT_NUMBER2 \ --service SERVICE \ --namespace NAMESPACE \ --location REGION
Replace the following:
ENDPOINT
andENDPOINT2
: the names of the endpoints that you are creating in your service.IP_ADDRESS
andIP_ADDRESS2
: the IPv6 and IPv4 addresses of the endpoints, respectively.PORT_NUMBER
andPORT_NUMBER2
: the ports that the endpoints are running on.SERVICE
: the name of the service that contains your endpoint.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.
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.
Resolve a service
Service Directory lets clients resolve services by using DNS, HTTP, and gRPC. Resolving the service returns all properties of the service, all endpoints, and all annotations.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud service-directory services resolve SERVICE \ --namespace NAMESPACE \ --location REGION
Replace the following:
SERVICE
: the name of the service that you are resolving.NAMESPACE
: the name of the namespace that contains your service.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.
Delete resources
If you don't need a resource, you can delete it from Service Directory.
Delete an endpoint from a service
Console
In the Google Cloud console, go to the Service Directory page.
- Click the service that you want to delete the endpoint from.
- Select the checkbox next to the endpoint that you want to delete.
- Click Delete.
- In the confirmation dialog, click Delete again.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud service-directory endpoints delete ENDPOINT \ --service=SERVICE \ --namespace=NAMESPACE \ --location=REGION
Replace the following:
ENDPOINT
: the name of the endpoint that you are deleting.SERVICE
: the name of the service that contains your endpoint.NAMESPACE
: the name of the namespace that contains your service.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.
Delete a service from a namespace
You can delete a service that has endpoints. When a service is deleted, all of its endpoints are deleted as well.
You can delete a service that has a
Service Directory zone
pointing to it. Any further DNS queries for that service return NXDOMAIN
.
Console
In the Google Cloud console, go to the Service Directory page.
- Select the checkbox next to the service that you want to delete.
- Click Delete.
- In the confirmation dialog, click Delete again.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud service-directory services delete SERVICE \ --namespace=NAMESPACE \ --location=REGION
Replace the following:
SERVICE
: the name of the service that you are deleting.NAMESPACE
: the name of the namespace that contains your service.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.
Delete a namespace
You can delete a namespace that has services and endpoints. When a namespace is deleted, all of its services and endpoints are deleted as well.
You can delete a namespace that has a
Service Directory zone
pointing to it. Any further DNS queries (not including SOA/NS
requests for the zone origin) return NXDOMAIN
.
Console
In the Google Cloud console, go to the Service Directory namespaces page.
- Select the checkbox next to the namespace that you want to delete.
- Click Delete.
- In the confirmation dialog, click Delete again.
gcloud
To use Service Directory on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud service-directory namespaces delete NAMESPACE \ --location=REGION
Replace the following:
NAMESPACE
: the name of the namespace that you are deleting.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.
What's next
- To configure a Service Directory zone so that you can query your service using DNS, see Configure a Service Directory zone.
- To get an overview of Service Directory, see the Service Directory overview.
- To find solutions for common issues that you might encounter when using Service Directory, see Troubleshooting.