Networking in Kubernetes

76
Google Cloud Platform Networking in Kubernetes K8s Meetup #8 Apr 21, 2016 Minhan Xia Software Engineer github.com/freehan

Transcript of Networking in Kubernetes

Page 1: Networking in Kubernetes

Google Cloud Platform

Networking in Kubernetes K8s Meetup #8

Apr 21, 2016

Minhan XiaSoftware Engineer

github.com/freehan

Page 2: Networking in Kubernetes

Docker Networking

Page 3: Networking in Kubernetes

Docker networking

docker start ...

Page 4: Networking in Kubernetes

Docker networking

docker start ...

Page 5: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

Page 6: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

docker run ...

Page 7: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

Page 8: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

172.16.1.1

vethAQ2IT

eth0

Page 9: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

172.16.1.1

vethAQ2IT

eth0

docker run ...

Page 10: Networking in Kubernetes

Docker networking

docker0 172.16.1.0/24

172.16.1.1

vethAQ2IT

eth0

172.16.1.2

vethS1LUI

eth0

Page 11: Networking in Kubernetes

172.16.1.1

172.16.1.2

Docker networking

172.16.1.1

172.16.1.1

Page 12: Networking in Kubernetes

172.16.1.1

172.16.1.2

Docker networking

172.16.1.1

172.16.1.1

NAT

NAT

NAT

NAT

NAT

Page 13: Networking in Kubernetes

Host ports

A: 172.16.1.1

3306

B: 172.16.1.2

80

9376

11878SNAT

SNATC: 172.16.1.1

8000

Page 14: Networking in Kubernetes

Host ports

A: 172.16.1.1

3306

B: 172.16.1.2

80

9376

11878SNAT

SNATC: 172.16.1.1

8000REJECTED

Page 15: Networking in Kubernetes

Kubernetes Networking

Page 16: Networking in Kubernetes

Kubernetes networking

IPs are routable• vs docker default private IP

Pods can reach each other without NAT• even across nodes

No brokering of port numbers• too complex, why bother?

This is a fundamental requirement• can be L3 routed• can be underlayed (cloud)• can be overlayed (SDN)

Page 17: Networking in Kubernetes

10.1.1.0/24

10.1.1.1

10.1.1.2

Kubernetes networking

10.1.2.0/2410.1.2.1

10.1.3.0/24

10.1.3.1

Page 18: Networking in Kubernetes

10.1.1.0/24

10.1.1.1

10.1.1.2

Kubernetes networking

10.1.2.0/2410.1.2.1

10.1.3.0/24

10.1.3.1?

Page 19: Networking in Kubernetes

Kubernetes networking

On GCE/GKE• GCE Advanced Routes (program the fabric)• “Everything to 10.1.1.0/24, send to this VM”

Plenty of other ways• AWS: Route Tables• Weave• Calico• Flannel• OVS• OpenContrail• Cisco Contiv• Others...

Page 20: Networking in Kubernetes

Kubernetes networking

On GCE/GKE• GCE Advanced Routes (program the fabric)• “Everything to 10.1.1.0/24, send to this VM”

Plenty of other ways• AWS: Route Tables• Weave• Calico• Flannel• OVS• OpenContrail• Cisco Contiv• Others...

Page 21: Networking in Kubernetes

Kubernetes networking

On GCE/GKE• GCE Advanced Routes (program the fabric)• “Everything to 10.1.1.0/24, send to this VM”

Plenty of other ways• AWS: Route Tables• Weave• Calico• Flannel• OVS• OpenContrail• Cisco Contiv• Others...

Page 22: Networking in Kubernetes

Pods

Page 23: Networking in Kubernetes

Pods

Small group of containers & volumes

Tightly coupled

The atom of scheduling & placement

Shared namespaces• share IP address & localhost• share IPC, etc.

Managed lifecycle• bound to a node, restart in place• can die, cannot be reborn with same ID

Example: data puller & web server

ConsumersContent Manager

File Puller

Web Server

Volume

Pod

Page 24: Networking in Kubernetes

