2.4 Services
Linking Local PC Network with Kube Network
To link a Kubernetes service network with your local PC’s network, allowing you to access services running inside the Kubernetes cluster, you have several options. The best approach depends on your environment (e.g., whether you’re running Kubernetes on your local machine, in a cloud, or on-premise). Here’s how you can do it:
1. Exposing Services via NodePort (Simple Approach)
- How it works: A
NodePort
exposes the service on a specific port of each node’s IP address. Your local PC can access the service using the node’s IP address and the NodePort. - Limitations: The service is available on all nodes, but it’s exposed on a higher port (range 30000-32767).
Steps:
-
Create a
NodePort
service:apiVersion: v1kind: Servicemetadata:name: my-servicespec:selector:app: my-appports:- protocol: TCPport: 80 # Port on the servicetargetPort: 80 # Port on the containernodePort: 30007 # Exposed port on the node (optional)type: NodePort -
Apply the YAML:
Terminal window kubectl apply -f service.yaml -
Access the service:
- From your PC’s browser, go to
http://<NodeIP>:30007
, where<NodeIP>
is the IP of one of the nodes in the cluster.
- From your PC’s browser, go to
2. Using LoadBalancer (Cloud/Managed Kubernetes)
- How it works: If you’re running Kubernetes on a cloud platform, using a
LoadBalancer
service is the easiest way to expose the service externally. - Limitations: Requires cloud provider support and may incur additional costs.
Steps:
-
Create a
LoadBalancer
service:apiVersion: v1kind: Servicemetadata:name: my-servicespec:selector:app: my-appports:- protocol: TCPport: 80targetPort: 80type: LoadBalancer -
Apply the YAML:
Terminal window kubectl apply -f service.yaml -
Access the service:
- Get the external IP using
kubectl get services
. - Access it from your PC’s browser via
http://<External-IP>
.
- Get the external IP using
3. Port Forwarding (Development/Testing)
- How it works: Port forwarding allows you to map a local port on your PC to a port on a Kubernetes service or pod.
- Limitations: Temporary and only works as long as the
kubectl port-forward
command is running.
Steps:
-
Run port forwarding:
Terminal window kubectl port-forward service/my-service 8080:80- This maps port 8080 on your local PC to port 80 on the service.
-
Access the service:
- Open your browser and go to
http://localhost:8080
.
- Open your browser and go to
4. Using Ingress (Advanced with DNS)
- How it works: An Ingress resource provides external access to services in the cluster, often with HTTP/HTTPS and DNS support.
- Limitations: Requires an Ingress controller like NGINX, Traefik, or another cloud-provided controller.
Steps:
-
Deploy an Ingress controller (if not already available).
-
Create an Ingress resource to expose your service.
apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: my-ingressspec:rules:- host: myapp.example.comhttp:paths:- path: /pathType: Prefixbackend:service:name: my-serviceport:number: 80 -
Apply the YAML:
Terminal window kubectl apply -f ingress.yaml -
Access the service:
- Ensure the DNS for
myapp.example.com
points to the ingress IP. - Access via
http://myapp.example.com
.
- Ensure the DNS for
Keypoints
- For local development: Use
kubectl port-forward
. - For simple access: Use a
NodePort
service. - For production: Use a
LoadBalancer
service orIngress
.
Each of these methods should enable your PC to communicate with services inside your Kubernetes cluster.