Kubernetes tutorial – Create simple cluster & Deploy app
In the last post, we have looked at the Introduction & key concepts of Kubernetes platform. Now in this post, we are going to create a new Kubernetes cluster using Minikube. Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node. Minikube is available for Linux, macOS, and Windows systems.
The Minikube CLI provides basic bootstrapping operations for working with your cluster, including start, stop, status, and delete. Also, we are going to use kubectl utility to deploy and manage applications on Kubernetes. Also, you can inspect cluster resources; create, delete, and update components; and look at new clusters.
Step #1. Minikube installation
Download the latest release with the command. This is for Linux, if you’re using other OS, please refer above link.
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Check the download by running the minikube version command:
To check what are the available commands, try minikube from the terminal
$ minikube Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows. Usage: minikube [command] Available Commands: start Starts a local kubernetes cluster. stop Stops a running local kubernetes cluster. version Print the version of minikube.
Step #2.kubectl installation
Download the latest release of
kubectl with the following command:
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
kubectl binary executable.
chmod +x ./kubectl
Move the binary into your PATH.
sudo mv ./kubectl /usr/local/bin/kubectl
To check what are the available kubectl commands,run kubectl from the terminal
$ kubectl kubectl controls the Kubernetes cluster manager. Find more information at https://github.com/kubernetes/kubernetes. Basic Commands (Beginner): create Create a resource from a file or from stdin. expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service run Run a particular image on the cluster set Set specific features on objects run-container Run a particular image on the cluster. This command is deprecated, use "run" instead Basic Commands (Intermediate): get Display one or many resources explain Documentation of resources edit Edit a resource on the server delete Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: rollout Manage the rollout of a resource rolling-update Perform a rolling update of the given ReplicationController scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController Cluster Management Commands: certificate Modify certificate resources. cluster-info Display cluster info top Display Resource (CPU/Memory/Storage) usage. cordon Mark node as unschedulable uncordon Mark node as schedulable drain Drain node in preparation for maintenance taint Update the taints on one or more nodes Troubleshooting and Debugging Commands: describe Show details of a specific resource or group of resources logs Print the logs for a container in a pod attach Attach to a running container exec Execute a command in a container port-forward Forward one or more local ports to a pod proxy Run a proxy to the Kubernetes API server cp Copy files and directories to and from containers. auth Inspect authorization Advanced Commands: apply Apply a configuration to a resource by filename or stdin patch Update field(s) of a resource using strategic merge patch replace Replace a resource by filename or stdin convert Convert config files between different API versions Settings Commands: label Update the labels on a resource annotate Update the annotations on a resource completion Output shell completion code for the specified shell (bash or zsh) Other Commands: api-versions Print the supported API versions on the server, in the form of "group/version" config Modify kubeconfig files help Help about any command plugin Runs a command-line plugin version Print the client and server version information Use "kubectl
--help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
Step #3.Create local cluster
Start the cluster, by running the minikube start command:
Once the cluster is started, we have a running Kubernetes local cluster now. Minikube has started a virtual machine for you, and a Kubernetes cluster is now running in that VM.
To interact with Kubernetes we’ll use the command-line interface,
kubectl. To check if
kubectl is installed you can run the
kubectl version command:
As you can see
kubectl is configured and we can see that both the version of the client and as well as the server. The client version is the
kubectl version; the server version is the Kubernetes version installed on the master. You can also see details about the build.
To view the cluster details, run kubectl cluster-info:
To view the nodes in the cluster, run the kubectl get nodes command:
Step #4 : Deploy Nginx app to one of the nodes of the cluster
Let’s run our first app on Kubernetes with the kubectl run command. The run command creates a new deployment. We need to provide the deployment name and app image location (include the full repository url for images hosted outside Docker hub) , currently I have provided Nginx image. If we want to run the app on a specific port so we could add the
--port parameter as well.
Congrats! We have just deployed the first application by creating a deployment. Following is what the command has done for us:
- Searched for a suitable node where an instance of the application could be run (we have only 1 available node).
- Scheduled the Nginx application to run on that node.
- Configured the cluster to reschedule the instance on a new Node when needed.
Once the application instances are created, a Kubernetes Deployment Controller continuously monitors those instances. If the Node hosting an instance goes down or is deleted, the Deployment controller replaces it.
A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker or rkt), and some shared resources for those containers. Those resources include:
- Shared storage, as Volumes
- Networking, as a unique cluster IP address
- Information about how to run each container, such as the container image version or specific ports to use
A Pod always runs on a Node. As discussed earlier, Node is a nothing but a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster. The Master’s automatic scheduling takes into account the available resources on each Node.
Every Kubernetes Node runs at least:
- Kubelet – responsible for communication between the Kubernetes Master and the Nodes
- Container runtime (like Docker, rkt)
For example, a Pod might include both the container with your Nginx app as well as a different container that feeds the data to be published by the Nginx webserver. The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.
To list your deployments, use the get deployments command:
Here we can see that there is 1 deployment running a single instance of the app.
Some of the useful
kubectl commands are below.
- kubectl get – list resources
- kubectl describe – show detailed information about a resource
- kubectl logs – print the logs from a container in a pod
- kubectl exec – execute a command on a container in a pod
Step #5 : Expose Nginx app outside of the cluster
To expose the app on to the outside world, use expose deployment command:
Pods that are running inside Kubernetes are running on a private, isolated network. By default, they are visible from other pods and services within the same Kubernetes cluster, but not outside that network. On some platforms (for example Google Compute Engine) the
kubectl command can integrate with your cloud provider to add a public IP address for the pods, to do this run:
To see the Nginx landing page, you can check the same at http://localhost:80
Also, note in order to access your Nginx landing page, you also have to make sure that traffic from external IPs is allowed. Do this by opening a firewall to allow traffic on port 80.
kubectl get services
This should print the service that has been created, and map an external IP address to the service. Where to find this external IP address will depend on the environment you run in. For instance, for Google Compute Engine the external IP address is listed as part of the newly created service and can be retrieved by running the above command.
A Service in Kubernetes is an abstraction that defines a logical set of Pods and a policy by which to access them. Services enable a loose coupling between dependent Pods. A Service routes traffic across a set of Pods. Services are the abstraction that allow pods to die and replicate in Kubernetes without impacting your application. Discovery and routing among dependent Pods (such as the frontend and backend components in an application) is handled by Kubernetes Services.
Step #6: Delete app
To delete the app, run delete deployment command
kubectl delete deployment my-nginx
In the next tutorial, we will learn how to scale & perform updates to the app on the cluster.
Like this post? Don’t forget to share it!
Additional Resources :
- Official documentation as a reference to understand any command.
- Take a free course on Building Scalable Java Microservices with Spring Boot and Spring Cloud
- Kubernetes tutorial – Scale & perform updates to your app
- Kubernetes tutorial – Create deployments using YAML file
- If you’re looking for Kubernetes examples, here it is GitHub