Skip to content

2.4 Services

https://youtu.be/xhva6DeKqVU

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:

  1. Create a NodePort service:

    apiVersion: v1
    kind: Service
    metadata:
    name: my-service
    spec:
    selector:
    app: my-app
    ports:
    - protocol: TCP
    port: 80 # Port on the service
    targetPort: 80 # Port on the container
    nodePort: 30007 # Exposed port on the node (optional)
    type: NodePort
  2. Apply the YAML:

    Terminal window
    kubectl apply -f service.yaml
  3. 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.

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:

  1. Create a LoadBalancer service:

    apiVersion: v1
    kind: Service
    metadata:
    name: my-service
    spec:
    selector:
    app: my-app
    ports:
    - protocol: TCP
    port: 80
    targetPort: 80
    type: LoadBalancer
  2. Apply the YAML:

    Terminal window
    kubectl apply -f service.yaml
  3. Access the service:

    • Get the external IP using kubectl get services.
    • Access it from your PC’s browser via http://<External-IP>.

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:

  1. 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.
  2. Access the service:

    • Open your browser and go to http://localhost:8080.

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:

  1. Deploy an Ingress controller (if not already available).

  2. Create an Ingress resource to expose your service.

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: my-ingress
    spec:
    rules:
    - host: myapp.example.com
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: my-service
    port:
    number: 80
  3. Apply the YAML:

    Terminal window
    kubectl apply -f ingress.yaml
  4. Access the service:

    • Ensure the DNS for myapp.example.com points to the ingress IP.
    • Access via http://myapp.example.com.

Keypoints

  • For local development: Use kubectl port-forward.
  • For simple access: Use a NodePort service.
  • For production: Use a LoadBalancer service or Ingress.

Each of these methods should enable your PC to communicate with services inside your Kubernetes cluster.