OpenShift 4 - 容器访问限速和Route访问限制流

OpenShift 4.x HOL教程汇总
说明:本文已经在OpenShift 4.14环境中验证

容器访问限速

  1. 创建测试项目
$ oc new-project limited-bandwidth
$ oc adm policy add-scc-to-user anyuid -z default
  1. 执行命令部署nginx容器,然后添加2个注释,其中对Pod的进出访问都限制了0.5Mb/s网络带宽。
$ oc new-app --image=nginx
$ oc patch deployment nginx -p '{"spec": {"template": {"metadata": {"annotations": {"kubernetes.io/egress-bandwidth": "0.5M", "kubernetes.io/ingress-bandwidth": "0.5M"}}}}}'
  1. 查看Pod名称
$ oc get pod -n limited-bandwidth
NAME             READY   STATUS      RESTARTS   AGE
nginx-1-rcn7j    1/1     Running     0          11m
  1. 进入Pod,然后下载一个文件。如果网络不是瓶颈的话,下载速度会稳定在60KB/s左右。
$ oc rsh $(oc get pod -n limited-bandwidth | grep Running | awk '{print $1}')
# curl -LO https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-install-linux.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 80.0M    0  400k    0     0  31848      0  0:43:54  0:00:12  0:43:42 59953

Route访问限流

OpenShift 使用了基于HAProxy的route实现对容器的Ingress访问,可以设置route的HAProxy相关参数来实现Ingress访问限流。通过设置route的annotion设置HAProxy相关参数。

  1. 执行以下命令生成route,在annotations区域增加2个HAProxy的参数,设置客户端每秒只能访问1次route。
$ oc expose svc nginx
$ oc annotate route nginx --overwrite haproxy.router.openshift.io/rate-limit-connections=true
$ oc annotate route nginx --overwrite haproxy.router.openshift.io/rate-limit-connections.rate-http=1
  1. 连续执行命令以下访问route,确认不能快速访问cakephp的route。
$ curl -I $(oc get route nginx -o jsonpath={.spec.host}`)
HTTP/1.1 200 OK
Date: Sun, 16 May 2021 12:57:49 GMT
Server: Apache/2.4.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1g
Content-Type: text/html; charset=UTF-8
Set-Cookie: 7be6a95f3528654dde226a230425d7e3=4288f93c042b36d415cb16a42dc930a9; path=/; HttpOnly
Cache-control: private
$ curl -I $(oc get route nginx -o jsonpath={.spec.host}`)
curl: (52) Empty reply from server
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值