Skip to content

Commit ac8d7ac

Browse files
From K8s Tutorial
1 parent 25fcfd3 commit ac8d7ac

File tree

471 files changed

+52545
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

471 files changed

+52545
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Section 1: History and Motivation
2+
3+
## 2000s: Traditional Deployment Era
4+
5+
During the 2000s, we experienced what is known as the "Traditional Deployment Era". This period was characterized by:
6+
7+
- **On-premises Deployments**: Companies managed their own data centers or used colocation services.
8+
- **Teams of Sysadmins**: Dedicated teams of system administrators were responsible for provisioning and managing fleets of servers, which was often a labor-intensive and immature process.
9+
- **Bare Metal Servers**: Applications ran directly on physical servers.
10+
- **Monolithic Architecture**: The prevalent architectural style was monolithic, where applications were built as single, indivisible units.
11+
- **Homegrown Monitoring Tools**: Monitoring and managing applications required custom-built tools due to the lack of standardized solutions.
12+
13+
## 2010s: Virtualized Deployment Era
14+
15+
The 2010s marked the transition to the "Virtualized Deployment Era". Key developments during this time included:
16+
17+
- **Cloud Computing**: The advent of cloud computing allowed Virtual Machines (VMs) to be created and destroyed in minutes, providing greater flexibility and scalability.
18+
- **Configuration Management Tools**: Tools like Puppet and Chef became popular for managing infrastructure as code, simplifying the configuration and management of large-scale deployments.
19+
- **Manual Bin-Packing**: Applications were manually allocated to VMs, optimizing resource usage but still requiring significant manual effort.
20+
- **Improved Tooling**: The emergence of better tooling made it practical to manage a larger number of applications and cloud resources.
21+
- **Challenges with Scale**: Despite the improvements, managing large numbers of cloud resources remained a significant challenge.
22+
23+
## 2020s: Container Deployment Era
24+
25+
In the 2020s, we entered the "Container Deployment Era", which brought about transformative changes in how workloads are managed:
26+
27+
- **Workload Orchestrators**: Tools like Kubernetes enabled treating clusters of machines as a single resource, simplifying management and scaling.
28+
- **Standard Interfaces and Utilities**: These orchestrators provided a range of utilities and interfaces to handle:
29+
- **Efficient Scheduling**: Optimally distributing workloads across instances.
30+
- **Health Checks**: Monitoring the health and status of applications.
31+
- **Service Discovery**: Automating the detection of service locations within the cluster.
32+
- **Configuration Management**: Standardizing the way configurations are managed and applied.
33+
- **Autoscaling**: Automatically adjusting the number of running instances based on demand.
34+
- **Persistent Storage**: Managing storage that persists beyond the lifecycle of individual containers.
35+
- **Networking**: Ensuring reliable and scalable networking between services.
36+
37+
## Kubernetes History
38+
39+
For a deeper dive into the history and development of Kubernetes, you can check out the two-part documentary by Honeypot on YouTube:
40+
41+
- [Kubernetes: The Documentary [PART 1]](https://www.youtube.com/watch?v=BE77h7dmoQU&).
42+
- [Kubernetes: The Documentary [PART 2]](https://www.youtube.com/watch?v=318elIq37PE)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Technology Overview
2+
3+
## Planes and Nodes
4+
5+
The first concepts to understand with regard to kubernetes are:
6+
7+
- **Node:** A "node" is a computer/server. Multiple nodes are joined together to form a "cluster".
8+
- **Control Plane:** A subset of nodes in the cluster dedicated to performing system tasks. Nodes that are part of the control plane are referred to as "control plane nodes".
9+
- **Data Plane:** A subset of nodes in the cluster dedicated to running user worklods. Nodes that are part of the data plane are referred to as "worker nodes".
10+
11+
![control-and-data-planes](readme-assets/control-and-data-planes.jpg)
12+
13+
## Kubernetes System Components
14+
15+
Kubernetes is comprised of many smaller components:
16+
17+
- **etcd**: Key-value store used for storing all cluster data. It serves as the source of truth for the cluster state and configuration.
18+
19+
- **kube-apiserver**: The front end for the Kubernetes control plane.
20+
21+
- **kube-scheduler**: Schedules pods onto the appropriate nodes based on resource availability and other constraints.
22+
23+
- **kube-controller-manager**: Runs controller processes. Each controller is a separate process that manages routine tasks such as maintaining the desired state of resources, managing replication, handling node operations, etc...
24+
25+
- **cloud-controller-manager**: Integrates with the underlying cloud provider (if running in one) to manage cloud-specific resources. It handles tasks such as managing load balancers, storage, and networking.
26+
27+
- **kubelet**: An agent that runs on each worker node and ensures that containers are running in pods and manages the lifecycle of containers.
28+
29+
- **kube-proxy**: This network proxy runs on each node and maintains network rules to allow communication to and from pods.
30+
31+
- ![k8s-architecture](readme-assets/k8s-architecture.jpg)
142 KB
Loading
185 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kind-config.yaml
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Installation and Setup
2+
3+
## Dependencies
4+
5+
### Docker Desktop
6+
7+
Please install Docker Desktop via their instructions (Install Docker Desktop on [Mac](https://docs.docker.com/desktop/install/mac-install/), [Windows](https://docs.docker.com/desktop/install/windows-install/), or [Linux](https://docs.docker.com/desktop/install/linux-install/).)
8+
9+
### Devbox
10+
11+
All other software dependencies for the course are defined in the `devbox.json` and `devbox.lock` files in the root directory.
12+
13+
Please install Devbox according to their instructions: https://www.jetify.com/devbox/docs/installing_devbox/
14+
15+
Once installed you can run:
16+
17+
```
18+
devbox shell
19+
```
20+
21+
from anywhere in the repo and devbox will use Nix package manager to install a copy of all of the required software in an isolated environment.
22+
23+
### Aliases
24+
25+
I suggest creating the following aliases:
26+
27+
```
28+
k=kubectl
29+
t=task
30+
tl='task --list-all'
31+
```
32+
33+
### Autocomplete:
34+
35+
Setting up tab completion for your shell of choice makes life much nicer:
36+
37+
- kubectl: https://kubernetes.io/docs/reference/kubectl/generated/kubectl_completion/
38+
- task: https://taskfile.dev/installation/#setup-completions
39+
40+
## Cluster Set Up
41+
42+
This directory contains configurations and commands for setting up 3 kubernetes clusters. Any of the 3 can be used for most of the examples, with a few exceptions.
43+
44+
### KinD
45+
46+
Runs a kubernetes cluster locally within Docker! Great for testing and development purposes (and doesn't cost any additional money). Nearly all examples in the course can be run within this cluster, with the exception of things demonstrating cloud specific features or exposing services via public DNS.
47+
48+
To start the cluster run:
49+
50+
```
51+
devbox shell # if you haven't already
52+
task kind:01-generate-config
53+
task kind:02-create-cluster
54+
```
55+
56+
### Civo Cloud
57+
58+
Civo cloud offers a simple managed Kubernetes cluster. Their clusters provision quickly and they offer a 1 month, $250 credit for new users: https://dashboard.civo.com/signup.
59+
60+
To create a cluster run:
61+
62+
```
63+
devbox shell # if you haven't already
64+
civo:01-authenticate-cli
65+
civo:04-create-all
66+
civo:05-get-kubeconfig
67+
```
68+
69+
To destroy the cluster run:
70+
71+
```
72+
civo:06-clean-up
73+
```
74+
75+
🚨 NOTE: If you have deployed additional resources (such as load balancers) that reference the VPC/Subnets/Etc... you may need to manually clean up those resources in order for this command to succeed. You should also verify that you have cleaned up all resources to avoid unwanted costs!
76+
77+
### Google Kubernetes Engine (GKE)
78+
79+
Google has been operating managed kubernetes clusters longer than any other cloud provider and the polish of the GKE experience shows that. They offer a 90-day, $300 free trial for new users: https://cloud.google.com/free.
80+
81+
To create a cluster run:
82+
83+
```
84+
devbox shell # if you haven't already
85+
gcp:01-init-cli
86+
gcp:07-create-all
87+
gcp:08-connect-to-cluster
88+
```
89+
90+
To destroy the cluster run:
91+
92+
```
93+
gcp:09-clean-up
94+
```
95+
96+
🚨 NOTE: If you have deployed additional resources (such as load balancers) that reference the VPC/Subnets/Etc... you may need to manually clean up those resources in order for this command to succeed. You should also verify that you have cleaned up all resources to avoid unwanted costs!
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
version: "3"
2+
3+
env:
4+
CLUSTER_NAME: devops-directive-kubernetes-course
5+
CIVO_REGION: NYC1
6+
GCP_REGION: us-central1
7+
GCP_ZONE: us-central1-a
8+
# Set default gum style options
9+
BORDER: double
10+
BORDER_FOREGROUND: "212"
11+
PADDING: "1 1"
12+
MARGIN: "1 1"
13+
14+
tasks:
15+
civo:00-authenticate-cli:
16+
cmds:
17+
- cmd: |
18+
gum style "$(cat <<EOF
19+
To get an API key you need to:
20+
---
21+
1. Log in or create an account at https://dashboard.civo.com/
22+
2. Create a team at https://dashboard.civo.com/teams
23+
3. Add yourself to the team
24+
4. Navigate to https://dashboard.civo.com/security to get the api key
25+
26+
🚨🚨🚨 NOTE: Sometimes account verification required for new accounts
27+
(so sign up before you want to use it!) 🚨🚨🚨
28+
EOF
29+
)"
30+
silent: true
31+
- civo apikey save
32+
- civo apikey ls
33+
- cmd: gum style "run \`civo apikey current <KEY_NAME>\` to set the current key as the default (if it is not already)"
34+
silent: true
35+
desc: Authenticate the Civo CLI
36+
37+
civo:01-create-network:
38+
cmds:
39+
- civo network create ${CLUSTER_NAME} --region ${CIVO_REGION}
40+
desc: Create a Civo network
41+
42+
civo:02-create-firewall:
43+
cmds:
44+
- |
45+
civo firewall create ${CLUSTER_NAME} \
46+
--network ${CLUSTER_NAME} \
47+
--create-rules false \
48+
--region ${CIVO_REGION}
49+
- |
50+
ingress_rule_ids=$(civo firewall rule ls --region ${CIVO_REGION} ${CLUSTER_NAME} -o json | jq -r '.[] | select(.direction == "ingress") | .id')
51+
for rule_id in $ingress_rule_ids; do
52+
civo firewall rule remove ${CLUSTER_NAME} $rule_id -y --region ${CIVO_REGION}
53+
done
54+
- civo firewall rule create ${CLUSTER_NAME} --startport 80 --endport 80 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION}
55+
- civo firewall rule create ${CLUSTER_NAME} --startport 443 --endport 443 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION}
56+
- civo firewall rule create ${CLUSTER_NAME} --startport 6443 --endport 6443 --cidr 0.0.0.0/0 --protocol TCP --region ${CIVO_REGION}
57+
- cmd: gum style "🚨 If you wanted to lock down access to the k8s api, you could instead only allow traffic on 6443 from your IP (or that of a bastion host)"
58+
silent: true
59+
desc: Create a Civo firewall and set up rules
60+
61+
civo:03-create-cluster:
62+
cmds:
63+
- |
64+
civo kubernetes create ${CLUSTER_NAME} \
65+
--region ${CIVO_REGION} \
66+
--network ${CLUSTER_NAME} \
67+
--existing-firewall ${CLUSTER_NAME} \
68+
--nodes 2 \
69+
--size g4s.kube.medium \
70+
--remove-applications "traefik2-nodeport" \
71+
--wait
72+
desc: Create a Civo Kubernetes cluster
73+
74+
civo:04-create-all:
75+
cmds:
76+
- task: civo:01-create-network
77+
- task: civo:02-create-firewall
78+
- task: civo:03-create-cluster
79+
desc: Create the Civo network, firewall, and cluster in sequence
80+
81+
civo:05-get-kubeconfig:
82+
cmds:
83+
- civo kubernetes config ${CLUSTER_NAME} --region ${CIVO_REGION} --save --switch
84+
desc: Get kubeconfig for the cluster
85+
86+
civo:06-clean-up:
87+
cmds:
88+
- civo kubernetes delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y
89+
- cmd: gum style "There is some delay on the civo side from cluster being deleted to it being removed from the firewall rule usage"
90+
silent: true
91+
- sleep 10
92+
- civo firewall delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y
93+
- civo network delete ${CLUSTER_NAME} --region ${CIVO_REGION} -y
94+
desc: Clean up the Civo Kubernetes cluster and associated resources
95+
96+
gcp:01-init-cli:
97+
cmds:
98+
- gcloud init
99+
desc: "Authenticate and configure the gcloud CLI"
100+
101+
gcp:02-enable-apis:
102+
cmds:
103+
- |
104+
gcloud services enable \
105+
compute.googleapis.com \
106+
container.googleapis.com \
107+
cloudresourcemanager.googleapis.com \
108+
iam.googleapis.com \
109+
secretmanager.googleapis.com \
110+
servicemanagement.googleapis.com \
111+
serviceusage.googleapis.com
112+
desc: "Enable necessary APIs"
113+
114+
gcp:03-set-region-and-zone:
115+
cmds:
116+
- gcloud config set compute/region ${GCP_REGION}
117+
- gcloud config set compute/zone ${GCP_ZONE}
118+
desc: "Set default region and zone"
119+
120+
gcp:04-create-vpc:
121+
cmds:
122+
- gcloud compute networks create ${CLUSTER_NAME} --subnet-mode=custom
123+
desc: "Create VPC"
124+
125+
gcp:05-create-subnet:
126+
cmds:
127+
- |
128+
gcloud compute networks subnets create subnet-1 \
129+
--network=${CLUSTER_NAME} \
130+
--region=${GCP_REGION} \
131+
--range=10.0.0.0/20
132+
desc: "Create subnet"
133+
134+
gcp:06-create-cluster:
135+
desc: "Create GKE cluster"
136+
vars:
137+
GCP_PROJECT_ID: kubernetes-course-424917
138+
cmds:
139+
- |
140+
gcloud container clusters create ${CLUSTER_NAME} \
141+
--zone ${GCP_ZONE} \
142+
--network ${CLUSTER_NAME} \
143+
--subnetwork subnet-1 \
144+
--machine-type e2-standard-2 \
145+
--num-nodes 2 \
146+
--gateway-api=standard \
147+
--workload-pool={{.GCP_PROJECT_ID}}.svc.id.goog
148+
149+
gcp:07-create-all:
150+
cmds:
151+
- task: gcp:02-enable-apis
152+
- task: gcp:03-set-region-and-zone
153+
- task: gcp:04-create-vpc
154+
- task: gcp:05-create-subnet
155+
- task: gcp:06-create-cluster
156+
desc: Create the GCP network, subnet, firewall rules, and cluster in sequence
157+
158+
gcp:09-clean-up:
159+
cmds:
160+
- gcloud container clusters delete ${CLUSTER_NAME} --zone ${GCP_ZONE} --quiet
161+
- gcloud compute networks subnets delete subnet-1 --region=${GCP_REGION} --quiet
162+
- gcloud compute networks delete ${CLUSTER_NAME} --quiet
163+
desc: Delete the GCP network, subnet, firewall rules, and cluster in reverse sequence
164+
165+
gcp:08-connect-to-cluster:
166+
cmds:
167+
- gcloud container clusters get-credentials ${CLUSTER_NAME} --zone ${GCP_ZONE}
168+
desc: "Connect to the GKE cluster"
169+
170+
kind:01-generate-config:
171+
cmds:
172+
- REPLACE_WITH_ABSOLUTE_PATH=${PWD} envsubst < kind-config.yaml.TEMPLATE > kind-config.yaml
173+
desc: "Generate kind config with local absolute paths for PV mounts"
174+
175+
kind:02-create-cluster:
176+
cmds:
177+
- kind create cluster --config kind-config.yaml
178+
desc: Create a Kubernetes cluster using kind
179+
180+
kind:03-run-cloud-provider-kind:
181+
desc: "Run sigs.k8s.io/cloud-provider-kind@latest to enable load balancer services with KinD"
182+
cmds:
183+
- sudo cloud-provider-kind
184+
185+
kind:04-delete-cluster:
186+
cmds:
187+
- kind delete cluster
188+
desc: Delete and existing a kind Kubernetes cluster
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
👋 (#1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
👋 (#2)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# three node (two workers) cluster config
2+
kind: Cluster
3+
apiVersion: kind.x-k8s.io/v1alpha4
4+
nodes:
5+
- role: control-plane
6+
- role: worker
7+
extraMounts:
8+
- hostPath: ${REPLACE_WITH_ABSOLUTE_PATH}/kind-bind-mount-1
9+
containerPath: /some/path/in/container
10+
- role: worker
11+
extraMounts:
12+
- hostPath: ${REPLACE_WITH_ABSOLUTE_PATH}/kind-bind-mount-2
13+
containerPath: /some/path/in/container

0 commit comments

Comments
 (0)