CodeBlanks Logo Icon CodeBlanks

Challenge - Service

Write a NodePort Service and connect to Pods.

Challenge: internal and external services

An application has two faces: internal services (API, database) are reached only from inside the cluster, while the front end is reached from outside. In this challenge you'll write the right Services for both and verify that they work.

api (ClusterIP)  : http://api:8000          # cluster-internal only
web (NodePort)   : http://<node-ip>:30080   # from outside

kubectl run -it --rm t --image=busybox:1.36 --restart=Never -- wget -qO- http://api:8000/saglik

Practice The API service

Write a Service YAML: name api, type: ClusterIP; selector app: api; port port: 8000, targetPort: 8000.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Service
metadata:
  name: api
spec:
  type: ClusterIP
  selector:
    app: api
  ports:
    - port: 8000
      targetPort: 8000

Expected output

service/api created

Practice The web service and verification

Write two commands: expose the web deployment with --type=NodePort --port=80 --target-port=8080; in an interactive pod t with busybox:1.36 that is deleted on exit and doesn't restart, run wget -qO- http://api:8000/saglik.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl expose deployment web --type=NodePort --port=80 --target-port=8080
kubectl run -it --rm t --image=busybox:1.36 --restart=Never -- wget -qO- http://api:8000/saglik

Expected output

service/web exposed
{"durum":"ok"}
pod "t" deleted

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Is it possible to write port: 8000 and targetPort: http in the API service?

  • Only with NodePort
  • Only for HTTPS
  • Yes; if the container port was named with name: http Correct answer
  • No, targetPort must be a number

Explanation: A named port keeps working even if the container port changes, without changing the service.

2 Expose the service only inside the cluster.

Question
spec:
  type: ____
Answer
spec:
  type: ClusterIP

Explanation: ClusterIP is the default, internal type.

3 Match.

Term Meaning
api ClusterIP Internal access
web NodePort External access
targetPort: http Named port
wget http://api:8000 Internal verification

All lessons in Intermediate