How to Expose an App in Kubernetes to the Internet
Features and services of Kubernetes

How to Expose an App in Kubernetes to the Internet

One of the features that Kubernetes provides is "service discovery and load balancing". Kubernetes can expose a container using the DNS name or using their own IP address. If traffic to a container is high, Kubernetes is able to load-balance and distribute the network traffic so that the deployment is stable.

This article shows how to create a Kubernetes Service object that exposes an external IP address.

Before you begin

Objectives

  • Run five instances of a Hello World application
  • Create a Service object that exposes an external IP address
  • Use the Service object to access the running application

Run five instances of a Hello World application

Run a Hello World application in your cluster by creating the following deployment file (load-balancer-example.yaml)

apiVersion: apps/v1
? ?
kind: Deployment
? ?
metadata:
? ?
? labels:
? ?
? ? app.kubernetes.io/name: load-balancer-example
? ?
? name: hello-world
? ?
spec:
? ?
? replicas: 5
? ?
? selector:
? ?
? ? matchLabels:
? ?
? ? ? app.kubernetes.io/name: load-balancer-example
? ?
? template:
? ?
? ? metadata:
? ?
? ? ? labels:
? ?
? ? ? ? app.kubernetes.io/name: load-balancer-example
? ?
? ? spec:
? ?
? ? ? containers:
? ?
? ? ? - image: gcr.io/google-samples/node-hello:1.0
? ?
? ? ? ? name: hello-world
? ?
? ? ? ? ports:
? ?
? ? ? ? - containerPort: 8080

        

The proceeding command creates a Deployment and an associated ReplicaSet. The ReplicaSet has five Pods each of which runs the Hello World application.

kubectl apply -f load-balancer-example.yaml        

Display information about the deployment

kubectl get deployments hello-world 

kubectl describe deployments hello-world        


Display information about the Kubernetes deployment

Display information about your ReplicaSet objects

kubectl get replicasets

kubectl describe replicasets        


Display information about your ReplicaSets

Create a Service object that exposes the deployment

The proceeding command will create a Service object that exposes Hello World application for access from the Internet. It will declaratively provision a classic load balancer within your AWS account.

kubectl expose deployment hello-world --type=LoadBalancer --name=my-service        
AWS Classic Load Balancer


Display information about the Service using the proceeding commands

kubectl get services my-service

kubectl describe services my-service        


Display the Kubernetes service

Make notes of the LoadBalancer Ingress's DNS name and the value of the Port and NodePort exposed by the Service. Use the DNS address and port number to access the Hello World application.

curl https://ae5cb07ebfaf14fb283c20d988a0da90-1987687362.eu-west-1.elb.amazonaws.com:8080        

The response to a successful request is a hello message:

Hello Kubernetes!        

Cleaning up

To delete the Service, enter this command:

kubectl delete services my-service        

To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:

kubectl delete deployment hello-world        

What's next

Through CloudTAC, I?can help with a range of DevOps requirements via our DevOps as a service offering. We help internal development teams, product companies and digital agencies focus on innovation by taking away product development headaches. From maturity assessments, consulting, cloud migration, Infrastructure-as-Code (IaC), continuous delivery and continuous integration, tooling to organisational change and on-going optimisation of operations.

Email us your needs at [email protected]

#kubernetes #devops

NTOKO Franz Ajebe-Munge III

MFI Practitioner, Startup Developer. Business Development Strategist, Business Manager, Wine & Whiskey Enthusiast

2 年

Congrats Hudson. See that you are doing an amazing job. Lets catch up sometime

要查看或添加评论,请登录

Mbong Ekwoge的更多文章

社区洞察

其他会员也浏览了