Module 7: Kubernetes Assignment - 2
Tasks To Be Performed:
- Use the previous deployment
- Create a service of type NodePort for NGINX deployment
- Check the NodePort service on a browser to verify
Step 1:
Previous deployment Assignment 1 – Kubernetes
Instances and Their Private IP Addresses
k-master:
10.0.1.82worker1:10.0.1.57worker2:10.0.1.119
Step 2: Create NodePort Service
To allow external traffic to reach my Nginx deployment, I will create a NodePort service. This is done by defining a service configuration in a YAML I named nginx-nodeport.yaml.
apiVersion: v1
kind: Service
metadata:
name: my-nginx-deployment
spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
app: nginxThe
type: NodePortspecification in the service configuration ensures that the service is accessible through a specific port allocated on each node in the cluster. By applying thenginx-nodeport.yamlfile, the service will be configured to allow access to the Nginx server via the allocatednodePortof 30008 on all the nodes.
Tip
The
selectorfield in this service configuration is set toapp: nginx. This matches the label of the pods created by the nginx-deployment.yaml (Assignment 1) . It is important to note that this service will route traffic specifically to the pods created by thenginx-deployment.yamlfile and not to those created by the one-linekubectl create deploymentcommand, unless they were also labeled withapp: nginx.spec: replicas: 3 selector: matchLabels: app: nginx
Once I’ve created this file, I apply it using the command:
kubectl apply -f nginx-nodeport.yamlI check to ensure the service is running with:
kubectl get svc
We’ve confirmed that the NodePort service is actively routing traffic. The defined NodePort of
30008is set up to forward to the internal port80of the Nginx pods
Step 3: Verification
I retrieve the public IP address of one of the worker nodes in the cluster to test the Nginx service externally. By entering http://<Worker_Node_Public_IP>:30008 into a web browser, I can access the Nginx landing page served through the NodePort, verifying that the service is correctly exposed.
Success
