您的位置:首页 > 游戏 > 手游 > 如何建立一个网站卖货_宁波网站建设制作公司哪家好_线上宣传方式有哪些_网络推广主要是做什么工作

如何建立一个网站卖货_宁波网站建设制作公司哪家好_线上宣传方式有哪些_网络推广主要是做什么工作

2025/2/24 9:54:33 来源:https://blog.csdn.net/Karoku/article/details/145615211  浏览:    关键词:如何建立一个网站卖货_宁波网站建设制作公司哪家好_线上宣传方式有哪些_网络推广主要是做什么工作
如何建立一个网站卖货_宁波网站建设制作公司哪家好_线上宣传方式有哪些_网络推广主要是做什么工作

文章目录

  • 补充
    • **Service 的作用**
    • **Kubernetes 外部访问方案**
  • Kubernetes Ingress
    • Ingress 概述
      • Kubernetes 外部访问方案对比
      • Ingress 的组成
      • **Ingress-Nginx 工作原理**
      • Ingress 控制器的部署方式
        • 1. DaemonSet + Host 网络模式
        • 2. Deployment + NodePort/LoadBalancer Service
      • 创建 Ingress 资源
          • 部署 Ingress-Nginx
        • 陈述式命令
        • YAML 配置
      • 高级功能
        • 1. 基于域名的代理转发
        • 2. 基于路径的代理转发
        • 3. HTTPS 支持
        • 4. Basic Auth 认证
        • 5. 路径重写
        • 6. 金丝雀发布(Canary)
    • 部署 nginx-ingress-controller
    • 部署 nginx-ingress-controller(二)
      • 准备工作
      • 修改镜像地址
      • 修改 Service 类型
      • 部署 nginx-ingress-controller
      • 部署示例应用和 Ingress 资源
        • 1. 创建 YAML 文件
        • 应用配置并验证
      • 测试访问
        • 内部访问
        • 外部访问
      • 常见问题排查
    • Ingress HTTP 与 HTTPS 代理访问
      • Ingress HTTP 代理访问配置
      • Ingress HTTPS 代理访问配置
    • Nginx BasicAuth 配置
    • Kubernetes Ingress-Nginx 重写功能
      • 核心注解(Annotations)
      • 常见场景与配置
        • 域名重定向
      • 路径重写
      • 根路径重定向
        • **场景 4:强制 HTTPS 跳转**
      • **完整示例**
        • **YAML 配置**
      • **验证步骤**
      • **注意**
    • Traefik Ingress Controller
      • Traefik 概述
      • Traefik 部署方式
        • 1. DaemonSet
        • **2. Deployment**
      • 部署 Traefik Ingress Controller
        • 准备工作
        • **2. 部署步骤**
      • 配置 Ingress HTTP 代理
        • 1. 创建 Deployment 和 Service
        • 2. 应用配置
        • 3. 验证部署

补充

Service 的作用

  • 对集群内部
    • 跟踪 Pod 的变化,更新 Endpoint。
    • 提供服务发现机制,解决 Pod IP 动态变化的问题。
  • 对集群外部
    • 类似负载均衡器,支持集群内外访问 Pod。
    • 提供统一的访问入口。

Kubernetes 外部访问方案

  1. NodePort

    • 将 Service 暴露在节点网络上。
    • 通过节点的 IP 和端口访问服务。
    • 缺点
      • 端口范围有限(默认 30000-32767)。
      • 管理复杂,不适合大规模集群。
  2. LoadBalancer

    • 使用外部负载均衡器分发流量。
    • 需要云环境支持(如 AWS、GCP)。
    • 缺点
      • 依赖云服务商。
      • 成本较高。
  3. ExternalIPs

    • 为 Service 分配外部 IP。
    • 外部 IP 路由到集群节点,流量转发到 Service 的 Endpoint。
    • 缺点
      • 需要手动管理外部 IP。
  4. Ingress

    • 提供七层代理(HTTP/HTTPS)。
    • 基于域名或 URL 路径转发请求。
    • 优点
      • 只需少量公网 IP 和负载均衡器。
      • 支持多服务暴露,灵活配置。

Kubernetes Ingress

Ingress 概述

  • 作用:作为 Kubernetes 集群的接入层,接收外部请求并根据规则(域名或 URL 路径)将请求转发到相应的 Service 和 Pod。
  • 特点
    • 支持七层代理(HTTP/HTTPS)。
    • 提供灵活的流量管理(如基于域名、路径的转发)。
    • 支持 TLS 加密、认证、重写路径等高级功能。

