Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pelajari cara membuat ConfigMap, lalu konfigurasikan layanan dan revisi
penayangan Knative untuk menggunakan ConfigMap tersebut.
Kasus penggunaan umum untuk layanan adalah menggunakan ConfigMaps untuk memisahkan kode
aplikasi dari konfigurasi. ConfigMap mirip dengan
Secret, kecuali Anda menggunakan
Secret untuk informasi sensitif dan menggunakan ConfigMap untuk menyimpan
data non-sensitif seperti string koneksi, kredensial publik, nama host,
dan URL. Anda dapat mempelajari lebih lanjut cara menggunakan ConfigMap di
dokumentasi Google Kubernetes Engine.
Saat mengaktifkan penampung untuk mengakses ConfigMap, Anda dapat memilih salah satu opsi berikut:
Pasang ConfigMap sebagai volume, dengan entri ConfigMap tersedia sebagai file dalam
volume yang dipasang. Hal ini direkomendasikan karena memastikan Anda mendapatkan
versi terbaru ConfigMap saat membacanya.
Ada beberapa cara untuk membuat ConfigMap, seperti yang dijelaskan di halaman ConfigMaps dalam dokumentasi GKE.
Untuk memudahkan Anda, langkah-langkah berikut menunjukkan cara sederhana untuk membuat
ConfigMap.
Saat membuat ConfigMap, pastikan Anda membuatnya di namespace yang sama dengan
cluster yang menjalankan layanan penayangan Knative Anda. Petunjuk
ini menggunakan namespace default.
Anda dapat membuat ConfigMap dari command line:
kubectlcreateconfigmapNAMEDATA
Ganti:
NAME adalah nama objek ConfigMap Anda.
DATA dapat berupa:
Flag dan jalur --from-file untuk setiap direktori yang berisi
satu atau beberapa file konfigurasi.
Tanda --from-literal dan key-value pair untuk setiap tanda yang ingin Anda
tambahkan ke ConfigMap.
ConfigMap dibuat dan diupload ke tempat yang tersedia untuk cluster di
namespace default.
Membuat ConfigMap tersedia untuk layanan
Setelah membuat ConfigMap, Anda dapat menyediakannya untuk
layanan penayangan Knative sebagai volume atau sebagai variabel
lingkungan menggunakan konsol Google Cloud atau Google Cloud CLI
saat men-deploy layanan baru atau mengupdate
layanan yang ada dan men-deploy revisi:
Klik Buat Layanan jika Anda mengonfigurasi
layanan baru yang akan di-deploy. Jika Anda mengonfigurasi
layanan yang sudah ada, klik layanan tersebut, lalu klik
Edit & Deploy New Revision.
Di bagian Setelan lanjutan, klik Variabel dan Secret.
Di bagian Referensikan ConfigMap, pilih
ConfigMap yang diinginkan dari menu pulldown.
Di menu pull-down Metode referensi, pilih cara Anda ingin menggunakan
ConfigMap, yang dipasang sebagai volume atau diekspos sebagai variabel lingkungan.
Jika Anda menggunakan pemasangan sebagai volume, tentukan jalurnya, lalu klik Selesai.
Jika Anda mengekspos sebagai variabel lingkungan:
Berikan Nama variabel dan pilih nilai ConfigMap yang sesuai dari menu pulldown Kunci.
Klik Tambahkan untuk menambahkan nilai ConfigMap lainnya.
Berikan Nama variabel dan pilih nilai ConfigMap yang sesuai dari menu pulldown Kunci.
Klik Selesai.
Klik Berikutnya untuk melanjutkan ke bagian berikutnya.
Di bagian Configure how this service is triggered,
pilih konektivitas yang ingin Anda gunakan untuk memanggil layanan.
Klik Create untuk men-deploy image ke layanan Knative dan tunggu deployment selesai.
Command line
Anda dapat menggunakan Google Cloud CLI untuk menetapkan ConfigMaps
untuk layanan baru atau mengupdate layanan yang ada:
Untuk layanan yang ada, update ConfigMap dengan menjalankan perintah
gcloud run services update
dengan salah satu parameter berikut:
KEY1=VALUE1,KEY2=VALUE2 dengan daftar yang dipisahkan koma dari pasangan nama dan nilai untuk setiap ConfigMap.
Untuk setiap KEY, Anda dapat menentukan jalur pemasangan, atau memberikan variabel lingkungan. Tentukan jalur pemasangan
dengan memulai dengan garis miring /. Semua kunci lainnya sesuai dengan
variabel lingkungan. Untuk setiap VALUE, tentukan nama ConfigMap.
Cara menentukan beberapa parameter.
Opsi parameter perintah
Untuk menentukan beberapa kumpulan pasangan nilai kunci, Anda
dapat menentukan beberapa parameter untuk keterbacaan. Contoh:
IMAGE_URL dengan referensi ke image container,
misalnya, gcr.io/cloudrun/hello.
SERVICE dengan nama layanan Anda.
KEY1=VALUE1,KEY2=VALUE2 dengan daftar yang dipisahkan koma dari pasangan nama dan nilai untuk setiap ConfigMap.
Untuk setiap KEY, Anda dapat menentukan jalur pemasangan, atau memberikan variabel lingkungan. Tentukan jalur pemasangan
dengan memulai dengan garis miring /. Semua kunci lainnya sesuai dengan
variabel lingkungan. Untuk setiap VALUE, tentukan nama ConfigMap.
Cara menentukan beberapa parameter.
Opsi parameter perintah
Untuk menentukan beberapa kumpulan pasangan nilai kunci, Anda
dapat menentukan beberapa parameter untuk keterbacaan. Contoh:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-04 UTC."],[],[],null,["# Using ConfigMaps\n\nLearn how to create a ConfigMap and then configure your Knative serving\nservices and revisions to use that ConfigMap.\n\nA common use case for a service is to use ConfigMaps to separate application\ncode from configuration. ConfigMap is similar to\n[Secret](/kubernetes-engine/enterprise/knative-serving/docs/configuring/using-secrets) except that you use\na Secret for sensitive information and you use a ConfigMap to store\nnon-sensitive data such as connection strings, public credentials, hostnames,\nand URLs. You can learn more about using ConfigMaps in the\n[Google Kubernetes Engine documentation](/kubernetes-engine/docs/concepts/configmap).\n\nWhen you enable containers to access ConfigMaps, you can choose either of these\noptions:\n\n- Mount the ConfigMap as a volume, with ConfigMap entries available as files in the mounted volume. This is recommended because it ensures that you get the latest version of the ConfigMap when you are reading it.\n- Pass the ConfigMap using [environment variables](/kubernetes-engine/enterprise/knative-serving/docs/configuring/environment-variables).\n\nCreating a ConfigMap\n--------------------\n\nThere are several ways to create a ConfigMap, as described in the\n[ConfigMaps](/kubernetes-engine/docs/concepts/configmap) page of the\nGKE documentation.\nFor your convenience, the following steps show a simple way to create a\nConfigMap.\n\nWhen you create a ConfigMap, make sure you create it in the same namespace as\nthe cluster that is running your Knative serving service. These\ninstructions use the `default` namespace.\n\nYou can create a ConfigMap from the command line: \n\n kubectl create configmap \u003cvar translate=\"no\"\u003eNAME\u003c/var\u003e \u003cvar translate=\"no\"\u003eDATA\u003c/var\u003e\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eNAME\u003c/var\u003e is the name of your ConfigMap object.\n- \u003cvar translate=\"no\"\u003eDATA\u003c/var\u003e can be either:\n\n - The `--from-file` flag and path for each directory that contains one or more configuration files.\n - The `--from-literal` flag and key-value pair for each that you want to add to the ConfigMap.\n\nFor example: \n\n kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm\n\nThe ConfigMap is created and uploaded where it is available to clusters in the\n`default` namespace.\n\nMaking a ConfigMap available to a service\n-----------------------------------------\n\nAfter you create a ConfigMap, you can make it available to your\nKnative serving service either as a volume or as environment\nvariables using the Google Cloud console or the Google Cloud CLI\nwhen you deploy a new [service](/kubernetes-engine/enterprise/knative-serving/docs/deploying#service) or update an\nexisting service and deploy 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 ConfigMap*, select the\n desired ConfigMap from the pulldown menu.\n\n - In the *Reference method* pulldown menu, select the way you want to use your ConfigMap, 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:\n 1. Supply the *Name* of the variable and select the corresponding ConfigMap value from the *Key* pulldown menu.\n 2. Click **Add** to add another ConfigMap value.\n 3. Supply the *Name* of the variable and select the corresponding ConfigMap 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### Command line\n\n\nYou can use the Google Cloud CLI to set ConfigMaps\nfor new services or to update an existing services:\n\n\u003cbr /\u003e\n\n- For existing services, update a ConfigMap by running the\n [`gcloud run services update`](/sdk/gcloud/reference/run/services/update)\n command with one of the following parameters:\n\n - [`--set-config-maps`](/sdk/gcloud/reference/run/services/update#--set-config-maps)\n - [`--update-config-maps`](/sdk/gcloud/reference/run/services/update#--update-config-maps)\n - [`--clear-config-maps`](/sdk/gcloud/reference/run/services/update#--clear-config-maps)\n - [`--remove-config-maps`](/sdk/gcloud/reference/run/services/update#--remove-config-maps)\n\n Example: \n\n ```bash\n gcloud run services update SERVICE --update-config-maps 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 ConfigMap. For each \u003cvar translate=\"no\"\u003eKEY\u003c/var\u003e you can specify a mount path, or provide an environment variable. Specify a mount path by starting with a forward slash `/`. All other keys correspond to environment variables. For each \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e, specify the ConfigMap 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-config-maps \"KEY=VALUE1\" \\\n --update-config-maps \"KEY=VALUE2\" \\\n --update-config-maps \"KEY=VALUE3\"\n ``` \n OK\n- For new services, set the ConfigMap by running the\n `gcloud run deploy` command with the\n [`--set-config-maps`](/sdk/gcloud/reference/run/deploy#--set-config-maps)\n parameter:\n\n ```bash\n gcloud run deploy SERVICE --image=IMAGE_URL --set-config-maps 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 ConfigMap. For each \u003cvar translate=\"no\"\u003eKEY\u003c/var\u003e you can specify a mount path, or provide an environment variable. Specify a mount path by starting with a forward slash `/`. All other keys correspond to environment variables. For each \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e, specify the ConfigMap 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-config-maps \"KEY=VALUE1\" \\\n --update-config-maps \"KEY=VALUE2\" \\\n --update-config-maps \"KEY=VALUE3\"\n ``` \n OK"]]