Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.8.1 #109

Merged
merged 17 commits into from
Feb 10, 2024
Prev Previous commit
Next Next commit
Ag 5 (#101)
add cka lab 2 (HPA by CPU)
  • Loading branch information
ViktorUJ authored Feb 2, 2024
commit cc4b33f00f3b9ea9a8fc318d17ace86a4b118e93
1 change: 1 addition & 0 deletions CHANGELOG/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Features
* update ping_pong service
* split CMDB and resources aws region
* add cka lab 02 (HPA by CPU)

## 0.7.1 (23.01.2024)
### Features
Expand Down
5 changes: 5 additions & 0 deletions tasks/cka/labs/02/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
We have an enterprise application (namespace **prod-jobs**).
The application fetches tasks for processing from a queue (Kafka).
We need to create **Horizontal Pod Autoscaling** based on **CPU** load.
When the CPU load rises to **100%** we need to increase the number of pods to **6**.
The **minimum** amount of pods should be **2**.
17 changes: 14 additions & 3 deletions tasks/cka/labs/02/env.hcl
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
locals {
questions_list = "https://github.com/ViktorUJ/cks/blob/0.8.1/tasks/cka/labs/02/README.MD"
solutions_scripts="https://github.com/ViktorUJ/cks/blob/0.8.1/tasks/cka/labs/02/worker/files/solutions/1.MD"
solutions_video="xyz"
debug_output = "false"
region = "eu-north-1"
vpc_default_cidr = "10.2.0.0/16"
az_ids = {
"10.2.0.0/19" = "eun1-az3"
"10.2.32.0/19" = "eun1-az2"
}
aws = "default"
prefix = "cka-task"
prefix = "cka-task02"
tags = {
"env_name" = "cka-task"
"env_name" = "cka-task02"
"env_type" = "dev"
"manage" = "terraform"
"cost_allocation" = "dev"
"owner" = "viktoruj@gmail.com"
}
k8_version = "1.28.0"
k8_version = "1.29.0"
node_type = "spot"
runtime = "containerd" # docker , cri-o , containerd ( need test it )
instance_type = "t4g.medium"
instance_type_worker = "t4g.small"
key_name = ""
ssh_password_enable = "true" # false | true
access_cidrs = ["0.0.0.0/0"] # "93.177.191.10/32" | "0.0.0.0/0"
ubuntu_version = "20.04"
ami_id = ""
# ubuntu : 20.04 LTS ami-06410fb0e71718398 22.04 LTS ami-00c70b245f5354c0a
Expand Down
40 changes: 40 additions & 0 deletions tasks/cka/labs/02/k8s-1/scripts/1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: prod-jobs
spec: {}
status: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: app
name: app
namespace: prod-jobs
spec:
replicas: 1
selector:
matchLabels:
app: app
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: app
spec:
containers:
- image: viktoruj/ping_pong
name: ping-pong-cp6bg
env:
- name: ENABLE_LOAD_CPU
value: "true"
- name: CPU_MAXPROC
value: "1"
- name: CPU_USAGE_PROFILE
value: '1=800=1=120 1=30=1=30'
resources: {}
status: {}
10 changes: 10 additions & 0 deletions tasks/cka/labs/02/k8s-1/scripts/master.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
echo " *** master node cka lab-2 k8s-1"
export KUBECONFIG=/root/.kube/config

kubectl apply -f https://raw.githubusercontent.com/ViktorUJ/cks/0.8.1/tasks/cka/labs/02/k8s-1/scripts/1.yaml

# Installation of metrics server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl -n kube-system patch deployment metrics-server --type=json \
-p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]]'
2 changes: 2 additions & 0 deletions tasks/cka/labs/02/k8s-1/scripts/worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo " *** worker node cka lab-2 k8s-1"
85 changes: 85 additions & 0 deletions tasks/cka/labs/02/k8s-1/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
include {
path = find_in_parent_folders()
}

locals {
vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
}

