This blog discusses some of the Kubernetes design patterns. All source code for the design patterns discussed below are available at kubernetes-java-sample.
Key Concepts of Kubernetes
At a very high level, there are three key concepts:
- Pods are the smallest deployable units that can be created, scheduled, and managed. Its a logical collection of containers that belong to an application.
- Master is the central control point that provides a unified view of the cluster. There is a single master node that control multiple minions.
- Node is a worker node that run tasks as delegated by the master. Minions can run one or more pods. It provides an application-specific “virtual host” in a containerized environment.
Some other concepts to be aware of:
- Replication Controller is a resource at Master that ensures that requested number of pods are running on nodes at all times.
- Service is an object on master that provides load balancing across a replicated group of pods.
- Label is an arbitrary key/value pair in a distributed watchable storage that the Replication Controller uses for service discovery.
Start Kubernetes Cluster
- Easiest way to start a Kubernetes cluster on a Mac OS is using Vagrant:
export KUBERNETES_PROVIDER=vagrant curl -sS https://get.k8s.io | bash
- Alternatively, Kubernetes can be downloaded from github.com/GoogleCloudPlatform/kubernetes/releases/download/v1.0.0/kubernetes.tar.gz, and cluster can be started as:
cd kubernetes export KUBERNETES_PROVIDER=vagrant cluster/kube-up.sh
A Pod with One Container
This section will explain how to start a Pod with one Container. WildFly base Docker image will be used as the Container.
Pod, Replication Controller, Service, etc are all resources in Kubernetes. They can be created using the kubectl by using a configuration file.
The configuration file in this case:
apiVersion: v1 kind: Pod metadata: name: wildfly-pod labels: name: wildfly spec: containers: - image: jboss/wildfly name: wildfly-pod ports: - containerPort: 8080
Complete details on how to create a Pod are explained at github.com/arun-gupta/kubernetes-java-sample#a-pod-with-one-container
Java EE Application Deployed in a Pod with One Container
This section will show how to deploy a Java EE application in a Pod with one Container. WildFly, with an in-memory H2 database, will be used as the container.
Configuration file is:
apiVersion: v1 kind: ReplicationController metadata: name: javaee7-hol labels: name: javaee7-hol spec: replicas: 1 selector: name: javaee7-hol template: metadata: labels: name: javaee7-hol spec: containers: - name: master image: arungupta/javaee7-hol ports: - containerPort: 8080 hostPort: 8080
A Replication Controller with Two Replicas of a Pod
This section will explain how to start a Replication Controller with two replicas of a Pod. Each Pod will have one WildFly container.
Configuration file is:
apiVersion: v1 kind: ReplicationController metadata: name: wildfly-rc labels: name: wildfly context: docker-k8s-lab spec: replicas: 1 template: metadata: labels: name: wildfly spec: containers: - name: wildfly-rc-pod image: arungupta/wildfly-mysql-javaee7:k8s ports: - containerPort: 8080
Replication Controller ensures that specified number of pod “replicas” are running at any one time. If there are too many, the replication controller kills some pods. If there are too few, it starts more.
Complete details at github.com/arun-gupta/kubernetes-java-sample#rescheduling-pods.
Replication Controller allows dynamic scaling up and down of Pods.
Complete details at github.com/arun-gupta/kubernetes-java-sample#scaling-pods.
Pods are ephemeral. IP address assigned to a Pod cannot be relied upon. Kubernetes, Replication Controller in particular, create and destroy Pods dynamically. A consumer Pod cannot rely upon the IP address of a producer Pod.
Kubernetes Service is an abstraction which defines a set of logical Pods. The set of Pods targeted by a Service are determined by labels associated with the Pods.
This section will show how to run a WildFly and MySQL containers in separate Pods. WildFly Pod will talk to the MySQL Pod using a Service.
Complete details at github.com/arun-gupta/kubernetes-java-sample#kubernetes-service.
Here are couple of blogs that will help you get started:
The complete set of Kubernetes blog entries provide more details.
|Kubernetes Design Patterns from our JCG partner Arun Gupta at the Miles to go 2.0 … blog.