github.com/imran-kn/cilium-fork@v1.6.9/Documentation/gettingstarted/dns.rst (about) 1 .. only:: not (epub or latex or html) 2 3 WARNING: You are looking at unreleased Cilium documentation. 4 Please use the official rendered version released here: 5 http://docs.cilium.io 6 7 .. _gs_dns: 8 9 **************************************************** 10 Locking down external access with DNS-based policies 11 **************************************************** 12 13 This document serves as an introduction for using Cilium to enforce DNS-based 14 security policies for Kubernetes pods. 15 16 .. include:: gsg_requirements.rst 17 18 Deploy the Demo Application 19 =========================== 20 21 DNS-based policies are very useful for controlling access to services running outside the Kubernetes cluster. DNS acts as a persistent service identifier for both external services provided by AWS, Google, Twilio, Stripe, etc., and internal services such as database clusters running in private subnets outside Kubernetes. CIDR or IP-based policies are cumbersome and hard to maintain as the IPs associated with external services can change frequently. The Cilium DNS-based policies provide an easy mechanism to specify access control while Cilium manages the harder aspects of tracking DNS to IP mapping. 22 23 In this guide we will learn about: 24 25 - Controlling egress access to services outside the cluster using DNS-based policies 26 - Using patterns (or wildcards) to whitelist a subset of DNS domains 27 - Combining DNS, port and L7 rules for restricting access to external service 28 29 In line with our Star Wars theme examples, we will use a simple scenario where the empire's ``mediabot`` pods need access to Twitter for managing the empire's tweets. The pods shouldn't have access to any other external service. 30 31 .. parsed-literal:: 32 33 $ kubectl create -f \ |SCM_WEB|\/examples/kubernetes-dns/dns-sw-app.yaml 34 $ kubectl get po 35 NAME READY STATUS RESTARTS AGE 36 pod/mediabot 1/1 Running 0 14s 37 38 39 Apply DNS Egress Policy 40 ======================= 41 42 The following Cilium network policy allows ``mediabot`` pods to only access ``api.twitter.com``. 43 44 .. literalinclude:: ../../examples/kubernetes-dns/dns-matchname.yaml 45 46 Let's take a closer look at the policy: 47 48 * The first egress section uses ``toFQDNs: matchName`` specification to allow egress to ``api.twitter.com``. The destination DNS should match exactly the name specified in the rule. The ``endpointSelector`` allows only pods with labels ``class: mediabot, org:empire`` to have the egress access. 49 * The second egress section allows ``mediabot`` pods to access ``kube-dns`` service. Note that ``rules: dns`` instructs Cilium to inspect and allow DNS lookups matching specified patterns. In this case, inspect and allow all DNS queries. 50 51 Note that with this policy the ``mediabot`` doesn't have access to any internal cluster service other than ``kube-dns``. Refer to :ref:`Network Policy` to learn more about policies for controlling access to internal cluster services. 52 53 Let's apply the policy: 54 55 .. parsed-literal:: 56 57 $ kubectl create -f \ |SCM_WEB|\/examples/kubernetes-dns/dns-matchname.yaml 58 59 Testing the policy, we see that ``mediabot`` has access to ``api.twitter.com`` but doesn't have access to any other external service, e.g., ``help.twitter.com``. 60 61 .. parsed-literal:: 62 63 $ kubectl exec -it mediabot -- curl -sL https://api.twitter.com 64 ... 65 ... 66 67 $ kubectl exec -it mediabot -- curl -sL https://help.twitter.com 68 ^C 69 70 DNS Policies Using Patterns 71 =========================== 72 73 The above policy controlled DNS access based on exact match of the DNS domain name. Often, it is required to allow access to a subset of domains. Let's say, in the above example, ``mediabot`` pods need access to any Twitter sub-domain, e.g., the pattern ``*.twitter.com``. We can achieve this easily by changing the ``toFQDN`` rule to use ``matchPattern`` instead of ``matchName``. 74 75 .. literalinclude:: ../../examples/kubernetes-dns/dns-pattern.yaml 76 77 .. parsed-literal:: 78 79 $ kubectl apply -f \ |SCM_WEB|\/examples/kubernetes-dns/dns-pattern.yaml 80 81 Test that ``mediabot`` has access to multiple Twitter services for which the DNS matches the pattern ``*.twitter.com``. It is important to note and test that this doesn't allow access to ``twitter.com`` because the ``*.`` in the pattern requires one subdomain to be present in the DNS name. You can simply add more ``matchName`` and ``matchPattern`` clauses to extend the access. 82 (See :ref:`DNS based` policies to learn more about specifying DNS rules using patterns and names.) 83 84 .. parsed-literal:: 85 86 $ kubectl exec -it mediabot -- curl -sL https://help.twitter.com 87 ... 88 89 $ kubectl exec -it mediabot -- curl -sL https://about.twitter.com 90 ... 91 92 $ kubectl exec -it mediabot -- curl -sL https://twitter.com 93 ^C 94 95 Combining DNS, Port and L7 Rules 96 ================================ 97 98 The DNS-based policies can be combined with port (L4) and API (L7) rules to further restrict the access. In our example, we will restrict ``mediabot`` pods to access Twitter services only on ports ``443``. The ``toPorts`` section in the policy below achieves the port-based restrictions along with the DNS-based policies. 99 100 .. literalinclude:: ../../examples/kubernetes-dns/dns-port.yaml 101 102 .. parsed-literal:: 103 104 $ kubectl apply -f \ |SCM_WEB|\/examples/kubernetes-dns/dns-port.yaml 105 106 Testing, the access to ``https://help.twitter.com`` on port ``443`` will succeed but the access to ``http://help.twitter.com`` on port ``80`` will be denied. 107 108 .. parsed-literal:: 109 110 $ kubectl exec -it mediabot -- curl https://help.twitter.com 111 ... 112 113 $ kubectl exec -it mediabot -- curl http://help.twitter.com 114 ^C 115 116 Refer to :ref:`l4_policy` and :ref:`l7_policy` to learn more about Cilium L4 and L7 network policies. 117 118 Clean-up 119 ======== 120 121 .. parsed-literal:: 122 123 $ kubectl delete -f \ |SCM_WEB|\/examples/kubernetes-dns/dns-sw-app.yaml 124 $ kubectl delete cnp fqdn