Post

Kubernetes - Services

Lab assignment for creating services using kubectl

Kubernetes - Services

Prerequisites

  • Kubernetes
  • kubectl

Assignment

  • Lets start by creating a deployment and exposing it as a service using different options.

Deployment

1
kubectl create deployment nginx --image=nginx --port=80 --replicas=3 -o yaml --dry-run=client
1
kubectl create deployment nginx --image=nginx --port=80 --replicas=3

ClusterIP Service

1️⃣ Create a simple deployment using nginx and expose it on port 80 with 3 replicas.

1
kubectl expose deployment/nginx --dry-run=client -o yaml
1
kubectl expose deployment/nginx

2️⃣ Check the exposed services and endpoints on the kubernetes cluster. Note that all the endpoints relate to the nginx pods ip addresses.

1
kubectl get services
1
kubectl get endpoints
1
kubectl get pods -o wide

3️⃣ Capture the Cluster IP and access the exposed service using curl.

1
CLUSTER_IP=$(kubectl get services | grep ClusterIP | grep nginx | awk {'print $3'}); echo $CLUSTER_IP
1
curl $CLUSTER_IP

4️⃣ Try accessing the service from another pod and check the DNS search path relating to the service.

1
kubectl run --rm -it curl --image=curlimages/curl:8.4.0 --restart=Never -- sh
1
curl nginx.default.svc.cluster.local
1
cat /etc/resolv.conf
1
curl nginx
1
exit

5️⃣ Delete the service.

1
kubectl delete service/nginx

NodePort Service

1️⃣ Expose the service using NodePort and verify that two ports are listed - Application and NodePort.

1
kubectl expose deployment/nginx --type=NodePort
1
kubectl get service

2️⃣ Capture the Cluster IP and the NodePort to query the NodePort service from one of the nodes.

1
kubectl get nodes -o wide
1
CONTROL_PLANE_IP=$(kubectl get nodes -o wide | grep control-plane | awk {'print $6'}); echo $CONTROL_PLANE_IP
1
NODEPORT_PORT=$(kubectl get services | grep NodePort | grep nginx | awk -F'[:/]' '{print $2}'); echo $NODEPORT_PORT
1
curl ${CONTROL_PLANE_IP}:${NODEPORT_PORT}

3️⃣ Delete the NodePort service.

1
kubectl delete service/nginx

LoadBalancer Service

1️⃣ Expose the deployment using the LoadBalancer service type.

1
kubectl expose deployment/nginx --type=LoadBalancer --port 8080 --target-port 80
1
kubectl get service

2️⃣ Capture the IP address and the port and query the nginx service to check how the load balancer routes the query to different pods.

1
LOADBALANCER_IP=$(kubectl get service | grep LoadBalancer | grep nginx | awk '{split($0,a," "); split(a[4],b,","); print b[1]}'); echo $LOADBALANCER_IP
1
LOADBALANCER_PORT=$(kubectl get service | grep LoadBalancer | grep nginx | awk -F'[:/]' '{print $2}'); echo $LOADBALANCER_PORT
1
watch --differences "curl ${LOADBALANCER_IP}:${LOADBALANCER_PORT} 2>/dev/null"

3️⃣ Delete the LoadBalancer service

1
kubectl delete deployment/nginx service/nginx

ExternalName Service

1️⃣ Create two deployments with different images.

1
kubectl create deployment nginx-red --image=spurin/nginx-red --port=80
1
kubectl create deployment nginx-blue --image=spurin/nginx-blue --port=80
1
kubectl get deployment

2️⃣ Expose both the deployments as a service.

1
kubectl expose deployment/nginx-red
1
kubectl expose deployment/nginx-red
1
kubectl get service

3️⃣ Create an ExternalName service of my-service that points to nginx-red pod.

1
kubectl create service externalname my-service --external-name nginx-red.default.svc.cluster.local
1
kubectl get service

4️⃣ Spin up a new pod and curl all the services. Notice that the ExternalName service points to nginx-red pod.

1
kubectl run --rm -it curl --image=curlimages/curl:8.4.0 --restart=Never -- sh
1
curl nginx-red
1
curl nginx-blue
1
curl my-service
1
nslookup my-service

5️⃣ In another tab, edit the server my-service and update it to point to nginx-blue.

1
kubectl edit service/my-service

6️⃣ In the original tab where we have ssh’ed into the curl container, check the my-service resolution.

1
nslookup my-service
1
curl my-service
1
exit

7️⃣ Cleanup the deployment and service for ExternalName service.

1
kubectl delete deployment/nginx-blue deployment/nginx-red service/nginx-blue service/nginx-red service/my-service

Headless Service

1️⃣ Create a nignx deployment with 3 replicas.

1
kubectl create deployment nginx --image=spurin/nginx-debug --replicas=3 --port=80

2️⃣ Create a headless service by modifying the ClusterIP yaml declaration and apply the changes.

1
kubectl expose deployment/nginx --dry-run=client -o yaml --type=ClusterIP | tee headless.yaml
1
grep -q 'clusterIP: None' headless.yaml || sed -i '/spec:/a\ \ clusterIP: None' headless.yaml; cat headless.yaml
1
kubectl apply -f headless.yaml 

3️⃣ Verify if the headless service was created successfully.

1
kubectl get service

4️⃣ Run the curl pod and note the IP address that are reference while we curl the headless service.

1
kubectl run --rm -it curl --image=curlimages/curl:8.4.0 --restart=Never -- sh
1
watch nslookup nginx
1
curl nginx
1
exit

5️⃣ Remove the headless service.

1
rm headless.yaml; kubectl delete deployment/nginx service/nginx
This post is licensed under CC BY 4.0 by the author.