Kubernetes 外部访问方案对比

方案特点适用场景
NodePort通过节点端口暴露服务,仅支持四层代理。简单测试环境
LoadBalancer通过云服务商的负载均衡器暴露服务,支持四层代理。云环境生产部署
ExternalIPs通过指定节点的外部 IP 暴露服务,仅支持四层代理。特定节点 IP 的场景
Ingress支持七层代理,基于域名或路径转发请求,功能强大且灵活。生产环境,复杂流量管理

Ingress 的组成

  1. Ingress 对象

    • 定义转发规则(如域名、路径)。
    • 告诉 Ingress 控制器如何将请求转发到后端 Service。
    • 功能
      • 提供外部 URL。
      • 支持负载均衡。
      • 提供 SSL/TLS 能力。
      • 实现基于域名的反向代理。
    • 配置方式:通过 YAML 文件定义。
  2. Ingress 控制器

    • 实际处理请求转发的组件。
    • 常见实现:Nginx Ingress Controller、Traefik、HAProxy 等。
    • 通常以 Pod 形式运行。
    • 功能
      • 监控集群变化,动态更新配置。
      • 根据 Ingress 规则生成代理配置(如 Nginx 配置)。
      • 应用配置并重载代理服务。

Ingress-Nginx 工作原理

  1. 监控集群变化

    • Ingress Controller 通过 Kubernetes API Server 动态感知 Ingress 规则的变化。
  2. 生成配置

    • 根据 Ingress 规则生成 Nginx 配置。
    • 配置包括域名、路径与 Service 的映射关系。
  3. 写入配置

    • 将生成的 Nginx 配置写入 /etc/nginx/nginx.conf
  4. 重载服务

    • 重载 Nginx 服务,使新配置生效。

Ingress 控制器的部署方式

1. DaemonSet + Host 网络模式
  • 特点
    • 每个节点运行一个 Ingress 控制器 Pod。
    • 使用节点的 80/443 端口直接接收外部请求。
  • 数据流
    客户端 -> 节点 -> 80/443 端口 -> Ingress 控制器 -> 后端 Pod
    
2. Deployment + NodePort/LoadBalancer Service
  • 特点
    • 集中部署 Ingress 控制器。
    • 通过 NodePort 或 LoadBalancer 暴露服务。
  • 数据流
    客户端 -> 节点 -> NodePort -> Ingress 控制器 -> 后端 Pod
    

创建 Ingress 资源

部署 Ingress-Nginx
  • 官方 GitHub:Ingress-Nginx
  • 官方文档:Ingress-Nginx 文档
陈述式命令
kubectl create ingress <ingress-name> \--rule=<域名>/<路径>=<service-name>:<service-port> \--class=<ingress-class>
YAML 配置
  • YAML 示例
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:name: example-ingressannotations:nginx.ingress.kubernetes.io/rewrite-target: /
    spec:ingressClassName: nginxrules:- host: www.example.comhttp:paths:- path: /apppathType: Prefixbackend:service:name: app-serviceport:number: 80tls:- hosts:- www.example.comsecretName: tls-secret
    

高级功能

1. 基于域名的代理转发
rules:
- host: www.example.comhttp:paths:- path: /appbackend:service:name: app-serviceport: 80
- host: api.example.comhttp:paths:- path: /apibackend:service:name: api-serviceport: 8080
2. 基于路径的代理转发
rules:
- host: www.example.comhttp:paths:- path: /appbackend:service:name: app-serviceport: 80- path: /apibackend:service:name: api-serviceport: 8080
3. HTTPS 支持
  • 创建 TLS Secret:
    kubectl create secret tls tls-secret --cert=tls.crt --key=tls.key
    
  • 配置 Ingress:
    tls:
    - hosts:- www.example.comsecretName: tls-secret
    
4. Basic Auth 认证
  • 创建认证文件:
    htpasswd -c auth admin
    
  • 创建 Secret:
    kubectl create secret generic basic-auth --from-file=auth
    
  • 配置 Ingress:
    metadata:annotations:nginx.ingress.kubernetes.io/auth-type: basicnginx.ingress.kubernetes.io/auth-secret: basic-authnginx.ingress.kubernetes.io/auth-realm: 'Authentication Required'
    
