Lab Exercises for Services & Networking
Exercise 0 - Setup
- Prepare a cluster (Single node, kubeadm, k3s, etc)
- Open browser tabs to https://kubernetes.io/docs/, https://github.com/kubernetes/ and https://kubernetes.io/blog/ (these are permitted as per the current guidelines)
Exercise 1 - Understand connectivity between Pods
- Deploy the following manifest
- Using
kubectl
, identify the Pod IP addresses - 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
- Create three
deployments
of your choosing - Expose one of these deployments with a service of type
ClusterIP
- Expose one of these deployments with a service of type
Nodeport
- Expose one of these deployments with a service of type
Loadbalancer
- 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 asmetallb
- Note, this remains in
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
-
Create an
ingress
object namedmyingress
with the following specification: -
Manages the host
myingress.mydomain
- Traffic to the base path
/
will be forwarded to aservice
calledmain
on port 80 - Traffic to the path
/api
will be forwarded to aservice
calledapi
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
- Identify the configuration location of
coredns
- Modify the coredns config file so DNS queries not resolved by itself are forwarded to the DNS server
8.8.8.8
- Validate the changes you have made
- Add additional configuration so that all DNS queries for
custom.local
are forwarded to the resolver10.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
}