In this guide, we’ll walk through exposing a Kubernetes application using a Service resource. After deploying your application using a Deployment resource, you can make it accessible to users through a web browser by creating a Service.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Verify Your Deployment
Before exposing your application, verify that your deployment is running correctly. In our example, we have a deployment named “myapp-deployment” that manages six replicas (six Pods) in the cluster:Create a Service Configuration
Now that your application is running, let’s expose it using a Kubernetes Service. Follow these steps:- Open your editor and navigate to the directory where you store your configuration files. For better organization, you can create a directory called
service. - Inside the
servicedirectory, create a file namedservice-definition.yaml.
You are not required to use the suggested directory structure. If preferred, all configuration files may be kept in a single directory.
Define the Service API and Specifications
Begin by defining the API version and kind within your YAML file. Paste in the following configuration:- The
apiVersionis set tov1as this is the version used for services. - The
kindis specified asService. - The
metadatasection assigns the service a name:myapp-service. - Within the
specsection:- The service type is
NodePort, which allows external access via a node port (ideal for Minikube). - The service listens on port 80, mapping it directly to port 80 on the Pods.
- The
nodePortis set to 30004 – an allowable value between 30000 and 32767 that exposes the service externally.
- The service type is
Bind the Service to Your Pods
To associate the service with the correct set of Pods, add a selector. Verify that your deployment YAML includes appropriate labels (e.g.,app: myapp). Then, update your YAML file with the selector field:
Deploy the Service
Once your service definition is complete, save the file and navigate to its directory to confirm its existence:Access Your Application
Since we’re using Minikube, retrieve the service URL with:This concludes the demo on creating and exposing a Kubernetes Service. Your application is now accessible externally, and you can use this approach to manage exposure in various Kubernetes environments.