Crea cluster

Creare un cluster Game Servers.

Per saperne di più

Per la documentazione dettagliata che include questo esempio di codice, consulta quanto segue:

Esempio di codice


Per informazioni su come installare e utilizzare la libreria client per Game Servers, consulta la sezione Librerie client di Game Servers.

using Google.Cloud.Gaming.V1;
using Google.LongRunning;
using System.Threading.Tasks;

public class CreateClusterSample
    public async Task<GameServerCluster> CreateClusterAsync(
        string projectId, string regionId, string realmId, string clusterId, string gkeName)
        // Create the client.
        GameServerClustersServiceClient client = await GameServerClustersServiceClient.CreateAsync();

        GameServerCluster cluster = new GameServerCluster()
            GameServerClusterName = GameServerClusterName.FromProjectLocationRealmCluster(projectId, regionId, realmId, clusterId),
            ConnectionInfo = new GameServerClusterConnectionInfo
                GkeClusterReference = new GkeClusterReference
                    Cluster = gkeName
                Namespace = "default"
        CreateGameServerClusterRequest request = new CreateGameServerClusterRequest
            ParentAsRealmName = RealmName.FromProjectLocationRealm(projectId, regionId, realmId),
            GameServerClusterId = clusterId,
            GameServerCluster = cluster

        // Make the request.
        Operation<GameServerCluster, OperationMetadata> response = await client.CreateGameServerClusterAsync(request);

        // Poll until the returned long-running operation is complete.
        Operation<GameServerCluster, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();

        // Retrieve the operation result.
        return completedResponse.Result;


Per informazioni su come installare e utilizzare la libreria client per Game Servers, consulta la sezione Librerie client di Game Servers.

import (

	gaming ""
	gamingpb ""

// createCluster registers a game server cluster.
func createCluster(w io.Writer, projectID, location, realmID, clusterID, gkeClusterName string) error {
	// projectID := "my-project"
	// location := "global"
	// realmID := "myrealm"
	// clusterID := "mycluster"
	// gkeClusterName := "projects/1234/locations/us-central1/clusters/gke-cluster"
	ctx := context.Background()
	client, err := gaming.NewGameServerClustersClient(ctx)
	if err != nil {
		return fmt.Errorf("NewGameServerClustersClient: %v", err)
	defer client.Close()

	req := &gamingpb.CreateGameServerClusterRequest{
		Parent:              fmt.Sprintf("projects/%s/locations/%s/realms/%s", projectID, location, realmID),
		GameServerClusterId: clusterID,
		GameServerCluster: &gamingpb.GameServerCluster{
			ConnectionInfo: &gamingpb.GameServerClusterConnectionInfo{
				Namespace: "default",
				ClusterReference: &gamingpb.GameServerClusterConnectionInfo_GkeClusterReference{
					GkeClusterReference: &gamingpb.GkeClusterReference{
						Cluster: gkeClusterName,
			Description: "My Game Server Cluster",

	op, err := client.CreateGameServerCluster(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateGameServerCluster: %v", err)
	resp, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %v", err)

	fmt.Fprintf(w, "Cluster created: %v", resp.Name)
	return nil


Per informazioni su come installare e utilizzare la libreria client per Game Servers, consulta la sezione Librerie client di Game Servers.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateCluster {

  public static void createGameServerCluster(
      String projectId, String regionId, String realmId, String clusterId, String gkeName)
      throws InterruptedException, ExecutionException, TimeoutException, IOException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // String realmId = "your-realm-id";
    // String clusterId = "your-game-server-cluster-id";
    // String gkeName = "projects/your-project-id/locations/us-central1/clusters/test";
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) {
      String parent =
          String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId);
      String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId);

      GameServerCluster gameServerCluster =

      CreateGameServerClusterRequest request =

      OperationFuture<GameServerCluster, OperationMetadata> call =

      GameServerCluster created = call.get(1, TimeUnit.MINUTES);
      System.out.println("Game Server Cluster created: " + created.getName());


Per informazioni su come installare e utilizzare la libreria client per Game Servers, consulta la sezione Librerie client di Game Servers.

const {
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function createGameServerCluster() {
   * TODO(developer): Uncomment these variables before running the sample.
  // const projectId = 'Your Google Cloud Project ID';
  // const location = 'A Compute Engine region, e.g. "us-central1"';
  // const realmId = 'The ID of the realm to locate this cluster in';
  // const gameClusterId = 'A unique ID for this Game Server Cluster';
  // const gkeClusterName= 'The full resource name of the GKE cluster to use';
  const request = {
    // Provide full resource name of a Game Server Realm
    parent: client.realmPath(projectId, location, realmId),
    gameServerClusterId: gameClusterId,
    gameServerCluster: {
      description: 'My Game Server Cluster',
      connectionInfo: {
        gkeClusterReference: {
          // Provide full resource name of a Kubernetes Engine cluster
          // In the form of 'projects/<project-id>/locations/<location>/clusters/<gke-cluster-name>'
          cluster: gkeClusterName,
        namespace: 'default',

  const [operation] = await client.createGameServerCluster(request);
  const [result] = await operation.promise();

  console.log('Game Server Cluster created:');
  console.log(`\tCluster name: ${}`);
  console.log(`\tCluster description: ${result.description}`);
    `\tGKE cluster: ${result.connectionInfo.gkeClusterReference.cluster}`



Per informazioni su come installare e utilizzare la libreria client per Game Servers, consulta la sezione Librerie client di Game Servers.

def create_cluster(project_id, location, realm_id, cluster_id, gke_cluster_name):
    """Creates a game server cluster."""

    client = gaming.GameServerClustersServiceClient()

    gke_cluster_reference = game_server_clusters.GkeClusterReference(
    request = game_server_clusters.CreateGameServerClusterRequest(
            description="My Game Server Cluster",

    operation = client.create_game_server_cluster(request)
    print(f"Create cluster operation: {}")

Passaggi successivi

Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, vedi il browser di esempio Google Cloud.