This post will show how to push an application to a Kubernetes cluster by exposing it through one of the supported services and making it available using HTTP.
Service and Proxy
The application you write can be any executable that runs on Linux. You must then use a supported service to expose the application through a Kubernetes node and make it available on the Internet. Once exposed and published, clients can access your application. Before we get into this process, we need to cover some of the basics behind services and how they work.
A Kubernetes component called the service proxy does the actual translation between external requests and your application. The proxy listens on both localhost and an arbitrary port as configured by the “clusterIP” field of the service object you create when creating a new service.
This blog post will continue from the previous post on this topic where we created a deployment, and now we will publish it and make it available from the Docker Desktop client using HTTP.
Below is a service definition in a YAML file that contains the service details and the app we have deployed in the previous post.
In the last section of this post, I have combined the deployment and service into a single file.
apiVersion: v1 kind: Service metadata: name: myapp labels: run: myapp spec: ports: - port: 80 protocol: TCP type: LoadBalancer selector: run: myapp
To deploy the service, I run this command.
kubectl apply -f service.yaml
If I open my browser where I have Docker Desktop installed and browse to HTTP://127.0.0.1, I will the published application.
All in One
If managing two files is sometimes you prefer not to do, you can combine the code of the deployment and service into a single file and deploy both of them together.
apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: selector: matchLabels: run: myapp replicas: 2 template: metadata: labels: run: myapp spec: containers: - name: myapp image: mcr.microsoft.com/dotnet/samples:aspnetapp ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: myapp labels: run: myapp spec: ports: - port: 80 protocol: TCP type: LoadBalancer selector: run: myapp