5. 路径重写
metadata:annotations:nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:rules:- host: www.example.comhttp:paths:- path: /old/(.*)backend:service:name: new-serviceport: 80
6. 金丝雀发布(Canary)
metadata:annotations:nginx.ingress.kubernetes.io/canary: "true"nginx.ingress.kubernetes.io/canary-weight: "50"
spec:rules:- host: www.example.comhttp:paths:- path: /backend:service:name: canary-serviceport: 80

部署 nginx-ingress-controller

  1. 创建目录并下载部署文件
    mkdir /opt/ingress
    cd /opt/ingress
    wget https://gitee.com/mirrors/ingress-nginx/raw/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
    
  2. 修改镜像地址
    编辑 deploy.yaml 文件,找到 image 字段,并将其修改为阿里云镜像仓库的地址:
    image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.0@sha256:d1707ca76d3b044ab8a28277a2466a02100ee9f58a86af1535a3edf9323ea1b5
    image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
    
  3. 选择部署方式
    您选择了 DaemonSet+HostNetwork+nodeSelector 的方式部署 nginx-ingress-controller。这种方式适合大并发的生产环境,因为它直接将 Pod 的网络与宿主机网络打通,减少了 NAT 的性能损耗。
  4. 为节点打标签
    kubectl label node node02 ingress=true
    kubectl get nodes --show-labels
    
  5. 修改 deploy.yaml 文件
    kindDeployment 修改为 DaemonSet,并添加 hostNetworknodeSelector 字段:
    apiVersion: apps/v1
    kind: DaemonSet
    # ... 其他配置 ...
    spec:template:spec:hostNetwork: truenodeSelector:ingress: "true"
    
  6. 部署 nginx-ingress-controller
    kubectl apply -f deploy.yaml
    
    部署完成后,检查 Pod 是否已正确运行在 node02 节点上:
    kubectl get pod -n ingress-nginx -o wide
    
  7. 创建 Ingress 规则
    首先,创建一个 nginx 应用及其服务:
    # service-nginx.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:name: nginx-app
    # ... 其他配置 ...
    ---
    apiVersion: v1
    kind: Service
    metadata:name: nginx-app-svc
    # ... 其他配置 ...
    
    然后,创建 Ingress 规则:
    # ingress-app.yaml
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:name: nginx-app-ingress
    spec:ingressClassName: "nginx"rules:- host: www.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-app-svcport:number: 80
    
    应用这些配置:
    kubectl apply -f service-nginx.yaml
    kubectl apply -f ingress-app.yaml
    
  8. 测试访问
    在本地 /etc/hosts 文件中添加域名解析:
    192.168.80.12 www.xy101.com
    
    然后使用 curl 命令测试访问:
    curl http://www.xy101.com
    
    如果一切正常,应该能够看到 nginx 的默认欢迎页面。

部署 nginx-ingress-controller(二)

准备工作

  • 创建目录
    mkdir /opt/ingress-nodeport
    cd /opt/ingress-nodeport
    
  • 下载配置文件
    使用官方地址或国内镜像源下载deploy.yaml文件:
    # 官方地址(可能因网络问题无法下载)
    wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml# 国内镜像源(推荐)
    wget https://gitee.com/mirrors/ingress-nginx/raw/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
    

修改镜像地址

为了在国内网络环境下顺利拉取镜像,需将镜像地址修改为阿里云镜像:

# 修改以下两处镜像地址
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.0@sha256:d1707ca76d3b044ab8a28277a2466a02100ee9f58a86af1535a3edf9323ea1b5
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660

修改 Service 类型

编辑deploy.yaml文件,将Service类型设置为NodePort

apiVersion: v1
kind: Service
metadata:labels:app.kubernetes.io/component: controllerapp.kubernetes.io/instance: ingress-nginxapp.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxapp.kubernetes.io/version: 1.3.0name: ingress-nginx-controllernamespace: ingress-nginx
spec:ports:- port: 80targetPort: 80protocol: TCPname: http- port: 443targetPort: 443protocol: TCPname: httpstype: NodePort  # 设置为 NodePort 或 LoadBalancer# externalTrafficPolicy: Cluster  # 可选,控制流量策略

部署 nginx-ingress-controller

应用配置文件并验证部署状态:

kubectl apply -f deploy.yaml# 查看 Pod 和 Service 状态
kubectl get pod,svc -n ingress-nginx

