You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org
Kubernetes/Kubernetes Workshop: Difference between revisions
imported>Wolfgang Kandek |
imported>Mbaoma |
||
Line 1: | Line 1: | ||
== Overview == | |||
This workshop outlines how to use Kubernetes (minikube) and Docker within the Wikimedia Foundation (WMF). Kubernetes (k8s, a [https://en.wikipedia.org/wiki/Numeronym numeronym] similar to I18n or l8n), is an open-source orchestration system. In practice, Kubernetes is used to automate a computer application deployment, support application scaling, and provide facilities for application management. | |||
The workshop grants you: | |||
Kubernetes | * A hands-on understanding of WMF's primary use-cases of Kubernetes. | ||
* An understanding of how WMF employs the tool in the production environment. | |||
* An in-depth review of how to use WMF’s Kubernetes to run your services. | |||
* A practical experience in running sample applications on k8s. | |||
'''Audience''' | |||
This document is aimed at: | |||
* Developers | |||
* Students | |||
* Engineers who are new to Kubernetes and want a basic hands-on introduction to how to use WMF's Kubernetes to run their services. | |||
'''What this is not''' | |||
This workshop is not a complete tutorial on practicing with Kubernetes. | |||
'''What this is''' | |||
This is a living document tailored to outline the specific ways WMF uses Kubernetes. | |||
== | == Getting Started == | ||
Kubernetes (k8s) runs on top of multiple Linux systems that form a k8s cluster. The cluster comprises the management systems (control plane), nodes, and dedicated systems upon which the Linux-based applications execute. | |||
In this workshop, you will deploy applications as Docker images and run these images as containers inside pods on the nodes. The cgroup kernel feature controls the application resources. | |||
[[File:Kubernetes.png|thumb|center|Kubernetes]] | |||
[[Kubernetes/Kubernetes Workshop | When planning for large-scale application deployments, k8s is a viable option. However, k8s is not the best solution for all environments, see the [[Kubernetes/Kubernetes Workshop|Additional Resources]] section for more information. | ||
[[ | The Wikimedia Foundation uses k8s in several scenarios. The [[Help:Cloud Services introduction|Wikimedia Cloud Services]] (WMCS) team runs k8s in [[Portal:Toolforge|Toolforge]], while the [[SRE/Service_Operations/Team|SRE ServiceOps]] team runs k8s as a microservice platform. | ||
'''Prerequisites''' | |||
[[ | To navigate through this workshop, WMF recommends that you possess the following: | ||
* A basic Linux knowledge. | |||
* Basic knowledge of Docker. | |||
* A basic understanding of Ansible. | |||
* Basic knowledge of [https://docs.github.com/en/get-started/quickstart/hello-world GitHub] and [https://en.wikipedia.org/wiki/Gerrit_(software) Gerrit]. | |||
Using a Linux machine is best for this workshop. The workshop also provides troubleshooting guides for Mac and Windows users. | |||
You can use a provisioned Virtual Machine (VM) from any cloud provider or WMCS. An ideal VM should possess the following: | |||
* 2 CPUs | |||
* 4 GB Memory | |||
* 40 GB disk | |||
'''Skills''' | |||
To complete this workshop's exercises, you should have the following skills: | |||
* Basic programming knowledge. | |||
* Basic understanding of containerization. | |||
* Basic familiarity with automation tools. | |||
== Setting Up == | |||
In this workshop, you will use a local installation of k8s, minikube, Docker, Python. | |||
'''Installing Docker''' | |||
[[ | [https://en.wikipedia.org/wiki/Docker_(software) Docker] is a containerization tool used to package applications and their dependencies into containers. This guide outlines steps on how to install Docker on your local machine. | ||
* Linux ([https://docs.docker.com/engine/install/ubuntu Ubuntu]) Installation | |||
* [https://docs.docker.com/desktop/mac/install/ MacOs] Installation | |||
* [https://docs.docker.com/get-started/ Windows] Installation | |||
* Take care to add your user to the docker group: | |||
<syntaxhighlight lang="bash"> | |||
$ sudo usermod -aG docker $USER && newgrp docker | |||
</syntaxhighlight> | |||
[[Kubernetes/Kubernetes Workshop/Load Testing| | '''To test your Docker installation, run the hello-world application''': | ||
<syntaxhighlight lang="bash"> | |||
$ docker run hello-world | |||
Unable to find image 'hello-world:latest' locally | |||
latest: Pulling from library/hello-world | |||
2db29710123e: Pull complete | |||
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67 | |||
Status: Downloaded newer image for hello-world:latest | |||
Hello from Docker! | |||
This message shows that your installation appears to be working correctly. | |||
</syntaxhighlight> | |||
'''Expect a similar output as the code block above if your installation was successful'''. | |||
'''Note''': Ensure the Docker daemon is running if you are using a MacOS machine. Start the Docker daemon by clicking on your Docker application before running the ''hello-world'' application. | |||
'''Installing Ansible''' | |||
[https://docs.ansible.com/ansible/latest/index.html Ansible] is an enterprise automation platform. This guide outlines steps on how to [https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html install] Ansible on your local machine. | |||
* Linux ([https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-ubuntu Ubuntu]) Installation | |||
* [http://MacOs MacOs] Installation | |||
* [https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-windows Windows] Installation | |||
'''[https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#confirming-your-installation Verify] your installation''': | |||
<syntaxhighlight lang="bash"> | |||
$ ansible all -m ping --ask-pass | |||
SSH password: | |||
[WARNING]: No inventory was parsed, only implicit localhost is available | |||
[WARNING]: provided hosts list is empty, only localhost is available. Note that | |||
the implicit localhost does not match 'all' | |||
</syntaxhighlight> | |||
At the prompt for the SSH password, press the ''Enter'' key. | |||
'''Note''': MacOs you can also install Ansible by running: | |||
<syntaxhighlight lang="bash"> | |||
$ brew install ansible | |||
</syntaxhighlight> | |||
This method uses the ''[https://brew.sh/ brew]'' installer instead of ''[https://pypi.org/project/pip/ pip]''. | |||
'''Installing Kubernetes''' | |||
This guide outlines steps to install kubectl, a [[Kubernetes]] CLI tool used to run commands against clusters. To quickly find common kubectl commands, use [https://kubernetes.io/docs/reference/kubectl/cheatsheet/ this] cheat sheet. From the links given below, install Kubernetes on your local machine: | |||
* [https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ Linux] Installation | |||
* [https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/ MacOs] Installation | |||
* [https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/ Windows] Installation | |||
'''Installing minikube''' | |||
[https://minikube.sigs.k8s.io/docs/start/ minikube] is used to run a Kubernetes cluster locally (on personal computers). Highlighted below are ways to install this tool on various operating systems. | |||
* [https://minikube.sigs.k8s.io/docs/start/ Linux] Installation | |||
* [https://minikube.sigs.k8s.io/docs/start/ MacOS] Installation | |||
* [https://minikube.sigs.k8s.io/docs/start/ Windows] Installation | |||
'''Note''': | |||
* Ensure the Docker daemon is running. | |||
* Enable Kubernetes on Docker Desktop: this can be done by: | |||
** Navigating to the settings page. | |||
** Clicking on Kubernetes. | |||
** Ticking the box for the option ''Enable Kubernetes'' | |||
'''Verify your installation''': | |||
<syntaxhighlight lang="bash"> | |||
$ minikube start | |||
😄 minikube v1.25.2 on Darwin 12.3.1 | |||
✨ Automatically selected the docker driver | |||
👍 Starting control plane node minikube in cluster minikube | |||
🚜 Pulling base image ... | |||
💾 Downloading Kubernetes v1.23.3 preload ... | |||
> preloaded-images-k8s-v17-v1...: 505.68 MiB / 505.68 MiB 100.00% 2.20 MiB | |||
> gcr.io/k8s-minikube/kicbase: 379.06 MiB / 379.06 MiB 100.00% 1.28 MiB p/ | |||
🔥 …………………………………………………….. | |||
namespace by default | |||
$ kubectl cluster-info | |||
Kubernetes control plane is running at https://127.0.0.1:50198 | |||
CoreDNS is running at https://127.0.0.1:50198/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy | |||
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. | |||
</syntaxhighlight> | |||
'''Getting a URL response implies that kubectl is correctly configured to access your cluster'''. | |||
'''Verify your kubectl installation''': | |||
<syntaxhighlight lang="bash"> | |||
$ kubectl get nodes | |||
NAME STATUS ROLES AGE VERSION | |||
minikube Ready control-plane,master 64s v1.23.3 | |||
$ kubectl run helloworld --image=hello-world | |||
pod/helloworld created | |||
$ kubectl logs helloworld | |||
Hello from Docker! | |||
This message shows that your installation appears to be working correctly. | |||
………………………………………………………………………… | |||
For more examples and ideas, visit: | |||
https://docs.docker.com/get-started/ | |||
$ kubectl get pods | |||
NAME READY STATUS RESTARTS AGE | |||
helloworld 0/1 CrashLoopBackOff 2 (23s ago) 42s | |||
$ minikube stop | |||
✋ Stopping node "minikube" ... | |||
🛑 Powering off "minikube" via SSH ... | |||
🛑 1 node stopped. | |||
</syntaxhighlight> | |||
== Workshop Outline == | |||
This workshop contains eleven modules to acquaint you with Kubernetes and its use in WMF’s production environments. The workshop introduces the following concepts: | |||
* Executing a generic k8s cluster on Wikimedia Cloud Services ([https://en.wikipedia.org/wiki/Wikipedia:Wikimedia_Cloud_Services WMCS]). | |||
* Using WMF's deployment pipeline tools, namely Gerrit, Blubber, and Continuous Integration/Continuous Deployment (CI/CD) systems. | |||
The appendix contains links to Docker tutorials that provide the necessary foundational knowledge. Each module should take around two or three hours to complete. | |||
The workshop presents you with a hands-on coding experience, plenty of examples, and references to unblock you when you experience blockers. | |||
'''Verify the following before attempting the workshop''': | |||
* Ensure you have ''[https://www.python.org/downloads/ Python]'' running on your machines. Linux OS users can install Python 3 by running the following commands: | |||
<syntaxhighlight lang="bash"> | |||
$ sudo apt update | |||
$ sudo apt install software-properties-common | |||
$ sudo add-apt-repository ppa:deadsnakes/ppa | |||
$ sudo apt update | |||
$ sudo apt install python3 | |||
$ python3 --version | |||
Python3.7.3 | |||
</syntaxhighlight> | |||
Your Python version output might be different from the above output. However, the output’s format remains the same. | |||
* Most virtual machines come preinstalled with [https://en.wikipedia.org/wiki/Git Git]. However, you can install Git on your VM or configure your laptop’s terminal to run git commands. To verify whether you have GitHub installed on your VM, run the following command: | |||
<syntaxhighlight lang="bash"> | |||
$ git version | |||
-bash: Git: command not found | |||
</syntaxhighlight> | |||
If you get the above output, you have to install Git; otherwise, you have Git installed on your VM. You can [https://git-scm.com/book/en/v2/Getting-Started-Installing-Git install] Git by running the following commands: | |||
<syntaxhighlight lang="bash"> | |||
$ sudo apt update | |||
$ sudo apt install git-all | |||
</syntaxhighlight> | |||
To verify your installation, run: | |||
<syntaxhighlight lang="bash"> | |||
$ git version | |||
git version 2.20.1 | |||
</syntaxhighlight> | |||
* Lastly, install pip on your VM: | |||
<syntaxhighlight lang="bash"> | |||
$ sudo apt update | |||
$ sudo apt install python3-pip | |||
$ pip3 --version | |||
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7) | |||
</syntaxhighlight> | |||
* Run the [https://minikube.sigs.k8s.io/docs/start/ hello-minikube] application | |||
Mac users on Big Sur should use [https://gerrit.wikimedia.org/r/plugins/gitiles/releng/local-charts/+/HEAD/bin/mac_prereq.sh. this] for the base setup. | |||
'''Workshop Modules''' | |||
[[Kubernetes/Kubernetes Workshop/Set up a batch application on Kubernetes|Module 1: Set up a batch application on Kubernetes]] | |||
[[Kubernetes/Kubernetes Workshop/Build a service application on Kubernetes|Module 2: Build a service application on Kubernetes]] | |||
[[Kubernetes/Kubernetes Workshop/Setting up Infrastructure as Code (IaC) in Kubernetes|Module 3: Setting up Infrastructure as Code (IaC) in Kubernetes]] | |||
[[Kubernetes/Kubernetes Workshop/Autoscaling in Kubernetes|Module 4: Autoscaling in Kubernetes]] | |||
[[Kubernetes/Kubernetes Workshop/Refactoring code using Kubernetes features|Module 5: Refactoring code using Kubernetes features]] | |||
[[Kubernetes/Kubernetes Workshop/Building a production ready cluster|Module 6: Building a production ready cluster]] | |||
[[Kubernetes/Kubernetes Workshop/Kubernetes Package Manager|Module 7: Kubernetes Package Manager]] | |||
[[Kubernetes/Kubernetes Workshop/Manageability and Security|Module 8: Manageability and Security]] | |||
[[Kubernetes/Kubernetes Workshop/Kubernetes at the Wikimedia Foundation (WMF)|Module 9: Kubernetes at the Wikimedia Foundation (WMF)]] | |||
[[Kubernetes/Kubernetes Workshop/Using WMF Kubernetes to run your services|Module 10: Using WMF Kubernetes to run your services]] | |||
[[Kubernetes/Kubernetes Workshop/Load Testing|Add-on Module: Load Testing]] | |||
[[Category:Kubernetes]] [[Category: How-To]] | [[Category:Kubernetes]] [[Category: How-To]] | ||
== Previous Work == | == Previous Work == | ||
A previous version of this workshop can be found [https://docs.google.com/document/d/1bccjDlu3v-6aHTdlMvH1Z3XiJ3lRNGe4hWTMYf3_1Ac/edit here]. | |||
== Additional Resources == | |||
[https://www.youtube.com/watch?v=m19D9vZ1QFQ&t=155s This] video by Cruise, presented at Kubecon, talks about the number of clusters and nodes. At the same time, [https://www.youtube.com/watch?v=m19D9vZ1QFQ&t=1479s this] video highlights the achieved benefits. k8s is not the best solution for all environments, and [https://web.archive.org/web/20200816141303/https://mbird.biz/writing/do-i-need-kubernetes.html this] article gives some interesting points. |
Revision as of 10:52, 17 June 2022
Overview
This workshop outlines how to use Kubernetes (minikube) and Docker within the Wikimedia Foundation (WMF). Kubernetes (k8s, a numeronym similar to I18n or l8n), is an open-source orchestration system. In practice, Kubernetes is used to automate a computer application deployment, support application scaling, and provide facilities for application management.
The workshop grants you:
- A hands-on understanding of WMF's primary use-cases of Kubernetes.
- An understanding of how WMF employs the tool in the production environment.
- An in-depth review of how to use WMF’s Kubernetes to run your services.
- A practical experience in running sample applications on k8s.
Audience
This document is aimed at:
- Developers
- Students
- Engineers who are new to Kubernetes and want a basic hands-on introduction to how to use WMF's Kubernetes to run their services.
What this is not
This workshop is not a complete tutorial on practicing with Kubernetes.
What this is
This is a living document tailored to outline the specific ways WMF uses Kubernetes.
Getting Started
Kubernetes (k8s) runs on top of multiple Linux systems that form a k8s cluster. The cluster comprises the management systems (control plane), nodes, and dedicated systems upon which the Linux-based applications execute.
In this workshop, you will deploy applications as Docker images and run these images as containers inside pods on the nodes. The cgroup kernel feature controls the application resources.
When planning for large-scale application deployments, k8s is a viable option. However, k8s is not the best solution for all environments, see the Additional Resources section for more information.
The Wikimedia Foundation uses k8s in several scenarios. The Wikimedia Cloud Services (WMCS) team runs k8s in Toolforge, while the SRE ServiceOps team runs k8s as a microservice platform.
Prerequisites
To navigate through this workshop, WMF recommends that you possess the following:
- A basic Linux knowledge.
- Basic knowledge of Docker.
- A basic understanding of Ansible.
- Basic knowledge of GitHub and Gerrit.
Using a Linux machine is best for this workshop. The workshop also provides troubleshooting guides for Mac and Windows users.
You can use a provisioned Virtual Machine (VM) from any cloud provider or WMCS. An ideal VM should possess the following:
- 2 CPUs
- 4 GB Memory
- 40 GB disk
Skills
To complete this workshop's exercises, you should have the following skills:
- Basic programming knowledge.
- Basic understanding of containerization.
- Basic familiarity with automation tools.
Setting Up
In this workshop, you will use a local installation of k8s, minikube, Docker, Python.
Installing Docker
Docker is a containerization tool used to package applications and their dependencies into containers. This guide outlines steps on how to install Docker on your local machine.
- Linux (Ubuntu) Installation
- MacOs Installation
- Windows Installation
- Take care to add your user to the docker group:
$ sudo usermod -aG docker $USER && newgrp docker
To test your Docker installation, run the hello-world application:
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Expect a similar output as the code block above if your installation was successful.
Note: Ensure the Docker daemon is running if you are using a MacOS machine. Start the Docker daemon by clicking on your Docker application before running the hello-world application.
Installing Ansible
Ansible is an enterprise automation platform. This guide outlines steps on how to install Ansible on your local machine.
Verify your installation:
$ ansible all -m ping --ask-pass
SSH password:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
At the prompt for the SSH password, press the Enter key.
Note: MacOs you can also install Ansible by running:
$ brew install ansible
This method uses the brew installer instead of pip.
Installing Kubernetes
This guide outlines steps to install kubectl, a Kubernetes CLI tool used to run commands against clusters. To quickly find common kubectl commands, use this cheat sheet. From the links given below, install Kubernetes on your local machine:
Installing minikube
minikube is used to run a Kubernetes cluster locally (on personal computers). Highlighted below are ways to install this tool on various operating systems.
Note:
- Ensure the Docker daemon is running.
- Enable Kubernetes on Docker Desktop: this can be done by:
- Navigating to the settings page.
- Clicking on Kubernetes.
- Ticking the box for the option Enable Kubernetes
Verify your installation:
$ minikube start
😄 minikube v1.25.2 on Darwin 12.3.1
✨ Automatically selected the docker driver
👍 Starting control plane node minikube in cluster minikube
🚜 Pulling base image ...
💾 Downloading Kubernetes v1.23.3 preload ...
> preloaded-images-k8s-v17-v1...: 505.68 MiB / 505.68 MiB 100.00% 2.20 MiB
> gcr.io/k8s-minikube/kicbase: 379.06 MiB / 379.06 MiB 100.00% 1.28 MiB p/
🔥 ……………………………………………………..
namespace by default
$ kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:50198
CoreDNS is running at https://127.0.0.1:50198/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Getting a URL response implies that kubectl is correctly configured to access your cluster.
Verify your kubectl installation:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 64s v1.23.3
$ kubectl run helloworld --image=hello-world
pod/helloworld created
$ kubectl logs helloworld
Hello from Docker!
This message shows that your installation appears to be working correctly.
…………………………………………………………………………
For more examples and ideas, visit:
https://docs.docker.com/get-started/
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld 0/1 CrashLoopBackOff 2 (23s ago) 42s
$ minikube stop
✋ Stopping node "minikube" ...
🛑 Powering off "minikube" via SSH ...
🛑 1 node stopped.
Workshop Outline
This workshop contains eleven modules to acquaint you with Kubernetes and its use in WMF’s production environments. The workshop introduces the following concepts:
- Executing a generic k8s cluster on Wikimedia Cloud Services (WMCS).
- Using WMF's deployment pipeline tools, namely Gerrit, Blubber, and Continuous Integration/Continuous Deployment (CI/CD) systems.
The appendix contains links to Docker tutorials that provide the necessary foundational knowledge. Each module should take around two or three hours to complete.
The workshop presents you with a hands-on coding experience, plenty of examples, and references to unblock you when you experience blockers.
Verify the following before attempting the workshop:
- Ensure you have Python running on your machines. Linux OS users can install Python 3 by running the following commands:
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3
$ python3 --version
Python3.7.3
Your Python version output might be different from the above output. However, the output’s format remains the same.
- Most virtual machines come preinstalled with Git. However, you can install Git on your VM or configure your laptop’s terminal to run git commands. To verify whether you have GitHub installed on your VM, run the following command:
$ git version
-bash: Git: command not found
If you get the above output, you have to install Git; otherwise, you have Git installed on your VM. You can install Git by running the following commands:
$ sudo apt update
$ sudo apt install git-all
To verify your installation, run:
$ git version
git version 2.20.1
- Lastly, install pip on your VM:
$ sudo apt update
$ sudo apt install python3-pip
$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
- Run the hello-minikube application
Mac users on Big Sur should use this for the base setup.
Workshop Modules
Module 1: Set up a batch application on Kubernetes
Module 2: Build a service application on Kubernetes
Module 3: Setting up Infrastructure as Code (IaC) in Kubernetes
Module 4: Autoscaling in Kubernetes
Module 5: Refactoring code using Kubernetes features
Module 6: Building a production ready cluster
Module 7: Kubernetes Package Manager
Module 8: Manageability and Security
Module 9: Kubernetes at the Wikimedia Foundation (WMF)
Module 10: Using WMF Kubernetes to run your services
Previous Work
A previous version of this workshop can be found here.
Additional Resources
This video by Cruise, presented at Kubecon, talks about the number of clusters and nodes. At the same time, this video highlights the achieved benefits. k8s is not the best solution for all environments, and this article gives some interesting points.