Tutorial ini menunjukkan cara mengonfigurasi dan men-deploy API contoh dan Extensible Service Proxy V2 (ESPv2) ke cluster Kubernetes yang tidak ada di Google Cloud. Jika Anda ingin menggunakan Google Kubernetes Engine (GKE), lihat Mulai menggunakan Endpoints di GKE.
Contoh API dijelaskan menggunakan spesifikasi OpenAPI. Tutorial ini juga menunjukkan cara membuat kunci API untuk mengirim permintaan ke API.
Tutorial ini menggunakan image container bawaan dari kode contoh dan ESPv2, yang disimpan di Artifact Registry. Jika Anda belum memahami container, lihat informasi selengkapnya di bawah:
Untuk mengetahui ringkasan Cloud Endpoints, lihat Tentang Endpoints dan Arsitektur Endpoints.
Tujuan
Gunakan daftar tugas tingkat tinggi berikut saat Anda mengerjakan tutorial ini. Semua tugas di Bagian 1 diperlukan agar berhasil mengirim permintaan ke API.
Bagian 1
- Siapkan Google Cloud project. Lihat Sebelum memulai.
- Instal dan konfigurasi software yang digunakan dalam tutorial. Lihat Menginstal dan mengonfigurasi software yang diperlukan.
- Secara opsional, download kode contoh. Lihat Mendapatkan kode contoh.
- Download file konfigurasi Kubernetes. Lihat Mendapatkan file konfigurasi Kubernetes.
- Konfigurasi file
openapi.yaml, yang digunakan untuk mengonfigurasi Endpoints. Lihat Mengonfigurasi Endpoint. - Deploy konfigurasi Endpoints untuk membuat layanan Cloud Endpoints. Lihat Men-deploy konfigurasi Endpoints.
- Buat kredensial untuk layanan Endpoints Anda. Lihat Membuat kredensial untuk layanan Anda.
- Deploy API dan ESPv2 ke cluster. Lihat Men-deploy backend API.
- Dapatkan alamat IP eksternal layanan. Lihat Mendapatkan alamat IP eksternal.
- Kirim permintaan ke API menggunakan alamat IP. Lihat Mengirim permintaan menggunakan alamat IP.
- Melacak aktivitas API. Lihat Melacak aktivitas API.
Bagian 2
- Konfigurasi data DNS untuk contoh API. Lihat Mengonfigurasi DNS untuk Endpoint.
- Kirim permintaan ke API menggunakan nama domain. Lihat Mengirim permintaan menggunakan FQDN.
Pembersihan
Setelah selesai, lihat Membersihkan untuk menghindari timbulnya biaya pada akun Google Cloud Anda.
Biaya
Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda,
gunakan kalkulator harga.
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
Sebelum memulai
Tutorial ini mengasumsikan bahwa Anda telah menyiapkan Minikube atau cluster Kubernetes. Untuk mengetahui informasi selengkapnya, lihat dokumentasi Kubernetes.
- Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- Catat ID Google Cloud project karena akan diperlukan nanti.
Menginstal dan mengonfigurasi software yang diperlukan
Dalam tutorial ini, Anda akan menginstal Google Cloud CLI untuk menggunakan
gcloud CLI
guna mengelola project Anda.
Anda menggunakan kubectl,
antarmuka command line, untuk menjalankan perintah terhadap cluster Kubernetes. Anda juga memerlukan cara untuk menguji API.
Dalam prosedur berikut, jika Anda sudah menginstal software yang diperlukan, lanjutkan ke langkah berikutnya.
Untuk menginstal dan mengonfigurasi software yang diperlukan:
-
Anda memerlukan aplikasi untuk mengirim permintaan ke contoh API.
- Pengguna Linux dan macOS: Tutorial ini memberikan contoh penggunaan
curl, yang biasanya sudah diinstal sebelumnya di sistem operasi Anda. Jika Anda belum memilikicurl, Anda dapat mendownloadnya daricurlHalaman rilis dan download. - Pengguna Windows: Tutorial ini memberikan contoh penggunaan
Invoke-WebRequest, yang didukung di PowerShell 3.0 dan yang lebih baru.
- Pengguna Linux dan macOS: Tutorial ini memberikan contoh penggunaan
- Instal dan lakukan inisialisasi gcloud CLI.
-
Update gcloud CLI dan instal komponen Endpoints:
gcloud components update
-
Pastikan Google Cloud CLI (
gcloud) diizinkan untuk mengakses data dan layanan Anda di Google Cloud: Di tab baru yang terbuka, pilih akun.gcloud auth login
- 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
gclouduntuk mengelolanya, lihat Mengelola konfigurasi gcloud CLI. - Instal
kubectl:gcloud components install kubectl
-
Dapatkan kredensial pengguna baru untuk digunakan sebagai kredensial default aplikasi.
Kredensial pengguna mengizinkan
kubectl.gcloud auth application-default login
- Di tab baru yang terbuka, pilih akun.
- Jalankan perintah berikut untuk memastikan klien Kubernetes Anda
dikonfigurasi dengan benar:
kubectl version
Anda akan melihat output yang mirip dengan yang berikut:
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.4", GitCommit:"9befc2b8928a9426501d3bf62f72849d5cbcd5a3", GitTreeState:"clean", BuildDate:"2017-11-20T05:28:34Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"7+", GitVersion:"v1.7.8-gke.0", GitCommit:"a7061d4b09b53ab4099e3b5ca3e80fb172e1b018", GitTreeState:"clean", BuildDate:"2017-10-10T18:48:45Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
Mendownload kode contoh
Secara opsional, download kode contoh. Dalam tutorial ini, Anda akan men-deploy image container yang telah dibuat sebelumnya, sehingga Anda tidak perlu membuat container dari kode contoh. Namun, Anda mungkin ingin mendownload kode contoh, yang disediakan dalam beberapa bahasa untuk membantu Anda memahami cara kerja contoh API.
Untuk mendownload kode contoh:
Untuk meng-clone atau mendownload API contoh:
- Buat clone repositori aplikasi contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
- Ubah ke direktori yang berisi kode contoh:
cd java-docs-samples/endpoints/getting-started
Untuk meng-clone atau mendownload API contoh:
- Buat clone repositori aplikasi contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
- Ubah ke direktori yang berisi kode contoh:
cd python-docs-samples/endpoints/getting-started
Untuk meng-clone atau mendownload API contoh:
- Pastikan
variabel lingkungan
GOPATHAnda ditetapkan. - Buat clone repositori aplikasi contoh ke komputer lokal Anda:
go get -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
- Ubah ke direktori yang berisi kode contoh:
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
Untuk meng-clone atau mendownload API contoh:
- Buat clone repositori aplikasi contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/php-docs-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
- Ubah ke direktori yang berisi kode contoh:
cd php-docs-samples/endpoints/getting-started
Untuk meng-clone atau mendownload API contoh:
- Buat clone repositori aplikasi contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
- Ubah ke direktori yang berisi kode contoh:
cd ruby-docs-samples/endpoints/getting-started
Untuk meng-clone atau mendownload API contoh:
- Buat clone repositori aplikasi contoh ke komputer lokal Anda:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
- Ubah ke direktori yang berisi kode contoh:
cd nodejs-docs-samples/endpoints/getting-started
Mendapatkan file konfigurasi Kubernetes
Clone repositori GitHub yang berisi file
yamlyang digunakan dalam tutorial ini ke komputer lokal Anda:git clone https://github.com/googlecloudplatform/endpoints-samples
Atau, download contoh sebagai file ZIP, lalu ekstrak.
Ubah ke direktori yang berisi file konfigurasi:
cd endpoints-samples/kubernetes
Mengonfigurasi Endpoint
Anda harus memiliki dokumen OpenAPI berdasarkan OpenAPI 2.0 atau OpenAPI 3.x yang menjelaskan platform aplikasi Anda dan persyaratan autentikasi apa pun. Untuk mempelajari lebih lanjut, lihat Versi OpenAPI yang didukung.
Anda juga perlu menambahkan kolom khusus Google yang berisi URL untuk setiap aplikasi agar ESPv2 memiliki informasi yang diperlukan untuk memanggil aplikasi. Jika Anda baru menggunakan OpenAPI, lihat Ringkasan OpenAPI untuk mengetahui informasi selengkapnya.
Untuk mengonfigurasi Endpoints:
-
Buat file teks baru bernama
openapi.yaml. Untuk memudahkan, halaman ini merujuk ke dokumen OpenAPI dengan nama tersebut, tetapi Anda dapat menamainya dengan nama lain jika mau. - Salin konten salah satu file berikut ke dalam file
openapi.yamlbaru Anda. Anda dapat memilih untuk menggunakan OpenAPI 2.0 atau OpenAPI 3.x.OpenAPI 2.0
swagger: "2.0" info: description: "A simple Google Cloud Endpoints API example." title: "Endpoints Example" version: "1.0.0" host: "echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog" consumes: - "application/json" produces: - "application/json" schemes: # Uncomment the next line if you configure SSL for this API. # - "https" - "http" paths: /echo: post: description: "Echo back a given message." operationId: "echo" produces: - "application/json" responses: 200: description: "Echo" schema: $ref: "#/definitions/echoMessage" parameters: - description: "Message to echo" in: body name: message required: true schema: $ref: "#/definitions/echoMessage" security: - api_key: [] /auth/info/googlejwt: get: description: "Returns the requests' authentication information." operationId: "auth_info_google_jwt" produces: - "application/json" responses: 200: description: "Authenication info." schema: $ref: "#/definitions/authInfoResponse" security: - api_key: [] google_jwt: [] /auth/info/googleidtoken: get: description: "Returns the requests' authentication information." operationId: "authInfoGoogleIdToken" produces: - "application/json" responses: 200: description: "Authentication info." schema: $ref: "#/definitions/authInfoResponse" security: - api_key: [] google_id_token: [] definitions: echoMessage: type: "object" properties: message: type: "string" authInfoResponse: properties: id: type: "string" email: type: "string" securityDefinitions: # This section configures basic authentication with an API key. api_key: type: "apiKey" name: "key" in: "query" # This section configures authentication using Google API Service Accounts # to sign a json web token. This is mostly used for server-to-server # communication. google_jwt: authorizationUrl: "" flow: "implicit" type: "oauth2" # This must match the 'iss' field in the JWT. x-google-issuer: "jwt-client.endpoints.sample.google.com" # Update this with your service account's email address. x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR_SERVICE_ACCOUNT_EMAIL" # This must match the "aud" field in the JWT. You can add multiple # audiences to accept JWTs from multiple clients. x-google-audiences: "echo.endpoints.sample.google.com" # This section configures authentication using Google OAuth2 ID Tokens. # ID Tokens can be obtained using OAuth2 clients, and can be used to access # your API on behalf of a particular user. google_id_token: authorizationUrl: "" flow: "implicit" type: "oauth2" x-google-issuer: "https://accounts.google.com" x-google-jwks_uri: "https://www.googleapis.com/oauth2/v3/certs" # Your OAuth2 client's Client ID must be added here. You can add # multiple client IDs to accept tokens from multiple clients. x-google-audiences: "YOUR_CLIENT_ID"
OpenAPI 3.x
openapi: 3.0.4 info: description: "A simple Google Cloud Endpoints API example." title: "Endpoints Example" version: "1.0.0" servers: - url: "http://echo-api.endpoints.YOUR_PROJECT_ID.cloud.goog" x-google-endpoint: {} paths: "/echo": post: description: "Echo back a given message." operationId: "echo" requestBody: description: "Message to echo" required: true content: "application/json": schema: $ref: "#/components/schemas/echoMessage" responses: '200': description: "Echo" content: "application/json": schema: $ref: "#/components/schemas/echoMessage" security: - api_key: [] "/auth/info/googlejwt": get: description: "Returns the requests' authentication information." operationId: "auth_info_google_jwt" responses: '200': description: "Authentication info." content: "application/json": schema: $ref: "#/components/schemas/authInfoResponse" security: - api_key: [] google_jwt: [] "/auth/info/googleidtoken": get: description: "Returns the requests' authentication information." operationId: "authInfoGoogleIdToken" responses: '200': description: "Authentication info." content: "application/json": schema: $ref: "#/components/schemas/authInfoResponse" security: - api_key: [] google_id_token: [] components: schemas: echoMessage: type: "object" properties: message: type: "string" authInfoResponse: properties: id: type: "string" email: type: "string" securitySchemes: api_key: type