Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite wird beschrieben, wann und warum deterministische Instanzvorlagen erstellt werden sollten. Deterministische Instanzvorlagen verdeutlichen explizit den Typ der Dienste oder Anwendungen von Drittanbietern, die beim Bereitstellen der Instanzvorlage auf Ihren Instanzen installiert werden sollten. Durch das Erstellen deterministischer Instanzvorlagen vermeiden Sie Mehrdeutigkeiten und unerwartetes Verhalten der Instanzvorlagen.
Gründe für das Erstellen deterministischer Instanzvorlagen
Im Allgemeinen empfehlen wir, die Attribute Ihrer Instanzvorlage so explizit und deterministisch wie möglich zu halten. Wenn Sie in Ihren Instanzvorlagen Startskripts verwenden, die Dienste von Drittanbietern installieren oder nutzen, sorgen Sie dafür, dass diese Skripts explizite Informationen angeben, z. B. die Version der Anwendung, die installiert werden soll. Compute Engine muss sich auf die Informationen verlassen, die in der Vorlage definiert sind, und hat keine Kontrolle über Drittanbieter-Dienste, auf die verwiesen wird. Wenn Ihre Vorlage zu vage ausfällt, kann unerwartetes Verhalten Ihrer Instanzvorlage die Folge sein.
Betrachten wir zum Beispiel den folgenden Befehl zur Erstellung einer Instanzvorlage mit einem Startskript, das apache2 installiert und eine Datei verwendet, die auf einem externen Server gehostet wird:
Es gibt zwei mögliche Probleme mit diesem Startskript:
Das Skript legt nicht explizit fest, welche Version von apache2 installiert werden soll, und bezieht die aktuelle Version aus dem Repository apt.
Das Skript verweist auf eine Datei, die bei einem Drittanbieter gehostet wird, nicht versioniert ist und seit der letzten Verwendung der Instanzvorlage geändert worden sein könnte.
Wenn Sie Autoscaling verwenden, kann eine nicht deterministische Instanzvorlage dazu führen, dass einer verwalteten Instanzgruppe neue Instanzen mit einer anderen Konfiguration hinzugefügt werden, etwa mit einer anderen Version von apache2.
Wenn Sie diese Vorlage auf eine verwaltete Instanzgruppe anwenden, die Gruppe auf eine andere Vorlage umstellen und sich schließlich entscheiden, zur vorherigen Vorlage zurückzukehren, könnte es ebenfalls sein, dass die Instanzen eine andere Version von apache2 oder der Datei index.php verwenden als vor der Aktualisierung, denn Instanzen beziehen beim Start immer die neueste Version.
Mehrdeutiges oder unerwartetes Verwalten von Instanzvorlagen vermeiden
Um unerwartetem Verhalten der Vorlage vorzubeugen, können Sie nach einer der folgenden Methoden vorgehen:
Verwenden Sie containeroptimierte Images oder Docker mit Docker-Tags. Wir empfehlen beispielsweise, für jeden neuen Build Ihres Docker-Images neue Tags zuzuweisen und diese Tags anstelle des standardmäßigen neuesten Tags in Ihren Instanzvorlagen zu verwenden. Bei einem containeroptimierten Image können Sie in der Manifestdatei explizit auf einen bestimmten Build des Images verweisen. Im folgenden Beispiel wird das Docker-Image "myimage" einer Version verwendet, die mit "version_2_1_3" getaggt ist:
Erstellen Sie ein benutzerdefiniertes Image, das als Image für die Vorlage verwendet wird. Diese Methode ist den Startskripts vorzuziehen, denn sie sorgt dafür, dass jede Instanz gleich ist. Startskripts hingegen könnten nach Updates von Distributionspaketen unterschiedliche Ergebnisse liefern. Verwenden Sie in Ihren Instanzvorlagen für Prototypen und schnelle Entwicklung Startskripts. Verwenden Sie jedoch benutzerdefinierte Images, wenn die Dienste in Produktionsqualität bereitgestellt werden sollen.
Wenn Sie mit Startskripts arbeiten möchten, sollten Sie dafür sorgen, dass die Skripts deterministisch sind. Erstellen Sie zum Beispiel eine neue Version der vorherigen Vorlage und schreiben Sie ein deterministisches Startskript:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-06-16 (UTC)."],[[["\u003cp\u003eDeterministic instance templates ensure clarity on the third-party services or apps installed on instances during deployment, minimizing ambiguity.\u003c/p\u003e\n"],["\u003cp\u003eUsing explicit versioning in startup scripts, such as specifying the exact version of Apache2 to install, helps avoid unexpected behavior due to changes in third-party services.\u003c/p\u003e\n"],["\u003cp\u003eNon-deterministic templates can cause autoscalers to deploy instances with varying configurations, such as different software versions, potentially leading to inconsistencies.\u003c/p\u003e\n"],["\u003cp\u003eTo maintain consistency and predictability, use container-optimized images with specific Docker tags, or create custom images instead of relying heavily on startup scripts.\u003c/p\u003e\n"],["\u003cp\u003eWhen using startup scripts, make them deterministic by specifying exact versions of software and files, like \u003ccode\u003eapache2=2.2.20-1ubuntu1\u003c/code\u003e, and versioned directories for hosted files.\u003c/p\u003e\n"]]],[],null,["# Deterministic instance templates\n\n*** ** * ** ***\n\nThis page describes when and why to create deterministic instance\ntemplates. Deterministic instance templates make explicitly clear\nthe type of third-party services or apps to install on\nyour instances when the instance template is deployed. By creating deterministic\ninstance templates, you minimize ambiguity and unexpected behavior from your\ninstance templates.\n\nWhy create deterministic instance templates\n-------------------------------------------\n\nIn general, we recommend that the properties of your instance template be as\nexplicit and deterministic as possible. If you employ startup\nscripts in your instance templates that install or use third-party services,\nmake sure that these scripts provide explicit information, such as the\nversion of app to install. Compute Engine can only rely on\ninformation defined in the template and has no control over referenced\nthird-party services. If your template is too vague, your instance template\nmight behave unexpectedly.\n\nFor example, consider the following command to create an instance template with\na startup script that installs apache2 and uses a file that is hosted on an\nexternal server:\n**Note:** This is just an example snippet pointing to a non-existent server at 108.59.87.185. Copying this example directly fails when the script attempts to connect to 108.59.87.185. Instead, replace the last line with your own server information, if applicable. \n\n gcloud compute instance-templates create example-template-with-startup \\\n --image-family debian-9 \\\n --image-project debian-cloud \\\n --metadata startup-script='#! /bin/bash\n sudo apt install -y apache2\n scp myuser@108.59.87.185:index.php /var/www/'\n\nThere are two potential issues with this startup script:\n\n- The script does not explicitly define which version of apache2 to install, and relies on the current version available in the `apt` repository.\n- The script relies on a file hosted on a third-party that isn't versioned and could have been changed since the last time the instance template was used.\n\nIf you use an [autoscaler](/compute/docs/autoscaler), a non-deterministic\ninstance template can cause your autoscaler to add new instances to a\n[managed instance group](/compute/docs/instance-groups) with a different\nconfiguration, such as a different version of apache2.\n\nSimilarly, if you applied this template to a managed instance group, [updated\nthe group to a different template](/compute/docs/instance-groups/updating-migs)\nservice, and then decided to roll back to the previous template, you might end\nup with instances that use a different version of apache2 or index.php file\nthan before the update because your instances would always fetch the most recent\nversion at startup.\n\nAvoiding ambiguous or unexpected instance template behavior\n-----------------------------------------------------------\n\nTo avoid unexpected template behavior, use the following methods:\n\n- Use [container-optimized images](/compute/docs/containers/container_vms) or\n Docker, with Docker tags. For example, we recommend that you assign new tags\n for every new build of your Docker image, and use these tags in your instance\n templates instead of the default latest tag. For a container-optimized image,\n you can explicitly reference a particular build of your image in your manifest\n file. The example below uses Docker image \"myimage\" at version tagged with\n \"version_2_1_3\":\n\n version: v1beta2\n containers:\n - name: simple-echo\n image: myimage:version_2_1_3\n [ rest of your manifest file ]\n\n- [Create a custom image](/compute/docs/images/create-delete-deprecate-private-images)\n to use as the image for the template. This is preferable to startup scripts\n because it guarantees that every instance is the same. Startup scripts might\n have different results after distribution package updates. Use startup scripts\n in your instance templates for prototyping and rapid development, and use\n custom images when you are ready to deploy production-quality services.\n\n- If you do use startup scripts, consider updating your scripts to be\n deterministic. For example, create a new version of the previous template, and\n specify a deterministic startup script as follows:\n\n gcloud compute instance-templates create example-template-with-startup-2-1-3 \\\n --image-family debian-9 \\\n --image-project debian-cloud \\\n --metadata startup-script='#! /bin/bash\n sudo apt install -y apache2=2.2.20-1ubuntu1\n scp myuser@108.59.87.185:version_2_1_3/index.php /var/www/'\n\n where \"version_2_1_3\" is a subdirectory containing PHP scripts for the\n version 2.1.3 of your service.\n- When specifying an instance template---for example when you are creating or\n updating a managed instance group---Google recommends that you specify the\n template's ID value instead of its name value. Although both values are valid,\n the ID is unique, which means that the instance template that you specify is\n the one that is used when creating VMs from that instance template. Using an\n ID instead of a name helps to mitigate potential security vulnerabilities---for\n example,\n [TOCTOU](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use_vulnerability)\n vulnerabilities, where an attacker can delete a template and recreate it with\n the same name prior to its use.\n\n To view the ID of an instance template, see\n [Get information about an instance template](/compute/docs/instance-templates/get-list-delete-instance-templates#get_information_about_an_instance_template).\n\nWhat's next\n-----------\n\n- [Create an instance template](/compute/docs/instance-templates/create-instance-templates).\n- [Manage your instance templates](/compute/docs/instance-templates/get-list-delete-instance-templates)."]]