部署示例应用和 Ingress 资源

1. 创建 YAML 文件

创建并编辑ingress-nginx.yaml文件,定义Deployment、Service和Ingress资源:

apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-app
spec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:ports:- port: 80targetPort: 80protocol: TCPselector:app: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-test
spec:ingressClassName: "nginx"rules:- host: www.xy102.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
应用配置并验证

应用YAML文件并查看资源状态:

kubectl apply -f ingress-nginx.yaml# 查看 Service 和 Pod 状态
kubectl get svc,pods -o wide

测试访问

内部访问

进入其中一个Nginx Pod,修改index.html内容:

kubectl exec -it <pod-name> -- /bin/bash
# 例如:kubectl exec -it nginx-app-65d7b99f6b-l4g65 -- /bin/bashcd /usr/share/nginx/html/
echo 'this is web1' > index.html
# 对另一个Pod执行类似操作,写入 'this is web2'

通过ClusterIP访问Service,验证负载均衡效果:

curl http://<nginx-svc-cluster-ip>
外部访问
  • 配置本地 /etc/hosts 文件
    添加域名解析,将 www.xy102.com 解析到集群节点的IP地址(例如 192.168.80.12):
    vim /etc/hosts
    
    添加以下内容:
    192.168.80.10   master
    192.168.80.11   node01
    192.168.80.12   node02
    192.168.80.12   www.xy101.com
    192.168.80.12   www.xy102.com
    
  • 通过 NodePort 访问
    使用节点IP和NodePort端口访问服务:
    curl http://192.168.80.12:32383
    
  • 通过域名访问
    配置完成后,可以直接使用域名进行访问:
    curl http://www.xy102.com:32383
    

常见问题排查

  • Pod 无法启动
    检查Pod的日志,确认是否有镜像拉取失败或其他错误:
    kubectl logs <pod-name> -n ingress-nginx
    
  • Service 无法访问
    确认Service的端口配置正确,并且后端Pod正常运行:
    kubectl describe svc ingress-nginx-controller -n ingress-nginx
    
  • Ingress 规则不生效
    检查Ingress资源的配置,确保hostpath匹配正确,并且DNS解析正常。

Ingress HTTP 与 HTTPS 代理访问

Ingress HTTP 代理访问配置

  1. 创建虚拟主机资源
    • 虚拟主机1(deployment1.yaml)
      apiVersion: apps/v1
      kind: Deployment
      metadata:name: deployment1
      spec:replicas: 2selector:matchLabels:name: nginx1template:metadata:labels:name: nginx1spec:containers:- name: nginx1image: soscscs/myapp:v1imagePullPolicy: IfNotPresentports:- containerPort: 80
      ---
      apiVersion: v1
      kind: Service
      metadata:name: svc-1
      spec:ports:- port: 80targetPort: 80protocol: TCPselector:name: nginx1
      
    • 虚拟主机2(deployment2.yaml)
      apiVersion: apps/v1
      kind: Deployment
      metadata:name: deployment2
      spec:replicas: 2selector:matchLabels:name: nginx2template:metadata:labels:name: nginx2spec:containers:- name: nginx2image: soscscs/myapp:v2imagePullPolicy: IfNotPresentports:- containerPort: 80
      ---
      apiVersion: v1
      kind: Service
      metadata:name: svc-2
      spec:ports:- port: 80targetPort: 80protocol: TCPselector:name: nginx2
      
    • 应用配置:
      kubectl apply -f deployment1.yaml
      kubectl apply -f deployment2.yaml
      
  2. 创建Ingress资源
    • ingress-nginx.yaml
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:name: ingress1
      spec:ingressClassName: "nginx"rules:- host: www1.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: svc-1port:number: 80
      ---
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:name: ingress2
      spec:ingressClassName: "nginx"rules:- host: www2.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: svc-2port:number: 80
      
    • 应用配置:
      kubectl apply -f ingress-nginx.yaml
      
  3. 测试访问
    • 获取Ingress服务NodePort端口:
      kubectl get svc -n ingress-nginx
      
    • 访问测试:
      curl www1.xy101.com:<NodePort>
      curl www2.xy101.com:<NodePort>
      

