我们首先需要部署我们的go应用程序。我们可以使用以下YAML文件定义我们的部署:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: xxxxxxx-image:v1
ports:
- containerPort: 8080
在这个YAML文件中,我们定义了一个名为“my-app”的部署,用于运行我们的go应用程序,它将在一个容器中运行,并在端口8080上暴露服务。同时定义了一个名为“my-app-service”的服务,它将指向我们的go部署,并将端口80映射到容器端口8080
kubectl apply -f deployment.yaml
把当前namespace打上istio的标签,我这里用默认
kubectl label namespace default istio-injection=enabled
定义一个Istio虚拟服务,将HTTP请求路由到我们的go应用程序:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-app-virtual-service
spec:
hosts:
- "*"
gateways:
- istio-system/ingressgateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: my-app-service.default.svc.cluster.local
port:
number: 80
为了更好地管理我们的服务网格,我们还可以使用Istio的监控和跟踪功能。例如,我们可以使用以下YAML文件定义一个Istio DestinationRule,使其启用所有传入和传出流量的跟踪和度量
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: my-app-destination-rule
spec:
host: my-app-service.default.svc.cluster.local
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
portLevelSettings:
- port:
number: 80
tls:
mode: ISTIO_MUTUAL
connectionPool:
http:
http1MaxPendingRequests: 100
maxRequestsPerConnection: 5
tcp:
maxConnections: 1000
outlierDetection:
consecutiveErrors: 5
interval: 5s
baseEjectionTime: 30s
maxEjectionPercent: 50
PS:或者Gateway与VirtualService分开部署,感觉合并到一起更方便
Gateway
# gateway
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: jgrpc
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "dev.api.your-domain.com"
VirtualService
# virtual service
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: jgrpc
spec:
hosts:
- "dev.api.your-domain.com"
gateways:
- jgrpc
http:
- match:
- uri:
prefix: /
route:
- destination:
port:
number: 80
host: ping