SlideShare a Scribd company logo
Kubernetes for beginners
Dominique Dumont
Debian Project
Nov 2017
Dominique Dumont k8s for beginners
Why Kubernetes ?
docker is nice, but containers tend to multiply
needs to manage / orchestrate them
need to handle network setup between all these images (micro
services beget mega network)
handle ”smooth” updates
Solutions: docker swarm, Kubernetes (other ?)
Dominique Dumont k8s for beginners
New concepts and terminology
New concepts compared to docker:
pod: a group of containers that provide a functionality
node: a worker machine (or VM). old term: minion
deployment: controls lifecycle of a group of pods (rolling
upgrades)
service: deployment frontend. Configure load-balancer and
external access
Dominique Dumont k8s for beginners
Pod in more details
A pod is a group of containers:
share the same IP address.
Must set different listening
ports on each container
declare external ports that
are mapped to container
ports
can mount shared volumes
can be configured with
ConfigMaps and Secrets
through environment
variables or files
Pod
Container
Dominique Dumont k8s for beginners
Deployment
A deployment with its
ReplicaSet is a group of
pods (of the same kind):
ReplicaSet ensures
failover
manage liveness and
readiness probes
manage rolling
upgrades
Deployment
Pod
Container
Replica
controler
Pod
Container
Pod
Container
Dominique Dumont k8s for beginners
Services
A Service is the external front end of a Deployment:
manage load balancing between the pod instances of a
Deployment
map port between external port (e.g. 80) and container ports
(e.g. 8080)
Deployment
Service
Pod
Container
Replica
controler
Balancer
Load
Pod
Container
Pod
Container
Dominique Dumont k8s for beginners
Pod example
Inside a deployment:
spec:
containers:
- name: my-contained-server
image: my-contained-image:latest
imagePullPolicy: Always
livenessProbe:
httpGet: { path: /ping, scheme: HTTPS }
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 3
readinessProbe: # similar
volumeMounts:
- name: shared-stuff
mountPath: "/var/lib/shared"
- name: side-car
image: side-car:latest
Dominique Dumont k8s for beginners
Deployment example
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: super-duper-server
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate: { maxUnavailable: 0, maxSurge: 2 }
template:
metadata:
name: super-mega-server
labels: # used by Service
my-server: mega-server
spec:
containers:
[ pod specification ]
Dominique Dumont k8s for beginners
Service example
apiVersion: v1
kind: Service
metadata:
labels:
name: my-super-service
name: super-service
spec:
ports:
- {port: 443, targetPort: 8090}
type: LoadBalancer
# load balancer target
selector:
my-server: mega-server
Dominique Dumont k8s for beginners
Config
namespace: isolated sandboxes within a cluster. Great for
tests
context: associate cluster and namespace
Commands:
$ kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
minikube minikube minikube
* dev-us-west-2 k-uw2.xxx.com us-west-2-user
dod-dev-us-w2 k-uw2.xxx.com us-west-2-user dod-playground
$ kubectl config use-context dod-dev-us-w2
$ kubectl config set-context ...
Dominique Dumont k8s for beginners
Config
namespace: isolated sandboxes within a cluster. Great for
tests
context: associate cluster and namespace
Commands:
$ kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
minikube minikube minikube
* dev-us-west-2 k-uw2.xxx.com us-west-2-user
dod-dev-us-w2 k-uw2.xxx.com us-west-2-user dod-playground
$ kubectl config use-context dod-dev-us-w2
$ kubectl config set-context ...
Dominique Dumont k8s for beginners
Sending config
Create Deployment or services:
$ kubectl apply -f .../file.yaml
Update a deployment:
$ kubectl replace -f .../file.yaml
Dominique Dumont k8s for beginners
Kubernetes console
If enabled by admin, cluster can be controled with a web interface
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
Dominique Dumont k8s for beginners
Connecting to a container
In enabled by sys admin, you can connect to a container in the
cluster:
$ kubectl exec --namespace francois-sandbox 
fco-mode1-n9r-812209105-9wfcs -c omg date
Thu Oct 26 18:01:58 UTC 2017
$ kubectl exec --namespace francois-sandbox
fco-mode1-n9r-812209105-9wfcs -c omg -ti sh / #
Dominique Dumont k8s for beginners
Getting logs
Getting log may be the only way to get debug information. To get
log from a container:
$ kubectl logs --namespace francois-sandbox 
fco-mode1-n9r-812209105-9wfcs omg --since 10s
$ kubectl logs --namespace francois-sandbox 
fco-mode1-n9r-812209105-9wfcs omg -f
Logs from many pods
Problems can occur in any deployed pod. You need to setup a log
aggregator (kibana...)
Dominique Dumont k8s for beginners
Auto-completion
Typings all these options and arguments is tedious and error prone.
Add this in your ~/.bashrc
KFILE=/tmp/kube-completion
if [ -f /usr/local/bin/kubectl ]; then
/usr/local/bin/kubectl completion bash > $KFILE
. $KFILE
rm $KFILE
fi
Dominique Dumont k8s for beginners
Other tools
You can have your prompt display the current environment:
For a command prompt that shows your context, add this in your
~/.bashrc
NORMAL="[033[00m]"
YELLOW="[e[1;33m]"
__kube_prompt() {
# Get current context
CONTEXT=$(perl -nE ’print if s/current-context: //;’ 
~/.kube/config)
if [[ -n "$CONTEXT" ]]
then
echo "$YELLOW(k8s: ${CONTEXT})$NORMALn"
fi
}
PS1=$(__kube_prompt)’${USER}@${HOSTNAME}:${PWD/$HOME/~}$ ’
Dominique Dumont k8s for beginners
Other tools
You can have your prompt display the current environment:
For a command prompt that shows your context, add this in your
~/.bashrc
NORMAL="[033[00m]"
YELLOW="[e[1;33m]"
__kube_prompt() {
# Get current context
CONTEXT=$(perl -nE ’print if s/current-context: //;’ 
~/.kube/config)
if [[ -n "$CONTEXT" ]]
then
echo "$YELLOW(k8s: ${CONTEXT})$NORMALn"
fi
}
PS1=$(__kube_prompt)’${USER}@${HOSTNAME}:${PWD/$HOME/~}$ ’
Dominique Dumont k8s for beginners
MiniKube
MiniKube lets you play with kubernetes your laptop.
Can use kubernetes console to control your minikube
Note: docker commands deal with a docker daemon running
in minikube, not with your ”regular” docker daemon
Dominique Dumont k8s for beginners

