Basically: they make environments more consistent & manageable
+
A kubernetes cluster contains:
Nodes on K8S are the basic worker machine
Masters run a set of components:
Masters can be replicated, to ensure High Availability
A cluster with a failed master can still work (Nodes will execute containers) but won't be able to change/self-heal
The basic building block of K8S
You never really run Pods directly, a Controller does!
A Deployment is used to manage Pods
To be more specific:
Deployments manage ReplicaSets, which in turn are responsible for managing Pods
When you change a Deployment:
An example Deployment (yaml file)
But your apps are not reachable from outside the cluster yet...
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Pods are mortal:
To make sure you can always reach Pods from the same group, Kubernetes uses Services
Services offer:
Service IPs and names are only routable by the cluster network
In the real world we want things like:
This can be done with Ingresses (using an IngressController)
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: k8s.io
http:
paths:
- path: /foo
backend:
serviceName: foo-service
servicePort: 80
- path: /bar
backend:
serviceName: bar-service
servicePort: 80
http://k8s.io/foo
http://k8s.io/bar
foo-service
bar-service
IngressControllers are usually implemented with reverse proxies (e.g. nginx)
ConfigMaps are used to decouple parameters from containers
kind: ConfigMap
apiVersion: v1
metadata:
name: environment-variables
labels:
name: environment-variables
data:
ENV: "test"
PORT: 8080
An example:
a map containing global environment variables
(Deployments can reference it)
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: redis
env:
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
Definition
Usage as an environment variable
Like a Deployment, but for infrastructure
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
containers:
- name: fluentd-elasticsearch
image: k8s.gcr.io/fluentd-elasticsearch:1.20
# pretty much, looks like a deployment
Example:
running fluentd to collect logs on every Node and send them to elasticsearch
Can be done with HorizontalPodAutoscalers
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
Example
Scale up the "php-apache" Deployment whenever CPU usage is > 50%
Will scale up to 10 replicas and go down to 1 if possible