ZBLOG

k3s traefik 域名转发到ip端口, 完整案例

以下是在 K3s 中使用 Traefik 实现域名转发到 IP 端口的完整案例:

  1. 安装 K3s

首先需要在服务器上安装 K3s,这可以通过下载二进制文件并执行安装脚本来完成。具体步骤详见 https://k3s.io/#installation

  1. 创建 Deployment 和 Service

我们假设要将 example.com 域名转发到 192.168.1.100:8080 的服务上。为了实现这个目标,需要创建一个 Deployment 和一个 Service。

示例 Deployment 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  selector:
    matchLabels:
      app: example-app
  replicas: 1
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: example-image
        ports:
        - containerPort: 8080

示例 Service 文件:

apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  selector:
    app: example-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: ClusterIP

这里创建了一个名为 example-app 的 Deployment 和一个名为 example-service 的 Service。Deployment 包含一个示例容器,它监听端口 8080。Service 把端口 80 映射到了容器的 8080 端口。

  1. 安装 Traefik

使用 Helm 安装 Traefik:

helm repo add traefik https://helm.traefik.io/traefik
helm repo update
helm install traefik traefik/traefik

这里使用了 Helm 3,如果你使用的是 Helm 2,则需要将 traefik 参数放到命令行的最后面。

  1. 创建 IngressRoute

根据上述示例,我们需要创建一个 IngressRoute,将域名 example.com 转发到 Service example-service 上。示例 IngressRoute 文件:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: example-ingress
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`example.com`)
      kind: Rule
      services:
        - name: example-service
          port: 80

这里创建了一个名为 example-ingress 的 IngressRoute,它将所有进入 Traefik 的请求都转发到 Service example-service 上。

  1. 配置 DNS

在你的 DNS 服务商中添加一个 A 记录,将域名 example.com 解析到服务器的 IP 地址上。

  1. 访问应用程序

访问 http://example.com 就可以访问到部署在 K3s 中的应用程序了。

完整的 Yaml 文件如下:

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  selector:
    matchLabels:
      app: example-app
  replicas: 1
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: example-image
        ports:
        - containerPort: 8080

Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: example-service
spec:
  selector:
    app: example-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: ClusterIP

IngressRoute.yaml

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: example-ingress
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`example.com`)
      kind: Rule
      services:
        - name: example-service
          port: 80

注意:以上示例中的 example-image 是一个占位符,需要替换为你的实际应用程序镜像名称。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=2088

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?