Mulai menggunakan Endpoints untuk Compute Engine dengan ESPv2

Tutorial ini menunjukkan cara men-deploy layanan gRPC contoh sederhana dengan Extensible Service Proxy V2 (ESPv2) dalam container Docker di Compute Engine.

Tutorial ini menggunakan contoh bookstore-grpc versi Python. Lihat bagian Langkah berikutnya untuk contoh gRPC dalam bahasa lain.

Untuk ringkasan Cloud Endpoints, lihat Tentang Endpoints dan Arsitektur Endpoints.

Tujuan

Gunakan daftar tugas tingkat tinggi berikut saat Anda mengerjakan tutorial ini. Semua tugas diperlukan agar berhasil mengirim permintaan ke API.

  1. Siapkan Google Cloud project, dan download software yang diperlukan. Lihat Sebelum memulai.
  2. Membuat instance VM Compute Engine. Lihat Membuat instance Compute Engine.
  3. Salin dan konfigurasi file dari contoh bookstore-grpc. Lihat Mengonfigurasi Endpoint.
  4. Deploy konfigurasi Endpoints untuk membuat layanan Endpoints. Lihat Men-deploy konfigurasi Endpoints.
  5. Deploy API dan ESPv2 di VM Compute Engine. Lihat Men-deploy backend API.
  6. Kirim permintaan ke API. Lihat Mengirim permintaan ke API.
  7. Hindari dikenai biaya untuk akun Google Cloud Anda. Lihat Pembersihan.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih sebagai berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga.

Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, baca bagian Pembersihan.

Sebelum memulai

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. Catat project ID karena akan diperlukan nanti.
  7. Instal dan lakukan inisialisasi Google Cloud CLI.
  8. Update gcloud CLI dan instal komponen Endpoints:
    gcloud components update
  9. Pastikan Google Cloud CLI (gcloud) diizinkan untuk mengakses data dan layanan Anda di Google Cloud:
    gcloud auth login
    Di tab browser baru yang terbuka, pilih akun.
  10. Tetapkan project default ke project ID Anda.
    gcloud config set project YOUR_PROJECT_ID

    Ganti YOUR_PROJECT_ID dengan project ID Anda. Jika Anda memiliki project Google Cloud lain, dan Anda ingin menggunakan gcloud untuk mengelolanya, lihat Mengelola Konfigurasi gcloud CLI.

  11. Ikuti langkah-langkah di Panduan memulai gRPC Python untuk menginstal gRPC dan alat gRPC.
  12. Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, lihat Pembersihan.

Membuat instance Compute Engine

Untuk membuat instance Compute Engine:

  1. In the Google Cloud console, go to the Create an instance page.

    Go to Create an instance

  2. Di bagian Firewall, pilih Allow HTTP traffic dan Allow HTTPS traffic.
  3. Untuk membuat VM, klik Create.
  4. Screenshot jendela pembuatan instance VM dengan opsi yang diperlukan telah ditetapkan

    Tunggu hingga instance memulai. Setelah siap, instance akan dicantumkan di halaman VM Instances dengan ikon status hijau.

  5. Pastikan Anda dapat terhubung ke instance VM.
    1. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
    2. Anda kini dapat menggunakan terminal untuk menjalankan perintah Linux di instance Debian.
    3. Masukkan exit untuk memutuskan koneksi dari instance.
  6. Catat nama instance, zona, dan alamat IP eksternal karena akan diperlukan nanti.

Mengonfigurasi Endpoint

Clone repositori contoh bookstore-grpc dari GitHub.

Untuk mengonfigurasi Endpoint:

  1. Create a self-contained protobuf descriptor file from your service .proto file:
    1. Save a copy of bookstore.proto from the example repository. This file defines the Bookstore service's API.
    2. Create the following directory: mkdir generated_pb2
    3. Create the descriptor file, api_descriptor.pb, by using the protoc protocol buffers compiler. Run the following command in the directory where you saved bookstore.proto:
      python -m grpc_tools.protoc \
          --include_imports \
          --include_source_info \
          --proto_path=. \
          --descriptor_set_out=api_descriptor.pb \
          --python_out=generated_pb2 \
          --grpc_python_out=generated_pb2 \
          bookstore.proto

      In the preceding command, --proto_path is set to the current working directory. In your gRPC build environment, if you use a different directory for .proto input files, change --proto_path so the compiler searches the directory where you saved bookstore.proto.

  2. Create a gRPC API configuration YAML file:
    1. Save a copy of the api_config.yamlfile. This file defines the gRPC API configuration for the Bookstore service.
    2. Replace MY_PROJECT_ID in your api_config.yaml file with your Google Cloud project ID. For example:
      #
      # Name of the service configuration.
      #
      name: bookstore.endpoints.example-project-12345.cloud.goog
      

      Note that the apis.name field value in this file exactly matches the fully-qualified API name from the .proto file; otherwise deployment won't work. The Bookstore service is defined in bookstore.proto inside package endpoints.examples.bookstore. Its fully-qualified API name is endpoints.examples.bookstore.Bookstore, just as it appears in the api_config.yaml file.

      apis:
        - name: endpoints.examples.bookstore.Bookstore

