Ngày 02 tháng 01 năm 2021 - Máy tính
Trong bài viết trước “Quản lý lưu lượng Istio với Ingress Gateway”, chúng ta đã tìm hiểu cách sử dụng Gateway để phơi bày một dịch vụ HTTP tầng 7 cho người dùng bên ngoài. Bài viết này sẽ hướng dẫn cách cấu hình TLS đơn hướng hoặc đôi hướng cho Gateway để cung cấp một dịch vụ HTTPS an toàn cho truy cập từ bên ngoài. Về việc cài đặt Istio và chuẩn bị môi trường, vui lòng tham khảo “Cài đặt và sử dụng Istio”.
1 Triển khai httpbin
Sử dụng tệp cấu hình có sẵn trong thư mục cài đặt Istio để triển khai ứng dụng httpbin vào không gian tên istio-demo
.
$ cd /usr/local/istio-1.8.1
$ kubectl apply -n istio-demo -f samples/httpbin/httpbin.yaml [tải tool hack tài xỉu 789 club miễn phí](/post/3718/)
2 Tạo chứng chỉ và khóa riêng
Sử dụng openssl để tạo chứng chỉ gốc và khóa riêng dùng để phát hành chứng chỉ cho dịch vụ, sau khi thực hiện lệnh dưới đây sẽ tạo ra hai tệp (example.com.crt
, example.com.key
).
$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt
Tiếp tục tạo chứng chỉ và khóa riêng cho httpbin.example.com
, sau khi chạy lệnh dưới đây sẽ tạo ra ba tệp (httpbin.example.com.csr
, httpbin.example.com.key
, httpbin.example.com.crt
).
$ openssl req -out httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization"
$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin.example.com.csr -out httpbin.example.com.crt
3 Cấu hình Gateway TLS Ingress
Tạo secret cho Ingress Gateway sử dụng khóa riêng và chứng chỉ đã tạo ở bước 2.
$ kubectl create -n istio-system secret tls httpbin-credential --key=httpbin.example.com.key --cert=httpbin.example.com.crt
Áp dụng cấu hình Gateway, cổng là 443, hosts là httpbin.example.com
, bật chế độ TLS SIMPLE và thiết lập credentialName thành tên của secret vừa tạo.
$ kubectl apply -n istio-demo -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: mygateway
spec:
selector:
istio: ingressgateway # sử dụng gateway mặc định của Istio
servers:
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: httpbin-credential # phải trùng với tên secret
hosts:
- httpbin.example.com
EOF
Sử dụng Virtual Service để cấu hình quy tắc định tuyến Gateway cho httpbin.
$ kubectl apply -n istio-demo -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- "httpbin.example.com"
gateways:
- mygateway
http:
- match:
- uri:
prefix: /status
- uri:
prefix: /delay
route:
- destination:
port:
number: 8000
host: httpbin
EOF
Sử dụng curl để gửi yêu cầu https đến httpbin (bài viết sử dụng môi trường Kubernetes Docker Desktop, INGRESS_HOST là 127.0.0.1, SECURE_INGRESS_PORT là 443), phản hồi thành công là “418 I’m a Teapot”.
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
--cacert example.com.crt "
...
-=[ teapot ]=-
_...._
.' _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
| ;/
\_ _/
`"""`
4 Cấu hình Gateway TLS cho nhiều Host
Cấu hình Gateway ở trên chỉ hỗ trợ TLS cho một nhóm Host duy nhất. Dưới đây, chúng ta sẽ triển khai thêm dịch vụ helloworld-v1
, sau đó cấu hình Ingress Gateway để hỗ trợ đồng thời TLS cho cả httpbin.example.com
và helloworld-v1.example.com
.
Triển khai mẫu helloworld-v1
.
$ kubectl apply -n istio-demo -f - <<EOF
...
metadata:
name: helloworld-v1
spec:
replicas: 1
selector:
matchLabels:
app: helloworld-v1
version: v1
template:
metadata:
labels:
app: helloworld-v1
version: v1
spec:
containers:
- name: helloworld
image: istio/examples-helloworld-v1
resources:
requests:
cpu: "100m"
imagePullPolicy: IfNotPresent #Always
ports:
- containerPort: 5000
EOF
Tạo chứng chỉ và khóa riêng cho helloworld-v1.example.com
.
$ openssl req -out helloworld-v1.example.com.csr -newkey rsa:2048 -nodes -keyout helloworld-v1.example.com.key -subj "/CN=helloworld-v1.example.com/O=helloworld organization"
$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 1 -in helloworld-v1.example.com.csr -out helloworld-v1.example.com.crt
Tạo secret helloworld-credential
cho Ingress Gateway.
$ kubectl create -n istio-system secret tls helloworld-credential --key=helloworld-v1.example.com.key --cert=helloworld-v1.example.com.crt
Chỉnh sửa cấu hình Gateway để thêm hỗ trợ TLS cho helloworld-v1.example.com
.
$ kubectl apply -n istio-demo -f - <<EOF
...
name: mygateway
spec:
selector:
istio: ingressgateway # sử dụng gateway mặc định của Istio
servers:
- port:
number: 443
name: https-httpbin
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: httpbin-credential
hosts:
- httpbin.example.com
- port:
number: 443
name: https-helloworld
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: helloworld-credential
hosts:
- helloworld-v1.example.com
EOF
Sử dụng Virtual Service để cấu hình quy tắc định tuyến Gateway.
$ kubectl apply -n istio-demo -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: helloworld-v1
spec:
hosts:
- helloworld-v1.example.com
gateways:
- mygateway
http:
- match:
- uri:
exact: /hello
route:
- destination:
host: helloworld-v1
port:
number: 5000
EOF
Sử dụng curl để gửi yêu cầu https đến helloworld-v1
, phản hồi thành công với mã trạng thái 200.
$ curl -v -HHost:helloworld-v1.example.com --resolve "helloworld-v1.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
--cacert example.com.crt "
Sử dụng lệnh tương tự để gửi m88vin - cổng game quốc tế yêu cầu https đến httpbin
, cũng phản hồi kết quả thành công. Điều này chứng tỏ Gateway hỗ trợ đồng thời TLS cho hai nhóm Host.
5 Cấu hình Gateway TLS hai chiều
Để Gateway hỗ trợ giao tiếp TLS hai chiều, cần xóa secret cũ, tạo secret mới và bao gồm chứng chỉ gốc dùng để xác thực client.
$ kubectl -n istio-system delete secret httpbin-credential
$ kubectl create -n istio-system secret generic httpbin-credential --from-file=tls.key=httpbin.example.com.key --from-file=tls.crt=httpbin.example.com.crt --from-file=ca.crt=example.com.crt
Cập nhật cấu hình Gateway để bật chế độ TLS hai chiều cho httpbin
.
$ kubectl apply -n istio-demo -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: mygateway
spec:
selector:
istio: ingressgateway # sử dụng gateway mặc định của Istio
servers:
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: MUTUAL
credentialName: httpbin-credential # phải trùng với tên secret
hosts:
- httpbin.example.com
EOF
Sau khi cấu hình có hiệu lực, cách yêu cầu httpbin trước đây sẽ không còn hoạt động. Sử dụng lệnh dưới đây để tạo chứng chỉ và khóa riêng cho client.
$ openssl req -out client.example.com.csr -newkey rsa:2048 -nodes -keyout client.example.com.key -subj "/CN=client.example.com/O=client organization"
$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 1 -in client.example.com.csr -out client.example.com.crt
Sử dụng tùy chọn --cert
và --key
để truyền chứng chỉ và khóa riêng của client, sau đó gửi yêu cầu https đến httpbin
, phản hồi thành công.
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
--cacert example.com.crt --cert client.example.com.crt --key client.example.com.key \
"
...
-=[ teapot ]=-
_...._
.' _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
| ;/
\_ _/
`"""`
6 Dọn dẹp môi trường
Kết thúc thử nghiệm, sử dụng các lệnh dưới đây để xóa Gateway, Virtual Service và Secret.
$ kubectl delete gateway mygateway -n istio-demo
$ kubectl delete virtualservice httpbin helloworld-v1 -n istio-demo
$ kubectl delete --ignore-not-found=true -n istio-system secret httpbin-credential helloworld-credential
Sử dụng lệnh dưới đây để gỡ bỏ dịch vụ httpbin và helloworld-v1.
$ kubectl delete deploy --ignore-not-found=true httpbin helloworld-v1 -n istio-demo
$ kubectl delete svc --ignore-not-found=true httpbin helloworld-v1 -n istio-demo
Tóm tắt bài viết, chúng ta đã giới thiệu về việc Istio Ingress Gateway hỗ trợ truy cập TLS đơn giản và hai chiều; sử dụng ví dụ httpbin để kiểm tra truy cập TLS đơn giản; giới thiệu thêm ví dụ helloworld-v1 để kiểm tra truy cập TLS cho nhiều Host; cuối cùng sử dụng ví dụ httpbin để kiểm tra truy cập TLS hai chiều.
[1] Rio88 Game Bài Twin Gateway An toàn Istio
[2] Quản lý Lưu lượng Istio
