k8s之Istio超时设置(1)

Istio超时

k8s之Istio超时设置(1)_第1张图片

  • 将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

你可能感兴趣的:(Kubernetes,Istio,linux,运维,k8s,istio,kubernetes)