Pods

Small group of containers & volumes

Tightly coupled

The atom of scheduling & placement

Shared namespaces• share IP address & localhost• share IPC, etc.

Managed lifecycle• bound to a node, restart in place• can die, cannot be reborn with same ID

Example: data puller & web server

10.1.1.2

Page 25: Networking in Kubernetes

Pods

Small group of containers & volumes

Tightly coupled

The atom of scheduling & placement

Shared namespace• share IP address & localhost• share IPC, etc.

Managed lifecycle• bound to a node, restart in place• can die, cannot be reborn with same ID

Example: data puller & web server

c1

--net=container:infra--ipc=container:infra

infra

10.1.1.2

c2

--net=container:infra--ipc=container:infra

Page 26: Networking in Kubernetes

Services

Page 27: Networking in Kubernetes

Services

A group of pods that work together• grouped by a selector

Defines access policy• “load balanced” or “headless”

Gets a stable virtual IP and port• sometimes called the service portal• also a DNS name

VIP is managed by kube-proxy• watches all services• updates iptables when backends change

Hides complexity - ideal for non-native apps

Client

Virtual IP

Page 28: Networking in Kubernetes

kube-proxy

Page 29: Networking in Kubernetes

iptables kube-proxy

Page 30: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

Page 31: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

watch

services & endpoints

Page 32: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

kubectl run ...

watch

Page 33: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

schedule

watch

Page 34: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

watch

kubectl expose ...

Page 35: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

new service!

update

Page 36: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

watch

configure

Page 37: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

watch

VIP

Page 38: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

new endpoints!

update

VIP

Page 39: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

configure

Page 40: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

Page 41: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

Client

Page 42: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

Client

Page 43: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

Client

Page 44: Networking in Kubernetes

iptables kube-proxy

iptables

kube-proxy apiserverNode X

VIP

watch

Client

Page 45: Networking in Kubernetes

Services are just an abstraction• Only requirement: route (and maybe load

balance) a virtual IP to a set of backends.

Kube-proxy is an implementation• Kube-proxy watches apiserver.• iptables is re-configured on changes.

There could be other ways• Userspace, iptables, IP Virtual Servers?

Services

Page 46: Networking in Kubernetes

DNS

Run SkyDNS as a pod in the cluster• kube2sky bridges Kubernetes API -> SkyDNS• Tell kubelets about it (static service IP)

Strictly optional, but practically required• LOTS of things depend on it• Probably will become more integrated

Or plug in your own!

kubernetes

kubernetes.default

kubernetes.default.svc.cluster.local

foo.my-namespace.svc.cluster.local

Page 47: Networking in Kubernetes

DNS

Run SkyDNS as a pod in the cluster• kube2sky bridges Kubernetes API -> SkyDNS• Tell kubelets about it (static service IP)

Strictly optional, but practically required• LOTS of things depend on it• Probably will become more integrated

Or plug in your own! apiserverwatch

etcd

kube-dns-qxin

kube2skyskyDNS

Page 48: Networking in Kubernetes

DNS

Run SkyDNS as a pod in the cluster• kube2sky bridges Kubernetes API -> SkyDNS• Tell kubelets about it (static service IP)

Strictly optional, but practically required• LOTS of things depend on it• Probably will become more integrated

Or plug in your own!

nameserver 10.0.0.10...

/etc/resolv.conf

apiserverwatch

etcd

kube-dns-qxin

kube2skyskyDNS

Page 49: Networking in Kubernetes

DNS

Run SkyDNS as a pod in the cluster• kube2sky bridges Kubernetes API -> SkyDNS• Tell kubelets about it (static service IP)

Strictly optional, but practically required• LOTS of things depend on it• Probably will become more integrated

Or plug in your own!

nameserver 10.0.0.10...

/etc/resolv.conf

apiserverwatch

etcd

kube-dns-qxin

kube2skyskyDNS10.0.0.10

Page 50: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client

Page 51: Networking in Kubernetes

What happens when I...

$ curl foo.my-namespace

Putting it Together

