Mengonfigurasi Direktori Layanan untuk GKE

Halaman ini memberikan informasi tentang cara mengonfigurasi Service Directory dengan Google Kubernetes Engine (GKE) dan mendaftarkan cluster GKE Anda secara otomatis ke Service Directory. Anda dapat mengonfigurasi Service Directory untuk GKE dengan cluster GKE Autopilot atau cluster GKE Standard. Untuk mengetahui informasi tentang kedua mode tersebut, lihat Memilih mode operasi GKE.

Untuk mempelajari lebih lanjut Direktori Layanan untuk GKE, lihat Ringkasan Direktori Layanan untuk GKE.

Batasan

Direktori Layanan untuk GKE memiliki batasan berikut:

  • Anda hanya dapat mendaftarkan layanan menggunakan integrasi Direktori Layanan-GKE. Anda harus mendaftarkan ingress, gateway, dan resource lainnya secara manual menggunakan Service Directory API.
  • Anda harus mendaftarkan cluster GKE menggunakan Fleet sebelum dapat menyinkronkan layanan di cluster tersebut ke Service Directory. Untuk mengetahui detail tentang cara mendaftarkan cluster GKE Anda, lihat Mendaftarkan cluster.
  • Semua resource Service Directory terdaftar di project fleet, bukan project cluster.
  • Anda tidak dapat mengubah entitas Direktori Layanan yang terdaftar menggunakan integrasi ini. Anda harus membuat semua perubahan pada resource layanan GKE itu sendiri agar diterapkan ke Service Directory.

Sebelum memulai

  • 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  • 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  • Anda harus mendaftarkan cluster GKE ke fleet. Untuk mendaftarkan cluster GKE, ikuti petunjuk di Mendaftarkan cluster.

Menyiapkan cluster GKE

Untuk menyiapkan cluster GKE, ikuti langkah-langkah berikut.

  • Aktifkan fitur fleet GKE Service Directory.
  • Konfigurasi kebijakan pendaftaran Direktori Layanan.
  • Deploy layanan GKE.

Mengaktifkan fitur fleet GKE Service Directory

Untuk mengaktifkan fitur fleet GKE Service Directory, jalankan perintah gcloud container hub.

gcloud

gcloud alpha container hub service-directory enable

Mengonfigurasi kebijakan pendaftaran Direktori Layanan untuk layanan

Kebijakan pendaftaran menentukan jenis layanan dalam GKE yang diimpor secara otomatis oleh Service Directory. Daftar berikut menunjukkan opsi yang dapat dikonfigurasi untuk kebijakan ini:

  • resources: jenis resource GKE yang diimpor. Anda dapat menentukan beberapa jenis resource. Resource diidentifikasi berdasarkan Group dan Kind. Secara default, Group adalah core. Hanya Kind: service yang tersedia.
  • selector: pemilih label GKE yang cocok dengan label key:value yang diterapkan pada resource GKE. Secara default, tidak ada resource yang dipilih.
  • annotationsToSync: kunci anotasi pada resource yang disinkronkan ke Service Directory. Jika daftar ini kosong, resource akan disinkronkan ke Service Directory tanpa anotasi.

Setiap namespace harus menentukan satu kebijakan. Kebijakan ini harus memiliki name: "default". Pengontrol tidak mengizinkan atau mencocokkan nama lain.

Konfigurasi ServiceDirectoryRegistrationPolicy.

Manifes berikut menjelaskan ServiceDirectoryRegistrationPolicy:

apiVersion: networking.gke.io/v1alpha1
kind: ServiceDirectoryRegistrationPolicy
metadata:
    # Only the name "default" is allowed.
    name: default
    # The ServiceDirectoryRegistrationPolicy is a namespaced resource
    namespace: NAMESPACE
spec:
  resources:
    # Kind specifies the types of Kubernetes resources that can be synced into Service Directory.
    - kind: Service
      # Selector is a label selector for the resource types specified in Kind.
      selector:
        matchLabels:
          LABELS
      # annotationsToSync specifies the annotations that are matched and imported.
      # Any annotations that do not match this set of keys will not be imported into Service Directory.
      annotationsToSync:
      - ANNOTATIONS
      

Ganti kode berikut:

  • NAMESPACE: nama namespace. Hanya boleh ada satu ServiceDirectoryRegistrationPolicy dalam namespace. Jika ada beberapa kebijakan dalam satu namespace, GKE akan memilih satu kebijakan untuk menjadi kebijakan aktif dan menandai kebijakan lainnya sebagai tidak aktif.
  • LABELS: pemilih label, seperti sd-import: "true". Nilai ini berlaku untuk resource yang ditentukan dalam jenis. Secara default, tidak ada resource yang dipilih.
  • ANNOTATIONS: daftar anotasi yang dicocokkan dan diimpor, seperti, - cloud.google.com/load-balancer-type Jika resource GKE yang dipilih memiliki salah satu anotasi dalam daftar, anotasi dan nilainya akan disinkronkan ke Service Directory dengan resource. Jika daftar ini kosong, resource akan disinkronkan ke Service Directory tanpa anotasi.

Simpan manifes sebagai registration_policy.yaml.

Terapkan manifes ke cluster Anda

Untuk menerapkan manifes ke cluster Anda, jalankan perintah kubectl apply -f:

kubectl apply -f registration_policy.yaml

Men-deploy layanan GKE

Deploy layanan dan aplikasi Load Balancer Jaringan passthrough eksternal GKE di cluster GKE region Anda, misalnya, jika region Anda adalah us-central1, deploy di cluster GKE gke-us-central1.

Perintah ini mengimpor layanan GKE Anda ke Direktori Layanan.

Buat file YAML dan terapkan seperti resource GKE lainnya. Berikut adalah manifes untuk layanan jenis LoadBalancer:

apiVersion: v1
kind: Service
metadata:
  name: hello-app-service
  labels:
    sd-import: "true"
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
spec:
  type: LoadBalancer
  selector:
    app: hello
  ports:
  - name: tcp-port
    protocol: TCP
    port: 80
    targetPort: 8080

Berikut adalah manifes untuk Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello
  name: hello-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - image: gcr.io/google-samples/hello-app:1.0
        name: hello
        ports:
        - containerPort: 8080
          protocol: TCP

Untuk memverifikasi, jalankan kueri Service Directory untuk layanan Anda menggunakan perintah gcloud service-directory services resolve:

gcloud

gcloud beta service-directory services resolve SERVICE_NAME \
  --location=LOCATION \
  --namespace=NAMESPACE

Ganti kode berikut:

  • SERVICE_NAME: nama atau ID yang sepenuhnya memenuhi syarat untuk layanan, misalnya hello-app-service
  • LOCATION: nama region untuk layanan, misalnya, us-central1
  • NAMESPACE: nama namespace untuk layanan, misalnya, default

Output Anda akan menampilkan layanan Anda dan mirip dengan berikut ini:

service:
  endpoints:
  - address: 10.140.0.5
    metadata:
      cloud.google.com/load-balancer-type: "Internal"
    name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app-service/endpoints/my-cluster-12345
    port: 80
    network: projects/123456789/locations/global/networks/default
  name: projects/my-project/locations/us-central1/namespaces/default/services/hello-app-service

Penagihan

Service Directory tidak mengenakan biaya untuk resource yang dibuat oleh integrasi GKE. Namun, ada biaya untuk panggilan API. Untuk mengetahui detail harga, lihat Harga Service Directory.

Langkah berikutnya