Lihat Mengonfigurasi Endpoint untuk mengetahui informasi selengkapnya.

Men-deploy konfigurasi Endpoint

Untuk men-deploy konfigurasi Endpoints, Anda menggunakan perintah gcloud endpoints services deploy. Perintah ini menggunakan Service Management untuk membuat layanan terkelola.

  1. Make sure you are in the directory where the api_descriptor.pb and api_config.yaml files are located.
  2. Confirm that the default project that the gcloud command-line tool is currently using is the Google Cloud project that you want to deploy the Endpoints configuration to. Validate the project ID returned from the following command to make sure that the service doesn't get created in the wrong project.
    gcloud config list project
    

    If you need to change the default project, run the following command:

    gcloud config set project YOUR_PROJECT_ID
    
  3. Deploy the proto descriptor file and the configuration file by using the Google Cloud CLI:
    gcloud endpoints services deploy api_descriptor.pb api_config.yaml
    

    As it is creating and configuring the service, Service Management outputs information to the terminal. When the deployment completes, a message similar to the following is displayed:

    Service Configuration [CONFIG_ID] uploaded for service [bookstore.endpoints.example-project.cloud.goog]

    CONFIG_ID is the unique Endpoints service configuration ID created by the deployment. For example:

    Service Configuration [2017-02-13r0] uploaded for service [bookstore.endpoints.example-project.cloud.goog]
    

    In the previous example, 2017-02-13r0 is the service configuration ID and bookstore.endpoints.example-project.cloud.goog is the service name. The service configuration ID consists of a date stamp followed by a revision number. If you deploy the Endpoints configuration again on the same day, the revision number is incremented in the service configuration ID.

Memeriksa layanan yang diperlukan

Minimal, Endpoints dan ESP memerlukan layanan Google berikut diaktifkan:
Nama Judul
servicemanagement.googleapis.com Service Management API
servicecontrol.googleapis.com Service Control API

Dalam sebagian besar kasus, perintah gcloud endpoints services deploy mengaktifkan layanan yang diperlukan ini. Namun, perintah gcloud berhasil diselesaikan, tetapi tidak mengaktifkan layanan yang diperlukan dalam keadaan berikut:

  • Jika Anda menggunakan aplikasi pihak ketiga seperti Terraform, dan Anda tidak menyertakan layanan ini.

  • Anda men-deploy konfigurasi Endpoints ke projectGoogle Cloud yang ada dan layanan ini dinonaktifkan secara eksplisit di project tersebut.

Gunakan perintah berikut untuk mengonfirmasi bahwa layanan yang diperlukan sudah diaktifkan:

gcloud services list

Jika Anda tidak melihat layanan yang diperlukan tercantum, aktifkan layanan tersebut:

gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com

Aktifkan juga layanan Endpoints Anda:

gcloud services enable ENDPOINTS_SERVICE_NAME

Untuk menentukan ENDPOINTS_SERVICE_NAME, Anda dapat:

  • Setelah men-deploy konfigurasi Endpoints, buka halaman Endpoints di Konsol Cloud. Daftar ENDPOINTS_SERVICE_NAME yang mungkin ditampilkan di kolom Nama layanan.

  • Untuk OpenAPI, ENDPOINTS_SERVICE_NAME adalah yang Anda tentukan di kolom host spesifikasi OpenAPI. Untuk gRPC, ENDPOINTS_SERVICE_NAME adalah yang Anda tentukan di kolom name konfigurasi gRPC Endpoints.

Untuk mengetahui informasi selengkapnya tentang perintah gcloud, lihat layanan gcloud.

Jika Anda menerima pesan error, lihat Memecahkan masalah deployment konfigurasi Endpoints. Lihat Men-deploy konfigurasi Endpoints untuk mengetahui informasi tambahan.

