Nếu bạn chưa cài istio thì sem bài sau nhé:
Từ đây là bạn đã cài istio rồi nhé.
Để run 1 app hoàn chỉnh thì mình tham khảo yaml sau:
Các bạn có thể lên udemy tìm khoá học về istio của anh này.
Dưới này là file của mình:
https://raw.githubusercontent.com/mrnim94/recheck-instio/main/deployment/deployment-test.yaml
Khi deploy sẽ ntn

GIờ test kết nối tự pod có istio-proxy sem như thế nào thì mình mình có 1 hình minh hoạ chút

giờ bạn apply app busybox có inject sidecar rồi thử ping, curl ta lả ra ngoài he (vd: ping service khác, ping 8.8.8.8).
https://raw.githubusercontent.com/mrnim94/recheck-instio/main/busybox/busybox-deployment.yaml
1. Giờ chúng ta test vài tính năng của ingress gateway nhé.
1.1 Test với service
---
# Source: istio/templates/gateway.yaml
kind: Gateway
apiVersion: networking.istio.io/v1alpha3
metadata:
name: ingress-gateway-configuration
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- "fleetman-webapp.tiki.nimtechnology.com"
port:
name: http
number: 80
protocol: HTTP
---
# Source: istio/templates/virtualservice.yaml
kind: VirtualService
apiVersion: networking.istio.io/v1alpha3
metadata:
name: fleetman-webapp
namespace: default
spec:
gateways:
- "ingress-gateway-configuration"
hosts:
- "fleetman-webapp.tiki.nimtechnology.com"
http:
- name: fleetman-webapp.tiki.nimtechnology.com
match:
- uri:
prefix: /
route:
- destination:
host: fleetman-webapp
port:
number: 80



Có bạn sẽ thắc mắc sao biết cài selector là istio:ingressgateway
chúng ta trở lại phần cài đặt istio thì bạn sẽ thấy có 1 deployment tên là istio-ingressgateway và được đánh label là istio: ingressgateway

Điều ngày nghĩa là ingress A mà bạn tạo cho các app A thì nó sẽ thuộc về isito-ingressgateway A và có trường hợp multi istio sẽ khá rõ về điều này.
giờ chúng ta kiểm tra lại ingress istio đang là ip bao nhiêu thì bạn vào dns hoặc file cấu hình cho domain đó thì phân giải ra ip đó


1.2 Test với service Headless các bạn lấy file manifest theo đường dẫn Git nhé
https://github.com/mrnim94/recheck-instio/tree/main/deployment-service-headless
1.3 Giờ thì ngịch canary chút



1.3.1 canary by weight
Giờ mình cung cấp cho bạn 1 file vs và dr cho service staff-service


Giờ bạn apply thôi

curl phát sem nào:
while true; do curl http://fleetman-webapp.tiki.nimtechnology.com/api/vehicles/driver/City%20Truck; echo; sleep 0.5; done

Tiến hành cài đặt các addons của istio như: kiali, grafana, jaeger, …..
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.11.0 TARGET_ARCH=x86_64 sh -
cd istio-1.11.0/samples/addons
kubectl -n istio-system apply -f grafana.yaml
kubectl -n istio-system apply -f jaeger.yaml
kubectl -n istio-system apply -f kiali.yaml
kubectl -n istio-system apply -f prometheus.yaml

1.3.2 canary by header
Giờ có 1 cái khá hay trong istio “Dark Release” nghĩa là trên 1 tính math -> headers
Ví dụ môi trường Prod app version v1 đang chạy rồi bạn deploy app đó version 2 –> vậy làm sao để user bình thường vẫn vào v1 còn tester của bạn vào v2
thưc hiện applay yaml sau

Saù khi apply thì test kiểu j cho dễ

Bình thường nếu ko thêm header thì nó sẽ ntn

Giờ bật header

Như này là đang chạy nhé

giở reload lại web

