[project]The basics of Docker and K8s
What is a Docker?
A container is a software unit with code and all necessary dependencies to run the application to be used quickly and reliably in various computing environments. Docker is an open platform for applications that separate apps from the infrastructure. It uses a client-server architecture, including images and containers (Docker, n.d.).
Demo
It will be described the essential steps from installing Docker, start and stop a container.
1. Install Docker
Docker needs to be installed on the Linux system. Docker’s package called docker.io can be installed with the following command in Ubuntu.
2. Docker registry
A Docker registry is a place to contain and exchange containers. The registry stores the Docker images. The standard registry is the Docker Hub. The following command shows how to download an Ubuntu image from the Docker Hub.
3. Docker names
There are three name explanations for docker names.
· Repository: the place where the image is stored.
· Tag: The description of the image version. This is a nickname for the image ID.
· Image ID: local image ID.
The following command lists the Docker images on the system.
4. How to start and stop a container from the Ubuntu image
A Docker container is a running process. Multiple containers can be launched out of one image. The following command can run a container from the Ubuntu Docker image.
The following command can check the running containers on the system.
The following command can stop the docker service.
· Main Docker commands
o “docker pull mongo” pulls a mongo image to my local environment
o “docker run” starts in an isolated container using an image.
In this case, I run a docker mongo DB image using port 27017, root username = takahiro, password = takahiro, name = mongotest, network name =mongo-network.
o “docker ps” shows the running containers with dome details, including container ID, images, and port numbers.
o “docker images” shows the list of docker images.
o “docker network create <network-name>” creates a new docker network.
o “docker network ls” shows the network connection between the isolated docker network and outside of its connection.
o “docker rm <container ID>”
o “docker rmi <image ID>”
· Debagging a container
o “docker exec -it <container ID> /bin/sh” enters a selected container
· Docker compose
o Creating a yml file shown below is more common to include all the components rather than executing each command.
o Need to select a file (mongo. yaml) in the same directory and use the “docker-compose up” command to start the file.
o “docker-compose -f <file name> down” stops the running containers.
· Dockerfile
o Docker can establish images automatically by reading a Dockerfile. A Dockerfile is a document that has all the commands a user could use the command line to build an image (Docker, 2021).
o “docker builds -t <image name >: tag <Dcokerfile path>” can create a docker image by reading a Dockerfile.
What is a Kubernetes?
Kubernetes is a container orchestration platform that enables the operation of a scalable web server framework for containerized applications. It manages containers that categorize an application into logical units for better management. Its features are automated rollouts and rollbacks of changes while monitoring application health to ensure that the instances run properly. Kubernetes will roll back the configurations to the previous healthy state when it runs without proper health.
demo
It will be described the essential steps from installing Kubernetes on Ubuntu, creating pods with Kubectl, and cleaning up.
1. Install Kubernetes on Ubuntu
Install the latest MicroK8s version using snap with the following command
2. Create alias with Kubectl
There is a command “kubectl” for managing Kubernetes clusters. It is easy to create a bash alias since the default is “microk8s kubectl”, but most of the documentation writes it as “kubectl.”
3. Check the current version
4. Create Pods with kubectl
The command “kubectl run” creates single pods inside a Kubernetes cluster. In this case, create a container running an Nginx image.
5. Validate Pod
The command “kubectl get pods” can list the pods.
6. Clean up the Pod
The command “kubectl delete pod <pod name>” can delete a pod.
The “kubectl get nodes” command shows the status of nodes in the Kubernetes cluster. The picture above means that the node name “minikube” is ready, and it has a control-plane, master role.
The “minikube status” command shows the status of the minikube node. In this case, it shows that the host is running, and some processes such as kubelet, API server are running as well.
“kubectl version” shows the client and server version in the Git Version. In this case, the client and server are both v1.22.3 versions.
· Kubectl basic commands
Get the list of available services.
Create a deployment called “Nginx-test” using the Nginx image from Dockerhub.
Check the deployment and pod running.
“kubectl exec -it [pod name] — bin/bash” login terminal of the pod.
“kubectl delete deployment [pod name]” delete pod.
In this case, there are four files under the current directory. “kubectl apply -f filename” command can create the file. Here, mongo-config. yaml is used to develop configmap/mongo-config.
The “kubectl get all” command shows all the nodes in the cluster.
“kubectl describe service (service name)” shows the service details.
“kubectl logs (pod name)” shows the log information of the target pod.
· Basic configuration settings
There are three basic configurations, including metadata, specification, and status.