k8s之Istio超时设置(1)

Istio超时

在这里插入图片描述

  • 将nginx-service这里设置超时为1s,httpd设置延迟请求为5s,会导致nginx无法及时收到数据,从而模拟服务之间调用的超时情况

测试yaml

[root@k8s-master-1 example-v1]# cat nginx-conf.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: timeout
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  namespace: timeout
data:
  nginx.conf: |-
    user  nginx;
    worker_processes  auto;
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  /var/log/nginx/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen 80;
            location / {
              proxy_pass http://httpd.timeout.svc.cluster.local;  #转发给httpd
              proxy_http_version 1.1;
            }
        }
    }
[root@k8s-master-1 example-v1]# cat deployment.yaml 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: timeout
spec:
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: httpd
  namespace: timeout
spec:
  selector:
    app: httpd
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: timeout
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: nginx-conf
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
  volumes:
  - name: nginx-conf
    configMap:
      name: nginx-conf
---
apiVersion: v1
kind: Pod
metadata:
  name: httpd
  namespace: timeout
  labels:
    app: httpd
spec:
  containers:
  - name: httpd
    image: busybox:1.28
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","echo 'this is busybox-httpd' > /var/www/index.html;httpd -f -h /var/www"] 
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","sleep 360000"]
[root@k8s-master-1 example-v1]# cat istio.yaml 
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: nginx
  namespace: timeout
spec:
  hosts:
  - nginx.timeout.svc.cluster.local
  http:
  - route:
    - destination:
        host: nginx.timeout.svc.cluster.local
    timeout: 1s			     # 超过1秒,istio就会返回错误
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: httpd
  namespace: timeout
spec:
  hosts:
  - httpd.timeout.svc.cluster.local
  http:
  - fault: 
      delay:
        percent: 100
        fixedDelay: 5s        # 流量转发前有5S延迟
    route:
    - destination:
        host: httpd.timeout.svc.cluster.local
---

部署

[root@k8s-master-1 example-v1]# kubectl apply -f nginx-conf.yaml 
namespace/timeout created
configmap/nginx-conf created

# busybox测试容器也需要注入,否则没有超时效果,除非从gateway进入的流量
[root@k8s-master-1 example-v1]# istioctl kube-inject -f deployment.yaml | kubectl apply -f -
service/nginx created
service/httpd created
pod/nginx created
pod/httpd created
pod/busybox created
[root@k8s-master-1 example-v1]# kubectl apply -f istio.yaml 
virtualservice.networking.istio.io/nginx created
virtualservice.networking.istio.io/httpd created

测试

# 可以发现访问httpd服务存在5秒的延迟
[root@k8s-master-1 example-v1]# kubectl exec busybox -- time wget -q -O - http://httpd.timeout.svc.cluster.local
Defaulting container name to busybox.
Use 'kubectl describe pod/busybox -n default' to see all of the containers in this pod.
real	0m 5.00s
user	0m 0.00s
sys	0m 0.00s
this is busybox-httpd

# 访问nginx,可以发现报了504超时错误
[root@k8s-master-1 example-v1]# kubectl exec busybox -- time wget -q -O - http://nginx.timeout.svc.cluster.local
Defaulting container name to busybox.
Use 'kubectl describe pod/busybox -n default' to see all of the containers in this pod.
wget: server returned error: HTTP/1.1 504 Gateway Timeout
Command exited with non-zero status 1
real	0m 1.05s
user	0m 0.00s
sys	0m 0.00s
command terminated with exit code 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺仔_牛奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值