Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Workshop Resources
Code: https://github.com/beekpr/public-workshopsSlides:https://tinyurl.com/yc2uo3wk
Make sure minikube and kubectl is setup(labs/1-setup-cluster.md has some instructions)
Kubernetes WorkshopDeploy your applications like a boss
What is Kubernetes?
• Kubernetes is essentially a cluster operating system
• Just like an operating system it has:٠ Kernel
⦿ Scheduler (plays tetris with resources)⦿ ABI (How userspace interacts with kernel)
٠ Userspace⦿ Run processes
Cluster Architecture
• One to several master nodes (provide kernel like services)
• A store of state (etcd)• One to several normal nodes (provide userspace
like services)
What is Kubernetes?
Master Node
Node 1
Node 2
Node 3
Master
Node 1
Node 2
Node 3
Kubernetes Master Components
• kube-apiserver• kube-controller-manager• kube-scheduler
Kubernetes Master Components
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubernetes Node Components
• Applies to master nodes as well• Kubelet agent• Container Runtime Interface (CRI) e.g. docker
Kubernetes Node Components
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
Where can I run kubernetes
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
How do I use kubernetes
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
Client
Kubectl is one client
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
kubectl
Kubectl is one client
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
kubectl
$ kubectl run test --rm -i --tty --image ubuntu -- bash
Kubernetes Basic Concepts
Master Node
Node 1
Node 2
Node 3
ControllerAPI
Scheduler
Kubelet
Kubelet
Kubelet
Kubelet
kubectl
$ kubectl run test --rm -i --tty --image ubuntu -- bash
test
Kubernetes Basic Concepts
• Kubernetes Objects• Namespace• Pod• ConfigMaps and Secrets• Service
Kubernetes Objects
• Persistent entities• Represent state of your cluster• Declarative• Normally specified and returned in YAML format• Loosely coupled
Kubernetes Objects
• Fields٠ apiVersion٠ kind٠ metadata
⦿ name⦿ (namespace)⦿ (labels)
٠ spec/data
apiVersion: v1kind: Podmetadata: name: test-pod namespace: default labels: app=pod env=testspec: containers: - name: container image: busybox command: ['sh', '-c', 'echo Hello World']
Labels• Metadata with semantic meaning• Arbitrary key value pairs• Used as a group mechanism
apiVersion: v1kind: Podmetadata: name: test-pod namespace: default labels: app=pod env=testspec: containers: - name: container image: busybox command: ['sh', '-c', 'echo Hello World']
Namespace
• Way of partitioning cluster, grouping related entities together
• Most Kubernetes objects exist within a namespace (Namespace is an exception as it creates a namespace)
• Let's have a look at our minikube cluster
Namespace - Exercise• List all namespaces
٠ kubectl get namespaces• Describe the default namespace
٠ kubectl describe namespace default• Get yaml representation of default namespace
٠ kubectl get namespace default -o yaml• Create a namespace
٠ kubectl create namespace demo• Get all entities which exist in namespace kube-system
٠ kubectl get all --namespace kube-system• Figure out the current context and cluster
٠ kubectl config get-contexts
Pod
• Smallest schedulable unit in kubernetes• Has a unique IP (all ports available)• Collection of tightly coupled containers (1 or
more)٠ Can share volumes٠ Talk to each other over local interface٠ Scheduled to same physical node
• Should think of them as disposable
Pod - Exercise• Lets do:
٠ labs/2-kubectl.md٠ labs/3-pods.md
• Pods٠ kubectl get pod some-name -o yaml٠ kubectl explain pod.spec
(for description of fields)
Namespace: default
Namespace: kube-system
Pod - Exercise• Lets do:
٠ labs/2-kubectl.md٠ labs/3-pods.md
• Pods٠ kubectl explain pod.spec
(for description of fields)
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Namespace: kube-system
Configuration• Configmaps and secrets allow you to inject configuration into your pods• They can be exposed as
٠ either environment variable٠ file on container filesystem
• Useful for customising containers for different environments٠ Development٠ Production
• Useful for porting existing apps to kubernetes
ConfigurationapiVersion: v1kind: ConfigMapmetadata: name: some-name namespace: defaultspec: index.html: | <html> ... </html> version: "latest”
ConfigurationapiVersion: v1kind: ConfigMapmetadata: name: some-name namespace: defaultspec: index.html: | <html> ... </html> version: "latest"
...spec: containers: - name: nginx env: - name: VERSION valueFrom: configMapKeyRef: name: some-name key: version volumeMounts: - name: html mountPath: /etc/nginx/html ... volumes: - name: nginx configMap: name: some-name items: - key: "index.html" path: "index.html"
Configuration - Exercise• Lets do:
٠ labs/5-configuration.md• Pods
٠ kubectl explain configmap٠ kubectl explain secret
(for description of fields)
Namespace: default
Namespace: kube-system
Service• Pods are often short-lived, as such we don’t want to keep track of them, at the
same time we want a way to use them٠ Sounds like DNS
• Services provide this abstraction, they give us a way to٠ Group pods based on labels٠ Route traffic from port on service to port on pod (can be different)
• They provide (for lifetime of service)٠ A unique persistent cluster IP٠ DNS resolution٠ Port resolution
• Have different types:٠ ClusterIP, Loadbalancer, ExternalName
Servicekind: Service
apiVersion: v1
metadata:
name: "demo-app"
spec:
selector:
app: "demo-app"
ports:
- protocol: "TCP"
port: 80
targetPort: 80
type: ClusterIP
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Pod: demo-appapp=demo-app
Service: demo-app
app=demo-app
Service - Exercise• Lets do:
٠ labs/5-services.md• Service
٠ kubectl explain service.spec(for description of fields)
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Pod: demo-appapp=demo-app
Service: demo-app
app=demo-app
Kubernetes Advanced Concepts
• Deployments• (Persistent Volumes)• (Statefulset)• (Daemonset)• (Job)• (Custom Resource Definition (CRD))
Deployments
• Allow us to ensure X many instances of pod are running
• Allow us to control how pods are updated via specifying strategy type
• Works via control loop٠ Observe cluster state -> Different from expected -> Act to
return state to expected state
Deployments
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Deployment: demo
Namespace: default
Pod: demo-appapp=demo-app
Node 2
Deployment: demo
Deployments
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Deployment: demo
Namespace: default
Pod: demo-appapp=demo-app
Node 2
Deployment: demo
Deployments
Namespace: default
Pod: demo-appapp=demo-app
Node 1
Deployment: demo
Pod: demo-appapp=demo-app
Deployments - Exercise• Lets do:
٠ labs/6-deployments.md• Pods
٠ kubectl explain deployments.spec(for description of fields)
Namespace: default
Namespace: kube-system
apiVersion: apps/v1kind: Deploymentmetadata: name: demo labels: app: demo-appspec: replicas: 2 selector: matchLabels: app: demo-app template: metadata: ... same as pod metadata ... spec: ... same as pod spec ...
Questions
Jason Brownbridge <jason at beekeeper.io>
Docker WorkshopDeploy your code like a boss
C++pythonJavaJavaScript
Machine setup
Automation...
Linux service files
Worked on my machine
Dependency Errors
App Updates
Different Environments
What is Docker?
• Software container platform٠ Docker daemon٠ Docker CLI
• Any App, Language, or Stack• Awesome Developer Experience• App Isolation
Docker
• Main docker artefacts:٠ Images٠ Containers
• Docker is using a layered architecture
Images
• A blueprint for a container• Is never running• Is instantiated to create containers• Layered and cached
Containers
• Instance of Image• A running program• Can be running/stopped
• Should be ephemeral (short-lived, stateless)
Containers vs. Virtual Machines
Containers vs. Virtual Machines
docker run
• Run a command in a new container• Examples
٠ docker run ubuntu٠ docker run ubuntu:16.04٠ docker run -it ubuntu:16.04٠ docker run -it ubuntu:16.04 bash٠ docker run --name bob -it ubuntu:16.04 bash٠ docker run --name bob -it -v folder:/etc/folder -p 5000:80
ubuntu:16.04 bash
docker ps
• List containers• Examples
٠ docker ps٠ docker ps -a
docker logs
• Fetch the logs of a container• Examples:
٠ docker logs bob٠ docker logs -f bob٠ docker logs -f --tail 50 bob
docker stop
• Stop one or more running containers• Examples:
٠ docker stop bob
docker start
• Start one or more stopped containers• Examples:
٠ docker start bob
docker rm
• Remove one or more containers• Examples
٠ docker rm bob٠ docker rm --force bob
Dockerfile
• A simple text file• Contains directives to execute for every layer
DockerfileBase Image
Environment variables
Copy Code
Run command
Expose portDefault Command to execute
Default Arguments to command
Workshop
workshop-docker-eth.beekeeper.io
Conclusion
Next steps
• You felt the pain of orchestration• Real services need a lot more of this• Next time we can see how to get a “real” service
running• Explore tools like kubernetes