2017-03-16 23:06:59 +00:00
---
2018-02-18 19:29:37 +00:00
reviewers:
2017-03-16 23:06:59 +00:00
- bprashanth
- davidopp
2017-06-08 20:29:52 +00:00
title: Configure Your Cloud Provider's Firewalls
2018-06-22 18:20:04 +00:00
content_template: templates/task
2018-05-20 03:40:51 +00:00
weight: 90
2017-03-16 23:06:59 +00:00
---
2018-06-26 22:16:52 +00:00
{{% capture overview %}}
2018-06-22 18:20:04 +00:00
2017-03-16 23:06:59 +00:00
Many cloud providers (e.g. Google Compute Engine) define firewalls that help prevent inadvertent
exposure to the internet. When exposing a service to the external world, you may need to open up
one or more ports in these firewalls to serve traffic. This document describes this process, as
well as any provider specific details that may be necessary.
2018-06-22 18:20:04 +00:00
{{% /capture %}}
{{% capture prerequisites %}}
{{< include " task-tutorial-prereqs . md " > }} {{< version-check > }}
{{% /capture %}}
{{% capture steps %}}
## Restrict Access For LoadBalancer Service
2017-03-16 23:06:59 +00:00
When using a Service with `spec.type: LoadBalancer` , you can specify the IP ranges that are allowed to access the load balancer
by using `spec.loadBalancerSourceRanges` . This field takes a list of IP CIDR ranges, which Kubernetes will use to configure firewall exceptions.
2019-01-23 16:55:05 +00:00
This feature is currently supported on Google Compute Engine, Google Kubernetes Engine, AWS Elastic Kubernetes Service, Azure Kubernetes Service, and IBM Cloud Kubernetes Service. This field will be ignored if the cloud provider does not support the feature.
2017-03-16 23:06:59 +00:00
2017-08-15 09:00:01 +00:00
Assuming 10.0.0.0/8 is the internal subnet. In the following example, a load balancer will be created that is only accessible to cluster internal IPs.
2017-03-29 17:04:51 +00:00
This will not allow clients from outside of your Kubernetes cluster to access the load balancer.
2017-03-16 23:06:59 +00:00
```yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
ports:
2017-08-21 23:28:12 +00:00
- port: 8765
targetPort: 9376
2017-03-16 23:06:59 +00:00
selector:
app: example
type: LoadBalancer
loadBalancerSourceRanges:
- 10.0.0.0/8
```
2017-03-29 17:04:51 +00:00
In the following example, a load balancer will be created that is only accessible to clients with IP addresses from 130.211.204.1 and 130.211.204.2.
2017-03-16 23:06:59 +00:00
```yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
ports:
2017-08-21 23:28:12 +00:00
- port: 8765
targetPort: 9376
2017-03-16 23:06:59 +00:00
selector:
app: example
type: LoadBalancer
loadBalancerSourceRanges:
- 130.211.204.1/32
- 130.211.204.2/32
```
2018-06-22 18:20:04 +00:00
## Google Compute Engine
2017-03-16 23:06:59 +00:00
When using a Service with `spec.type: LoadBalancer` , the firewall will be
opened automatically. When using `spec.type: NodePort` , however, the firewall
is *not* opened by default.
Google Compute Engine firewalls are documented [elsewhere ](https://cloud.google.com/compute/docs/networking#firewalls_1 ).
You can add a firewall with the `gcloud` command line tool:
```shell
2017-10-03 02:08:52 +00:00
gcloud compute firewall-rules create my-rule --allow=tcp:< port >
2017-03-16 23:06:59 +00:00
```
2018-06-22 18:20:04 +00:00
{{< note > }}
2018-11-06 19:33:04 +00:00
GCE firewalls are defined per-vm, rather than per-ip address. This means that
when you open a firewall for a service's ports, anything that serves on that
port on that VM's host IP address may potentially serve traffic. Note that this
is not a problem for other Kubernetes services, as they listen on IP addresses
that are different than the host node's external IP address.
2017-03-16 23:06:59 +00:00
Consider:
* You create a Service with an external load balancer (IP Address 1.2.3.4)
and port 80
* You open the firewall for port 80 for all nodes in your cluster, so that
the external Service actually can deliver packets to your Service
* You start an nginx server, running on port 80 on the host virtual machine
2017-08-15 07:29:38 +00:00
(IP Address 2.3.4.5). This nginx is also exposed to the internet on
2017-03-16 23:06:59 +00:00
the VM's external IP address.
Consequently, please be careful when opening firewalls in Google Compute Engine
2017-11-13 20:02:31 +00:00
or Google Kubernetes Engine. You may accidentally be exposing other services to
2017-03-16 23:06:59 +00:00
the wilds of the internet.
2018-06-22 18:20:04 +00:00
{{< / note > }}
2017-03-16 23:06:59 +00:00
2018-06-22 18:20:04 +00:00
{{% /capture %}}