kubernetes_services

Home

Kubernetes Services by Example

This guide demonstrates how to expose a Kubernetes Deployment using different Service types: ClusterIP, NodePort, and LoadBalancer.

1. Prerequisites: Sample Nginx Deployment

First, let's create a simple Nginx Deployment with 3 replicas.

  1. Create nginx_deploy.yaml:

    cat > nginx_deploy.yaml <<EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9
            # Note: nginx:1.7.9 is an older version. Consider using a more recent tag for new deployments.
            ports:
            - containerPort: 80
    EOF
  2. Apply the Deployment and check Pods:

    kubectl apply -f nginx_deploy.yaml
    kubectl get po -l app=nginx # List pods with label app=nginx

2. Exposing the Deployment with Service Types

Now, we'll expose the nginx-deployment using different Service types. The Service uses the selector: app: nginx to find the Pods managed by our Deployment.

2.1 ClusterIP Service

A ClusterIP service exposes the application on an internal IP address within the cluster. This is the default Service type.

  1. Create nginx_clusterip.yaml:

    (Added targetPort for clarity, though it defaults to port if not specified and if the container port is also 80.)

  2. Apply the ClusterIP Service and check Services:

2.2 NodePort Service

A NodePort service exposes the application on a static port on each Node's IP address.

  1. Create nginx_nodeport.yaml:

    (Added targetPort and commented nodePort example.)

  2. Apply the NodePort Service and check Services:

    (Note the assigned NodePort from the output. You can access this service via <NodeIP>:<NodePort>.)

2.3 LoadBalancer Service

A LoadBalancer service exposes the application externally using a cloud provider's load balancer.(Note: This Service type requires a cloud provider or an environment that can provision load balancers, like Minikube with minikube tunnel.)

  1. Create nginx_loadbalancer.yaml:

    (Added targetPort.)

  2. Apply the LoadBalancer Service and check Services:

    (It might take some time for the EXTERNAL-IP to be assigned by the cloud provider.)

For more advanced examples, see https://github.com/xxradar/kuberneteslearning.

3. How Services Work: Inspecting Endpoints

Services work by watching Pods that match their selector and updating an Endpoints object (or using EndpointSlices in newer Kubernetes) with the IP addresses and ports of the ready Pods.

4. Pods Matching a Service Selector

If you create a new Pod with labels that match an existing Service's selector, that Pod will automatically become part of the Service and start receiving traffic.

The original heading mentioned "capture a password??", which is out of scope for a simple label matching demonstration. This section shows how a pod can be added to a service via labels.

  1. Run a new Nginx pod with matching labels: The my-nginx-clusterip Service uses the selector app: nginx.

    (Corrected ningx to nginx. Simplified kubectl run for a basic pod.)

  2. Observe the Endpoints: Wait a few moments for the new pod to become ready.

    You should see the IP address of the fish-nginx pod added to the list of endpoints for my-nginx-clusterip.

Home

Last updated