Skip to content

Instantly share code, notes, and snippets.

@danehans
Created May 12, 2020 20:39
Show Gist options
  • Save danehans/57cbd4f6a953ee0d79219d89eabb823f to your computer and use it in GitHub Desktop.
Save danehans/57cbd4f6a953ee0d79219d89eabb823f to your computer and use it in GitHub Desktop.
k8s_multi_cluster_dns_discovery
# This example exposes service/foo from cluster1 and cluster2 so it's reachable
# by foo.example.com. DNS is used to load-balance traffic to foo.example.com
# across cluster1 and cluster2.
#
# Note that a DNSRecord is not needed to create the multi-cluster FQDN for the
# foo services in both clusters. A controller watches xRoutes and creates a DNS
# record from the xRoute's hostname.
---
# A new resource for managing multi-cluster DNS records.
apiVersion: dns.x-k8s.io/v1alpha1
kind: DNSRecord
metadata:
name: foo
spec:
# dnsName could be an xref to a separate resource for DNS domain, zone, etc. management.
# Creates a CNAME record for foo.example.com -> foo.default.cluster1.svc.cluster.local & foo.default.cluster2.svc.cluster.local
dnsName: foo.example.com
recordTTL: 300
targets:
- resource: Gateway
name: cluster1-gateway
- resource: Gateway
name: cluster2-gateway
# A secret containing cluster-2 kubeconfig. Local cluster assumed if creds is nil.
creds: cluster2-kubeconfig
status:
addresses:
- name: cluster1-gateway
listener: http-listener
address: 1.1.1.1 # Cluster 1 gateway listener ip.
- name: cluster2-gateway
listener: http-listener
address: 2.2.2.2 # Cluster 2 gateway listener ip.
---
# Cluster-1 Gateway config.
kind: Gateway
apiVersion: networking.x-k8s.io/v1alpha1
metadata:
name: cluster1-gateway
spec:
class: my-lb # Note the GatewayClass is not ommitted from this example.
listeners:
- name: http-listener
protocol: HTTP
routes:
routeSelector:
matchLabels:
"app": "foo"
status:
addresses:
- name: http-listener
address: 1.1.1.1
---
# Cluster-1 HTTPRoute that forwards http requests for foo1.example.com to service "foo-service"
kind: HTTPRoute
apiVersion: networking.x-k8s.io/v1alpha1
metadata:
name: http-example
labels:
app: foo
spec:
hosts:
- hostname: foo1.example.com
rules:
action:
forwardTo:
targetRef:
name: foo1-service
---
# Cluster-2 Gateway config.
kind: Gateway
apiVersion: networking.x-k8s.io/v1alpha1
metadata:
name: cluster2-gateway
spec:
class: my-lb # Note the GatewayClass is not ommitted from this example.
listeners:
- name: http-listener
protocol: HTTP
routes:
routeSelector:
matchLabels:
"app": "foo"
status:
addresses:
- name: http-listener
address: 2.2.2.2
---
# Cluster-2 HTTPRoute that forwards http requests for foo2.example.com to service "foo-service"
kind: HTTPRoute
apiVersion: networking.x-k8s.io/v1alpha1
metadata:
name: http-example
labels:
app: foo
spec:
hosts:
- hostname: foo2.example.com
rules:
action:
forwardTo:
targetRef:
name: foo-service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment