1: 确定入口IP和端口

执行以下命令以确定Kubernetes集群是否在支持外部负载均衡器的环境中运行:

 kubectl get svc istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.108.152.2 <pending> 15020:32736/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:32101/TCP,15030:32222/TCP,15031:30551/TCP,15032:31262/TCP,15443:30129/TCP 3d1h

如果EXTERNAL-IP设置了该值,则环境具有可用于入口网关的外部负载平衡器。如果EXTERNAL-IP值是<none>(或永久<pending>),则环境不为入口网关提供外部负载平衡器。在这种情况下,您可以使用服务的节点端口来访问网关。

我的pending,使用服务的节点端口来访问:

设置入口端口:

export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')

export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')

设置入口IP取决于群集提供者:

export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}')

2: 开启一个服务, istio-a.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: istio-a
spec:
selector:
matchLabels:
name: istio-a
replicas: 1
template:
metadata:
annotations:
sidecar.istio.io/inject: "true" # 开启sidecar自动注入
labels:
name: istio-a
spec:
containers:
- name: istio-a
# 我自己的一个镜像
image: registry.cn-shenzhen.aliyuncs.com/zsifan/istio-a:v1
imagePullPolicy: Always
ports:
- containerPort: 10001
---
apiVersion: v1
kind: Service
metadata:
name: istio-a
spec:
ports:
- port: 10001
# # ClusterIP, NodePort, LoadBalancer
type: ClusterIP
selector:
name: istio-a 

3:使用Istio网关配置入口

1): 创建一个Istio Gateway

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "k8s.test.com"

 2): 创建一个VirtualService,配置通过以下路径进入的流量的路由Gateway

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin-a
spec:
hosts:
- "k8s.test.com" # 对应gateway 的hosts
gateways:
- httpbin-gateway # 对应gateway name
http:
- match:
- uri:
prefix: /a # 允许路径/a的通过
rewrite:
uri: / # 重写url
route:
- destination:
port:
number: 10001 # 服务端口号
host: istio-a # 对用的服务
timeout: 10s

 3): 使用curl访问istio-a服务:

curl -I -HHost:k8s.test.com http://$INGRESS_HOST:$INGRESS_PORT/a/test
HTTP/1.1 200 OK
content-type: text/plain;charset=UTF-8
content-length: 13
date: Wed, 13 Nov 2019 13:27:44 GMT
x-envoy-upstream-service-time: 80
server: istio-envoy

我的istio-a服务中就写了一个test:

如果访问url不是/a开头的而是未知的,将显示404:

curl -I -HHost:k8s.test.com http://$INGRESS_HOST:$INGRESS_PORT/b
HTTP/1.1 404 Not Found
vary: Origin,Access-Control-Request-Method,Access-Control-Request-Headers
content-type: application/json
date: Wed, 13 Nov 2019 13:32:06 GMT
x-envoy-upstream-service-time: 21
server: istio-envoy
transfer-encoding: chunked

如果我们想使用浏览器访问入口服务:

可以将gateway中的hosts修改为*,相应的VirtualService的hosts也要修改为*

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin-a
spec:
hosts:
- "*"
gateways:
- httpbin-gateway
http:
- match:
- uri:
prefix: /a
rewrite:
uri: /
route:
- destination:
port:
number: 10001
host: istio-a
timeout: 10s

先输出ip和port确定请求地址:

echo $INGRESS_HOST:$INGRESS_PORT

192.168.17.210:31380

在浏览器请求<ip>:<port>/a/test

就搭建成功了