terraform {
source = "../../..//modules/k8s_self_managment/"
#

extra_arguments "retry_lock" {
commands = get_terraform_commands_that_need_locking()
arguments = ["-lock-timeout=20m"]
}

}

dependency "vpc" {
config_path = "../vpc"
}
dependency "ssh-keys" {
config_path = "../ssh-keys"
}

inputs = {
questions_list = local.vars.locals.questions_list
region = local.vars.locals.region
aws = local.vars.locals.aws
prefix = "cluster1"
tags_common = local.vars.locals.tags
app_name = "k8s"
subnets_az = dependency.vpc.outputs.subnets_az_cmdb
vpc_id = dependency.vpc.outputs.vpc_id
cluster_name = "k8s1"
node_type = local.vars.locals.node_type
ssh_password_enable = local.vars.locals.ssh_password_enable

k8s_master = {
k8_version = local.vars.locals.k8_version
runtime = local.vars.locals.runtime # docker , cri-o , containerd ( need test it ) , containerd_gvizor
runtime_script = "template/runtime.sh"
instance_type = local.vars.locals.instance_type
key_name = local.vars.locals.key_name
ami_id = local.vars.locals.ami_id
subnet_number = "0"
ubuntu_version = local.vars.locals.ubuntu_version
user_data_template = "template/master.sh"
pod_network_cidr = "10.0.0.0/16"
cidrs = local.vars.locals.access_cidrs
eip = "false"
utils_enable = "false"
task_script_url = "https://raw.githubusercontent.com/ViktorUJ/cks/0.8.1/tasks/cka/labs/02/k8s-1/scripts/master.sh"
calico_url = "https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml"
ssh = {
private_key = dependency.ssh-keys.outputs.private_key
pub_key = dependency.ssh-keys.outputs.pub_key
}
root_volume = local.vars.locals.root_volume
}
k8s_worker = {
# we can configure each node independently

"node_2" = {
k8_version = local.vars.locals.k8_version
instance_type = local.vars.locals.instance_type
key_name = local.vars.locals.key_name
ami_id = local.vars.locals.ami_id
subnet_number = "0"
ubuntu_version = local.vars.locals.ubuntu_version
user_data_template = "template/worker.sh"
runtime = local.vars.locals.runtime
runtime_script = "template/runtime.sh"
task_script_url = "https://raw.githubusercontent.com/ViktorUJ/cks/0.8.1/tasks/cka/labs/02/k8s-1/scripts/worker.sh"
node_labels = "work_type=infra_core"
ssh = {
private_key = dependency.ssh-keys.outputs.private_key
pub_key = dependency.ssh-keys.outputs.pub_key
}
cidrs = local.vars.locals.access_cidrs
root_volume = local.vars.locals.root_volume
}
}
}
17 changes: 17 additions & 0 deletions tasks/cka/labs/02/ssh-keys/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include {
path = find_in_parent_folders()
}

locals {
vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
}

terraform {
source = "../../..//modules/ssh-keys/"

extra_arguments "retry_lock" {
commands = get_terraform_commands_that_need_locking()
arguments = ["-lock-timeout=20m"]
}

}
8 changes: 2 additions & 6 deletions tasks/cka/labs/02/vpc/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ inputs = {
prefix = local.vars.locals.prefix
tags_common = local.vars.locals.tags
app_name = "network"
vpc_default_cidr = "10.2.0.0/16"
az_ids = {
"10.2.0.0/19" = "eun1-az1"
"10.2.32.0/19" = "eun1-az2"

}
vpc_default_cidr = local.vars.locals.vpc_default_cidr
az_ids = local.vars.locals.az_ids

}
131 changes: 131 additions & 0 deletions tasks/cka/labs/02/worker/files/solutions/1.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

[documentation](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)

[example](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/)

### steps
* check cpu load (find min and max usage )
* add request / limit
* create HPA
* check result



```
watch -n 1 'kubectl top po -n prod-jobs ; kubectl get po -n prod-jobs '

```

