1、准备测试应用

准备两个nginx Pod和一个proxy

创建应用

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
name: www-v1
namespace: dev
spec:
selector:
app: www
replicas:
template:
metadata:
labels:
app: www
version: v1
spec:
containers:
- name: www
image: nginx
ports:
- containerPort:
---
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
name: www-v2
namespace: dev
spec:
selector:
app: www
replicas:
template:
metadata:
labels:
app: www
version: v2
spec:
containers:
- name: www
image: nginx
ports:
- containerPort:
---
apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
name: www-proxy
namespace: dev
spec:
selector:
app: www-proxy
replicas:
template:
metadata:
labels:
app: www-proxy
spec:
containers:
- name: www
image: nginx
ports:
- containerPort:
---
apiVersion: v1
kind: Service
metadata:
name: www
namespace: dev
spec:
selector:
app: www
ports:
- name: http
protocol: TCP
port:
targetPort:
---
apiVersion: v1
kind: Service
metadata:
name: www-proxy
namespace: dev
spec:
selector:
app: www-proxy
ports:
- name: http
protocol: TCP
port:
targetPort:

注意Service中的 - name: http一定要加上,后面要匹配流量类型

# oc create -f app.yaml

准备3个Config Maps

www-v1

#获取客户端真实IP
set_real_ip_from 10.0.0.0/;
set_real_ip_from 192.168.0.0/;
set_real_ip_from 172.16.0.0/;
set_real_ip_from 100.0.0.0/;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
#日志相关格式配置
log_format json '{"@timestamp":"$time_local",'
'"podname":"$hostname",'
'"clientip":"$remote_addr",'
'"request":"$request",'
'"status":"$status",'
'"forwarded_proto":"$http_x_forwarded_proto",'
'"scheme":"$scheme",'
'"request_method": "$request_method",'
'"size":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"forwarded_for":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent"}'; access_log /dev/stdout json;
error_log /dev/stderr error;
server {
location / {
default_type text/html;
return 'nginx-v1';
}
}

www-v2

#获取客户端真实IP
set_real_ip_from 10.0.0.0/;
set_real_ip_from 192.168.0.0/;
set_real_ip_from 172.16.0.0/;
set_real_ip_from 100.0.0.0/;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
#日志相关格式配置
log_format json '{"@timestamp":"$time_local",'
'"podname":"$hostname",'
'"clientip":"$remote_addr",'
'"request":"$request",'
'"status":"$status",'
'"forwarded_proto":"$http_x_forwarded_proto",'
'"scheme":"$scheme",'
'"request_method": "$request_method",'
'"size":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"forwarded_for":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent"}'; access_log /dev/stdout json;
error_log /dev/stderr error;
server {
location / {
default_type text/html;
return 'nginx-v2';
}
}

www-proxy

#获取客户端真实IP
set_real_ip_from 10.0.0.0/;
set_real_ip_from 192.168.0.0/;
set_real_ip_from 172.16.0.0/;
set_real_ip_from 100.0.0.0/;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
#日志相关格式配置
log_format json '{"@timestamp":"$time_local",'
'"podname":"$hostname",'
'"clientip":"$remote_addr",'
'"request":"$request",'
'"status":"$status",'
'"forwarded_proto":"$http_x_forwarded_proto",'
'"scheme":"$scheme",'
'"request_method": "$request_method",'
'"size":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"forwarded_for":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent"}'; access_log /dev/stdout json;
error_log /dev/stderr error;
server {
location / {
proxy_http_version 1.1;
proxy_pass http://www;
}
}

挂载

# oc set volume  dc/www-v1 --add --overwrite --name=config-volume --mount-path=/etc/nginx/conf.d --source='{"configMap": { "name": "www-v1"}}'
# oc set volume dc/www-v2 --add --overwrite --name=config-volume --mount-path=/etc/nginx/conf.d --source='{"configMap": { "name": "www-v2"}}'
# oc set volume dc/www-proxy --add --overwrite --name=config-volume --mount-path=/etc/nginx/conf.d --source='{"configMap": { "name": "www-proxy"}}'

为了方便测试www-proxy 需要绑定外部Ingress 比如openshif route

apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: www-proxy
namespace: dev
spec:
host: www-proxy.ingress.com
to:
kind: Service
name: www-proxy
port:
targetPort:

2、路由流量

默认情况下在内网网络访问www应用,可以看出来流量是轮询的

# curl www-proxy.ingress.com
nginx-v2
nginx-v2
nginx-v1
nginx-v2
nginx-v1
nginx-v1

创建istio VirtualService、DestinationRule 路由流量

kind: VirtualService
metadata:
name: www-intranet
namespace: dev
spec:
hosts:
- www
http:
- route:
- destination:
host: www
subset: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: www
namespace: dev
spec:
host: www
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN

这里的http要对应Service -name 的值

测试一下,此时应该只会返回"nginx-v2"

# curl www-prxoy.ingress.com
nginx-v2
nginx-v2
nginx-v2
nginx-v2
nginx-v2

根据权重路由流量

spec:
hosts:
- www
http:
- route:
- destination:
host: www
subset: v1
weight:
- destination:
host: www
subset: v2
weight:

测试一下

# curl www-proxy.ingress.com
nginx-v1
nginx-v2
nginx-v2
nginx-v2
nginx-v2
nginx-v1
nginx-v2
nginx-v2

