zoukankan      html  css  js  c++  java
  • ckad练习题- Services & Networking

    Services & Networking (13%)

    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?

    Solution:

    Create a deployment with 2 replicas first. You should end up with one deployment and two Pods.

    $ 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.

    $ kubectl expose deploy myapp --target-port=80
    service/myapp exposed
    $ kubectl get services
    NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    myapp        ClusterIP   10.108.88.208   <none>        80/TCP    15s

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

    $ kubectl run tmp --image=busybox --restart=Never -it --rm -- wget -O- 10.108.88.208:80
    Connecting to 10.108.88.208:80 (10.108.88.208:80)
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    -                    100% |********************************|   612  0:00:00 ETA
    pod "tmp" deleted

    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.

    $ kubectl edit service myapp
    ...
    spec:
      type: NodePort
    ...
    
    kubectl get services
    NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
    myapp        NodePort    10.108.88.208   <none>        80:30441/TCP   3m

    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 

    $ wget -O- localhost:30441
    --2019-05-10 16:32:35--  http://localhost:30441/
    Resolving localhost (localhost)... ::1, 127.0.0.1
    Connecting to localhost (localhost)|::1|:30441... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 612 [text/html]
    Saving to: ‘STDOUT’
    
    -                                          0%[                                                                                   ]       0  --.-KB/s               <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    -                                        100%[==================================================================================>]     612  --.-KB/s    in 0s
    
    2019-05-10 16:32:35 (24.3 MB/s) - written to stdout [612/612]

    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.

    kind: Pod
    apiVersion: v1
    metadata:
      name: frontend
      namespace: app-stack
      labels:
        app: todo
        tier: frontend
    spec:
      containers:
        - name: frontend
          image: nginx
    
    ---
    
    kind: Pod
    apiVersion: v1
    metadata:
      name: backend
      namespace: app-stack
      labels:
        app: todo
        tier: backend
    spec:
      containers:
        - name: backend
          image: nginx
    
    ---
    
    kind: Pod
    apiVersion: v1
    metadata:
      name: database
      namespace: app-stack
      labels:
        app: todo
        tier: database
    spec:
      containers:
        - name: database
          image: mysql
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: example
    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.

     Solution:

    Create the namespace

     kubectl create namespace app-stack
    namespace/app-stack created
    
    $ vim app-stack.yaml
    $ kubectl create -f app-stack.yaml
    pod/frontend created
    pod/backend created
    pod/database created
    
    $ kubectl get pods --namespace app-stack
    NAME       READY   STATUS    RESTARTS   AGE
    backend    1/1     Running   0          22s
    database   1/1     Running   0          22s
    frontend   1/1     Running   0          22s

    The following definition ensure that all rules are fulfilled.

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: app-stack-network-policy
      namespace: app-stack
    spec:
      podSelector:
        matchLabels:
          app: todo
          tier: database
      policyTypes:
      - Ingress
      - Egress
      ingress:
      - from:
        - podSelector:
            matchLabels:
              app: todo
              tier: backend
        ports:
        - protocol: TCP
          port: 3306

    Create the network policy.

    $ vim app-stack-network-policy.yaml
    $ kubectl create -f app-stack-network-policy.yaml
    $ kubectl get networkpolicy --namespace app-stack
    NAME                       POD-SELECTOR             AGE
    app-stack-network-policy   app=todo,tier=database   5s
  • 相关阅读:
    输入一个1-9的数i,再输入一个数字n,表示 i 出现的次数,输入的2个数字 i 和 n 组合成如下表达式:如i=2,n=4,2+22+222+2222=?,计算结果是多少?
    现有数列1/2;2/3;3/5;5/8······第十次出现的是什么?
    猜数游戏:范围时1-100,若错误就提示大了还是小了,猜对则结束,允许猜10次,游戏结束后对玩家评价:1次猜对;5次内猜对;10次内猜对;没有猜对
    登录模拟,用户名和密码输入错误后给出相关错误提示,并告知还有多少次错误机会,如果5次验证失败将冻结账户
    30人围坐轮流表演节目,按顺序数1-3,每次数到3的人就表演节目,表演过的人不再参加报数,那么在仅剩一个人没有表演的时候,共报数多少人次?
    docker 自定义镜像
    php 镜像richarvey/nginx-php-fpm的ngnix配置
    php tp5常用小知识
    php Tp5下mysql的增删改查
    php 面试常问问题
  • 原文地址:https://www.cnblogs.com/peteremperor/p/12835974.html
Copyright © 2011-2022 走看看