Istio配置之配置ingress流量Gateway暴露服务提供外部访问

注意:此任务使用新的 v1alpha3 流量管理 API。旧的 API 已被弃用,ngress Gateway 组件替代了符合 Kubernetes 规范的 Ingress Controller,因此对入站流量具有了更大的控制能力。

1、创建pod应用,确保namespace开启自动注入Pod所在的namespace包含istio-injection=enabled的Label
否则就必须在部署 tomcatapp应用程序之前手动注入 Sidecar

1
kubectl apply -f <(istioctl kube-inject -f tomcat-demo.yaml) //namespaceswei

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
kubectl apply -f tomcat-demo.yaml
[root@k8s-master ~]# cat tomcat-demo.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
labels:
app: tomcat-istio
name: tomcat-istio
namespace: default
spec:
ports:
- name: 8080-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: tomcat-istio
type: NodePort
status:
loadBalancer: {}
---
apiVersion: v1
kind: Pod
metadata:
name: tomcat-istio
annotations:
spec:
replicas: 1
template:
metadata:
labels:
app: tomcat-istio
spec:
containers:
- name: tomcat-istio
image: toamcat:demo
env:
- name: JAVA_OPTS
value: "-server -Xms4096M -Xmx4096M -Xss256K -Dmy.pod.name=$MY_POD_NAME -Djava.awt.headless=true -Dfile.encoding=utf-8 -XX:MaxPermSize=256M -XX:PermSize=128M"
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name

创建Gateway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
kubectl get svc -n istio-system -l istio=ingressgateway
cat <<EOF | istioctl create -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: tomcatapp-gateway
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "tomcat.idcsec.com"
EOF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
创建VirtualService组绑定网关
cat <<EOF | istioctl create -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: tomcat-istio
spec:
hosts:
- "tomcat.idcsec.com"
gateways:

- tomcatapp-gateway
http:

- match:
- uri:
exact: /

true- uri:
prefix: /

route:
- destination:
host: tomcat-istio

port:
number: 8080
EOF

接下来就可以在浏览器的访问域名自行修改host或者使用curl
一个简单使用IstioGateway 配置资源允许外部流量进入 Istio 服务网就完成

清理

删除 Gateway 和 VirtualService,并关闭 tomcat-demo 服务:

1
2
3
$ istioctl delete gateway tomcatapp-gateway
$ istioctl delete virtualservice tomcat-istio
$ kubectl delete --ignore-not-found=true -f tomcat-demo.yaml