云服务器以域名形式访问机房Kubernetes集群服务之解决方案

发布于:2025-08-02 ⋅ 阅读:(17) ⋅ 点赞:(0)

1. 背景介绍

1)机房K8S集群采用KubeSphere方式部署,集群信息如下:

服务器

内网地址

主机名

备注

配置

部署方式

ESXI 虚拟机

10.10.18.151

test-master151

主节点

4C16G

1master

3worker

脚本自动部署

10.10.18.154

test-node154

node154节点

16C32G

10.10.18.155

test-node155

node155节点

16C32G

10.10.18.156

test-node156

node156节点

16C32G

2)上述节点公用同一联通专线出站公网IP,地址为1.1.1.1 ;入站公网IP为2.2.2.2

3)机房入站公网IP2.2.2.2的80和443端口未做互联网备案

4)云服务器IP为3.3.3.3

2. 访问需求

        通过HTTPS域名形式【https://www.lmzf.com】访问机房k8s集群服务。

3. 解决方案

3.1  转发流程

https://www.lmzf.com——>3.3.3.3:443——> 2.2.2.2:32532——>10.10.18.151:32532

3.2 方案配置

3.2.1 域名解析

www.lmzf.com解析至3.3.3.3

3.2.2 Nginx配置

服务器3.3.3.3上nginx配置如下

[root@3.3.3.3 ~]# cat  /etc/nginx/conf.d/www.lmzf.com.conf
server {
    listen 443 ssl;
    server_name www.lmzf.com;

    ssl_certificate    /etc/nginx/ssl/www.lmzf.com.pem;
    ssl_certificate_key   /etc/nginx/ssl/www.lmzf.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;


    location / {
        proxy_pass https://2.2.2.2:32532;  # 指向Kubesphere网关的节点端口
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Content-Type';
        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_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    error_log  /var/log/www.lmzf.com.error.log;
}

备注:服务器3.3.3.3的防火墙需放开32532端口 

3.2.3 网关映射

路由器NAT配置或Nginx四层转发实现

2.2.2.2:32532——映射——>10.10.18.151:32532  

3.2.4 KubeSphere网关配置

3.3.5 Ingress配置

kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: www.lmzf.com
  namespace: lmzf
  creationTimestamp: '2025-07-21T08:06:16Z'
  annotations:
    kubesphere.io/alias-name: lmzf
    kubesphere.io/creator: admin
spec:
  ingressClassName: kubesphere-router-cluster
  tls:
    - hosts:
        - www.lmzf.com
      secretName: www.lmzf.com_ssl
  rules:
    - host: www.lmzf.com
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: lmzf-nginx-web
                port:
                  number: 80

 3.2.6 域名访问

浏览器输入:https://www.lmzf.com  


网站公告

今日签到

点亮在社区的每一天
去签到