Kubernetes CKA sample exam question 16 with answer

Question
Expose the audit-web-app pod as Service audit-web-app-service application on port 30002 on the nodes on the cluster.
Note: the web application listens on port 8080

Answer
Get details about environment:

kubectl get po
kubectl describe pod audit-web-app
Create the service which exposes this pod:
kubectl expose pod audit-web-app --name=audit-web-app-service --type=NodePort --dry-run=client -o yaml > svc.yaml
Open svc.yaml and modify it as following:
apiVersion: v1
kind: Service
metadata:
  labels:
    run: audit-web-app
  name: audit-web-app-service
spec:
  ports:
    port: 8080
    protocol: TCP
    targetPort: 8080
    nodePort: 30002
  selector:
    run: audit-web-app
  type: NodePort
Apply and check the endpoints - the IP from pod should match the Service endpoint:
kubectl apply -f svc.yaml
kubectl get po audit-web-app -o wide
kubectl describe svc audit-web-app-service