```
# max usage (usage time)

NAME CPU(cores) MEMORY(bytes)
app-6f6846bc44-8hfm6 267m 1Mi
NAME READY STATUS RESTARTS AGE
app-6f6846bc44-8hfm6 1/1 Running 0 20m


```

```
# min usage (idle time)

NAME CPU(cores) MEMORY(bytes)
app-6f6846bc44-8hfm6 15m 1Mi
NAME READY STATUS RESTARTS AGE
app-6f6846bc44-8hfm6 1/1 Running 0 21m

```
276/15 *100 = 1840 % (increase )
```
# update deployment (add resources.limits.cpu resources.requests.cpu
# k edit deployment app -n prod-jobs

apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"creationTimestamp":null,"labels":{"app":"app"},"name":"app","namespace":"prod-jobs"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"app"}},"strategy":{},"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"app"}},"spec":{"containers":[{"env":[{"name":"ENABLE_LOAD_CPU","value":"true"},{"name":"CPU_MAXPROC","value":"1"},{"name":"CPU_USAGE_PROFILE","value":"1=800=1=60 1=30=1=60"}],"image":"viktoruj/ping_pong","name":"ping-pong-cp6bg","resources":{}}]}}},"status":{}}
creationTimestamp: "2024-02-02T04:38:24Z"
generation: 2
labels:
app: app
name: app
namespace: prod-jobs
resourceVersion: "3100"
uid: c4f19a91-8549-4424-83fd-814d79291d3e
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: app
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: app
spec:
containers:
- env:
- name: ENABLE_LOAD_CPU
value: "true"
- name: CPU_MAXPROC
value: "1"
- name: CPU_USAGE_PROFILE
value: 1=800=1=120 1=30=1=30
image: viktoruj/ping_pong
imagePullPolicy: Always
name: ping-pong-cp6bg
resources: # add
limits: # add
cpu: 400m # add
requests: # add
cpu: 20m # add
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30



```

````
k autoscale deployment app --cpu-percent=500 --min=2 --max=6 -n prod-jobs
````

```
watch -n 1 'kubectl top po -n prod-jobs ; kubectl get po -n prod-jobs ; kubectl get hpa -n prod-jobs '
```

````
Every 1.0s: kubectl top po -n prod-jobs ; kubectl get po -n prod-jobs ; kubectl get hpa -n prod-jobs worker: Fri Feb 2 06:00:06 2024

NAME CPU(cores) MEMORY(bytes)
app-569b78dcb4-4cs6z 262m 1Mi
app-569b78dcb4-6zktc 210m 1Mi
app-569b78dcb4-zsf9z 14m 1Mi
NAME READY STATUS RESTARTS AGE
app-569b78dcb4-4cs6z 1/1 Running 0 40m
app-569b78dcb4-6zktc 1/1 Running 0 2m40s
app-569b78dcb4-cnmcj 1/1 Running 0 10s
app-569b78dcb4-f5rjq 1/1 Running 0 10s
app-569b78dcb4-rvlrn 1/1 Running 0 10s
app-569b78dcb4-zsf9z 1/1 Running 0 6m32s
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
app Deployment/app 873%/500% 2 6 3 3m25s


````
20 changes: 20 additions & 0 deletions tasks/cka/labs/02/worker/files/tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bats
export KUBECONFIG=/home/ubuntu/.kube/_config

@test "0 Init " {
echo ''>/var/work/tests/result/all
echo ''>/var/work/tests/result/ok
[ "$?" -eq 0 ]

}

#1
@test "1. Deploy a pod named webhttpd " {
echo '1'>>/var/work/tests/result/all
result=$(kubectl get po webhttpd -n apx-z993845 -o jsonpath='{.spec.containers..image}' --context cluster1-admin@cluster1 )
if [[ "$result" == "httpd:alpine" ]]; then
echo '1'>>/var/work/tests/result/ok
fi
[ "$result" == "httpd:alpine" ]
}
# 1 1
4 changes: 4 additions & 0 deletions tasks/cka/labs/02/worker/files/worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
echo " *** worker pc cka lab 2 "
export KUBECONFIG=/root/.kube/config

Loading