6. Networking (13%)
1. Routing Traffic to Pods from Inside and Outside of a Cluster
Create a deployment named
myappthat creates 2 replicas for Pods with the imagenginx. Expose the container port 80.Expose the Pods so that requests can be made against the service from inside of the cluster.
Create a temporary Pods using the image
busyboxand run a wget command against the IP of the service.Change the service type so that the Pods can be reached from outside of the cluster.
Run a
wgetcommand against the service from outside of the cluster.(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 59sExpose 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.
任务:
Create the required namespace.
Copy the Pod definition to the file
app-stack.yamland create all three Pods. Notice that the namespace has already been defined in the YAML definition.Create a network policy in the YAML file
app-stack-network-policy.yaml.The network policy should allow incoming traffic from the backend to the database but disallow incoming traffic from the frontend.
Incoming traffic to the database should only be allowed on TCP port
3306and no other port.
参考官网:networkpolicy Create the namespace
The following definition ensure that all rules are fulfilled.
Create the network policy.
最后更新于