简体   繁体   中英

How do I forward port 80 to a service port 3000 using Traefik on K3s?

This is about Traefik not about the way it works with K3s Kubernetes generally, so please don't give me a general K8s answer.

I have a simple k3s deployment and service that looks like this...

apiVersion: v1
kind: Service
metadata:
  labels:
    app: hello-express
  name: app-tier
spec:
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000
  selector:
    tier: app

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-express-deployment
  labels:
    app: hello-express
    tier: app
spec:
  replicas: 1
  selector:
    matchLabels:
      tier: app
  template:
    metadata:
      labels:
        app: hello-express
        tier: app
    spec:
      containers:
        - name: server
          image: partyk1d24/hello-express:latest
          ports:
            - containerPort: 3000

I can then access the application using the external ip and port 3000. Now I would like to change that port from 3000 to 80. Apparently this is controlled locally on K3s using Traefik . I tried the following while looking here ...

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-express-ingress
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: app-tier
                port:
                  number: 80

But when I try to go to the site I get...

curl 192.168.X.XXX      
Service Unavailable% 

The Blog is a little old so I am sure I am doing something wrong can someone help me identify it?

You should change the port of service to 80.

  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

Keep the target port as 3000.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM