Pengalihan URL untuk Azure Application Gateway untuk Kontainer - GATEWAY API
Application Gateway untuk Kontainer memungkinkan Anda mengembalikan respons pengalihan ke tiga aspek URL berbasis klien: protokol, nama host, dan jalur. Untuk setiap pengalihan, kode status HTTP yang ditentukan dapat dikembalikan ke klien untuk menentukan sifat pengalihan.
Detail penggunaan
Pengalihan URL memanfaatkan filter aturan RequestRedirect seperti yang didefinisikan oleh API Gateway Kubernetes.
Pengalihan
Pengalihan mengatur kode status respons yang dikembalikan ke klien untuk memahami tujuan pengalihan. Jenis pengalihan berikut ini didukung:
- 301 (Dipindahkan secara permanen): Menunjukkan bahwa sumber daya target diberi URI permanen baru. Referensi di masa mendatang ke sumber daya ini menggunakan salah satu URI yang diapit. Gunakan kode status 301 untuk pengalihan HTTP ke HTTPS.
- 302 (Ditemukan): Menunjukkan bahwa sumber daya target sementara berada di bawah URI yang berbeda. Karena pengalihan dapat berubah sesekali, klien harus terus menggunakan URI permintaan yang efektif untuk permintaan di masa mendatang.
Kemampuan pengalihan
Pengalihan protokol umumnya digunakan untuk memberi tahu klien untuk berpindah dari skema lalu lintas yang tidak terenkripsi ke lalu lintas, seperti pengalihan HTTP ke HTTPS.
Pengalihan nama host cocok dengan nama domain yang sepenuhnya memenuhi syarat (fqdn) permintaan. Ini biasanya diamati dalam mengalihkan nama domain lama ke nama domain baru; seperti
contoso.com
kefabrikam.com
.Pengalihan jalur memiliki dua varian yang berbeda:
prefix
danfull
.Prefix
jenis pengalihan akan mengalihkan semua permintaan yang dimulai dengan nilai yang ditentukan. Misalnya: awalan /shop akan cocok dengan /shop dan teks apa pun setelahnya. Misalnya, /shop, /shop/checkout, dan /shop/item-a juga akan dialihkan ke /shop.Full
jenis pengalihan cocok dengan nilai yang tepat. Misalnya: /shop dapat mengalihkan ke /store, tetapi /shop/checkout tidak akan dialihkan ke /store.
Gambar berikut mengilustrasikan contoh permintaan yang ditujukan untuk contoso.com/summer-promotion dialihkan ke contoso.com/shop/category/5. Selain itu, permintaan kedua yang dimulai untuk contoso.com melalui protokol http dikembalikan pengalihan untuk memulai koneksi baru ke varian https-nya.
Prasyarat
Jika mengikuti strategi penyebaran BYO, pastikan Anda menyiapkan Application Gateway untuk sumber daya Kontainer dan Pengontrol ALB.
Jika mengikuti strategi penyebaran terkelola ALB, pastikan Anda memprovisikan Pengontrol ALB Anda dan menyediakan Application Gateway untuk sumber daya Kontainer melalui sumber daya kustom ApplicationLoadBalancer.
Sebarkan contoh aplikasi HTTP:
Terapkan file deployment.yaml berikut pada kluster Anda untuk menyebarkan sampel sertifikat TLS untuk menunjukkan kemampuan pengalihan.
kubectl apply -f kubectl apply -f https://trafficcontrollerdocs.blob.core.windows.net/examples/https-scenario/ssl-termination/deployment.yaml
Perintah ini membuat yang berikut ini pada kluster Anda:
- namespace yang dipanggil
test-infra
- satu layanan yang dipanggil
echo
ditest-infra
namespace layanan - satu penyebaran yang dipanggil
echo
ditest-infra
namespace - satu rahasia yang dipanggil
listener-tls-secret
ditest-infra
namespace
- namespace yang dipanggil
Menyebarkan sumber daya API Gateway yang diperlukan
Membuat Gateway
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: gateway-01 namespace: test-infra annotations: alb.networking.azure.io/alb-namespace: alb-test-infra alb.networking.azure.io/alb-name: alb-test spec: gatewayClassName: azure-alb-external listeners: - name: http-listener port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same - name: https-listener port: 443 protocol: HTTPS allowedRoutes: namespaces: from: Same tls: mode: Terminate certificateRefs: - kind : Secret group: "" name: listener-tls-secret EOF
Catatan
Saat Pengontrol ALB membuat Application Gateway untuk sumber daya Kontainer di ARM, pengontrol tersebut akan menggunakan konvensi penamaan berikut untuk sumber daya frontend: fe-8< karakter yang dihasilkan secara acak>
Jika Anda ingin mengubah nama frontend yang dibuat di Azure, pertimbangkan untuk mengikuti strategi bawa penyebaran Anda sendiri.
Setelah sumber daya gateway dibuat, pastikan statusnya valid, pendengar diprogram, dan alamat ditetapkan ke gateway.
kubectl get gateway gateway-01 -n test-infra -o yaml
Contoh output pembuatan gateway yang berhasil.
status:
addresses:
- type: Hostname
value: xxxx.yyyy.alb.azure.com
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Valid Gateway
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
listeners:
- attachedRoutes: 0
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Listener is accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
name: https-listener
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
Buat sumber daya HTTPRoute untuk contoso.com
yang menangani lalu lintas yang diterima melalui https.
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: https-contoso
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
sectionName: https-listener
hostnames:
- "contoso.com"
rules:
- backendRefs:
- name: echo
port: 80
EOF
Saat sumber daya HTTPRoute dibuat, pastikan sumber daya HTTPRoute menunjukkan Diterima dan Application Gateway untuk sumber daya Kontainer diprogram.
kubectl get httproute rewrite-example -n test-infra -o yaml
Verifikasi bahwa sumber daya Application Gateway for Containers berhasil diperbarui untuk setiap HTTPRoute.
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
Setelah gateway dibuat, buat sumber daya HTTPRoute untuk contoso.com
dengan filter RequestRedirect yang mengalihkan lalu lintas http ke https.
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: http-to-https-contoso-redirect
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
sectionName: http-listener
hostnames:
- "contoso.com"
rules:
- matches:
filters:
- type: RequestRedirect
requestRedirect:
scheme: https
statusCode: 301
EOF
Saat sumber daya HTTPRoute dibuat, pastikan sumber daya HTTPRoute menunjukkan Diterima dan Application Gateway untuk sumber daya Kontainer diprogram.
kubectl get httproute rewrite-example -n test-infra -o yaml
Verifikasi bahwa sumber daya Application Gateway for Containers berhasil diperbarui untuk setiap HTTPRoute.
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
Buat sumber daya HTTPRoute untuk contoso.com
yang menangani pengalihan untuk jalur /summer-promotion ke URL tertentu. Dengan menghilangkan sectionName, yang ditunjukkan dalam sumber daya HTTP ke https HTTPRoute, aturan pengalihan ini berlaku untuk permintaan HTTP dan HTTPS.
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: summer-promotion-redirect
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
sectionName: https-listener
hostnames:
- "contoso.com"
rules:
- matches:
- path:
type: PathPrefix
value: /summer-promotion
filters:
- type: RequestRedirect
requestRedirect:
path:
type: ReplaceFullPath
replaceFullPath: /shop/category/5
statusCode: 302
- backendRefs:
- name: echo
port: 80
EOF
Saat sumber daya HTTPRoute dibuat, pastikan sumber daya HTTPRoute menunjukkan Diterima dan Application Gateway untuk sumber daya Kontainer diprogram.
kubectl get httproute rewrite-example -n test-infra -o yaml
Verifikasi bahwa sumber daya Application Gateway for Containers berhasil diperbarui untuk setiap HTTPRoute.
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
Menguji akses ke aplikasi
Sekarang kami siap untuk mengirim beberapa lalu lintas ke aplikasi sampel kami, melalui FQDN yang ditetapkan ke frontend. Gunakan perintah berikut untuk mendapatkan FQDN.
fqdn=$(kubectl get gateway gateway-01 -n test-infra -o jsonpath='{.status.addresses[0].value}')
Saat Anda menentukan indikator nama server menggunakan perintah curl, http://contoso.com
harus mengembalikan respons dari Application Gateway untuk Kontainer dengan header yang location
menentukan pengalihan 301 ke https://contoso.com
.
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com/ -v
Melalui respons, kita akan melihat:
* Added contoso.com:80:xxx.xxx.xxx.xxx to DNS cache
* Hostname contoso.com was found in DNS cache
* Trying xxx.xxx.xxx.xxx:80...
* Connected to contoso.com (xxx.xxx.xxx.xxx) port 80 (#0)
> GET / HTTP/1.1
> Host: contoso.com
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< location: https://contoso.com/
< date: Mon, 26 Feb 2024 22:56:23 GMT
< server: Microsoft-Azure-Application-LB/AGC
< content-length: 0
<
* Connection #0 to host contoso.com left intact
Saat Anda menentukan indikator nama server menggunakan perintah curl, https://contoso.com/summer-promotion
Application Gateway untuk Kontainer harus mengembalikan pengalihan 302 ke https://contoso.com/shop/category/5
.
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:443:$fqdnIp https://contoso.com/summer-promotion -v
Melalui respons, kita akan melihat:
> GET /summer-promotion HTTP/2
> Host: contoso.com
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 302
< location: https://contoso.com/shop/category/5
< date: Mon, 26 Feb 2024 22:58:43 GMT
< server: Microsoft-Azure-Application-LB/AGC
<
* Connection #0 to host contoso.com left intact
Selamat, Anda telah menginstal Pengontrol ALB, menyebarkan aplikasi backend, dan menggunakan API Gateway untuk mengonfigurasi pengalihan HTTP ke HTTPS dan pengalihan berbasis jalur ke permintaan klien tertentu.
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk