Dzone Refcardz Kubernetes Rc233
Dzone Refcardz Kubernetes Rc233
Kubernetes
What Is Kubernetes?
Key Concepts of Kubernetes
Kubernetes Architecture
Getting Started With Kubernetes
Kubernetes, or "k8s" or "kube" for short, allows the user to declaratively A Replica Set creating two instances of a Couchbase pod can be
specify the desired state of a cluster using high-level primitives. For defined as:
example, the user may specify that they want three instances of
the Couchbase server container running. Kubernetes’ self-healing apiVersion: extensions/v1beta1
kind: ReplicaSet
mechanisms, such as auto-restarting, re-scheduling, and replicating metadata:
containers then converge the actual state towards the desired state. name: couchbase-rs
spec:
# Two replicas of the Pod to be created
Kubernetes supports Docker and Rocket containers. An abstraction replicas: 2
around the containerization layer will allow for other container image # Identifies the label key and value on the Pod that
# this Replica Set is responsible for managing
formats and runtimes to be supported in the future. selector:
matchLabels:
app: couchbase-rs-pod
matchExpressions:
KEY CONCEPTS OF KUBERNETES - {key: tier, operator: In, values: ["backend"]}
template:
POD metadata:
A Pod is the smallest deployable unit that can be created, scheduled, labels:
# label key and value on the pod.
and managed. It’s a logical collection of containers that belong to # These must match the selector above.
an application. app: couchbase-rs-pod
tier: backend
spec:
Each resource in Kubernetes is defined using a configuration file. For containers:
- name: couchbase
example, a Couchbase pod can be defined with the following .yaml file: image: couchbase
ports:
- containerPort: 8091
apiVersion: v1
kind: Pod
# labels attached to this Pod
metadata:
name: couchbase-pod
labels:
name: couchbase-pod
spec:
containers:
- name: couchbase
# Docker image that will run in this Pod
image: couchbase
ports:
Improve Performance,
- containerPort: 8091
Minimize Cost
LABEL
A label is a key/value pair that is attached to objects, such as pods. In
Packet is a bare metal cloud built
the previous example, metadata.labels define the labels attached for developers.
to the pod.
Labels define identifying attributes for the object and is only meaningful
Get Started with $25
and relevant to the user. Multiple labels can be attached to a resource.
Labels can be used to organize and to select subsets of objects.
We started Packet to bring the automation experience of the cloud to bare metal
infrastructure. Whether you’re a cloud native developer running container
workloads, an at-scale SaaS platform with massive scale, or a security obsessed
enterprise working through a digital transformation, you’ll find Packet to be
a flexible and performance-focused infrastructure partner.
Packet is a proud member of the Cloud Native Computing Foundation (the home of the Kubernetes project) and
donates the CNCF Community Infrastructure Lab to accelerate adoption through scale and performance testing.
3 KUBERNETES
SERVICE
---
Each Pod is assigned a unique IP address. If the Pod inside a apiVersion: extensions/v1beta1
Replication Set dies, when it the pod is recreated it may be given a kind: ReplicaSet
metadata:
different IP address. This makes it difficult for an application server, name: wildfly-rs
such as WildFly, to access a database, such as Couchbase, using spec:
replicas: 1
its IP address. selector:
matchLabels:
A Service defines a logical set of Pods and a policy by which to access app: wildfly-rs-pod
matchExpressions:
them. The IP address assigned to a Service does not change over time, - {key: tier, operator: In, values: ["frontend"]}
and thus can be relied upon by other Pods. In addition, pods can find template:
metadata:
the services using service discovery either via environment variables labels:
or DNS. app: wildfly-rs-pod
tier: frontend
spec:
NOTE: You can combine a Service and Replica Set in the same yaml containers:
- name: wildfly
file by separating them with ---. image: arungupta/wildfly-couchbase-javaee7
env:
For example, the following creates a comprehensive Couchbase - name: COUCHBASE_URI
value: couchbase-svc
Service and an application Service running in Wildfly exposed via port ports:
30080 that discovers the Couchbase Service using the COUCHBASE_ - containerPort: 8080
URI environment variable and the couchbase-svc DNS value:
VOLUMES
apiVersion: v1
A Volume is a directory on disk or in another container. A volume outlives
kind: Service
metadata: any containers that run within the Pod, and the data is preserved across
name: couchbase-svc
Container restarts. The directory, the medium that backs it, and the
spec:
selector: contents within it are determined by the particular volume type used.
app: couchbase-rs-pod
ports:
- name: admin Multiple types of volumes are supported. Some of the commonly used
port: 8091 volume types are shown below:
- name: views
port: 8092
- name: query VOLUME TYPE MOUNTS INTO YOUR POD
port: 8093
- name: memcached A file or directory from the host node’s
hostPath
port: 11210 filesystem
---
apiVersion: extensions/v1beta1 nfs Existing Network File System share
kind: ReplicaSet
metadata: awsElasticBlockStore An Amazon Web Service EBS Volume
name: couchbase-rs
gcePersistentDisk A Google Compute Engine Persistent Disk
spec:
replicas: 1
selector: A Volume is specified in the Pod configuration file as shown:
matchLabels:
app: couchbase-rs-pod
matchExpressions: apiVersion: extensions/v1beta1
- {key: tier, operator: In, values: ["backend"]} kind: ReplicaSet
template: metadata:
metadata: name: couchbase-rs
labels: spec:
app: couchbase-rs-pod replicas: 1
tier: backend selector:
spec: matchLabels:
containers: app: couchbase-rs-pod
- name: couchbase matchExpressions:
image: arungupta/couchbase:travel - {key: tier, operator: In, values: ["backend"]}
ports: template:
- containerPort: 8091 metadata:
- containerPort: 8092 labels:
- containerPort: 8093 app: couchbase-rs-pod
- containerPort: 11210 tier: backend
--- spec:
apiVersion: v1 containers:
kind: Service - name: couchbase
metadata: image: couchbase
name: wildfly-svc ports:
spec: - containerPort: 8091
type: NodePort volumeMounts:
selector: - mountPath: /var/couchbase/lib
app: wildfly-rs-pod name: couchbase-data
ports: volumes:
- name: http - name: couchbase-data
port: 8080 hostPath:
nodePort: 30080 path: /tmp/couchbase
NOTE: hostPath is a fine option for testing, but it is not suitable for interacting with the cluster. For instructions to install Minikube and its
production use. dependencies, visit kubernetes.io/docs/tasks/tools/install-minikube.
KUBERNETES ARCHITECTURE Once Minikube is installed, you can use the minikube command-line
to start a cluster by running the following command:
A Kubernetes architecture with some key components is shown here:
minikube start
minikube stop
minikube ip
RUN YOUR FIRST CONTAINER If for some reason an instance crashes or gets shut down, Kubernetes
will immediately spin up another one. To destroy the whole thing, you
A Container can be started on a Kubernetes cluster using the kubectl must delete it at the deployment level.
script. The easiest way to do this is to specify the Docker image name
to the run command:
APPLICATION USING MULTIPLE CONTAINERS
kubectl run couchbase --image=arungupta/couchbase Typical applications consist of a "frontend" and a "backend." The
deployment "couchbase" created
"frontend" would typically be an application server, and the "backend"
would typically be a database. For this example, we’ll use WildFly for
This command will start a pre-configured Couchbase container in a
our application server and Couchbase for our database.
Pod wrapped inside a Replica Set wrapped inside a Deployment. The
status of the Deployment can be seen:
kubectl get rs
NAME DESIRED CURRENT READY AGE
couchbase-3179999270 1 1 1 39s
The file couchbase-pod.yaml contains the Pod definition, as • Start the "frontend" Replica Set: The WildFly Replica Set should
explained earlier. contain the spec for the WildFly pod. The Pod should include
the application predeployed. This is typically done by extending
SCALE APPLICATIONS WildFly’s Docker image, copying the WAR file in the /opt/jboss/
wildfly/standalone/deployments directory, and creating a new
Pods in a Replica Set can be scaled up and down:
Docker image. The application can connect to the database by
kubectl scale --replicas=3 deploy/couchbase discovering "backend" services using Environment Variables or DNS.
deployment "couchbase" scaled
NOTE: The Service example above does this all in one file.
Then the updated number of deployments can be seen:
A B O U T T H E AU T H O R
CHRISTOPHER M. JUDD is the CTO and a partner at Manifest Solutions (manifestcorp.com), an international speaker,
an open source evangelist, the Central Ohio Java Users Group (www.cojug.org) and Columbus iPhone Developer User Group
leader, and the co-author of Beginning Groovy and Grails (Apress, 2008), Enterprise Java Development on a Budget (Apress,
2003), and Pro Eclipse JST (Apress, 2005), as well as the author of the children’s book "Bearable Moments." He has spent 20
years architecting and developing software for Fortune 500 companies in various industries, including insurance, health care,
retail, government, manufacturing, service, and transportation. His currentfocus is on consulting, mentoring, and training with
Java, Java EE,Groovy, Grails, Cloud Computing, and mobile platforms like iPhone, Android, Java ME, and mobile web.
DZone communities deliver over 6 million pages each month to more than 3.3 million
software developers, architects and decision makers. DZone offers something for
everyone, including news, tutorials, cheat sheets, research guides, feature articles,
source code and more.
DZONE, INC. REFCARDZ FEEDBACK
"DZone is a developer's dream," says PC Magazine. 150 PRESTON EXECUTIVE DR. WELCOME
refcardz@dzone.com
CARY, NC 27513
SPONSORSHIP
Copyright © 2017 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval
888.678.0399 OPPORTUNITIES
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior
919.678.0300 sales@dzone.com
written permission of the publisher.