Introduction Containerization is a powerful technology that allows developers to create, deploy and run applications in an efficient and scalable way. Docker is a popular containerization tool that enables developers to package their applications into containers, which can then be deployed and run on any platform that supports Docker. Kubernetes is an open-source platform that helps you automate the deployment, scaling, and management of containerized applications. In this tutorial, we will guide you through the steps to deploy and manage containerized applications on CentOS 7 with Docker and Kubernetes.
Prerequisites Before we begin, you need to have the following:
- A CentOS 7 server with Docker and Kubernetes installed.
- A basic understanding of Docker and Kubernetes concepts.
Step-1: Install Docker
To install Docker on CentOS 7, you need to run the following commands:
sudo yum update sudo yum install docker sudo systemctl start docker sudo systemctl enable docker
Step-2: Install Kubernetes
To install Kubernetes on CentOS 7, you need to run the following commands:
sudo yum install -y kubeadm kubelet kubectl sudo systemctl start kubelet sudo systemctl enable kubelet
Step-3: Deploy a Docker container
To deploy a Docker container, you need to create a Dockerfile that describes the application you want to run. Here’s an example Dockerfile for a simple web application:
FROM nginx COPY index.html /usr/share/nginx/html/index.html
Save this file as Dockerfile and run the following command to build the image:
sudo docker build -t my-web-app .
This will create a Docker image called my-web-app.
To run the container, run the following command:
sudo docker run -d -p 80:80 my-web-app
This will start a container based on the my-web-app image and map port 80 on the host to port 80 on the container. You can now access your web application by visiting http://localhost in your browser.
Step 4: Deploy a Kubernetes pod
To deploy a Kubernetes pod, you need to create a YAML file that describes the pod. Here’s an example YAML file for a pod that runs the my-web-app container:
apiVersion: v1 kind: Pod metadata: name: my-web-app-pod spec: containers: - name: my-web-app-container image: my-web-app ports: - containerPort: 80
Save this file as my-web-app-pod.yaml and run the following command to create the pod:
sudo kubectl apply -f my-web-app-pod.yaml
This will create a pod called my-web-app-pod that runs the my-web-app container.
Step 5: Deploy a Kubernetes service
To access the application running in the pod, you need to create a Kubernetes service. Here’s an example YAML file for a service that exposes the my-web-app container:
apiVersion: v1 kind: Service metadata: name: my-web-app-service spec: selector: app: my-web-app ports: - name: http port: 80 targetPort: 80 type: NodePort
Save this file as my-web-app-service.yaml and run the following command to create the service:
sudo kubectl apply -f my-web-app-service.yaml
This will create a service called my-web-app-service that exposes the my-web-app container on port 80.
Step 6: Access the application
To access the application, you need to find the IP address and port of the Kubernetes node that the my-web-app-service is running on. You can do this by running the following command:
sudo kubectl get nodes -o wide
This will display a list of all the Kubernetes nodes and their IP addresses.
Next, you need to find the port that the my-web-app-service is running on. You can do this by running the following command:
sudo kubectl describe service my-web-app-service
This will display detailed information about the my-web-app-service, including the port it is running on.
Now, you can access your application by visiting the IP address of the node and the port of the service in your browser. For example, if the IP address of the node is 192.168.0.1 and the port of the service is 31000, you can access your application by visiting http://192.168.0.1:31000 in your browser.
Step 7: Scaling the application
One of the key benefits of using Kubernetes is its ability to scale your application automatically based on demand. To scale your application, you can use the following command:
sudo kubectl scale --replicas=3 deployment/my-web-app-deployment
This will scale the deployment to three replicas, which means that three instances of your application will be running at the same time.
Step 8: Updating the application To update your application, you can modify the Dockerfile to include your changes and then rebuild the image. You can then update the Kubernetes deployment to use the new image by running the following command:
sudo kubectl set image deployment/my-web-app-deployment my-web-app=my-new-web-app-image
This will update the my-web-app-deployment to use the new my-new-web-app-image.
Conclusion In this tutorial, we have shown you how to deploy and manage containerized applications on CentOS 7 with Docker and Kubernetes. We covered the basics of Docker and Kubernetes, and we walked you through the process of deploying a Docker container and a Kubernetes pod and service. We also showed you how to scale and update your application. By following these steps, you can take advantage of the power and flexibility of containerization and Kubernetes to deploy and manage your applications with ease.