Men-deploy backend API

Sejauh ini Anda telah men-deploy konfigurasi API ke Service Management, tetapi Anda belum men-deploy kode yang melayani backend API. Bagian ini memandu Anda menyiapkan Docker di instance VM dan menjalankan kode backend API serta ESPv2 dalam container Docker.

Instal Docker di instance VM

Untuk menginstal Docker di instance VM:

  1. Tetapkan zona untuk project Anda dengan menjalankan perintah:
    gcloud config set compute/zone YOUR_INSTANCE_ZONE
    

    Ganti YOUR_INSTANCE_ZONE dengan zona tempat instance Anda berjalan.

  2. Hubungkan ke instance Anda menggunakan perintah berikut:
    gcloud compute ssh INSTANCE_NAME
    

    Ganti INSTANCE_NAME dengan nama instance VM Anda.

  3. Lihat dokumentasi Docker untuk menyiapkan repositori Docker. Pastikan untuk mengikuti langkah-langkah yang sesuai dengan versi dan arsitektur instance VM Anda:
    • Jessie atau yang lebih baru
    • x86_64 / amd64

Menjalankan API contoh dan ESPv2 di container Docker

Untuk menjalankan layanan gRPC contoh dengan ESPv2 dalam container Docker sehingga klien dapat menggunakannya:

  1. Di instance VM, buat jaringan container Anda sendiri yang disebut esp_net.
    sudo docker network create --driver bridge esp_net
    
  2. Jalankan server Bookstore contoh yang menyediakan contoh API:
    sudo docker run \
        --detach \
        --name=bookstore \
        --net=esp_net \
        gcr.io/endpointsv2/python-grpc-bookstore-server:1
    
  3. Jalankan container Docker ESPv2 yang telah dipaketkan sebelumnya. Di opsi startup ESPv2, ganti SERVICE_NAME dengan nama layanan Anda. Ini adalah nama yang sama dengan yang Anda konfigurasi di kolom name dalam file api_config.yaml. Contoh: bookstore.endpoints.example-project-12345.cloud.goog
    sudo docker run \
        --detach \
        --name=esp \
        --publish=80:9000 \
        --net=esp_net \
        gcr.io/endpoints-release/endpoints-runtime:2 \
        --service=SERVICE_NAME \
        --rollout_strategy=managed \
        --listener_port=9000 \
        --backend=grpc://bookstore:8000
    

    Opsi --rollout_strategy=managed mengonfigurasi ESPv2 untuk menggunakan konfigurasi layanan yang di-deploy terbaru. Saat Anda menentukan opsi ini, dalam waktu satu menit setelah Anda men-deploy konfigurasi layanan baru, ESPv2 akan mendeteksi perubahan dan otomatis mulai menggunakannya. Sebaiknya tentukan opsi ini, bukan memberikan ID konfigurasi tertentu untuk digunakan ESPv2. Untuk mengetahui detail selengkapnya tentang argumen ESPv2, lihat Opsi startup ESPv2.

Jika Anda menerima pesan error, lihat Memecahkan masalah Endpoints di Compute Engine.

Mengirim permintaan ke API

Jika Anda mengirim permintaan dari instance yang sama dengan tempat container Docker berjalan, Anda dapat mengganti SERVER_IP dengan localhost. Jika tidak, ganti SERVER_IP dengan IP eksternal instance.

Anda dapat menemukan alamat IP eksternal dengan menjalankan:

gcloud compute instances list

To send requests to the sample API, you can use a sample gRPC client written in Python.

  1. Clone the git repo where the gRPC client code is hosted:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
       

  2. Change your working directory:

    cd python-docs-samples/endpoints/bookstore-grpc/
      

  3. Install dependencies:

    pip install virtualenv
    virtualenv env
    source env/bin/activate
    python -m pip install -r requirements.txt

  4. Send a request to the sample API:

    python bookstore_client.py --host SERVER_IP --port 80
    

Jika Anda tidak mendapatkan respons yang berhasil, lihat Memecahkan masalah error respons.

Anda baru saja men-deploy dan menguji API di Endpoints.

Pembersihan

Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

  1. Hapus API:
    gcloud endpoints services delete SERVICE_NAME

    Ganti SERVICE_NAME dengan nama layanan Anda.

  2. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  3. Select the checkbox for the instance that you want to delete.
  4. To delete the instance, click More actions, click Delete, and then follow the instructions.

Langkah berikutnya