1.4 Test với Service GRPC (update thu 30 Sep 2021)
https://istio.io/latest/docs/ops/configuration/traffic-management/protocol-selection/
Với các service GRPC như sau:
kind: Service
metadata:
name: myservice
spec:
ports:
- name: grpc-0-80
port: 80
protocol: TCP
targetPort: 80
- name: http-1-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
workload.user.cattle.io/workloadselector: nim-grpc-http
Istio có thể automatically detect có 2 cách
This can be configured in two ways:
- By the name of the port:
name: <protocol>[-<suffix>]
. - In Kubernetes 1.18+, by the
appProtocol
field:appProtocol: <protocol>
.
The following protocols are supported:
http
http2
https
tcp
tls
grpc
grpc-web
mongo
mysql
*redis
*udp
(UDP will not be proxied, but the port can be explicitly declared as UDP)
Ở kube từ 1.18+ bạn có thể sài appProtocol ở service và ko cần khai báo prefix
kind: Service
metadata:
name: myservice
spec:
ports:
- number: 3306
name: database
appProtocol: mysql
- number: 80
name: http-web
command check grpc
grpc_health_probe -addr localhost:80
2 Authentication
2.1 Giờ Test mTLS and Authentication
Bạn hiểu đơn gian là khi bạn triển khai proxy sidecar istio thì kết nối giữa các proxy là https/SSL và đây là thiết lập auto của istio -> app của bạn ko cần thay đổi gì nếu có triển khai isito nhe

Vậy nếu app ko có sidecar proxy istio gọi vào 1 app có sidecar proxy istio thì sao?


2.2 Giờ bạn muốn Force request là required HTTPS/mTLS:
Mình đang force trên ns default nhé!
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: default
namespace: default
spec:
mtls:
mode: STRICT
xong bạn sửa service của fleetman-webapp thanh Nodeport để kiểm chứng mode Permissive:

apply PeerAuthentication



2.3 Enable mutual TLS per workload
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: "web-app"
namespace: "default"
spec:
selector:
matchLabels:
app: webapp
mtls:
mode: STRICT

Bạn cần sửa destiantion rule nữa vì đây là mình force strict mTLS cho 1 deployment

# Source: istio/templates/virtualservice.yaml
kind: DestinationRule
apiVersion: networking.istio.io/v1alpha3
metadata:
name: fleetman-webapp
namespace: default
spec:
host: fleetman-webapp
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- labels:
version: original
name: original
- labels:
version: experimental
name: experimental

2.4 Giờ bạn muốn strict mTLS lên tất cả các port và ngoài trừ 1 vài port
all service(not required inject sidecar proxy istio) —–still connected—-> port 80 of service A
all service(required inject sidecar proxy istio) –connected–> all port left (các port còn lại) of service A
(PeerAuthentication) argocd đang bị lỗi
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: "web-app"
namespace: "default"
spec:
selector:
matchLabels:
app: webapp
mtls:
mode: STRICT
portLevelMtls:
80:
mode: DISABLE
## đang lỗi
## admission webhook “validation.istio.io” denied the request: configuration is invalid: mesh/namespace peer authentication cannot have port level mTLS
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: "httpbin"
spec:
host: httpbin.bar.svc.cluster.local
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
portLevelSettings:
- port:
number: 8000
tls:
mode: DISABLE
2.5 Thêm phần nữa là Policy precedence – Ưu tiên
Đầu tiên bạn tạo 1 Strict mTLS cho cả namespace.
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: default
namespace: default
spec:
mtls:
mode: STRICT
xong bạn cho 1 deployment được disable Strict mTLS
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: web-app
namespace: default
spec:
mtls:
mode: DISABLE
selector:
matchLabels:
app: webapp
###thêm disable cho dr nhé
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: fleetman-webapp
namespace: default
spec:
host: fleetman-webapp
subsets:
- labels:
version: original
name: original
- labels:
version: experimental
name: experimental
trafficPolicy:
tls:
mode: DISABLE

các bạn có thể test thử với các mode khác:

2.5 End-user authentication –> researching
3. Authorization Policy
3.1 Đầu tiên:
- service A —-deny—> service B
- all service —->allow—> service B
- gt -> vs -> dr -> service B
Để làm được điều này thì:
bật strict mode trên namespace và workload mà bạn muốn bật author, Ở ví dụ này thì là service B và strict trên ns nhé!
# cấu hình authen
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
labels:
name: default
namespace: default
spec:
mtls:
mode: STRICT
cấu hình mở tất cả, chặn 1 vài thằng và chặn get
#cấu hình author
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: default
namespace: default
spec:
action: DENY
rules:
- from:
- source:
namespaces:
- author-istio
to:
- operation:
methods:
- GET
Trước đó mình có create 1 ns và workload author-istio, tiến hành curl liên tục và kiểm tra.
while true; do curl http://fleetman-webapp.default/api/vehicles/driver/City%20Truck; echo; sleep 0.5; done

