Pastikan penagihan diaktifkan untuk project Cloud Anda. Pelajari cara memeriksa apakah penagihan diaktifkan pada suatu proyek
Untuk menginisialisasi gcloud CLI, jalankan perintah berikut
gcloud initCatatan. Jika Anda menginstal gcloud CLI sebelumnya, pastikan Anda memiliki versi terbaru dengan menjalankan gcloud components updateDi Google Cloud Console, di halaman pemilih project, pilih atau buat project Google Cloud
Catatan. Jika Anda tidak berencana untuk mempertahankan sumber daya yang Anda buat dalam prosedur ini, buatlah proyek daripada memilih proyek yang sudah ada. Setelah Anda menyelesaikan langkah-langkah ini, Anda dapat menghapus proyek, menghapus semua sumber daya yang terkait dengan proyek tersebutPergi ke pemilih proyek
Pastikan penagihan diaktifkan untuk project Cloud Anda. Pelajari cara memeriksa apakah penagihan diaktifkan pada suatu proyek
Untuk menginisialisasi gcloud CLI, jalankan perintah berikut
gcloud initCatatan. Jika Anda menginstal gcloud CLI sebelumnya, pastikan Anda memiliki versi terbaru dengan menjalankan gcloud components update- Untuk menyetel project default untuk layanan Cloud Run Anda. gcloud config set project PROJECT_IDGanti PROJECT_ID dengan nama proyek yang Anda buat untuk quickstart ini
Tulis contoh aplikasinya
Untuk menulis aplikasi dalam PHP
Buat direktori baru bernama gcloud init_0 dan ubah direktori ke dalamnya
mkdir helloworld-php cd helloworld-php _Buat file bernama gcloud init_1 dan rekatkan kode berikut ke dalamnya (pastikan Anda menambahkan gcloud init2 ke bagian atas file)
jalankan/helloworld/index. php
Lihat di GitHub
--> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name);
Kode ini menanggapi permintaan dengan sapaan "Hello World" kami. Penanganan HTTP dilakukan oleh server web Apache di dalam wadah
Buat file baru bernama gcloud init_3 di direktori yang sama dengan file sumber. PHP Dockerfile memulai server web Apache yang mendengarkan port yang ditentukan oleh
jalankan/helloworld/Dockerfile
Lihat di GitHub
# Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" _
Tambahkan file gcloud init5 untuk mengecualikan file dari gambar penampung Anda
lari/helloworld/. dockerignore
Lihat di GitHub
# The .dockerignore file excludes files from the container build process. # # //docs.docker.com/engine/reference/builder/#dockerignore-file # Exclude locally vendored dependencies. vendor/ # Exclude "build-time" ignore files. .dockerignore .gcloudignore # Exclude git history and configuration. .gitignore
Aplikasi Anda sudah selesai dan siap untuk di-deploy
Terapkan ke Cloud Run dari sumber
Penting. Quickstart ini mengasumsikan bahwa Anda memiliki peran pemilik atau editor dalam proyek yang Anda gunakan untuk quickstart. Jika tidak, lihat , , dan untuk izin yang diperlukan
Men-deploy dari sumber secara otomatis membuat image container dari kode sumber dan men-deploy-nya
Untuk menerapkan dari sumber
Di direktori kode sumber Anda, terapkan dari sumber menggunakan perintah berikut
gcloud run deployJika diminta untuk mengaktifkan API, Balas y untuk mengaktifkan
Saat Anda diminta untuk lokasi kode sumber, tekan Enter untuk menggunakan folder saat ini
Saat Anda diminta untuk nama layanan, tekan Enter untuk menerima nama default, gcloud init6
Jika Anda diminta untuk mengaktifkan Artifact Registry API, tanggapi dengan menekan gcloud init7
Ketika Anda diminta untuk wilayah. pilih pilihan Anda, misalnya ________0______8
Anda akan diminta untuk mengizinkan pemanggilan yang tidak diautentikasi. menanggapi gcloud init7
Kemudian tunggu beberapa saat hingga penerapan selesai. Jika berhasil, baris perintah menampilkan URL layanan
Kunjungi layanan yang Anda terapkan dengan membuka URL layanan di browser web
Lokasi Cloud Run
Cloud Run bersifat regional, artinya infrastruktur yang menjalankan layanan Cloud Run Anda berada di region tertentu dan dikelola oleh Google agar tersedia secara redundan di semua zona dalam region tersebut
Memenuhi persyaratan latensi, ketersediaan, atau daya tahan adalah faktor utama untuk memilih region tempat layanan Cloud Run dijalankan. Biasanya Anda dapat memilih wilayah yang terdekat dengan pengguna Anda, tetapi Anda harus mempertimbangkan lokasi wilayah yang digunakan oleh layanan Cloud Run Anda. Menggunakan produk Google Cloud secara bersamaan di beberapa lokasi dapat memengaruhi latensi dan biaya layanan Anda
Cloud Run tersedia di region berikut
Tunduk pada
- gcloud config set project PROJECT_ID0 (Taiwan)
- gcloud config set project PROJECT_ID1 (Tokyo)
- gcloud config set project PROJECT_ID2 (Osaka)
- gcloud config set project PROJECT_ID3 (Finlandia) CO2 rendah
- gcloud config set project PROJECT_ID4 (Madrid) CO2 rendah
- gcloud config set project PROJECT_ID5 (Belgia) CO2 rendah
- gcloud config set project PROJECT_ID6 (Belanda)
- gcloud config set project PROJECT_ID7 (Milan)
- gcloud config set project PROJECT_ID8 (Paris) CO2 rendah
- gcloud config set project PROJECT_ID9 (Tel Aviv)
- gcloud init8 (Iowa) CO2 rendah
- mkdir helloworld-php cd helloworld-php _1 (Carolina Selatan)
- mkdir helloworld-php cd helloworld-php _2 (Virginia Utara)
- mkdir helloworld-php cd helloworld-php _3 (Columbus)
- mkdir helloworld-php cd helloworld-php _4 (Dallas)
- mkdir helloworld-php cd helloworld-php 5 (Oregon) CO2 rendah
Tunduk pada
- mkdir helloworld-php cd helloworld-php _6 (Hong Kong)
- mkdir helloworld-php cd helloworld-php _7 (Seoul, Korea Selatan)
- mkdir helloworld-php cd helloworld-php _8 (Singapura)
- mkdir helloworld-php cd helloworld-php _9 (Jakarta)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 0 (Mumbai, India)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 1 (Delhi, India)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 2 (Sydney)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 3 (Melbourne)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 4 (Warsawa, Polandia)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 5 (London, Inggris Raya)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 6 (Frankfurt, Jerman)
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 7 (Zurich, Swiss) CO2 rendah
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 8 (Montreal) CO2 rendah
- --> <?php $name = getenv('NAME', true) ?: 'World'; echo sprintf('Hello %s!', $name); 9 (Toronto) CO2 rendah
- # Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" 0 (Sao Paulo, Brasil) CO2 rendah
- # Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" _1 (Santiago, Chili)
- # Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" _2 (Los Angeles)
- # Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" _3 (Kota Salt Lake)
- # Use the official PHP image. # //hub.docker.com/_/php FROM php:8.0-apache # Configure PHP for Cloud Run. # Precompile PHP code with opcache. RUN docker-php-ext-install -j "$(nproc)" opcache RUN set -ex; \ { \ echo "; Cloud Run enforces memory & timeouts"; \ echo "memory_limit = -1"; \ echo "max_execution_time = 0"; \ echo "; File upload at Cloud Run network limit"; \ echo "upload_max_filesize = 32M"; \ echo "post_max_size = 32M"; \ echo "; Configure Opcache for Containers"; \ echo "opcache.enable = On"; \ echo "opcache.validate_timestamps = Off"; \ echo "; Configure Opcache Memory (Application-specific)"; \ echo "opcache.memory_consumption = 32"; \ } > "$PHP_INI_DIR/conf.d/cloud-run.ini" # Copy in custom code from the host machine. WORKDIR /var/www/html COPY . ./ # Use the PORT environment variable in Apache configuration files. # //cloud.google.com/run/docs/reference/container-contract#port RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf # Configure PHP for development. # Switch to the production php.ini for production operations. # RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" # //github.com/docker-library/docs/blob/master/php/README.md#configuration RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" _4 (Las Vegas)
Jika sudah membuat layanan Cloud Run, Anda dapat melihat region di dasbor Cloud Run di Google Cloud Console
OKE
Selamat. Anda baru saja men-deploy image container dari kode sumber ke Cloud Run. Cloud Run secara otomatis dan horizontal menskalakan image container Anda untuk menangani permintaan yang diterima, lalu menskalakannya saat permintaan menurun. Anda hanya membayar CPU, memori, dan jaringan yang digunakan selama penanganan permintaan
Membersihkan
Hapus proyek pengujian Anda
Meskipun Cloud Run tidak mengenakan biaya saat layanan tidak digunakan, Anda mungkin masih dikenakan biaya untuk menyimpan image container di Artifact Registry. Anda dapat atau menghapus proyek Cloud Anda untuk menghindari timbulnya biaya. Menghapus project Cloud Anda akan menghentikan penagihan untuk semua resource yang digunakan dalam project tersebut