Lab Exercises for Services & Networking

Exercise 0 - Setup

Exercise 1 - Understand connectivity between Pods

  1. Deploy the following manifest
  2. Using kubectl, identify the Pod IP addresses
  3. Determine the DNS name of the service.
Answer

Identify the selector for the service:

kubectl describe service nginx-service | grep -i selector
Selector:          app=nginx
Filter kubectl output:

kubectl get po -l app=nginx -o wide

Service name will be, based on the format [Service Name].[Namespace].[Type].[Base Domain Name] :

nginx-service.default.svc.cluster.local

Exercise 2 - Understand ClusterIP, NodePort, LoadBalancer service types and endpoints

  1. Create three deployments of your choosing
  2. Expose one of these deployments with a service of type ClusterIP
  3. Expose one of these deployments with a service of type Nodeport
  4. Expose one of these deployments with a service of type Loadbalancer
    1. Note, this remains in pending status unless your cluster has integration with a cloud provider that provisions one for you (ie AWS ELB), or you have a software implementation such as metallb
Answer - Imperative
kubectl create deployment nginx-clusterip --image=nginx --replicas 1
kubectl create deployment nginx-nodeport --image=nginx --replicas 1
kubectl create deployment nginx-loadbalancer --image=nginx --replicas 1
kubectl expose deployment nginx-clusterip --type="ClusterIP" --port="80"
kubectl expose deployment nginx-nodeport --type="NodePort" --port="80"
kubectl expose deployment nginx-loadbalancer --type="LoadBalancer" --port="80"
Answer - Declarative

Apply the following:

kind: Service
apiVersion: v1
metadata:
  name: nginx-clusterip
spec:
  selector:
    app: nginx-clusterip
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-clusterip
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-clusterip
  template:
    metadata:
      labels:
        app: nginx-clusterip
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-nodeport
spec:
  selector:
    app: nginx-nodeport
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-nodeport
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-nodeport
  template:
    metadata:
      labels:
        app: nginx-nodeport
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-loadbalancer
spec:
  selector:
    app: nginx-loadbalancer
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-loadbalancer
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-loadbalancer
  template:
    metadata:
      labels:
        app: nginx-loadbalancer
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Exercise 3 - Know how to use Ingress controllers and Ingress resources

  1. Create an ingress object named myingress with the following specification:

  2. Manages the host myingress.mydomain

  3. Traffic to the base path / will be forwarded to a service called main on port 80
  4. Traffic to the path /api will be forwarded to a service called api on port 8080
Answer - Imperative
kubectl create ingress myingress --rule="myingress.mydomain/=main:80" --rule="myingress.mydomain/api=api:8080"
Answer - Declarative

Apply the following YAML:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  creationTimestamp: null
  name: myingress
spec:
  rules:
  - host: myingress.mydomain
    http:
      paths:
      - backend:
          service:
            name: main
            port:
              number: 80
        path: /
        pathType: Exact
      - backend:
          service:
            name: api
            port:
              number: 8080
        path: /api
        pathType: Exact

Exercise 4 - Know how to configure and use CoreDNS

  1. Identify the configuration location of coredns
  2. Modify the coredns config file so DNS queries not resolved by itself are forwarded to the DNS server 8.8.8.8
  3. Validate the changes you have made
  4. Add additional configuration so that all DNS queries for custom.local are forwarded to the resolver 10.5.4.223
Answer
kubectl get cm coredns -n kube-system                                                
NAME      DATA   AGE
coredns   2      94d
kubectl edit cm coredns -n kube-system 

replace:
forward . /etc/resolv.conf

with
forward . 8.8.8.8

Add the block:

custom.local:53 {
        errors 
        cache 30
        forward . 10.5.4.223
        reload
    }