下面介绍不通过www-proxy直接路由外部流量,需要额外创建istio Gateway并和VirtualService绑定

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: default-gateway
namespace: dev
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- '*'
port:
name: http
number:
protocol: HTTP
---
kind: VirtualService
metadata:
name: www
namespace: dev
spec:
gateways:
- default-gateway
hosts:
- www.ingress.com
http:
- route:
- destination:
host: www
subset: v1
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
labels:
app: istio-ingressgateway
chart: gateways
heritage: Tiller
istio: ingressgateway
release: istio
name: www
namespace: istio-system
spec:
host: www.ingress.com
port:
targetPort: http2
to:
kind: Service
name: istio-ingressgateway

注意如果没有外部ingress 使用NodePort的方式引入流量,并且Gateway和VirtualService任意一个hosts没有*,这时候Gateway无法识别具体的host域名

那么可以修改istio-ingressgateway的Deployment用hostPort直接暴露80和443端口,在把istio-ingressgateway的Pod绑定到固定的节点上运行

...
- containerPort:
hostPort:
protocol: TCP
- containerPort:
hostPort:
protocol: TCP
....

下面测试一下

# curl www.ingress.com
nginx-v1
nginx-v1
nginx-v1
nginx-v1
nginx-v1

istio1.1(openshift) 流量路由的更多相关文章

  1. Istio(五):使用服务网格Istio进行流量路由

    目录 一.模块概览 二.系统环境 三.简单路由 3.1 简单路由 四.Subset和DestinationRule 4.1 Subset 和 DestinationRule 4.2 Destinati ...

  2. 通过 Traefik 使用 Kubernetes Service APIs 进行流量路由 (http,https,金丝雀发布)

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247490229&idx=1&sn=ca817054 ...

  3. Oceanus:美团HTTP流量定制化路由的实践

    背景简述 Oceanus是美团基础架构部研发的统一HTTP服务治理框架,基于Nginx和ngx_lua扩展,主要提供服务注册与发现.动态负载均衡.可视化管理.定制化路由.安全反扒.session ID ...

  4. istio1.0.2配置

    项目的组件相对比较复杂,原有的一些选项是靠 ConfigMap 以及 istioctl 分别调整的,现在通过重新设计的Helm Chart,安装选项用values.yml或者 helm 命令行的方式来 ...

  5. Istio中的流量配置

    Istio中的流量配置 目录 Istio中的流量配置 Istio注入的容器 Istio-init istio-proxy Envoy架构 Pilot-agent生成的初始配置文件 Envoy管理接口获 ...

  6. istio流量管理:非侵入式流量治理

    在服务治理中,流量管理是一个广泛的话题,一般情况下,常用的包括: 动态修改服务访问的负载均衡策略,比如根据某个请求特征做会话保持: 同一个服务有多版本管理,将一部分流量切到某个版本上: 对服务进行保护 ...

  7. Nginx动态路由的新姿势:使用Go取代lua

    导语: 在Nitro 中, 我们需要一款专业的负载均衡器. 经过一番研究之后,Mihai Todor和我使用Go构建了基于Nginx.Redis 协议的路由器解决方案,其中nginx负责所有繁重工作, ...

  8. istio路由配置

    istio路由配置   istio的代理配置参考文档: 中文文档: https://istio.io/zh/docs/reference/config/istio.networking.v1alpha ...

  9. 通过流量清理防御DDoS

    导读 在2018年2月,世界上最大的分布式拒绝服务(DDoS)攻击在发起20分钟内得到控制,这主要得益于事先部署的DDoS防护服务. 这次攻击是针对GitHub–数百万开发人员使用的主流在线代码管理服 ...

随机推荐

  1. C# 获取今天是星期几

    //获取今天是星期几 string[] Day = new string[] { "星期日", "星期一", "星期二", "星期 ...

  2. Hibernate的多对多映射

    一.创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql.Oracle.SqlServer等)的Jar包,创建 hibernate.cfg.xml 文件,并配置,配置项如下 ...

  3. 【分块】P4135 作诗

    分块太暴力惹... 没做出来.看了题解qaq 分析: 两头$\sqrt{n}$暴力维护 预处理ans[i][j],sum[i][j] sum[i][j]是一个前缀和,前i块值为j的数量 ans[i][ ...

  4. DataTime 和 时间转化

    如果知道tostring 的字符串格式那么可以根据字符串格式转化成 DateTime string timeText = DateTime.Now.ToString("yy/MM/dd HH ...

  5. Mysql 1864 主从错误解决方法

    故障描述: 在mysql 主库上增加了一个主键操作,没过5分钟就接受到zabbix报警mysql主从同步异常停止信息,一首凉凉送给自己.... 查看现在主从状态 (root@192.168.1.2) ...

  6. 使用WCF上传文件

              在WCF没出现之前,我一直使用用WebService来上传文件,我不知道别人为什么要这么做,因为我们的文件服务器和网站后台和网站前台都不在同一个机器,操作人员觉得用FTP传文件太麻 ...

  7. 深入浅出 Java Concurrency (27): 并发容器 part 12 线程安全的List/Set[转]

    本小节是<并发容器>的最后一部分,这一个小节描述的是针对List/Set接口的一个线程版本. 在<并发队列与Queue简介>中介绍了并发容器的一个概括,主要描述的是Queue的 ...

  8. Lucene 的 Field 域和索引维护

    一.Field 域 1.Field 属性 Field 是文档中的域,包括 Field 名和 Field 值两部分,一个文档可以包括多个 Field,Document 只是 Field 的一个承载体,F ...

  9. npm install模块时 报错:not such file or directory

    通过报错信息可以知道,是因为缺少 package.json 这个文件. 解决方法: 首先,初始化项目,一路回车就行 npm init -f 接着安装依赖 npm install formidable ...

  10. property中ref、value、name的区别

    转载: 版权声明:本文为CSDN博主「qq_36098284」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net ...