Istio Gateway发布服务

1. Istio Gateway发布服务

在集群中部署一个 tomcat 应用程序。然后将部署一个 Gateway 资源和一个与 Gateway 绑定的 VirtualService,以便在外部 IP 地址上公开该应用程序。

1.1 部署 Gateway 资源

vim ingressgateway.yaml

复制代码
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: ingressgateway80
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - '*'

把 hosts 字段设置为 *,可以直接从外部 IP 地址访问入口网关。

1.2 部署Tomcat 应用

拉取所需的镜像:

复制代码
docker pull tomcat:latest
docker save tomcat:latest -o tomcat-latest.img
docker load < tomcat-latest.img

部署tomcat

vim tomcat.yaml

复制代码
---
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: tomcat
  name: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: tomcat
    spec:
      containers:
      - image: tomcat:latest
        imagePullPolicy: IfNotPresent
        name: tomcat
        ports:
          - containerPort: 8080
        resources: {}
status: {}

kubectl apply -f tomcat.yaml

deployment创建成功,并且有两个容器在运行。一个是 Envoy sidecar 代理,第二个是应用程序tomcat。如下:

1.3 部署Tomcat service

vim tomcat.yaml

复制代码
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: tomcat
  name: tomcat
spec:
  ports:
  - port: 80
    name: tcp
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat
status:
  loadBalancer: {}

创建service

复制代码
kubectl apply -f service.yaml

1.4 部署VirtualService

为 tomcat 服务创建一个 VirtualService,并将其绑定到 Gateway 资源上

vim virtualservice.yaml

复制代码
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: virtualservice
spec:
  hosts:
    - "*"
  gateways:
    - ingressgateway
  http:
    - route:
        - destination:
            host: tomcat.default.svc.cluster.local
            port:
              number: 80

创建virtualservice

复制代码
kubectl apply -f virtualservice.yaml

在 hosts 字段中使用 *,就像我们在 Gateway 中做的那样。我们还将之前创建的 Gateway 资源(gateway)添加到 gateways 数组中。最后,我们指定了一个目的地为 Kubernetes 服务 tomcat.default.svc.cluster.local 的单一路由。

复制代码
kubectl get svc -l istio=ingressgateway -n istio-system

如果 EXTERNAL-IP 有值(IP 地址或主机名),则说明环境具有可用于 Ingress 网关的外部负载均衡器。如果 EXTERNAL-IP 值是 (或一直是 ),则说明的环境并没有为 Ingress 网关提供外部负载均衡器的功能。

可以通过以下方法添加外部IP

复制代码
kubectl edit  service istio-ingressgateway -n istio-system

添加externalIPs,此处填在为master的IP地址

重新查看,有地址了

对 GATEWAY_URL 运行 cURL 或在浏览器中打开它,我们将得到 tomcat 的响应如下:

另外,注意到 server 头设置为 istio-envoy,告诉我们该请求通过了 Envoy 代理。

1.4 清理资源

删除 Deployment、Service、VirtualService 和 Gateway:

复制代码
kubectl delete deployments tomcat
kubectl delete service tomcat
kubectl delete virtualservice virtualservice
kubectl delete gateways ingressgateway

2. 参考文献

https://www.cnblogs.com/renshengdezheli/p/16838966.html

https://blog.csdn.net/weixin_41709748/article/details/122695478

https://developer.aliyun.com/article/886726

https://www.bookstack.cn/read/istio-handbook/best-practices-how-to-implement-ingress-gateway.md

https://www.cnblogs.com/boshen-hzb/p/10679863.html

https://istio.io/latest/zh/docs/tasks/traffic-management/ingress/ingress-control/

相关推荐
zoulingzhi_yjs5 小时前
haproxy配置详解
linux·云原生
qq_529835356 小时前
Zookeeper的简单了解
分布式·zookeeper·云原生
程序员小羊!9 小时前
Zookeeper 3.6.3【详细技术讲解】整
分布式·zookeeper·云原生
终端行者11 小时前
k8s之Ingress服务接入控制器
云原生·容器·kubernetes
不会敲代码的XW13 小时前
高可用集群KEEPALIVED的详细部署
运维·云原生
小裕哥略帅13 小时前
架构师--基于常见组件的微服务场景实战
微服务·云原生·架构
Reggie_L15 小时前
Eureka-服务注册,服务发现
云原生·eureka·服务发现
云和数据.ChenGuang17 小时前
`neutron router-gateway-set` 操作失败的可能原因及解决方案
运维·gateway·运维技术总结·运维技术
aashuii1 天前
k8s通过NUMA亲和分配GPU和VF接口
云原生·容器·kubernetes
Kentos(acoustic ver.)1 天前
云原生 —— K8s 容器编排系统
云原生·容器·kubernetes·云计算·k8s