Task 1:Create a Service for accessing todo-app
Create a Service for your todo-app Deployment from Day-32
Create a Service definition for your todo-app Deployment in a YAML file.
Now create a service.yml where we will deploy NodePort.
apiVersion: v1 kind: Service metadata: name: my-django-app-service namespace: my-django-app spec: type: NodePort selector: app: django-app ports: # By default and for convenience, the 'targetPort' is set to the same value as the 'port' field. - port: 80 targetPort: 8000 # Optional field # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767) nodePort: 30009
Apply the Service definition to your K8 cluster using the kubectl apply -f service.yml.
kubectl apply -f service.yml # Getting the service. (svc is short form of service) kubectl get svc -n=my-django-app
So edit the inbound rule of the worker node and add port number 30009 so that we can access the pod outside
Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
Task 2:Create a ClusterIP Service for accessing the todo-app
Create a ClusterIP Service for accessing the todo-app from within the cluster
vi cluster-ip-service.yml
apiVersion: v1 kind: Service metadata: name: my-django-app-cluster namespace: my-django-app spec: type: ClusterIP selector: app: django-app ports: - name: http protocol: TCP port: 8000 targetPort: 8000
Apply the Service definition to the cluster using the following command:
kubectl apply -f cluster-ip-service.yml -n my-django-app
Verify that the service is running by running the following command:
kubectl get svc -n my-django-app
Deploy another Pod in the my-django-app namespace to test the service. You can use the following YAML definition to create a simple test Pod:
apiVersion: v1 kind: Pod metadata: name: test-pod namespace: my-django-app spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'while true; do wget -q -O- my-django-app-cluster-ip:8000; done']
Apply the Pod definition using the following command:
kubectl apply -f test-pod.yml -n my-django-app
Now enter into this pod :
kubectl exec -it test-pod -n my-django-app -- /bin/sh
Open the port for the new pod in the inbound rule
Test Todo app
# Inside the test pod execute the wget commad. wget -qO- http://<ip-of-my-django-app-cluster-ip>:<port # wget -qO- http://10.111.195.250:8000
Now you successfully Access it through another Port:
Task 3:Create a LoadBalancer Service for accessing the todo-app
Create a YAML file named load-balancer-service.yml with the following contents:
apiVersion: v1 kind: Service metadata: name: my-django-app-cluster-ip namespace: my-django-app spec: selector: app: django-app ports: - port: 80 targetPort: 8000 type: LoadBalancer
Apply the LoadBalancer Service definition to your K8s cluster using the following command:
kubectl apply -f load-balancer-service.yml -n my-django-app
Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your namespace.
kubectl get service -n=my-django-app
Get the IP from the service and then open the port in the Worker Node.
copy the Public IPv4 DNS and access through the browser by providing the port number in the URL.
Public-IPv4-DNS:<loadbalancer-port-number>
Thank You,
I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey as a becoming DevOps Engineer.
Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.
~ Abhisek Moharana ๐