More Related Content

kubernetes for beginners

  • 1. Kubernetes for beginners Dominique Dumont Debian Project Nov 2017 Dominique Dumont k8s for beginners
  • 2. Why Kubernetes ? docker is nice, but containers tend to multiply needs to manage / orchestrate them need to handle network setup between all these images (micro services beget mega network) handle ”smooth” updates Solutions: docker swarm, Kubernetes (other ?) Dominique Dumont k8s for beginners
  • 3. New concepts and terminology New concepts compared to docker: pod: a group of containers that provide a functionality node: a worker machine (or VM). old term: minion deployment: controls lifecycle of a group of pods (rolling upgrades) service: deployment frontend. Configure load-balancer and external access Dominique Dumont k8s for beginners
  • 4. Pod in more details A pod is a group of containers: share the same IP address. Must set different listening ports on each container declare external ports that are mapped to container ports can mount shared volumes can be configured with ConfigMaps and Secrets through environment variables or files Pod Container Dominique Dumont k8s for beginners
  • 5. Deployment A deployment with its ReplicaSet is a group of pods (of the same kind): ReplicaSet ensures failover manage liveness and readiness probes manage rolling upgrades Deployment Pod Container Replica controler Pod Container Pod Container Dominique Dumont k8s for beginners
  • 6. Services A Service is the external front end of a Deployment: manage load balancing between the pod instances of a Deployment map port between external port (e.g. 80) and container ports (e.g. 8080) Deployment Service Pod Container Replica controler Balancer Load Pod Container Pod Container Dominique Dumont k8s for beginners
  • 7. Pod example Inside a deployment: spec: containers: - name: my-contained-server image: my-contained-image:latest imagePullPolicy: Always livenessProbe: httpGet: { path: /ping, scheme: HTTPS } initialDelaySeconds: 15 periodSeconds: 10 timeoutSeconds: 3 readinessProbe: # similar volumeMounts: - name: shared-stuff mountPath: "/var/lib/shared" - name: side-car image: side-car:latest Dominique Dumont k8s for beginners
  • 8. Deployment example apiVersion: extensions/v1beta1 kind: Deployment metadata: name: super-duper-server spec: replicas: 1 strategy: type: RollingUpdate rollingUpdate: { maxUnavailable: 0, maxSurge: 2 } template: metadata: name: super-mega-server labels: # used by Service my-server: mega-server spec: containers: [ pod specification ] Dominique Dumont k8s for beginners
  • 9. Service example apiVersion: v1 kind: Service metadata: labels: name: my-super-service name: super-service spec: ports: - {port: 443, targetPort: 8090} type: LoadBalancer # load balancer target selector: my-server: mega-server Dominique Dumont k8s for beginners
  • 10. Config namespace: isolated sandboxes within a cluster. Great for tests context: associate cluster and namespace Commands: $ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE minikube minikube minikube * dev-us-west-2 k-uw2.xxx.com us-west-2-user dod-dev-us-w2 k-uw2.xxx.com us-west-2-user dod-playground $ kubectl config use-context dod-dev-us-w2 $ kubectl config set-context ... Dominique Dumont k8s for beginners
  • 11. Config namespace: isolated sandboxes within a cluster. Great for tests context: associate cluster and namespace Commands: $ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE minikube minikube minikube * dev-us-west-2 k-uw2.xxx.com us-west-2-user dod-dev-us-w2 k-uw2.xxx.com us-west-2-user dod-playground $ kubectl config use-context dod-dev-us-w2 $ kubectl config set-context ... Dominique Dumont k8s for beginners
  • 12. Sending config Create Deployment or services: $ kubectl apply -f .../file.yaml Update a deployment: $ kubectl replace -f .../file.yaml Dominique Dumont k8s for beginners
  • 13. Kubernetes console If enabled by admin, cluster can be controled with a web interface $ kubectl proxy Starting to serve on 127.0.0.1:8001 Dominique Dumont k8s for beginners
  • 14. Connecting to a container In enabled by sys admin, you can connect to a container in the cluster: $ kubectl exec --namespace francois-sandbox fco-mode1-n9r-812209105-9wfcs -c omg date Thu Oct 26 18:01:58 UTC 2017 $ kubectl exec --namespace francois-sandbox fco-mode1-n9r-812209105-9wfcs -c omg -ti sh / # Dominique Dumont k8s for beginners
  • 15. Getting logs Getting log may be the only way to get debug information. To get log from a container: $ kubectl logs --namespace francois-sandbox fco-mode1-n9r-812209105-9wfcs omg --since 10s $ kubectl logs --namespace francois-sandbox fco-mode1-n9r-812209105-9wfcs omg -f Logs from many pods Problems can occur in any deployed pod. You need to setup a log aggregator (kibana...) Dominique Dumont k8s for beginners
  • 16. Auto-completion Typings all these options and arguments is tedious and error prone. Add this in your ~/.bashrc KFILE=/tmp/kube-completion if [ -f /usr/local/bin/kubectl ]; then /usr/local/bin/kubectl completion bash > $KFILE . $KFILE rm $KFILE fi Dominique Dumont k8s for beginners
  • 17. Other tools You can have your prompt display the current environment: For a command prompt that shows your context, add this in your ~/.bashrc NORMAL="[033[00m]" YELLOW="[e[1;33m]" __kube_prompt() { # Get current context CONTEXT=$(perl -nE ’print if s/current-context: //;’ ~/.kube/config) if [[ -n "$CONTEXT" ]] then echo "$YELLOW(k8s: ${CONTEXT})$NORMALn" fi } PS1=$(__kube_prompt)’${USER}@${HOSTNAME}:${PWD/$HOME/~}$ ’ Dominique Dumont k8s for beginners
  • 18. Other tools You can have your prompt display the current environment: For a command prompt that shows your context, add this in your ~/.bashrc NORMAL="[033[00m]" YELLOW="[e[1;33m]" __kube_prompt() { # Get current context CONTEXT=$(perl -nE ’print if s/current-context: //;’ ~/.kube/config) if [[ -n "$CONTEXT" ]] then echo "$YELLOW(k8s: ${CONTEXT})$NORMALn" fi } PS1=$(__kube_prompt)’${USER}@${HOSTNAME}:${PWD/$HOME/~}$ ’ Dominique Dumont k8s for beginners
  • 19. MiniKube MiniKube lets you play with kubernetes your laptop. Can use kubernetes console to control your minikube Note: docker commands deal with a docker daemon running in minikube, not with your ”regular” docker daemon Dominique Dumont k8s for beginners