github.com/imran-kn/cilium-fork@v1.6.9/Documentation/gettingstarted/aws.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  ***********************************************
     8  Locking down external access using AWS metadata
     9  ***********************************************
    10  
    11  This document serves as an introduction to using Cilium to enforce policies
    12  based on AWS instances metadata. It is a detailed walk-through of getting a
    13  single-node Cilium environment running on your machine. It is designed to take
    14  15-30 minutes with some experience running Kubernetes.
    15  
    16  
    17  Setup Cilium
    18  ============
    19  
    20  This guide will work with any approach to installing Cilium, including minikube,
    21  as long as the cilium-operator pod in the deployment can reach the AWS API server
    22  However, since the most common use of this mechanism is for Kubernetes clusters
    23  running in AWS, we recommend trying it out along with the guide: :ref:`k8s_install_eks` .
    24  
    25  Create AWS secrets
    26  ==================
    27  
    28  Before installing Cilium, a new Kubernetes Secret with the AWS Tokens needs to
    29  be added to your Kubernetes cluster. This Secret will allow Cilium to gather
    30  information from the AWS API which is needed to implement ToGroups policies.
    31  
    32  AWS Access keys and IAM role
    33  ------------------------------
    34  
    35  To create a new access token the `following guide can be used
    36  <https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-quick-configuration>`_.
    37  These keys need to have certain permissions set:
    38  
    39  .. code-block:: javascript
    40  
    41      {
    42          "Version": "2012-10-17",
    43          "Statement": [
    44              {
    45                  "Effect": "Allow",
    46                  "Action": "ec2:Describe*",
    47                  "Resource": "*"
    48              }
    49          ]
    50      }
    51  
    52  As soon as you have the access tokens, the following secret needs to be added,
    53  with each empty string replaced by the associated value as a base64-encoded string:
    54  
    55  
    56  .. code-block:: yaml
    57      :name: cilium-secret.yaml
    58  
    59      apiVersion: v1
    60      kind: Secret
    61      metadata:
    62        name: cilium-aws
    63        namespace: kube-system
    64      type: Opaque
    65      data:
    66        AWS_ACCESS_KEY_ID: ""
    67        AWS_SECRET_ACCESS_KEY: ""
    68        AWS_DEFAULT_REGION: ""
    69  
    70  The base64 command line utility can be used to generate each value, for example:
    71  
    72  .. parsed-literal::
    73  
    74      $ echo -n "eu-west-1"  | base64
    75      ZXUtd2VzdC0x
    76  
    77  This secret stores the AWS credentials, which will be used to connect the AWS
    78  API.
    79  
    80  .. parsed-literal::
    81  
    82      $ kubectl create -f cilium-secret.yaml
    83  
    84  To validate that the credentials are correct, the following pod can be created
    85  for debugging purposes:
    86  
    87  .. code-block:: yaml
    88  
    89      apiVersion: v1
    90      kind: Pod
    91      metadata:
    92        name: testing-aws-pod
    93        namespace: kube-system
    94      spec:
    95        containers:
    96        - name: aws-cli
    97          image: mesosphere/aws-cli
    98          command: ['sh', '-c', 'sleep 3600']
    99          env:
   100            - name: AWS_ACCESS_KEY_ID
   101              valueFrom:
   102                secretKeyRef:
   103                  name: cilium-aws
   104                  key: AWS_ACCESS_KEY_ID
   105                  optional: true
   106            - name: AWS_SECRET_ACCESS_KEY
   107              valueFrom:
   108                secretKeyRef:
   109                  name: cilium-aws
   110                  key: AWS_SECRET_ACCESS_KEY
   111                  optional: true
   112            - name: AWS_DEFAULT_REGION
   113              valueFrom:
   114                secretKeyRef:
   115                  name: cilium-aws
   116                  key: AWS_DEFAULT_REGION
   117                  optional: true
   118  
   119  To list all of the available AWS instances, the following command can be used:
   120  
   121  .. parsed-literal::
   122  
   123     $ kubectl  -n kube-system exec -ti testing-aws-pod -- aws ec2 describe-instances
   124  
   125  Once the secret has been created and validated, the cilium-operator pod must be
   126  restarted in order to pick up the credentials in the secret.
   127  To do this, identify and delete the existing cilium-operator pod, which will be
   128  recreated automatically:
   129  
   130  .. parsed-literal::
   131  
   132      $ kubectl get pods -l name=cilium-operator -n kube-system
   133      NAME                              READY   STATUS    RESTARTS   AGE
   134      cilium-operator-7c9d69f7c-97vqx   1/1     Running   0          36h
   135  
   136      $ kubectl delete pod cilium-operator-7c9d69f7c-97vqx
   137  
   138  
   139  
   140  It is important for this demo that ``coredns`` is working correctly. To know the
   141  status of ``coredns`` you can run the following command:
   142  ::
   143  
   144      $ kubectl get deployment kube-dns -n kube-system
   145      NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
   146      coredns    2         2         2            2           13h
   147  
   148  Where at least one pod should be available.
   149  
   150  Configure AWS Security Groups
   151  =============================
   152  
   153  Cilium's AWS Metadata filtering capability enables explicit whitelisting
   154  of communication between a subset of pods (identified by Kubernetes labels)
   155  with a set of destination EC2 VMs (identified by membership in an AWS security group).
   156  
   157  In this example, the destination EC2 VMs are a member of a single AWS security
   158  group ('sg-0f2146100a88d03c3') and pods with label class=xwing should
   159  only be able to make connections outside the cluster to the destination
   160  VMs in that security group.
   161  
   162  To enable this, the VMs acting as Kubernetes worker nodes must be able to
   163  send traffic to the destination VMs that are being accessed by pods.  One approach
   164  for achieving this is to put all Kubernetes worker VMs in a single 'k8s-worker'
   165  security group, and then ensure that any security group that is referenced in a
   166  Cilium toGroups policy has an allow all ingress rule (all ports) for connections from the
   167  'k8s-worker' security group.  Cilium filtering will then ensure that the only pods allowed
   168  by policy can reach the destination VMs.
   169  
   170  Create a sample policy
   171  ======================
   172  
   173  Deploy a demo application:
   174  ----------------------------
   175  
   176  In this case we're going to use a demo application that is used in other guides.
   177  These manifests will create three microservices applications: *deathstar*,
   178  *tiefighter*, and *xwing*. In this case, we are only going to use our *xwing*
   179  microservice to secure communications to existing AWS instances.
   180  
   181  .. parsed-literal::
   182  
   183      $ kubectl create -f \ |SCM_WEB|\/examples/minikube/http-sw-app.yaml
   184      service "deathstar" created
   185      deployment "deathstar" created
   186      deployment "tiefighter" created
   187      deployment "xwing" created
   188  
   189  
   190  Kubernetes will deploy the pods and service in the background. Running ``kubectl
   191  get pods,svc`` will inform you about the progress of the operation.  Each pod
   192  will go through several states until it reaches ``Running`` at which point the
   193  pod is ready.
   194  
   195  ::
   196  
   197      $ kubectl get pods,svc
   198      NAME                             READY     STATUS    RESTARTS   AGE
   199      po/deathstar-76995f4687-2mxb2    1/1       Running   0          1m
   200      po/deathstar-76995f4687-xbgnl    1/1       Running   0          1m
   201      po/tiefighter                    1/1       Running   0          1m
   202      po/xwing                         1/1       Running   0          1m
   203  
   204      NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
   205      svc/deathstar    ClusterIP   10.109.254.198   <none>        80/TCP    3h
   206      svc/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   3h
   207  
   208  Policy Language:
   209  -----------------
   210  
   211  **ToGroups** rules can be used to define policy in relation to cloud providers, like AWS.
   212  
   213  .. code-block:: yaml
   214  
   215      ---
   216      kind: CiliumNetworkPolicy
   217      apiVersion: cilium.io/v2
   218      metadata:
   219        name: to-groups-sample
   220        namespace: default
   221      spec:
   222        endpointSelector:
   223          matchLabels:
   224            org: alliance
   225            class: xwing
   226        egress:
   227        - toPorts:
   228          - ports:
   229            - port: '80'
   230              protocol: TCP
   231          toGroups:
   232          - aws:
   233              securityGroupsIds:
   234              - 'sg-0f2146100a88d03c3'
   235  
   236  This policy allows traffic from pod *xwing* to any AWS instance that is in
   237  the security group with ID ``sg-0f2146100a88d03c3``.
   238  
   239  Validate that derived policy is in place
   240  ----------------------------------------
   241  
   242  Every time that a new policy with ToGroups rules is added, an equivalent policy
   243  (also called "derivative policy"), will be created. This policy will contain the
   244  set of CIDRs that correspond to the specification in ToGroups, e.g., the IPs of
   245  all instances that are part of a specified security group. The list of IPs will
   246  be updated periodically.
   247  
   248  .. parsed-literal::
   249  
   250      $ kubectl get cnp
   251      NAME                                                             AGE
   252      to-groups-sample                                                 11s
   253      to-groups-sample-togroups-044ba7d1-f491-11e8-ad2e-080027d2d952   10s
   254  
   255  Eventually, the derivative policy will contain IPs in the ToCIDR section:
   256  
   257  .. parsed-literal::
   258  
   259     $ kubectl get cnp to-groups-sample-togroups-044ba7d1-f491-11e8-ad2e-080027d2d952
   260  
   261  
   262  .. code-block:: yaml
   263  
   264      apiVersion: cilium.io/v2
   265      kind: CiliumNetworkPolicy
   266      metadata:
   267        creationTimestamp: 2018-11-30T11:13:52Z
   268        generation: 1
   269        labels:
   270          io.cilium.network.policy.kind: derivative
   271          io.cilium.network.policy.parent.uuid: 044ba7d1-f491-11e8-ad2e-080027d2d952
   272        name: to-groups-sample-togroups-044ba7d1-f491-11e8-ad2e-080027d2d952
   273        namespace: default
   274        ownerReferences:
   275        - apiVersion: cilium.io/v2
   276          blockOwnerDeletion: true
   277          kind: CiliumNetworkPolicy
   278          name: to-groups-sample
   279          uid: 044ba7d1-f491-11e8-ad2e-080027d2d952
   280        resourceVersion: "34853"
   281        selfLink: /apis/cilium.io/v2/namespaces/default/ciliumnetworkpolicies/to-groups-sample-togroups-044ba7d1-f491-11e8-ad2e-080027d2d952
   282        uid: 04b289ba-f491-11e8-ad2e-080027d2d952
   283      specs:
   284      - egress:
   285        - toCIDRSet:
   286          - cidr: 34.254.113.42/32
   287          - cidr: 172.31.44.160/32
   288          toPorts:
   289          - ports:
   290            - port: "80"
   291              protocol: TCP
   292        endpointSelector:
   293          matchLabels:
   294            any:class: xwing
   295            any:org: alliance
   296            k8s:io.kubernetes.pod.namespace: default
   297        labels:
   298        - key: io.cilium.k8s.policy.name
   299          source: k8s
   300          value: to-groups-sample
   301        - key: io.cilium.k8s.policy.uid
   302          source: k8s
   303          value: 044ba7d1-f491-11e8-ad2e-080027d2d952
   304        - key: io.cilium.k8s.policy.namespace
   305          source: k8s
   306          value: default
   307        - key: io.cilium.k8s.policy.derived-from
   308          source: k8s
   309          value: CiliumNetworkPolicy
   310      status:
   311        nodes:
   312          k8s1:
   313            enforcing: true
   314            lastUpdated: 2018-11-30T11:28:03.907678888Z
   315            localPolicyRevision: 28
   316            ok: true
   317  
   318  The derivative rule should contain the following information:
   319  
   320  - *metadata.OwnerReferences*: that contains the information about the ToGroups
   321    policy.
   322  
   323  - *specs.Egress.ToCIDRSet*:  the list of private and public IPs of the instances
   324    that correspond to the spec of the parent policy.
   325  
   326  - *status*: whether or not the policy is enforced yet, and when the policy was
   327    last updated.
   328  
   329  The Cilium Endpoint status for the *xwing* should have policy enforcement
   330  enabled only for egress connectivity:
   331  
   332  .. parsed-literal::
   333  
   334      $ kubectl get cep xwing
   335      NAME    ENDPOINT ID   IDENTITY ID   POLICY ENFORCEMENT   ENDPOINT STATE   IPV4         IPV6
   336      xwing   23453         63929         egress               ready            10.10.0.95   f00d::a0a:0:0:22cf
   337  
   338  In this example, *xwing* pod can only connect to ``34.254.113.42/32`` and
   339  ``172.31.44.160/32`` and connectivity to other IP will be denied.