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
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 your ReplicaSet objects
kubectl get replicasets
kubectl describe 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
Display information about the Service using the proceeding commands
kubectl get services my-service
kubectl describe services my-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
DevOps Engineer
1 年https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/
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