Ingress HTTPS 代理访问配置

  1. 创建SSL证书
    • 生成自签名证书:
      openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
      
  2. 创建Secret资源
    • 创建存储证书的Secret:
      kubectl create secret tls tls-secret --key tls.key --cert tls.crt
      
  3. 创建Deployment、Service、Ingress资源
    • ingress-https.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:name: nginx-app
      spec:replicas: 2selector:matchLabels:name: nginxtemplate:metadata:labels:name: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80
      ---
      apiVersion: v1
      kind: Service
      metadata:name: nginx-svc
      spec:ports:- port: 80targetPort: 80protocol: TCPselector:name: nginx
      ---
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:name: nginx-https
      spec:ingressClassName: "nginx"tls:- hosts:- www3.xy101.comsecretName: tls-secretrules:- host: www3.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
      
    • 应用配置:
      kubectl apply -f ingress-https.yaml
      
  4. 访问测试
    • 修改hosts文件,添加域名映射:
      192.168.80.10 www3.xy101.com
      
    • 使用浏览器访问:
      https://www3.xy101.com:<NodePort-443>
      

Nginx BasicAuth 配置

  1. 生成用户密码认证文件
    • 安装httpd-tools并生成认证文件:
      yum -y install httpd-tools
      htpasswd -c auth zhangsan
      
  2. 创建Secret资源
    • 创建存储认证文件的Secret:
      kubectl create secret generic basic-auth --from-file=auth
      
  3. 创建Ingress资源
    • ingress-auth.yaml
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:name: ingress-authannotations:nginx.ingress.kubernetes.io/auth-type: basicnginx.ingress.kubernetes.io/auth-secret: basic-authnginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - zhangsan'
      spec:ingressClassName: "nginx"rules:- host: auth.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
      
    • 应用配置:
      kubectl apply -f ingress-auth.yaml
      
  4. 访问测试
    • 修改hosts文件,添加域名映射:
      192.168.80.10 auth.xy101.com
      
    • 使用浏览器访问:
      http://auth.xy101.com:<NodePort>
      
    • 输入用户名和密码进行认证。

Kubernetes Ingress-Nginx 重写功能

核心注解(Annotations)