nameserver 10.0.0.10...

/etc/resolv.conf

Client10.1.0.1

Page 52: Networking in Kubernetes

10.1.0.1

Putting it Together

What happens when I...

$ curl foo.my-namespace

etcd

kube-dns-qxin

kube2skyskyDNS10.0.0.10

foo.my-namespace?

Client

Page 53: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

etcd

kube-dns-qxin

kube2skyskyDNS10.0.0.10

10.0.123.45

Client10.1.0.1

Page 54: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

10.0.123.45Client10.1.0.1

Page 55: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

10.1.0.1

Page 56: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1.0.1

Page 57: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

Page 58: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1

.3.1

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

Page 59: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1

.3.1

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

10.1.3.0/24 -> Node X

Page 60: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1

.3.1

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

Page 61: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client VIP10.0.123.45

iptables

10.1

.3.1

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

Hello World!

Page 62: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client

iptables

Hello World!

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

10.1

.0.1

Page 63: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client

iptables

Hello World!

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

10.1.0.0/24 -> Node Y

10.1

.0.1

Page 64: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Client

iptables

Hello World!

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

10.1.0.0/24 -> Node Y

10.1

.0.1

Page 65: Networking in Kubernetes

Putting it Together

What happens when I...

$ curl foo.my-namespace

Hello World!

Client

iptables

Hello World!

10.1.0.1

10.1.0.6 10.1.3.1 10.1.6.3

10.1.0.0/24 -> Node Y

10.1

.0.1

Page 66: Networking in Kubernetes

What about external?

Page 67: Networking in Kubernetes

External Services

Services IPs are only available inside the cluster

Need to receive traffic from “the outside world”

Builtin: Service “type”• nodePort: expose on a port on every node• loadBalancer: provision a cloud load-balancer

DiY load-balancer solutions• socat (for nodePort remapping)• haproxy• nginx

Page 68: Networking in Kubernetes

The Bleeding Edge

Page 69: Networking in Kubernetes

Ingress (L7)

Services are assumed L3/L4

Lots of apps want HTTP/HTTPS

Ingress maps incoming traffic to backend services

• by HTTP host headers• by HTTP URL paths

HAProxy and GCE implementationsWith SSL now!

Status: BETA in Kubernetes v1.2

URL Map

Client

Page 70: Networking in Kubernetes

Ingress (L7)

Services are assumed L3/L4

Lots of apps want HTTP/HTTPS

Ingress maps incoming traffic to backend services

• by HTTP host headers• by HTTP URL paths

HAProxy and GCE implementationsWith SSL now!

Status: BETA in Kubernetes v1.2

URL Map

Client

api.company.com

api.company.com/foo api.company.com/bar

othercompany.com/*

Page 71: Networking in Kubernetes

Network Plugins

Page 72: Networking in Kubernetes

Network Plugins

Introduced in Kubernetes v1.0• VERY experimental

Uses CNI (CoreOS) in v1.1• Simple exec interface• Not using Docker libnetwork

• but can defer to Docker for networking

Cluster admins can customize their installs• DHCP, MACVLAN, Flannel, custom

net

Plugin

Plugin

Plugin

Page 73: Networking in Kubernetes

Google Cloud Platform

Network Isolation

Page 74: Networking in Kubernetes

Google Cloud Platform

Network Isolation

Describe the DAG of your app, enforce it in the network

Restrict Pod-to-Pod traffic or across Namespaces

Designed by the network SIG• implementations for Calico, OpenShift, Romana,

OpenContrail (so far)

Status: Alpha in v1.2, expect beta in v1.3

Page 75: Networking in Kubernetes

Kubernetes is Open- open community- open design- open source- open to ideas

Networking is Hard- help guide us!

http://kubernetes.iohttps://github.com/kubernetes/kubernetes

slack: kubernetes twitter: @kubernetesio

Page 76: Networking in Kubernetes

iptables kube-proxy Mean Latencycontrib/for-tests/netperf-tester --number=1000

Mean Latency Microseconds

iptables kube-proxy

legacy kube-proxy