解决k8s中nginx域名无动态解析及启动失败问题

问题1:Nginx不能动态解析域名,重启服务Nginx不能自动切换访问的问题。
问题2:未配置set指令,就报错直接不能启动,启动失败的问题。

示例: [emerg] 1#1: host not found in upstream "xxx-gateway" in /etc/nginx/conf.d/default.conf:60

server {
    listen  80;
    server_name localhost;
    # access_log /var/log/nginx/pro.log;

    location  / {
        # 传递真实的请求头信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         
        # 限制文件大小为1G 
        client_max_body_size    10240m; 
        client_body_buffer_size 256k;
        proxy_connect_timeout 1200;
        proxy_read_timeout  1200;
        proxy_send_timeout  6000;
        proxy_buffer_size  32k;
        proxy_buffers   4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 10m;
            
        # 允许跨域
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

        # 不缓存页面
        # if ($request_filename ~* .*\.(?:htm|html)$) {
        #   add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        # }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
     }
   
    location /api {
        # 传递真实的请求头信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        # 调优
        client_max_body_size    10240m; 
        client_body_buffer_size 256k;
        proxy_connect_timeout 1200;
        proxy_read_timeout  1200;
        proxy_send_timeout  6000;
        proxy_buffer_size  32k;
        proxy_buffers   4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 10m;

        # 允许跨域   
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    
        rewrite ^.+api/?(.*)$ /$1 break;
        include uwsgi_params;
        proxy_pass http://xxx-gateway:9999;
    }
    
    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

在k8s中使用nginx作为后端接口反向代理,关键配置如下:

 # 指向k8s集群内的DNS,解决不能动态解析问题。
 resolver kube-dns.kube-system.svc.cluster.local valid=30s;
# 根据k8s实际情况进行调整,解决启动失败问题。
set $apiserver (服务名).(命名空间).svc.cluster.local[:端口];
# 案例set $endpoint_service service-reserved-gateway.xxxx-site.svc.cluster.local;

 location /api {
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   Host              $http_host;
      proxy_set_header   X-Real-IP         $remote_addr;
      rewrite /api/(.*) /$1 break;
      proxy_pass http://$apiserver:8080; # 若set 未配置端口,这里加端口。
  }

完整配置参考:

server {
    listen  80;
    server_name localhost;
    # access_log /var/log/nginx/pro.log;

    location  / {
        # 传递真实的请求头信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         
        # 限制文件大小为10G 
        client_max_body_size    10240m; 
        client_body_buffer_size 256k;
        proxy_connect_timeout 1200;
        proxy_read_timeout  1200;
        proxy_send_timeout  6000;
        proxy_buffer_size  32k;
        proxy_buffers   4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 10m;
            
        # 允许跨域
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

        # 不缓存页面
        # if ($request_filename ~* .*\.(?:htm|html)$) {
        #   add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
        # }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
     }
   
    # 将DNS指向kubernetes集群内的DNS
    resolver kube-dns.kube-system.svc.cluster.local valid=5s;
    set $endpoint_service service-reserved-gateway.hbgs-site.svc.cluster.local;

    location ~ ^/service-.*$ {
        # 传递真实的请求头信息
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        # 调优
        client_max_body_size    10240m; 
        client_body_buffer_size 256k;
        proxy_connect_timeout 1200;
        proxy_read_timeout  1200;
        proxy_send_timeout  6000;
        proxy_buffer_size  32k;
        proxy_buffers   4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 10m;

        # 允许跨域   
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    
        # rewrite ^.+api/?(.*)$ /$1 break;
        include uwsgi_params;
        proxy_pass http://$endpoint_service:8000;
              
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    
    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}


你可能感兴趣的:(解决k8s中nginx域名无动态解析及启动失败问题)