nginx-k8s 相关配置详解

1. nginx 想要将对应请求直接转发到k8s集群,需要配置集群所有的node节点, nginx 配置

nginx.conf 中新增

http {
  include mime.types;
  include /etc/nginx/conf.d/*.conf;

}

在/etc/nginx/conf.d 中新增config配置server.conf

#api访问k8s集群配置http
upstream api_server_http {
     server 10.x.x.node1:80;
     server 10.x.x.node2:80;
     server 10.x.x.node3:80;
     server 10.x.x.node4:80;
     server 10.x.x.node5:80;
     server 10.x.x.node6:80;
     server 10.x.x.node7:80;
     server 10.x.x.node8:80;
     server 10.x.x.node9:80;
     server 10.x.x.master1:80;
     server 10.x.x.master2:80;
     server 10.x.x.master3:80;
     server 10.x.x.master4:80;
}

#server1配置
server {
    server_name 10.x.x.19;
    listen 80;

   index index.php index.htm index.html;
   
   #ng健康检查
   location /nginx-health {
        access_log off;
        add_header Content-Type 'text/html;charset=utf-8';
        return 200 'OK';
   }
   
   #kibana
   location /kibana/ {
        proxy_pass http://10.x.x.16:5601/;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        rewrite ^/kibana/(.*)$ /$1 break;
   }

   #nacos
   location /nacos {
        proxy_pass http://10.x.x.16:8848/nacos;
       # proxy_set_header Host $host:$server_port;
       # proxy_set_header X-Real-IP $remote_addr;
       # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       # rewrite ^/nacos/(.*)$ /$1 break;
  }

  #mq
  location /mqconsole {
       #auth_basic "secret";
       #auth_basic_user_file /usr/local/nginx/db/passwd.db;
       proxy_set_header   Host             $host;
       proxy_set_header   X-Real-IP        $remote_addr;
       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
       proxy_pass http://10.x.x.16:8080/mqconsole;
  }

 location /api/actuator{
    return 404;
  }
  #xxl-job
  location /xxl-job-admin {
       proxy_pass http://10.x.x.16:8090/xxl-job-admin;
  
  }
   
  #前端路由
  location / {
    proxy_redirect off;
    proxy_set_header Host ispace.crland.com.cn;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://api_server_http/;
 }

  #后端api路由
  location /api/ {
    proxy_redirect off;
    client_max_body_size 64M;
    proxy_set_header Host api.ispace.crland.com.cn;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://api_server_http/;
 }
  
}

#服务器server2配置
server {
    server_name aam.xyz.com.cn;
    listen 80;

   index index.php index.htm index.html;
   
   #ng健康检查
   location /nginx-health {
        access_log off;
        add_header Content-Type 'text/html;charset=utf-8';
        return 200 'OK';
   }
   
   #kibana
   location /kibana/ {
        proxy_pass http://10.x.x.16:5601/;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        rewrite ^/kibana/(.*)$ /$1 break;
   }

   #nacos
   location /nacos {
        proxy_pass http://10.x.x.16:8848/nacos;
       # proxy_set_header Host $host:$server_port;
       # proxy_set_header X-Real-IP $remote_addr;
       # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       # rewrite ^/nacos/(.*)$ /$1 break;
  }

  #mq
  location /mqconsole {
       #auth_basic "secret";
       #auth_basic_user_file /usr/local/nginx/db/passwd.db;
       proxy_set_header   Host             $host;
       proxy_set_header   X-Real-IP        $remote_addr;
       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
       proxy_pass http://10.x.x.16:8080/mqconsole;
  }
   
  location /api/actuator{
    return 404;
 }
  #xxl-job
  location /xxl-job-admin {
       proxy_pass http://10.x.x.16:8090/xxl-job-admin;
  
  }
   
  #前端路由
  location / {
    proxy_redirect off;
    proxy_set_header Host mms.crland.com.cn;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://api_server_http/;
 }

  #后端api路由
  location /api/ {
    proxy_redirect off;
    client_max_body_size 64M;
    proxy_set_header Host api.mms.crland.com.cn;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://api_server_http/;
 }

}

}

 

posted @ 2024-08-21 09:25  Antony_hubei  阅读(84)  评论(0编辑  收藏  举报