6. Networking (13%)

1. Routing Traffic to Pods from Inside and Outside of a Cluster

  1. Create a deployment named myapp that creates 2 replicas for Pods with the image nginx. Expose the container port 80.

  2. Expose the Pods so that requests can be made against the service from inside of the cluster.

  3. Create a temporary Pods using the image busybox and run a wget command against the IP of the service.

  4. Change the service type so that the Pods can be reached from outside of the cluster.

  5. Run a wget command against the service from outside of the cluster.

  6. (Optional) Can you expose the Pods as a service without a deployment?

答案: Create a deployment with 2 replicas first. You should end up with one deployment and two Pods. 参考官网:deployment

$ kubectl run myapp --image=nginx --restart=Always --replicas=2 --port=80
deployment.apps/myapp created
$ kubectl get deployments,pods
NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/myapp   2         2         2            2           59s

NAME                         READY   STATUS    RESTARTS   AGE
pod/myapp-7bc568bfdd-972wg   1/1     Running   0          59s
pod/myapp-7bc568bfdd-l5nmz   1/1     Running   0          59s

Expose the service with the type ClusterIP and the target port 80.

Determine the cluster IP and use it for the wget command.

Turn the type of the service into NodePort to expose it outside of the cluster. Now, the service should expose a port in the 30000 range.

Run a wget or curl command against the service using port 30441. On Docker for Windows/Mac you may have to use localhost or 127.0.0.1 (see issue).

Restricting Access to and from a Pod

Let’s assume we are working on an application stack that defines three different layers: a frontend, a backend and a database. Each of the layers runs in a Pod. You can find the definition in the YAML file app-stack.yaml. The application needs to run in the namespace app-stack.

任务:

  1. Create the required namespace.

  2. Copy the Pod definition to the file app-stack.yaml and create all three Pods. Notice that the namespace has already been defined in the YAML definition.

  3. Create a network policy in the YAML file app-stack-network-policy.yaml.

  4. The network policy should allow incoming traffic from the backend to the database but disallow incoming traffic from the frontend.

  5. Incoming traffic to the database should only be allowed on TCP port 3306 and no other port.

参考官网:networkpolicy Create the namespace

The following definition ensure that all rules are fulfilled.

Create the network policy.

最后更新于