root@work-space-u20:~# kubectl -n author-outline-istio get pod
NAME READY STATUS RESTARTS AGE
busybox-deployment-6bc9c96d4b-zknzq 2/2 Running 0 2m6s
kubectl -n author-outline-istio exec -it busybox-deployment-6bc9c96d4b-zknzq -c busybox -- sh
# curl http://fleetman-webapp.default/api/vehicles/driver/City%20Truck
{"name":"Pam Parry","photo":"https://rac-istio-course-images.s3.amazonaws.com/1.jpg"}

workload này có istio nhưng namespace ko bị deny tới defaul và vẫn có kêt nối
3.2 Tiếp theo ví dụ nữa
- Serive A –allow–> service B
- all service —deny-> service B
- gt->vs->dr –deny–> service B (chỗ này chưa allow được, chưa rõ usecase thực tế có cần không?)
Chỗ này chỉ rõ namespace và selector của workload
Kiểm tra workload

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
labels:
app.kubernetes.io/instance: ingress-istio-apptest
name: default
namespace: default
spec:
selector:
matchLabels:
app: webapp
action: ALLOW
rules:
- from:
- source:
namespaces:
- author-istio
to:
- operation:
methods:
- GET
paths: ["/ap*"]


Có Điều lưu ý là phần ALLOW với ver 1.9.7 thì nếu ko có selector -> matchLabels nó sẽ không allow, kiểu phải chỉ rõ mày muốn allow cho ai và tao sẽ chặn all
các link tham khảo author
https://istio.io/latest/docs/reference/config/security/authorization-policy/#Source
https://istio.io/latest/docs/concepts/security/#allow-all-and-deny-all
https://istiobyexample.dev/authorization/
4 Envoy Filler
Các bài test envoy thì được cập nhật ở bài viết bên dưới:
5. ServiceEntry
Cái này bạn có thể hiểu là: app web của bạn ở trong k8s sẽ kết nối với mysql ở ngoại cluster k8s và app của bạn kết nối đến db mysql bằng domain. (vd: mysql.nimtechnology.com)
Link tham khảo:
https://imroc.cc/istio/best-practice/specify-protocol/
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:
name: external-mysql
spec:
hosts:
- mysql.example.com
location: MESH_EXTERNAL
ports:
- number: 4000
name: mysql
protocol: mysql
resolution: DNS
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: httpbin-fleetman-webapp
spec:
hosts:
- fleetman-webapp.nimtechnology.com
ports:
- number: 80
name: http
protocol: HTTP
resolution: DNS
location: MESH_EXTERNAL
>>>update Tue 9 Nov 2021
>>>>>>>>>>>>>>>>>>>>>>>>
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: vm-postgresql
namespace: default
spec:
hosts:
- vm-postgresql.nimtechnology.com
addresses:
- 10.8.3.240/32
ports:
- number: 5432
name: ps-port
protocol: TCP
location: MESH_EXTERNAL
>>>update Tue 23 Nov 2021
>>>>>>>>>>>>>>>>>>>>>>>>
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: external-svc-wikipedia
spec:
hosts:
- wikipedia.org
location: MESH_EXTERNAL
ports:
- number: 80
name: example-grpc
protocol: GRPC
resolution: DNS

More detain —> Coming soon
6. Cors Policy
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
labels:
app.kubernetes.io/instance: fulfillment-api-argocd-app
name: fulfillment-api.nimtechnology.com
namespace: fulfillment
spec:
gateways:
- mesh
- fulfillment-api-dev-tiki-services
hosts:
- fulfillment-api.nimtechnology.com
http:
- corsPolicy:
allowHeaders:
- Origin
- Authorization
- Access-Control-Allow-Origin
- Apikey
- Token
- Station
- Warehouse
- Clientversion
- Content-Type
allowOrigins:
- exact: 'https://fulfillment-admin.nimtechnology.com'
match:
- uri:
prefix: /admin
name: fulfillment-admin-api-80
route:
- destination:
host: fulfillment-admin-api.fulfillment.svc.cluster.local
port:
number: 80