Kubernetes-Istio之Gateway和VirtualService的更多相关文章

  1. kubernetes istio之gateway

    [root@master istio-]# kubectl apply -f samples/httpbin/httpbin.yaml service/httpbin created deployme ...

  2. Istio Routing 实践掌握virtualservice/gateway/destinationrule/AB版本发布/金丝雀发布

    原文 在学习像 Istio 这样的新技术时,看一下示例应用程序总是一个好主意. Istio repo 有一些示例应用程序,但它们似乎有各种不足. 文档中的 BookInfo 是一个很好的示例. 但是, ...

  3. kubernetes + istio进行流量管理

    实验目的: 本文介绍如何通过istio实现域名访问k8s部署的nginx服务 前提: 已经安装了kubernetes的服务器 了解 kubernetes 基本命令如何使用 (kubectl creat ...

  4. kubernetes istio的快速安装和使用例子

    安装 [root@master ~]# wget https://github.com/istio/istio/releases/download/1.1.5/istio-1.1.5-linux.ta ...

  5. 15分钟在笔记本上搭建 Kubernetes + Istio开发环境

    11月13~15日,KubeCon 上海大会召开,云原生是这个秋天最火热的技术.很多同学来问如何上手 Kubernetes和Istio 服务网格开发.本文将帮助你利用Docker CE桌面版,15分钟 ...

  6. Kubernetes+Istio

    Kubernetes+Istio   微服务.SpringCloud.k8s.Istio杂谈   一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产 ...

  7. ambassador kubernetes native api gateway

    github 上的介绍: Ambassador is an open source Kubernetes-native API Gateway built on Envoy, designed for ...

  8. kubernetes istio之流量管理

    1.部署 Bookinfo 应用 要在 Istio 中运行这一应用,无需对应用自身做出任何改变.我们只要简单的在 Istio 环境中对服务进行配置和运行,具体一点说就是把 Envoy sidecar ...

  9. 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

    部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序   在下载的Istio安装包的samples目录中包含了示例应用程序. ...

随机推荐

  1. async + promise 解决回调地狱

    // 解决异步回调地狱的方案: async + promise async function writeFile() {   // 打开文件   const fd = await new Promis ...

  2. 【vim小记】自动保存配置

    刚接触vim会发现有很多不习惯,其中,不能自动保存当前配置,每次退出要重新配置,很麻烦,好在vim早就为我们想到这些,在看手册的时候,发现里面有session,  这是用户手册的介绍: “会话保存所有 ...

  3. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  4. 04 javascirpt基础知识---听课笔记

    1.JavaScript概念 一门客户端脚本语言运行在客户端浏览器中的.每一个浏览器都有JavaScript的解析引擎脚本语言:不需要编译,直接就可以被浏览器解析执行了 * 功能:可以来增强用户和ht ...

  5. [转帖]央行推出数字货币DCEP:基于区块链技术、将取代现钞

    央行推出数字货币DCEP:基于区块链技术.将取代现钞 天天快报的内容. 密码财经 2019-10-29 18:15 关注   前不久的10月23日,Facebook的首席执行官扎克伯格在美国国会听证会 ...

  6. js中引用类型Math一些常用的方法和属性

    js中有一种引用类型叫做Math,和Global属于单体内置对象,里面有一些非常常用的数学方法和数学常量 常用数学常量 Math.E; // 自然对数的底数Math.LN10 10的自然对数 Math ...

  7. MongoDB初始化创建管理员账户登录

    1.查看mongodb服务是否开启: ps -ef | grep mongod 2.管理员角色必须在启用--auth认证参数之前创建,否则会没有操作权限.如果之前已经创建过用户,请先删除. kill掉 ...

  8. flink ETL数据处理

    Flink ETL 实现数据清洗   一:需求(针对算法产生的日志数据进行清洗拆分) 1. 算法产生的日志数据是嵌套json格式,需要拆分 2.针对算法中的国家字段进行大区转换 3.最后把不同类型的日 ...

  9. 解决unzip解压中文乱码问题

    使用 unzip XXX.zip 方式解压的时候会出现中文乱码 很多人推荐以下方式: 在windows执行命令,可显示字符集数字一般为936: # chcp // 解压时加上-O cp936,xxx为 ...

  10. UI单据字段值查看方式

    1.单据界面右键属性,获取当前单据URL连接:http://172.16.168.12/U9/erp/display.aspx?lnk=SCM.INV.INV2020_10&sId=3017n ...