github.com/googlecloudplatform/kubernetes-workshops@v0.0.0-20180501174420-d8199445b2c3/bundles/kubernetes-101/workshop/labs/creating-and-managing-services.md (about)

     1  # Creating and Managing Services
     2  
     3  Services provide stable endpoints for Pods based on a set of labels.
     4  
     5  In this lab you will create the `monolith` service and "expose" the `secure-monolith` Pod externally. You will learn how to:
     6  
     7  * Create a service
     8  * Use label selectors to expose a limited set of Pods externally
     9  
    10  ## Tutorial: Create a Service
    11  
    12  Explore the monolith service configuration file:
    13  
    14  ```
    15  cat services/monolith.yaml 
    16  ```
    17  
    18  Create the monolith service using kubectl:
    19  
    20  ```
    21  kubectl create -f services/monolith.yaml
    22  ```
    23  
    24  Use the `gcloud compute firewall-rules` command to allow traffic to the `monolith` service:
    25  
    26  ```
    27  gcloud compute firewall-rules create allow-monolith-nodeport \
    28    --allow=tcp:31000
    29  ```
    30  
    31  ## Exercise: Interact with the Monolith Service Remotely
    32  
    33  ### Hints
    34  
    35  ```
    36  gcloud compute instances list
    37  ```
    38  
    39  ```
    40  curl -k https://<EXTERNAL_IP>:31000
    41  ```
    42  
    43  ### Quiz
    44  
    45  * Why are you unable to get a response from the `monolith` service?
    46  
    47  ## Exercise: Explore the monolith Service
    48  
    49  ### Hints
    50  
    51  ```
    52  kubectl get services monolith
    53  ```
    54  
    55  ```
    56  kubectl describe services monolith
    57  ```
    58  
    59  ### Quiz
    60  
    61  * How many endpoints does the `monolith` service have?
    62  * What labels must a Pod have to be picked up by the `monolith` service?
    63  
    64  ## Tutorial: Add Labels to Pods
    65  
    66  Currently the `monolith` service does not have any endpoints. One way to troubleshoot an issue like this is to use the `kubectl get pods` command with a label query.
    67  
    68  ```
    69  kubectl get pods -l "app=monolith"
    70  ```
    71  
    72  ```
    73  kubectl get pods -l "app=monolith,secure=enabled"
    74  ```
    75  
    76  > Notice this label query does not print any results
    77  
    78  Use the `kubectl label` command to add the missing `secure=enabled` label to the `secure-monolith` Pod.
    79  
    80  ```
    81  kubectl label pods secure-monolith 'secure=enabled'
    82  ```
    83  
    84  View the list of endpoints on the `monolith` service:
    85  
    86  ```
    87  kubectl describe services monolith
    88  ```
    89  
    90  ### Quiz
    91  
    92  * How many endpoints does the `monolith` service have?
    93  
    94  ## Exercise: Interact with the Monolith Service Remotely
    95  
    96  ### Hints
    97  
    98  ```
    99  gcloud compute instances list
   100  ```
   101  
   102  ```
   103  curl -k https://<EXTERNAL_IP>:31000
   104  ```
   105  
   106  ## Tutorial: Remove Labels from Pods
   107  
   108  In this exercise you will observe what happens when a required label is removed from a Pod.
   109  
   110  Use the `kubectl label` command to remove the `secure` label from the `secure-monolith` Pod.
   111  
   112  ```
   113  kubectl label pods secure-monolith secure-
   114  ```
   115  
   116  View the list of endpoints on the `monolith` service:
   117  
   118  ```
   119  kubectl describe services monolith
   120  ```
   121  
   122  ### Quiz
   123  
   124  * How many endpoints does the `monolith` service have?
   125  
   126  ## Summary
   127  
   128  In this lab you learned how to expose Pods using services and labels.