Containerization has revolutionized the way applications are deployed and managed. Containers are a lightweight and portable way to package software and its dependencies, enabling developers to build once and deploy anywhere. In this blog post, we will discuss how to deploy and manage containerized applications on Fedora using Docker and Kubernetes.
Prerequisites:
Before we begin, you should have the following prerequisites in place:
- A Fedora 34 or later system.
- Docker and Kubernetes installed on your system.
- A basic understanding of Docker and Kubernetes concepts.
Step 1: Installing Docker
The first step is to install Docker on your Fedora system. Docker is available in the default Fedora repositories, so you can install it using the dnf command:
sudo dnf install docker
Once Docker is installed, you can start the Docker service and enable it to start at boot time:
sudo systemctl start docker
sudo systemctl enable docker
You can verify that Docker is running by running the following command:
sudo docker info
Step 2: Building a Docker Image
The next step is to build a Docker image for your application. A Docker image is a snapshot of your application and its dependencies, packaged together in a portable format. To create a Docker image, you need to write a Dockerfile that describes how to build the image.
Let’s assume that you have a simple Node.js application that you want to containerize. Here is an example Dockerfile for this application:
# Use the official Node.js runtime as a parent image
FROM node:14-alpine
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install the dependencies
RUN npm install
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Define the command to run your application
CMD ["npm", "start"]
Save this Dockerfile to a file named Dockerfile
in the root directory of your application.
To build the Docker image, run the following command:
sudo docker build -t my-node-app .
This command tells Docker to build a new image called my-node-app
using the Dockerfile in the current directory (.
).
Step 3: Running a Docker Container
Once you have built a Docker image for your application, you can run a container from that image. A container is an instance of an image that is running as a process on your system.
To run a Docker container from your image, use the following command:
sudo docker run -p 3000:3000 my-node-app
This command tells Docker to run a new container from the my-node-app
image and map port 3000 inside the container to port 3000 on your host system.
You should now be able to access your Node.js application by opening a web browser and navigating to http://localhost:3000
.
Step 4: Deploying with Kubernetes
Now that you have a Docker image and a running container, you can deploy your application to a Kubernetes cluster. Kubernetes is a container orchestration platform that automates the deployment, scaling, and management of containerized applications.
To deploy your application to Kubernetes, you need to create a Kubernetes deployment that specifies how many replicas of your application should be running, and a Kubernetes service that exposes your application to the outside world.
Here is an example Kubernetes deployment for your Node.js application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app
spec:
replicas: 3
selector:
matchLabels:
app: my-node-app
template
In the deployment specification above, we are defining a deployment named my-node-app
with three replicas. This means that Kubernetes will ensure that there are always three instances of your application running.
The selector
field specifies that we want to select the pods based on the label app=my-node-app
, which we will set in the pod template.
The template
field specifies the pod template for our deployment. In this example, we are using the same Docker image that we built earlier (my-node-app
), and we are setting the app
label to my-node-app
.
Save the above specification to a file named deployment.yaml
.
To create the deployment, run the following command:
kubectl apply -f deployment.yaml
This command tells Kubernetes to create a new deployment based on the deployment.yaml
file.
Next, we need to create a Kubernetes service that exposes our application to the outside world. A service is a Kubernetes object that provides a stable IP address and DNS name for your application, and can load balance traffic across multiple replicas.
Here is an example Kubernetes service for our Node.js application:
apiVersion: v1
kind: Service
metadata:
name: my-node-app
spec:
type: NodePort
selector:
app: my-node-app
ports:
- name: http
port: 3000
targetPort: 3000
In the service specification above, we are defining a service named my-node-app
with the type NodePort
. This means that Kubernetes will allocate a port on each node in the cluster to forward traffic to the service.
The selector
field specifies that we want to select the pods based on the label app=my-node-app
, which we set in the deployment.
The ports
field specifies that we want to expose port 3000 on our application, and forward traffic to port 3000 on our pods.
Save the above specification to a file named service.yaml
.
To create the service, run the following command:
kubectl apply -f service.yaml
This command tells Kubernetes to create a new service based on the service.yaml
file.
Step 5: Accessing your application
Now that your application is deployed to Kubernetes, you can access it by finding the external IP address of your Kubernetes cluster and the NodePort allocated for your service.
To find the external IP address of your cluster, run the following command:
kubectl get nodes -o wide
This command will show you a list of all the nodes in your cluster, along with their IP addresses.
To find the NodePort allocated for your service, run the following command:
kubectl get services
This command will show you a list of all the services in your cluster, along with their type, IP address, and port mappings.
To access your application, open a web browser and navigate to http://<node-ip>:<node-port>
. Replace <node-ip>
with the external IP address of one of your nodes, and <node-port>
with the NodePort allocated for your service.
Conclusion
In this blog post, we discussed how to deploy and manage containerized applications on Fedora using Docker and Kubernetes. We covered the basics of building a Docker image, running a Docker container, and deploying to Kubernetes. While this is just the tip of the iceberg, it should give you a good starting point to explore the exciting world of containerization and orchestration.