This is a small container image which allows you to define outbound and inbound connections via domains, rather than by IP addresses (as you would with say a NetworkPolicy).
The main reason you might want to do this is because you might not know all the IP addresses or ranges of one or more DNS addresses that your container should talk to, or you don't feel like maintaining a list of these in a NetworkPolicy, and don't feel like deploying a Service Mesh for whatever reason.
It's particularly useful for whitelisting egress connectivity by domain, rather than by IP.
See the source at https://github.com/nebev/k8s-dynamic-iptables
This works as an initContainer and sets iptables rules for you. See IPTables in Istio for more information on how this works.
The container accepts the following arguments:
hosts_csv (required) - This represents the outbound hosts you'd like to whitelist, separated by commaslistening_ports_csv - This represents the inbound ports you'd like to allow.extra_whitelist_ips_csv - This is a simple list of extra IP(range)-based whitelists you'd like.With the hosts_csv list of hosts, the container will look up the IPs using dig and put them into your whitelist.
This is a very simple (stupid) DNS lookup with IPTables. Some (but not all) ways this could go wrong include:
If you can, you really should look at NetworkPolicy definitions instead of doing this.
In this example, we define a regular deployment (curl) which simply has an initContainer with this image.
apiVersion: apps/v1
kind: Deployment
metadata:
name: curl-deployment
namespace: test
labels:
app: curl
spec:
replicas: 1
selector:
matchLabels:
app: curl
template:
metadata:
namespace: test
labels:
app: curl
spec:
containers:
# The Application we want to run
- name: curl
image: alpine/curl
command: ["/bin/sh"]
args: ["-c", "sleep infinity"]
ports:
- containerPort: 3000
# InitContainers initialise IP Tables to redirect traffic to Envoy
initContainers:
image: nebev/k8s-dynamic-iptables:latest # init container
name: iptables-init
env:
- name: hosts_csv
value: "svc.cluster.local,cluster.local,dl-cdn.alpinelinux.org,deb.debian.org,googleapis.com,storage.googleapis.com"
- name: listening_ports_csv
value: "3000"
- name: extra_whitelist_ips_csv
value "192.168.0.0/24"
securityContext:
capabilities:
add:
- NET_ADMIN # Otherwise we can't update iptables
If we shell into the container, and run some commands:
/ #: curl https://google.com
<TIMES OUT> ^C
/ #: curl https://googleapis.com
<!DOCTYPE html>
<html lang=en>
...
We can see that one resolved, and the other didn't, as per our spec.
Content type
Image
Digest
sha256:fd6c4e4cc…
Size
7.6 MB
Last updated
over 3 years ago
docker pull nebev/k8s-dynamic-iptables