Git Product home page Git Product logo

k8s-workshop's Introduction

Kubernetes Workshop (I)

What is Kubernetes

Kubernetes in 5 mins

Setup the Cluster

Clone https://github.com/betabandido/minikube-provisioner and follow the instructions there to setup a Kubernetes cluster using minikube.

tmux

You might want to use tmux to have multiple virtual terminals multiplexed into a single real one. Ctrl+B C creates a new window. Ctrl+B <number> switches to the specified window (using its index). Ctrl+B [ enters scroll mode, and q goes back to normal mode.

Enable Add-ons

sudo minikube addons enable heapster
sudo minikube addons enable metrics-server

Explore the Cluster

Run the following command to list all the different resources in the cluster:

kubectl get all --all-namespaces

Creating a Docker Image for the Application

cd ~/k8s-workshop/hello
sudo docker build -t hello:0.1 .

Creating a Deployment

Deploy the application on Kubernetes:

kubectl apply -f k8s/deployment.yaml

List the deployments and pods:

kubectl get deployments
kubectl get pods

Display some more information:

kubectl describe deployment hello
kubectl describe pod hello

Show the application logs:

kubectl logs -l app=hello

# It is also possible to get the logs for a particular pod, given its name:
kubectl logs <pod-name>

Connecting to a pod:

kubectl exec <pod-name> -it -- sh

Updating a Deployment

Update k8s/deployment.yaml so that the deployment uses two replicas:

# ...
spec:
  # ...
  replicas: 2
  # ...

And then apply the configuration again:

kubectl apply -f k8s/deployment.yaml

Listing the pods must now show two pods for application hello:

kubectl get pods -l app=hello

Creating a Service

kubectl apply -f k8s/service.yaml

List the services in the cluster:

kubectl get services

Find some more information about the created service:

kubectl describe service hello

In a traditional Kubernetes cluster the external IP would be shown. In Minikube, however, the external IP must be obtained by running:

minikube service hello --url

Use curl to verify you can access the application:

curl $(minikube service hello --url)

Use the external address of the EC2 instance (not the one for the service) to access the application from your browser: http://ec2-<external-IP>.eu-west-1.compute.amazonaws.com:<port>

Dashboard

Get the dashboard's port running the following command:

minikube dashboard --url

Then use that port together with the external address of the EC2 instance to access the dashboard.

Explore the different resources in the cluster (pods, deployments, services, etc.).

Updating a Deployment

Change main.py so that it returns another message, and then create a new docker image with a new tag:

sudo docker build -t hello:0.2 .

Now, update deployment.yaml so that it uses image hello:0.2. After that, update the deployment using:

kubectl apply -f k8s/deployment.yaml

Use your browser to check whether the message has changed.

Dealing with Deployment Failures

Change main.py so that the healthz endpoint returns an error:

from flask import Flask, abort

# ...

@app.route('/healthz')
def healthz():
    abort(500)

Create a new docker image:

sudo docker build -t hello:0.3 .

Update the deployment.yaml file to use the new image, and update the deployment:

kubectl apply -f k8s/deployment.yaml

Watch pod status:

watch kubectl get pods

To rollback the deployment execute:

kubectl rollout undo deployment/hello

Autoscaling

Create and deploy the cpu application (this application performs a CPU-intensive operation when it receives a request):

cd ~/k8s-workshop/cpu
sudo docker build -t cpu:0.1 .
kubectl apply -f k8s

Performance Monitoring

Run the following command to send traffic to the application:

while true; do curl $(minikube service cpu --url) ; done

After some seconds you should see an increase in CPU utilization. Then, the auto-scale mechanism will trigger, creating new replicas. Run the following command to see the new replicas being created:

watch kubectl get pods

Next Steps

To create a full-fledged Kubernetes cluster have a look at kops.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.