Skip to content

Instantly share code, notes, and snippets.

@dfang
Last active August 1, 2019 16:00
Show Gist options
  • Save dfang/4955be38a122d354d52e160ce88697f8 to your computer and use it in GitHub Desktop.
Save dfang/4955be38a122d354d52e160ce88697f8 to your computer and use it in GitHub Desktop.
simple demo on routing cross namespace by traefik ingress, docker for mac

create 2 services in 2 namespaces, and expose them

kubectl create ns t1
kubectl create ns t2
kubectl run hello-nginx --image=nginx --port=80 -n t1
kubectl run hello-nginx --image=nginx --port=80 -n t2
kubectl expose deployment hello-nginx --port=80 -n t1
kubectl expose deployment hello-nginx --port=80 -n t2

install traefik using helm charts

helm init
helm install stable/traefik --set dashboard.enabled=true,dashboard.domain=dashboard.localhost

test

  1. port forwarding
kubectl port-forward hello-nginx-64874584f5-k7zpn -n t1 7777:80

open localhost:7777

or

kubectl apply -f https://k8s.io/examples/admin/dns/busybox.yaml kubectl exec -ti busybox -- nslookup t1.svc.cluster.local

create svc and ingress rules

svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-1
  namespace: default
spec:
  type: ExternalName
  externalName: hello-nginx.t1.svc.cluster.local
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  name: service-2
  namespace: default
spec:
  type: ExternalName
  externalName: hello-nginx.t2.svc.cluster.local
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefixStrip
  name: ingress-to-other-ns
  namespace: default
spec:
  rules:
  - host: t1.localhost
    http:
      paths:
      - backend:
          serviceName: service-1
          servicePort: 80
        path: /
  - host: t2.localhost
    http:
      paths:
      - backend:
          serviceName: service-2
          servicePort: 80
        path: /

traefik ui url: dashboard.localhost

t1: t1.localhost t2: t2.localhost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment