{"id":3986,"date":"2023-11-04T23:13:58","date_gmt":"2023-11-04T23:13:58","guid":{"rendered":"http:\/\/localhost:10003\/deploying-applications-with-kubernetes\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"deploying-applications-with-kubernetes","status":"publish","type":"post","link":"http:\/\/localhost:10003\/deploying-applications-with-kubernetes\/","title":{"rendered":"Deploying applications with Kubernetes"},"content":{"rendered":"
Kubernetes is an open-source container orchestration platform that helps to automate docker container deployment, scaling, and management. It makes it easy to deploy your applications in a distributed environment by effectively managing the containers that run your applications. In this tutorial, we will go through the process of deploying an application with Kubernetes.<\/p>\n
Before we begin, we need to ensure we have the following in place:<\/p>\n
The first step in deploying applications with Kubernetes is to prepare Docker images. Start by creating a Dockerfile, which defines how to build your Docker image for your application. In this example, we will use a simple Python Flask web application.<\/p>\n
Create a new directory for the Docker project, then create the following files in this directory:<\/p>\n
Dockerfile<\/strong><\/p>\n requirements.txt<\/strong><\/p>\n This Dockerfile specifies that we want to base our Python environment on the official Python runtime (version 3.6-slim). The Python dependencies needed for the Flask application are installed and any files in the current working directory that end with .py are copied to the \/app directory in the container. We also set the environment variable FLASK_APP to point to index.py, which is our Flask application’s main file that contains the application code. We then expose port 80 so that requests can be routed to the container. Finally, we start the web server inside the container with the command flask run.<\/p>\n To build the Docker image, run the following command from within the Docker project directory:<\/p>\n Here, we are building a Docker image with the name my-app:v1.<\/p>\n To deploy your application, you define a set of Kubernetes objects, such as Deployments, Services, and Ingress. These definitions can be written in YAML or JSON format. In this tutorial, we will use YAML.<\/p>\n Create a new file called deployment.yaml with the following content:<\/p>\n This YAML file specifies a Deployment object which will create and manage two replicas of the my-app container. It sets up a selector to match the labels we set up earlier and specifies the Docker image to use. We expose port 80 for the container.<\/p>\n Next, create a new file called service.yaml with the following content:<\/p>\n This YAML file specifies a Service object for our application. It sets the selector to match the labels we set up earlier and exposes port 80. Finally, create a new file called ingress.yaml with the following content:<\/p>\n This YAML file specifies an Ingress object which exposes the service to the internet. We define the path To create these Kubernetes objects, run the following commands:<\/p>\n To verify the deployment, run the following command:<\/p>\n This command should return the IP address assigned to the NGINX Ingress controller service. Open your browser and enter this IP address with the Congratulations! You have successfully deployed your application with Kubernetes.<\/p>\n In this tutorial, we went through the process of deploying an application with Kubernetes. We prepared Docker images, set up Kubernetes objects definitions and verified the deployment. Kubernetes offers a robust and scalable platform for managing containerized applications. With Kubernetes, you can scale your application up or down as demand changes and ensure that your application can handle any level of traffic.<\/p>\n","protected":false},"excerpt":{"rendered":" Introduction Kubernetes is an open-source container orchestration platform that helps to automate docker container deployment, scaling, and management. It makes it easy to deploy your applications in a distributed environment by effectively managing the containers that run your applications. In this tutorial, we will go through the process of deploying Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[701,704,30,616,572,700,702,703,699],"yoast_head":"\n# Use an official Python runtime as a parent image\nFROM python:3.6-slim\n\n# Set the working directory to \/app\nWORKDIR \/app\n\n# Copy the requirements.txt file into the container at \/app\nCOPY requirements.txt \/app\n\n# Install any needed packages specified in requirements.txt\nRUN pip install --trusted-host pypi.python.org -r requirements.txt\n\n# Copy the current directory contents into the container at \/app\nCOPY . \/app\n\n# Set environment variables\nENV FLASK_APP=index.py\n\n# Make port 80 available to the world outside this container\nEXPOSE 80\n\n# Run app.py when the container launches\nCMD [\"flask\", \"run\", \"--host=0.0.0.0\", \"--port=80\"]\n<\/code><\/pre>\n
Flask\n<\/code><\/pre>\n
$ docker build -t my-app:v1 .\n<\/code><\/pre>\n
Step 2: Deploying Kubernetes object definitions<\/h2>\n
apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n name: my-app-deployment\nspec:\n replicas: 2\n selector:\n matchLabels:\n app: my-app\n template:\n metadata:\n labels:\n app: my-app\n spec:\n containers:\n - name: my-app\n image: my-app:v1\n ports:\n - containerPort: 80\n<\/code><\/pre>\n
apiVersion: v1\nkind: Service\nmetadata:\n name: my-app-service\nspec:\n selector:\n app: my-app\n ports:\n - name: http\n port: 80\n targetPort: 80\n type: LoadBalancer\n<\/code><\/pre>\n
type: LoadBalancer<\/code> specifies that we want an external IP address to be created that we can use to access our application.<\/p>\n
apiVersion: networking.k8s.io\/v1\nkind: Ingress\nmetadata:\n name: my-app-ingress\n annotations:\n nginx.ingress.kubernetes.io\/rewrite-target: \/\nspec:\n rules:\n - http:\n paths:\n - path: \/my-app\n pathType: Prefix\n backend:\n service:\n name: my-app-service\n port:\n name: http\n<\/code><\/pre>\n
\/my-app<\/code> as a prefix for our application. The annotation
nginx.ingress.kubernetes.io\/rewrite-target: \/<\/code> ensures that we don’t need to include the \/my-app prefix in our URL when accessing the application.<\/p>\n
$ kubectl apply -f deployment.yaml\n$ kubectl apply -f service.yaml\n$ kubectl apply -f ingress.yaml\n<\/code><\/pre>\n
Step 3: Verify<\/h2>\n
$ kubectl get ingress\n<\/code><\/pre>\n
\/my-app<\/code> path. You should see the Flask application’s home page.<\/p>\n
Conclusion<\/h2>\n