Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Scopri come creare un secret e configurare i servizi e le revisioni di Knative serving per utilizzarlo.
Un caso d'uso comune per un servizio è accedere ad applicazioni di terze parti tramite nomi utente e password. Per Google Kubernetes Engine, è buona prassi memorizzare questo tipo di informazioni sensibili in un oggetto Kubernetes Secret.
Per fornire ai container l'accesso ai secret, puoi montare ogni secret come volume, rendendo le voci del secret disponibili per il container come file. Devi montare il secret per assicurarti di ottenere la versione più recente di ogni secret quando viene letto.
I passaggi riportati di seguito mostrano semplicemente come creare un secret, ma esistono diversi modi per farlo, come spiegato nell'argomento Secret.
Quando crei un secret, assicurati di farlo nello stesso spazio dei nomi del
cluster in cui è in esecuzione il servizio di pubblicazione Knative. In questi
esempi viene utilizzato lo spazio dei nomi default.
Per creare un secret nello spazio dei nomi default del cluster:
Puoi associare i secret a un servizio utilizzando la Google Cloud console o gli strumenti a riga di comando quando esegui il deployment di un nuovo
servizio o aggiorni un servizio esistente e
esegui il deployment di una revisione:
Console
Vai alla pubblicazione Knative nella Google Cloud console:
Fai clic su Crea servizio se stai configurando un nuovo servizio di destinazione del deployment. Se stai configurando un servizio esistente, fai clic sul servizio, poi su Modifica ed esegui il deployment di una nuova revisione.
In Impostazioni avanzate, fai clic su Variabili e secret.
In Fai riferimento a un secret, seleziona il secret preferito dal menu a discesa.
Nel menu a discesa Metodo di riferimento, seleziona il modo in cui vuoi utilizzare il secret, montato come volume o esposto come variabili di ambiente.
Se utilizzi il montaggio come volume, specifica il percorso e poi fai clic su
Fine.
Se le esponi come variabili di ambiente:
Fornisci il nome della variabile e seleziona il valore secret corrispondente dal menu a discesa Chiave.
Fai clic su Aggiungi per aggiungere un altro valore segreto.
Fornisci il nome della variabile e seleziona il valore secret corrispondente dal menu a discesa Chiave.
Fai clic su Fine.
Fai clic su Avanti per passare alla sezione successiva.
Nella sezione Configura il funzionamento del trigger per questo servizio,
seleziona la connettività che vuoi utilizzare per richiamare il servizio.
Fai clic su Crea per eseguire il deployment dell'immagine in Knative Serving e attendi
il completamento del deployment.
gcloud
Puoi utilizzare Google Cloud CLI per associare i segreti ai nuovi servizi o per aggiornare i servizi esistenti:
Per i servizi esistenti, aggiorna un segreto eseguendo il comando
gcloud run services update
con uno dei seguenti parametri:
KEY1=VALUE1,KEY2=VALUE2 con un elenco di coppie di nome e valore per ogni segreto separato da virgole.
Per ogni KEY specificato, inizia il percorso con una barra / per montare un segreto come file. Facoltativamente, puoi escludere la barra verticale per montare il secret come variabile di ambiente.
Per ogni VALUE, specifica il nome del secret.
Come specificare più parametri.
Opzioni del parametro del comando
Per specificare più insiemi di coppie chiave-valore, puoi indicare più parametri per la leggibilità. Esempio:
IMAGE_URL con un riferimento all'immagine del container, ad esempio gcr.io/cloudrun/hello.
SERVICE con il nome del servizio.
KEY1=VALUE1,KEY2=VALUE2 con un elenco di coppie di nome e valore per ogni segreto separato da virgole.
Per ogni KEY specificato, inizia il percorso con una barra / per montare un segreto come file. Facoltativamente, puoi escludere la barra verticale per montare il secret come variabile di ambiente.
Per ogni VALUE, specifica il nome del secret.
Come specificare più parametri.
Opzioni del parametro del comando
Per specificare più insiemi di coppie chiave-valore, puoi indicare più parametri per la leggibilità. Esempio:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-09 UTC."],[],[],null,["# Using secrets\n\nLearn how to create a Secret and configure your Knative serving\nservices and revisions to use that Secret.\n\nA common use case for a service is to access third-party applications through\nusernames and passwords. For Google Kubernetes Engine, it's a best practice to\nstore this type of sensitive information in a Kubernetes\n[Secret](https://kubernetes.io/docs/concepts/configuration/secret/) object.\n\nTo provide your containers with access to Secrets, you can mount each Secret as\na volume, which makes the entries in the Secret available to the container as\nfiles. You should mount your Secret to ensure that you get the latest version of\neach Secret when it is read.\n\nYou can also pass a Secret using\n[environment variables](/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables).\n| **Caution:** Do not use environment variables for sensitive information because environment variables are stored as text and are accessible to the cluster.\n\nCreating a Secret\n-----------------\n\nThe following steps simply demonstrate how to create a Secret but there are\nseveral ways to create a Secret, as explained in the\n[Secret](https://kubernetes.io/docs/concepts/configuration/secret/#creating-a-secret)\ntopic.\n\nWhen you create a Secret, make sure you create it in the same namespace as the\ncluster that is running your Knative serving service. In these\nexamples, the `default` namespace is used.\n\nTo create a Secret in the `default` namespace of your cluster:\n\n- Create a Secret using a file:\n\n echo -n 'devuser' \u003e ./username.txt\n echo -n 'S!B\\*d$zDsb' \u003e ./password.txt\n kubectl create secret generic user-creds --from-file=./username.txt --from-file=./password.txt\n\n- Create a Secret using a `kubectl` command only:\n\n kubectl create secret generic user-creds --from-literal=username=devuser --from-literal=password='S!B\\*d$zDsb'\n\nMaking a Secret available to a service\n--------------------------------------\n\nYou can associate secrets with a service using the Google Cloud console or\ncommand-line tools when you deploy a new\n[service](/kubernetes-engine/enterprise/knative-serving/docs/deploying#service) or update an existing service and\ndeploy a [revision](/kubernetes-engine/enterprise/knative-serving/docs/deploying#revision): \n\n### Console\n\n\n1. Go to Knative serving in the Google Cloud console:\n\n [Go to Knative serving](https://console.cloud.google.com/kubernetes/run)\n2. Click **Create Service** if you are configuring a\n new service you are deploying to. If you are configuring an\n existing service, click on the service, then click\n **Edit \\& Deploy New Revision**.\n\n3. Under *Advanced settings* , click **Variables and Secrets**.\n\n4. Under *Reference a Secret*, select the\n desired Secret from the pulldown menu.\n\n - In the *Reference method* pulldown menu, select the way you want to use your Secret, mounted as a volume or exposed as environment variables.\n - If you are using mount as a volume, specify the path, then click **Done**.\n - If you are exposing as environment variables:**Caution** : Do not use [environment\n | variables](/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables) for sensitive information.\n 1. Supply the *Name* of the variable and select the corresponding Secret value from the *Key* pulldown menu.\n 2. Click **Add** to add another secret value.\n 3. Supply the *Name* of the variable and select the corresponding Secret value from the *Key* pulldown menu.\n 4. Click **Done**.\n\n \u003cbr /\u003e\n\n5. Click **Next** to continue to the next section.\n\n6. In the **Configure how this service is triggered** section,\n select which connectivity you would like to use to invoke the service.\n\n7. Click **Create** to deploy the image to Knative serving and wait\n for the deployment to finish.\n\n### gcloud\n\n\nYou can use the Google Cloud CLI to associate Secrets\nwith new services or to update existing services:\n\n\u003cbr /\u003e\n\n- For existing services, update a Secret by running the\n [`gcloud run services update`](/sdk/gcloud/reference/run/services/update)\n command with one of the following parameters:\n\n - [`--clear-secrets`](/sdk/gcloud/reference/run/services/update#--clear-secrets)\n - [`--remove-secrets`](/sdk/gcloud/reference/run/services/update#--remove-secrets)\n - [`--set-secrets`](/sdk/gcloud/reference/run/services/update#--set-secrets)\n - [`--update-secrets`](/sdk/gcloud/reference/run/services/update#--update-secrets)\n\n Example: \n\n ```bash\n gcloud run services update SERVICE --update-secrets KEY1=VALUE1,KEY2=VALUE2\n ```\n\n Replace:\n - \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your service.\n - \u003cvar translate=\"no\"\u003eKEY1=VALUE1,KEY2=VALUE2\u003c/var\u003e with a comma separated list of name and value pairs for each Secret. For each \u003cvar translate=\"no\"\u003eKEY\u003c/var\u003e you specify the path by starting with a forward slash `/` to mount a Secret as a file. Optionally, you can exclude the forward slash to mount the Secret as an [environment variable](/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables). For each \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e, specify the secret name. [How to specify multiple parameters](#command-line). \n\n ### Command parameter options\n\n To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example: \n\n ```bash\n [...]\n --update-secrets \"KEY=VALUE1\" \\\n --update-secrets \"KEY=VALUE2\" \\\n --update-secrets \"KEY=VALUE3\"\n ``` \n OK\n- For new services, associate a Secret by running the\n `gcloud run deploy` command with the\n [`--set-secrets`](/sdk/gcloud/reference/run/deploy#--set-secrets)\n parameter:\n\n ```bash\n gcloud run deploy SERVICE --image=IMAGE_URL --set-secrets KEY1=VALUE1,KEY2=VALUE2\n ```\n\n Replace:\n - \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for example, `gcr.io/cloudrun/hello`.\n - \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your service.\n - \u003cvar translate=\"no\"\u003eKEY1=VALUE1,KEY2=VALUE2\u003c/var\u003e with a comma separated list of name and value pairs for each Secret. For each \u003cvar translate=\"no\"\u003eKEY\u003c/var\u003e you specify the path by starting with a forward slash `/` to mount a Secret as a file. Optionally, you can exclude the forward slash to mount the Secret as an [environment variable](/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables). For each \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e, specify the secret name. [How to specify multiple parameters](#command-line). \n\n ### Command parameter options\n\n To specify several sets of key-value pairs, you can specify multiple parameters for readability. Example: \n\n ```bash\n [...]\n --update-secrets \"KEY=VALUE1\" \\\n --update-secrets \"KEY=VALUE2\" \\\n --update-secrets \"KEY=VALUE3\"\n ``` \n OK"]]