A ‘service’ is a way to make an application accessible from the network in Kubernetes. It involves exposing one or multiple network ports that are tied to a containerized application. For example, you can run a containerized web server and expose it over the network to turn it into a service. This then allows incoming client connections, so in this case users could visit the hosted web server.
In this tutorial, we will go through the step by step instructions to create, manage, and expose a service in Kubernetes on a Linux system. This is not a very different process from deploying an application, except that a port will also get exposed so that it can be accessible over the network. Let’s get started!
In this tutorial you will learn:
- How to create, manage, and expose a service in Kubernetes
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Create and Expose a Kubernetes Service step by step instructions
Before getting started, this tutorial assumes that you have already installed Kubernetes and began creating a Kubernetes cluser, whether you have a production ready configuration or just a single node test system.
- The first step in creating the service is to fill out our YAML file with the requirements. For this example, we will create an Nginx deployment. The following configuration will suffice to create a barebones Nginx deployment with 3 replicas. Notice the last line where we specify that Nginx will listen on port 80 with the
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.14.2 ports: - containerPort: 80
Save changes and exit the YAML file when you are done making your edits.
- Next, use the kubectl command to apply the deployment in Kubernetes.
$ kubectl apply -f ./nginx-deployment.yaml
- Next, start the instance of Nginx with the
kubectl runcommand. Our configuration in the deployment file from earlier will automatically be applied to it.
$ kubectl run --image=nginx nginx-server --port=80 --env="DOMAIN=cluster"
- Then, we expose the Nginx application on port 80, since that is what we defined earlier as the application’s listening port:
$ kubectl expose deployment nginx-deployment --port=80 --name=nginx-http
- Use the following command to ensure that the three replicas of Nginx are up and running:
$ kubectl get pods
- Next, check on the
nginx-httpservice to see what its listening IP is.
$ kubectl get svc nginx-http
- We can now navigate to this IP address in browser. Since port 80 was exposed previously, we should have no problems accessing the website.
In this tutorial, we saw how to create, manage, and expose a service in Kubernetes on a Linux system. As an example, we ran an Nginx image and controlled its configuration with a deployment file. Then, we exposed it on port 80, which allowed us to access the web server by navigating to the IP address of the service. Using services in Kubernetes is rather straightforward and only requires a few commands to get set up.