Docker and Kubernetes Tutorial

1 lug 2023 3 min di lettura
Docker and Kubernetes Tutorial
Indice dei contenuti

Introduction

Docker is a platform that simplifies software deployment by allowing you to bundle an application with its runtime environment into a single package called a container. This ensures that the application will run the same regardless of where it's deployed.

Kubernetes (often abbreviated as K8s) is an open-source container-orchestration system. It's used to automate deployment, scaling, and management of containerized applications, such as those created with Docker.

Prerequisites

Step 1: Docker - Building an Image

First, let's create a simple Dockerfile for a Node.js app.

Create a new directory on your local machine and navigate into it.

mkdir my-node-app && cd my-node-app

Create a file named Dockerfile.

touch Dockerfile

Open the Dockerfile and add the following code:

# Use an official Node.js runtime as the base image
FROM node:14

# Set the working directory in the container to /app
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install the application dependencies
RUN npm install

# Copy the rest of your application's source code to the working directory
COPY . .

# Make the container listen on port 8080 at runtime
EXPOSE 8080

# Define the command to run the app
CMD [ "node", "server.js" ]

Build the Docker image using the docker build command.

docker build -t my-node-app .

Step 2: Docker - Running a Container

Once the image is built, you can run it as a container using docker run.

docker run -p 8080:8080 -d my-node-app

This command tells Docker to run a container with the my-node-app image, map port 8080 inside the Docker container to port 8080 on the host machine, and run the container in the background (detached mode).

Step 3: Kubernetes - Creating a Deployment

Kubernetes manages Docker containers through objects like Pods and Deployments. A Pod is the smallest and simplest Kubernetes object. It represents a single instance of a running process in a cluster and can contain one or more containers.

Create a file named deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
      - name: my-node-app
        image: my-node-app:latest
        ports:
        - containerPort: 8080

Apply this Deployment to your Kubernetes cluster using the kubectl apply command.

kubectl apply -f deployment.yaml

This Deployment creates three replicated Pods that run the my-node-app container. If a Pod goes down, the Deployment will automatically recreate it to maintain a count of three.

Step 4: Kubernetes - Exposing Your Deployment

Once the Deployment is up, you might want to expose it to the Internet. In Kubernetes, you can do this using a Service, which is an abstraction that defines a set of Pods and a policy by which to access them.

Create a file named service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: my-node-app
spec:
  selector:
    app: my-node-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Apply this Service to your Kubernetes cluster using the kubectl apply command.

kubectl apply -f service.yaml

This Service exposes the my-node-app Deployment outside of the cluster. The type "LoadBalancer" ensures that the Service is accessible through a dedicated network IP on the cloud, if supported by your cloud provider.

Step 5: Kubernetes - Verifying Deployment and Service

To verify if the Deployment is working as expected, run the following command:

kubectl get deployments

You should see the "my-node-app" Deployment with three replicas.

Similarly, to verify the Service, use the following command:

kubectl get services

You should see the "my-node-app" Service with an external IP address. You can use this IP address to access your application from a web browser.

Conclusion

And that's it! This is a very basic example, but it should give you an idea of how to use Docker and Kubernetes to containerize an application and deploy it to a Kubernetes cluster.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.