Skip to content
Snippets Groups Projects
Commit 12cee8bd authored by francisc.mendonca's avatar francisc.mendonca
Browse files

README done

parent 3fbe9ae5
Branches master
No related tags found
No related merge requests found
# lab-k8s
## Objective
This exercise will guide you through the process of deploying and managing a Kubernetes cluster using Kind (Kubernetes IN Docker) on a beefy SE (Standard Edition) instance. You will:
1. Install Kind on your instance.
2. Create a Kind cluster with the base configuration.
3. Interact with the cluster to understand its components.
4. Modify the cluster configuration to add worker nodes.
5. Redeploy and verify the new cluster setup.
6. Deploy a service with a load balancer and test it.
7. Clean up resources and snapshot the instance.
## Prerequesites:
- **Switch Linux Instance**: Ensure you have access to a beefy Switch instance with (at least a c1.large [4 cpus, 4GB RAM]) - Kind uses quite a few resources, after all we are simulating a full-fledged cluster. For the purposes of this exercise, choose Ubuntu 22.04 as the image.
## Part 1: Installing Kind
### 1. Installing Docker
If Docker is not already installed on your instance, install it using the following commands:
1. Set up Docker's apt repostiory:
```bash
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
```
2. Install Docker packages:
```bash
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```
## Getting started
3. Postinstall Steps:
By default, Docker doens't work for non-root users. But running it as root is dangerous, so, run the following steps:
```bash
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
```
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Afterwards, check your Docker installation by running:
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
```bash
docker run hello-world
```
## Add your files
### 2. Installing Kind
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
To install kind, run:
```bash
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.24.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
```
cd existing_repo
git remote add origin https://gitedu.hesge.ch/lsds/teaching/bachelor/cloud-and-deployment/lab-k8s.git
git branch -M main
git push -uf origin main
Then, verify the installation:
```bash
kind version
```
## Integrate with your tools
Good, you are ready to create a cluster.
- [ ] [Set up project integrations](https://gitedu.hesge.ch/lsds/teaching/bachelor/cloud-and-deployment/lab-k8s/-/settings/integrations)
## Part 2: Creating a Kind cluster with the Base Image
## Collaborate with your team
There are two methods to create a cluster:
1. Manually, by using the `kind create cluster` method, which will create a single node cluster
2. Using a `kind-config.yaml` configuration file, and running `kind create cluster --config kind-config.yaml`. In this case, we can specifcy the number of nodes and the type of nodes.
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
Using [this webpage](https://kind.sigs.k8s.io/docs/user/quick-start/#creating-a-cluster) create a configuration to deploy a cluster with one `control-plane` node, and up to 10 worker nodes, you can choose the number of worker nodes, but less than 10.
## Test and Deploy
## Part 3: Interacting with the Cluster
Use the built-in continuous integration in GitLab.
### 1. Installing Kubectl
To interact with this cluster, let's install `kubectl`, the main command line tool to interact with kubernetes clusters.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
```bash
# Download the binary
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# Install kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
# Test kubectl
kubectl version --client
```
***
### 2. Check the Nodes
# Editing this README
- How many nodes are deployed? Are they all working?
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
```bash
kubectl get nodes
```
## Suggestions for a good README
### 3. Check the Cluster
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
A k8s cluster is much more than its nodes. Check all the moving parts of your cluster. P.S: it should be empty for now, but use this command later to verify if your deployment is successful:
## Name
Choose a self-explaining name for your project.
```bash
kubectl get all
```
## Part 4: Modifying the Cluster Configuration
You already have a configuration file. Make some alteration to the file (remove worker nodes, add worker nodes | DO NOT REMOVE THE CONTROL PLANE NODE!!!), and try to launch the cluster again.
To re-deploy, first delete the original cluster (hint: check kind's man-page), and then redeploy using the updated configuration file.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
- Has the number of nodes changed?
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Part 5: Actually deploying an application
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
### 1. Installing a Load Balancer
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
While Kind allows us to test out a K8S cluster, it doesn't have all the bells. One of the things it lacks is a Load Balancer. Luckly, K8S is easily extendable, so, let's install one.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
To install the MetalLB load balancer:
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
```bash
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.10/config/manifests/metallb-native.yaml
```
Then, in the VM, create a config file `metallb.yaml`:
```yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: default
namespace: metallb-system
spec:
addresses:
- 172.18.255.1-172.18.255.250 # Adjust this range based on your Docker network
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: default
namespace: metallb-system
```
Then, we need to apply this configuration to the cluster:
```bash
kubectl apply -f metallb.yaml
```
Good, now we can create deployment and service file, which in this case, we'll have in one large deployment file:
### 2. Deployment and Service File
Finally! Here is the little app we are going to deploy. Create a YAML file in your VM with the following code:
```yaml
# deployment-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-echo
spec:
replicas: 2
selector:
matchLabels:
app: http-echo
template:
metadata:
labels:
app: http-echo
spec:
nodeSelector:
kubernetes.io/hostname: kind-worker # Schedule pods on one worker node
containers:
- name: http-echo
image: hashicorp/http-echo
args:
- >-
-text=Hello from Kubernetes! My IP is $(POD_IP)
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
ports:
- containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: LoadBalancer
spec:
type: LoadBalancer
selector:
app: http-echo
ports:
- port: 80
targetPort: 5678
```
This deployment file will deploy 2 replicas of the HTTP-Echo container, which receive an environmental variable as input with the Pod's IP address. When the replica is reached, the program running inside of the container will return `Hello from Kubernetes! My IP is $(POD_IP)`.
The service is of type `LoadBalancer`, and looks for pods with the `app: http-echo` label.
To deploy:
```bash
kubectl apply -f <FILENAME>.yaml
```
### 3. I deployed, now what?
You deployed, now what? Well, now you are going to do a bash program to constantly `curl` the load balancer.
First, check the External IP of the load balancer:
```bash
kubectl get service http-echo-service
```
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
Then, write your program. Make sure to print out the response from `curl`.
Remember to change the permissions of your code before running (`chmod +x ....`)
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
Run your code, what do you see? (PS: it will take some time to show both of the instances, as this Load Balancer is not really a Round-Robin style Load Balancer)
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
## Part 6: Destroying the Cluster
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
Destroy you cluster
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## Part 7: Destroying the VM
## License
For open source projects, say how it is licensed.
Now, destroy the VM.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment