Menyiapkan penghentian TLS di gateway masuk

Ringkasan

Halaman ini menunjukkan cara menyiapkan penghentian TLS di gateway masuk di Cloud Service Mesh untuk mengelola traffic HTTPS eksternal ke layanan Anda. Anda akan mempelajari cara mengonfigurasi gateway untuk komunikasi aman menggunakan TLS, yang memungkinkan akses terenkripsi ke aplikasi Anda. Proses ini memanfaatkan kemampuan Cloud Service Mesh untuk mengekspos layanan dengan aman.

Sebelum memulai

Untuk menyelesaikan langkah-langkah dalam dokumen ini, Anda memerlukan resource berikut:

  • Cluster Kubernetes dengan Cloud Service Mesh terinstal.

Menyiapkan lingkungan Anda

Jalankan perintah berikut dari workstation yang dapat mengakses cluster yang ingin Anda gunakan. Pastikan alat kubectl dikonfigurasi untuk menggunakan konteks cluster khusus untuk cluster Anda.

  1. Tetapkan variabel lingkungan.

    export ASM_INGRESSGATEWAY_NAMESPACE=asm-ingressgateway
    export ASM_INGRESSGATEWAY_DEPLOYMENT_NAME=asm-ingressgateway
    export ASM_INGRESSGATEWAY_SERVICE_NAME=asm-ingressgateway
    
  2. Aplikasi foo yang di-deploy di cluster Anda. Instal dengan:

    apiVersion: v1
    kind: Service
    metadata:
      name: foo
      namespace: foo
    spec:
      selector:
        app: test-backend
      ports:
      - port: 8080
        targetPort: 8080
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: foo
      namespace: foo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: test-backend
      template:
        metadata:
          labels:
            app: test-backend
        spec:
          containers:
          - name: whereami
            image: gcr.io/google-samples/whereami:v1.2.23
            ports:
            - containerPort: 8080
    EOF
    
  3. Membuat sertifikat dan kunci

Untuk mengamankan gateway masuk, Anda memerlukan sertifikat dan kunci TLS. Anda dapat menggunakan alat pembuatan sertifikat apa pun atau mengikuti langkah-langkah ini menggunakan openssl untuk membuat kredensial yang diperlukan.

  • Membuat sertifikat dan kunci CA root

    mkdir example_certs
    openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=Example Corp/CN=example.com' \
      -keyout example.com.key -out example.com.crt
    
  • Membuat sertifikat dan kunci untuk ingress

    openssl req -out foo.example.com.csr -newkey rsa:2048 -nodes \
      -keyout foo.example.com.key -subj "/CN=foo.example.com/O=Foo Org"
    
    openssl x509 -req -sha256 -days 365 -CA example.com.crt \
      -CAkey example.com.key -set_serial 0 \
      -in foo.example.com.csr -out foo.example.com.crt
    

Menyiapkan gateway traffic masuk TLS

Sebelum menyelesaikan petunjuk di bagian ini, Anda harus menentukan implementasi platform kontrol. Gunakan petunjuk di Mengidentifikasi penerapan bidang kontrol untuk melakukannya.

  1. Buat namespace. Namespace ini digunakan untuk men-deploy gateway entrance.

    kubectl create namespace ${ASM_INGRESSGATEWAY_NAMESPACE}
    
  2. Terapkan label injeksi default ke namespace:

    kubectl label namespace ${ASM_INGRESSGATEWAY_NAMESPACE} \
        istio.io/rev- istio-injection=enabled --overwrite
    
  3. Terapkan file manifes gateway masuk.

    kubectl --namespace ${ASM_INGRESSGATEWAY_NAMESPACE} apply --filename https://raw.githubusercontent.com/GoogleCloudPlatform/anthos-service-mesh-samples/main/docs/ingress-gateway-external-lb/ingress-gateway.yaml
    

    Output yang diharapkan:

    serviceaccount/asm-ingressgateway created
    role.rbac.authorization.k8s.io/asm-ingressgateway created
    rolebinding.rbac.authorization.k8s.io/asm-ingressgateway created
    deployment.apps/asm-ingressgateway created
    service/asm-ingressgateway created
    poddisruptionbudget.policy/asm-ingressgateway created
    horizontalpodautoscaler.autoscaling/asm-ingressgateway created
    
  4. Simpan kredensial TLS di secret Kubernetes:

    kubectl create -n ${ASM_INGRESSGATEWAY_NAMESPACE} secret tls foo-credential \
      --key=example_certs/foo.example.com.key \
      --cert=example_certs/foo.example.com.crt
    
  5. Tentukan gateway masuk: Buat resource Gateway untuk menangani traffic HTTPS di port 443:

    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: Gateway
    metadata:
      name: secure-gateway
      namespace: ${ASM_INGRESSGATEWAY_NAMESPACE}
    spec:
      selector:
        app: asm-ingressgateway
        istio: ingressgateway
      servers:
      - port:
          number: 443
          name: https
          protocol: HTTPS
        tls:
          mode: SIMPLE
          credentialName: foo-credential
        hosts:
        - "foo.example.com"
    EOF
    
  6. Rutekan traffic ke layanan foo: Tentukan VirtualService untuk mengarahkan traffic ke deployment foo:

    cat <<EOF | kubectl apply -f -
    apiVersion: networking.istio.io/v1
    kind: VirtualService
    metadata:
      name: foo-routing
      namespace: ${ASM_INGRESSGATEWAY_NAMESPACE}
    spec:
      hosts:
      - "foo.example.com"
      gateways:
      - secure-gateway
      http:
      - match:
        - uri:
            prefix: /status
        - uri:
            prefix: /delay
        route:
        - destination:
            host: foo
            port:
              number: 8080
    EOF
    
  7. Menyiapkan load balancer eksternal untuk terhubung dengan gateway traffic masuk dari cluster

  8. Uji koneksi aman: Gunakan curl untuk memverifikasi penyiapan:

    export EXTERNAL_LB_IP_ADDRESS=EXTERNAL_LB_IP_ADDRESS
    curl -v -H "Host: foo.example.com" --resolve "foo.example.com:443:$EXTERNAL_LB_IP_ADDRESS" \
      --cacert example_certs/example.com.crt "https://foo.example.com:443/ping"
    

Ganti EXTERNAL_LB_IP_ADDRESS dengan ip load balancer eksternal.

Outputnya mirip dengan hal berikut ini:

    {
      "cluster_name": "gke-us",
      "host_header": "34.120.175.141",
      "pod_name": "whereami-deployment-954cbf78-mtlpf",
      "pod_name_emoji": "😎",
      "project_id": "my-project",
      "timestamp": "2021-11-29T17:01:59",
      "zone": "us-central1-b"
    }

Langkah berikutnya