注解名称功能说明示例值
nginx.ingress.kubernetes.io/rewrite-target定义重定向的目标 URI(支持正则捕获组)http://www1.xy101.com:32383
nginx.ingress.kubernetes.io/ssl-redirect是否强制跳转 HTTPS(当 Ingress 配置 TLS 时默认为 true"true"
nginx.ingress.kubernetes.io/force-ssl-redirect即使未启用 TLS 也强制跳转 HTTPS"false"
nginx.ingress.kubernetes.io/app-root定义根路径(/)的重定向目标"/app"
nginx.ingress.kubernetes.io/use-regex是否启用正则表达式匹配路径"true"

常见场景与配置

域名重定向

将请求重定向到指定域名(无需真实后端服务):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-rewriteannotations:nginx.ingress.kubernetes.io/rewrite-target: http://www1.xy101.com:32383
spec:ingressClassName: nginxrules:- host: re.xy101.comhttp:paths:- path: /pathType: Prefixbackend:service:name: dummy-svc  # 任意名称(无需真实存在)port:number: 80

验证

curl -v http://re.xy101.com:32383
# 返回 302 跳转到 http://www1.xy101.com:32383

路径重写

使用正则表达式捕获路径并重写:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: http-ingressannotations:nginx.ingress.kubernetes.io/use-regex: "true"nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:ingressClassName: nginxrules:- host: www.xy101.comhttp:paths:- pathType: Prefixpath: /something(/|$)(.*)  # 正则匹配路径backend:service:name: myapp-svcport:number: 9090

效果

  • 请求 http://www.xy101.com/something/foo
  • 被重写为 http://myapp-svc:9090/foo

根路径重定向

将根路径 / 重定向到指定子路径:

metadata:annotations:nginx.ingress.kubernetes.io/app-root: /app1

效果

  • 访问 http://www.xy101.com/
  • 自动跳转至 http://www.xy101.com/app1

场景 4:强制 HTTPS 跳转
metadata:annotations:nginx.ingress.kubernetes.io/ssl-redirect: "true"nginx.ingress.kubernetes.io/force-ssl-redirect: "true"  # 强制所有流量跳转

完整示例

YAML 配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: complex-ingressannotations:nginx.ingress.kubernetes.io/use-regex: "true"nginx.ingress.kubernetes.io/rewrite-target: /$2nginx.ingress.kubernetes.io/app-root: /dashboardnginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:ingressClassName: nginxtls:- hosts:- www.xy101.comsecretName: tls-secretrules:- host: www.xy101.comhttp:paths:- path: /api(/|$)(.*)pathType: Prefixbackend:service:name: backend-apiport:number: 8080- path: /pathType: Prefixbackend:service:name: frontendport:number: 80

验证步骤

  1. 应用配置

    kubectl apply -f ingress.yaml
    
  2. 检查 Ingress 状态

    kubectl get ingress
    
  3. 测试访问

    # 测试根路径跳转
    curl -L http://www.xy101.com# 测试路径重写
    curl http://www.xy101.com/api/v1/data
    

注意

  1. 正则表达式语法
    • 使用 (.*) 捕获路径片段,通过 $1$2 引用。
    • 启用 use-regex: "true" 后,路径匹配严格遵循正则规则。
  2. HTTPS 配置
    • 必须提前创建 TLS Secret:
      kubectl create secret tls tls-secret --cert=tls.crt --key=tls.key
      
  3. 服务依赖
    • 重定向到其他域名时,无需关联真实 Service。
    • 路径重写时,目标 Service 必须存在且可访问。

Traefik Ingress Controller

Traefik 概述

  • 功能:现代 HTTP 反向代理和负载均衡工具,专为微服务架构设计。
  • 特点
    • 实时与 Kubernetes API 交互,自动感知后端 Service 和 Pod 的变化。
    • 支持自动服务发现和配置更新。
    • 提供 Web UI 界面,方便监控和管理。

Traefik 部署方式

1. DaemonSet
  • 特点
    • 每个节点运行一个 Traefik Pod。
    • 可以确定哪些节点运行 Traefik,适合外部流量接入。
  • 缺点
    • 伸缩性较差。
2. Deployment
  • 特点
    • 集中部署 Traefik Pod。
    • 方便伸缩,适合内部流量接入。
  • 缺点
    • 无法确定具体运行节点。

部署 Traefik Ingress Controller

准备工作
  • 创建目录并下载配置文件:
    mkdir /opt/traefik
    cd /opt/traefik
    wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
    wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-deployment.yaml
    wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
    wget https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/ui.yaml
    
2. 部署步骤
  1. 启用 RBAC

    kubectl apply -f traefik-rbac.yaml
    
  2. 部署 Traefik

    kubectl apply -f traefik-deployment.yaml
    
  3. 部署 Traefik Web UI

    kubectl apply -f ui.yaml
    
  4. 验证部署

    kubectl get svc -o wide -n kube-system | grep traefik
    
    • 输出示例:
      traefik-ingress-service   NodePort    10.96.241.13   <none>        80:32383/TCP,8080:32133/TCP   103m   k8s-apptraefik-ingress-lb
      traefik-web-ui            ClusterIP   10.96.67.119   <none>        80/TCP                        101m   k8s-apptraefik-ingress-lb
      
  5. 访问 Web UI

    • 浏览器访问:http://<NodeIP>:32133/dashboard/

配置 Ingress HTTP 代理

1. 创建 Deployment 和 Service
# ingress-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-app
spec:replicas: 2selector:matchLabels:name: nginxtemplate:metadata:labels:name: nginxspec:containers:- name: nginximage: nginxports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:ports:- port: 80targetPort: 80selector:name: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-test
spec:rules:- host: www.benet.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
2. 应用配置
kubectl apply -f ingress-nginx.yaml
3. 验证部署
  1. 查看 Pod 和 Service

    kubectl get svc,pods -o wide
    
  2. 修改 Pod 内容

    kubectl exec -it pod/nginx-app-<pod-id> -- bash
    echo 'this is web1' > /usr/share/nginx/html/index.html
    
  3. 测试访问

    curl http://www.benet.com:32383
    
  4. 查看 Traefik UI

    • 刷新 Web UI 界面,查看生成的集群信息。
  • Traefik 是一个强大的 Ingress Controller,支持自动服务发现和动态配置更新。
  • 部署方式
    • DaemonSet:适合外部流量接入。
    • Deployment:适合内部流量接入,便于伸缩。
  • 功能
    • 提供 Web UI 界面,方便监控和管理。
    • 支持 HTTP/HTTPS 代理、负载均衡等高级功能。

参考文档:Traefik 官方文档

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com