KISS Kubernetes: Building Your First Container Management Cluster
Interested in getting started with Kubernetes? Then this post is for you! We take a look at how to build your first container management cluster.
Join the DZone community and get the full member experience.Join For Free
in the space of a few years, container platforms like docker have become mainstream tools for deploying applications and microservice architectures. they offer gains in speed, efficiency, and security. they also fit well with another rising star, devops. but container platforms typically do not offer tools for managing containers at scale. manual management processes may work for a few containers and the apps or microservices they contain, but they rapidly become unworkable as the number of containers rises.
spotting the problem, or indeed the opportunity to improve matters, google created kubernetes to automate container management. kubernetes (greek for “pilot (of a ship)”) deploys, scales, and manages “boatloads” of containers for you. the building blocks for the kubernetes technology are reasonably straightforward, at least for initial working configurations.
in this article, we go through the main steps of getting your first kubernetes management cluster up and running. we use the kiss (keep it simple, stupid!) approach to focus on the key points. more advanced aspects are left as subjects for other posts. we also assume you have a basic understanding of containers, images, and container platforms like docker or rocket.
kubernetes concepts 101
a common understanding of terms is always a good idea. here’s a mini-glossary for kubernetes.
- master . the server or machine running kubernetes and directing the container management activities on other servers.
- nodes . the other servers, also known as slaves or minions, running kubernetes components like agents and proxies, as well as pods (see below) that hold the containers. nodes take their orders from, and report back to, the master.
- pod . the smallest manageable unit for kubernetes. a pod may contain one container, several containers, a complete app or part of an app. in other words, a pod is also flexible.
- service . an interface (endpoint) for access to one or more pods on a node. acts as a proxy and does load balancing across a group of replicated pods. pods may come and go, but a service is designed to stay put.
- replication controller . management functionality for ensuring deployment of the required number of pods. will automatically create or eliminate pods, as needed to maintain the required state. newer functionality (kubernetes “deployment”) now offers an advanced alternative to replication controllers, but for the sake of simplicity, we still discuss them here.
- kubectl . a command line interface for you to tell kubernetes what to do.
kubernetes terminology extends further, but this is enough for us to get started.
getting and installing kubernetes
google made kubernetes freely available and free to use. you can:
- download binaries for installation in a linux or similar environment (linux running in windows, etc.)
- download a version called minikube to automatically install a master and a node on the same machine (possibilities include linux, mac os x and windows).
a minikube installation can greatly simplify things. initial installation is often much easier. master-node communication is done within the same (virtual) machine, so network configuration is not a factor. minikube also conveniently includes container runtime functionality (the docker engine). later, you may well want the master on one machine and nodes on others. to start with, however, the minikube installation for mac os x as an example can be as simple as running the following two commands:
brew cask install minikube brew install kubectl
you can also try out this kubernetes online server that lets you launch kubernetes and try out its commands, even before you proceed to an installation of your own.
after installation, starting minikube is simple too:
kubectl and apiserver
when kubernetes is installed (via minikube or another way), it makes an api available through which you can issue different kubernetes commands. the component for this is called apiserver and it is part of the master. kubectl, the cli, communicates with apiserver. kubectl can communicate locally, as in minikube, or remotely, for example when the kubernetes master is running on another machine or in the cloud. so, when you’re ready, you can simply point kubectl to a remote installation from the same machine on which you started with minikube. of course, by that stage you may be accessing apiserver programmatically anyway, rather than via the cli.
starting a kubernetes pod
a pod holds one or more containers. pods are the ultimate management goal of kubernetes, so let’s make a pod and start managing it. a simple way to do this is to make a yaml file with the basic pod configuration details inside. after, we’ll use kubectl to make a pod, simply giving it the file name to point it to the configuration of the pod to be made. the configuration file (“
” for instance) will have content like this:
apiversion: v1 kind: pod metadata: name: my_pod labels: app: my_app spec: containers: name: my_app image: my_app ports: containerport: 80
the kubectl command to make the pod looks like this:
kubectl create -f pod1.yml
we can check that the pod now exists by entering:
kubectl get pods
to find out more about the range of kubectl commands available, simply enter:
creating a kubernetes service
next, we’ll make a kubernetes service. the service will let us address a group of replicated pods as one entity. it will also do load balancing across the group. we can use the same approach as for making a pod, i.e. make another yaml file (“
”), this time with the service configuration details. the contents of the file will look something like this:
apiversion: v1 kind: service metadata: name: my_service spec: selector: app: my_app version: v1 ports: protocol: tcp port: 4000
the kubectl command to make the service looks like this:
kubectl create -f service1.yml
we can check that the service now exists by entering:
kubectl get svc
making a replication controller
we’re now ready for some kubernetes automation, albeit on a local basis. the replication controller for a group of replicated pods makes sure that the number of pods you specify is also the number of pods running at any given time. once again, we can create another yaml file (“
”) along the lines of the following:
apiversion: v1 kind: replicationcontroller metadata: name: my_rc labels: app: my_app version: v1 spec: replicas: 8 selector: app: my_app version: v1 template: metadata labels: app: my_app version: v1 spec: containers: name: my_app image: (path)/my_app:1.0.0 ports: containerport: 4000
the kubectl command to make the replication controller looks like this:
kubectl create -f rc1.yml
when you now use the command:
kubectl get pods
you will see eight pods running, because of the “
” instruction in the “
” yaml file. kubernetes also uses information in the yaml files we have shown here to associate
, so that somebody addressing
will now be routed by
to one of the eight pods created by
the replication controller keeps track of each pod via a label that kubernetes gives the pod, in the form of a key/value pair. the kubernetes master keeps all such labels in an etcd key value store. after you have started the replication controller, you can try deleting (kubectl command “
”) one of the replicated pods to see how the replication controller automatically detects the deletion because a known label has disappeared, and makes a new replicated pod to replace the deleted one.
setting up separate kubernetes nodes
so far, we’ve done everything on the same local machine. this is good for seeing how to start using kubernetes. it is also a useful environment for development, so the local one-master-one-node cluster that we have set up already brings benefit.
the next step is to distribute kubernetes management of containers over multiple nodes that are also remote from the master. this allows you to make use of additional kubernetes functionality, like its ability to decide which servers to use for which containers, making efficient use of server resources as it goes. the kubernetes components in the master are the apiserver, the etcd key value store, a scheduler, and a controller-manager. for each node, the components are an agent (kubelet) for communicating with the master, a container runtime component (like docker), and a proxy (a kubernetes load-balancing service).
the main steps to split out the master and the nodes are:
make sure the system with the master and the node systems can resolve each other’s address (for example, by adding
/etc/hostsentries for linux systems)
etcdcomponents to listen on the network connecting the master and the nodes
- install the kubernetes-node package on each node
- install and configure tunneling (such as flannel) for inter-node communication.
more advanced steps
further possibilities with kubernetes include automated rollover and rollback for new container images, the use of a graphical dashboard instead of a command line interface, and the addition of monitoring tools. kubernetes also continues to be actively supported and developed, so expect new functionality and possibilities to arrive at regular intervals to supplement what we have discussed here. sooner or later, our kiss approach will have to be modified, too. simplicity can only take you so far, but at least far enough to already get the benefit of a local kubernetes management cluster, as we have described in this article.
Published at DZone with permission of Izzy Azeri, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.