I want to publish my website. I have created Azure Kubernetes Service on portal.azure.com but I don't have LoadBalancer:
Should I create a "Service" with type "LoadBalancer"? But I don't know what I should enter in "Port number" and "Target port". Could you tell me? Or maybe I should create "Ingress (preview)", not the "Service"?
On dev.azure.com during creating pipeline - Deploy to Azure Kubernetes Service I have choosen "8080" as a Service Port so maybe I should enter "8080" as a "Port Number" and "Target Port"?
To expose your website running on Azure Kubernetes Service, both ways are correct as I mentioned in the comment section, you can create a Service with type Load Balancer. In the Service manifest, you can specify the port number and target port for your website. If you have chosen "8080" as a Service Port during pipeline creation, then you should enter "8080" as the Port Number and Target Port. Here is an example manifest:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: my-app
After you deploy the manifest, you can confirm that the Service is created, and the Load Balancer is configured using the following command:
kubectl get service my-service
When you view the service details, the public IP address created for this service on the load balancer is shown in the EXTERNAL-IP column. It might take a few minutes for the IP address to change from <pending>
to an actual public IP address.
Let me take a flask app example that says This is a Hello World application using the below code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>This is a Hello World application</p>"
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
Then containerize the app using docker and then build it using docker build -t bravinwasike/flask-kubernetes .
Output:
after that push the Docker image into my Azure container repository using the following commands:
az login
az acr login --name <youracrname>
docker tag <the_name_you_want_to_show_in_ACR> <youracrname>.azurecr.io/<the_image_name_which_you_built>
docker push <youracrname>.azurecr.io/<the_name_you_want_to_show_in_ACR>
then I deployed the containerized application to the created AKS cluster
Followed by creating a deployment and service (Load Balancer) yaml file that refers to the image that I just pushed in my acr
Deployment and service yaml file combined
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- name: test-app
image: bravinwasike/flask-kubernetes:latest
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: flask-app-service
spec:
selector:
app: flask-app-deployment
ports:
- port: 6000
targetPort: 5000
type: LoadBalancer
then apply the yaml
kubectl apply -f <whatever_you_have_named_your_file>.yaml
Done.. Now when I do kubectl get pods
my pods are up
and my flask app is up and reachable via the load balancer.
Alternatively, you can use Ingress to expose your website as well. To use Ingress, you need to deploy an Ingress controller in your cluster. Here is an example Ingress manifest:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: your-website-name
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: my-website.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-website
port:
name: http
In this example, the Ingress controller listens on port 80 for requests to the host my-website.com
. Requests to the root path /
are forwarded to the my-website
service on port 80. The nginx.ingress.kubernetes.io/rewrite-target
annotation rewrites the URL path to remove the /
prefix. You can deploy this manifest using kubectl apply -f <filename>
.
References: