LAB_8.1
LAB_8.1
Overview
Services (also called microservices) are objects which declare a policy to access a logical set of Pods. They are typ-
ically assigned with labels to allow persistent access to a resource, when front or back end containers are terminated
and replaced.
Native applications can use the Endpoints API for access. Non-native applications can use a Virtual IP-based bridge
to access back end pods. ServiceTypes Type could be:
• LoadBalancer Exposes service externally using cloud providers load balancer. NodePort and ClusterIP auto-
matically created.
We use services as part of decoupling such that any agent or object can be replaced without interruption to access
from client to back end application.
1. Deploy two nginx servers using kubectl and a new .yaml file. The kind should be Deployment and label it with nginx.
Create two replicas and expose port 8080. What follows is a well documented file. There is no need to include the
comments when you create the file. This file can also be found among the other examples in the tarball.
student@lfs458-node-1a0a:˜$ vim nginx-one.yaml
nginx-one.yaml
1 apiVersion: apps/v1
2 # Determines YAML versioned schema.
3 kind: Deployment
4 # Describes the resource defined in this file.
5 metadata:
6 name: nginx-one
7 labels:
8 system: secondary
9 # Required string which defines object within namespace.
10 namespace: accounting
11 # Existing namespace resource will be deployed into.
12 spec:
13 selector:
14 matchLabels:
15 system: secondary
16 # Declaration of the label for the deployment to manage
17 replicas: 2
18 # How many Pods of following containers to deploy
19 template:
20 metadata:
21 labels:
22 system: secondary
3. Run the following command and look for the errors. Assuming there is no typo, you should have gotten an error about
about the accounting namespace.
student@lfs458-node-1a0a:˜$ kubectl create -f nginx-one.yaml
Error from server (NotFound): error when creating
"nginx-one.yaml": namespaces "accounting" not found
4. Create the namespace and try to create the deployment again. There should be no errors this time.
student@lfs458-node-1a0a:˜$ kubectl create ns accounting
namespace/accounting" created
5. View the status of the new pods. Note they do not show a Running status.
6. View the node each has been assigned to (or not) and the reason, which shows under events at the end of the output.
student@lfs458-node-1a0a:˜$ kubectl -n accounting describe pod \
nginx-one-74dd9d578d-fcpmv
Name: nginx-one-74dd9d578d-fcpmv
Namespace: accounting
Node: <none>
<output_omitted>
Events:
Type Reason Age From ....
---- ------ ---- ----
Warning FailedScheduling <unknown> default-scheduler
0/2 nodes are available: 2 node(s) didn't match node selector.
7. Label the secondary node. Note the value is case sensitive. Verify the labels.
8. View the pods in the accounting namespace. They may still show as Pending. Depending on how long it has been
since you attempted deployment the system may not have checked for the label. If the Pods show Pending after a
minute delete one of the pods. They should both show as Running after a deletion. A change in state will cause the
Deployment controller to check the status of both Pods.
9. View Pods by the label we set in the YAML file. If you look back the Pods were given a label of app=nginx.
10. Recall that we exposed port 8080 in the YAML file. Expose the new deployment.
11. View the newly exposed endpoints. Note that port 8080 has been exposed on each Pod.
12. Attempt to access the Pod on port 8080, then on port 80. Even though we exposed port 8080 of the container the
application within has not been configured to listen on this port. The nginx server listens on port 80 by default. A curl
command to that port should return the typical welcome page.
13. Delete the deployment. Edit the YAML file to expose port 80 and create the deployment again.
nginx-one.yaml
1 ....
2 ports:
3 - containerPort: 8080 #<-- Edit this line
4 protocol: TCP
5 ....