diff --git a/.tools/labCheckers/checker_common.py b/.tools/labCheckers/checker_common.py new file mode 100644 index 0000000..7c494b1 --- /dev/null +++ b/.tools/labCheckers/checker_common.py @@ -0,0 +1,25 @@ +import yaml +from yaml import CLoader as Loader +from cerberus import Validator + + + +def _load_doc(yamlPath): + with open(yamlPath, 'r') as stream: + try: + return yaml.load(stream, Loader=Loader) + except yaml.YAMLError as exception: + raise exception + +def checker(schema, yamlPath): + #schema = eval(open('./schema.py', 'r').read()) + v = Validator(schema) + doc = _load_doc(yamlPath) + correctness = "" + if v.validate(doc, schema): + correctness = "Your answer is correct!" + else: + correctness = "Your answer was not correct. Please try again." + + print("IS VALID: ", correctness) + print("ERRORS: ", v.errors) \ No newline at end of file diff --git a/.tools/labCheckers/l1_e1.py b/.tools/labCheckers/l1_e1.py new file mode 100755 index 0000000..712ec58 --- /dev/null +++ b/.tools/labCheckers/l1_e1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +schema = { + 'name': { + 'required': True, + 'type': 'string' + }, + 'date': { + 'required': True, + 'type': 'date' + }, + 'metrics': { + 'required': True, + 'type': 'dict', + 'schema': { + 'percentage': { + 'required': True, + 'type': 'dict', + 'schema': { + 'value': { + 'required': True, + 'type': 'number', + 'min': 0, + 'max': 100 + }, + 'trend': { + 'type': 'string', + 'nullable': True, + 'regex': '^(?i)(down|equal|up)$' + } + } + } + } + } +} + +import checker_common as C +import sys + +if __name__ == "__main__": + startMsg = "Checking your work for Lab-1, Exercise-1..." + print(startMsg) + C.checker(schema, sys.argv[1]) \ No newline at end of file diff --git a/.tools/labCheckers/t.yaml b/.tools/labCheckers/t.yaml new file mode 100644 index 0000000..9681a94 --- /dev/null +++ b/.tools/labCheckers/t.yaml @@ -0,0 +1,6 @@ +name: 'my_name' +date: 2017-10-01 +metrics: + percentage: + value: 87 + trend: down \ No newline at end of file diff --git a/LAB_README.md b/LAB_README.md new file mode 100644 index 0000000..6d54b6d --- /dev/null +++ b/LAB_README.md @@ -0,0 +1,31 @@ +# Summary: + +The purpose of the lab exercises is to get you up to speed on using Kubernetes (abbreviated as K8s) with the following learning objectives in mind: +- To get introduced to core terminology +- To become familiar with the core concepts +- To understand how to interact with K8s using kubectl +- To become familiar with the basics of YAML +- To understand how to create or deploy your own resources on K8s using YAML and kubectl +- And to understand how to read an interptet the K8s API documentation + +# About this VM: +In order to help you along your K8s learning path, this VM has been designed to be as lightweight aspossible and with collection of software pre-installed to make your learning experience much easier so you can focus on just using kubernetes. + +The core software that you'll be interacting with in order to learn K8s are: +- Visual Studio Code with the following extensions: + - YAML (from Red Hat) + - Kubernetes (from Microsoft) + - Markdown (from Yu Zhang) +- Docker Community Edition for Linux +- K3d + +VS Code and Docker are popular pieces of software that I'm just going to assume that you already know about, so I won't explain what they are for, but what about K3d? + +# About K3d +K3d, is probably a new name that you haven't heard of before. Basically K3d is a tool developed by Rancher (now owned by Suse) that makes it **super** easy to create single-node or multi-node kubernetes clusters on your local machine. In our case, K3d will be used to help us to easily create and destroy K8s clusters on this VM. + +K3d accomplishes this in a few ways: + 1. K3d provides you a very easy to use command line interface that you can use to create, delete, and manage your Kubernetes cluster. + 2. When you create a cluster using K3d, all Kubernetes nodes that K3d creates are running inside of a Docker container. + 3. The containers that act as K8s nodes all have a very lightweight container runtime which uses containerd, runc, and cri. + 4. When you create pods (containers) to run on your K8s cluster, you're essentially creating that container, in another container. \ No newline at end of file diff --git a/k3d_cheatsheet.md b/k3d_cheatsheet.md new file mode 100644 index 0000000..000cbfc --- /dev/null +++ b/k3d_cheatsheet.md @@ -0,0 +1,42 @@ +# About +Here is a quick reference document to provide examples of some commonly used commands with K3d. + +# Example Commands + +## Creating stuff +### Creating a single node cluster with a default cluster name: +`k3d cluster create` + +### Creating a single node cluster with a custom cluster name: +`k3d cluster create myCluster1` + +### Creating a multi node cluster with 2 masters and 2 workers: +`k3d cluster create -s 2 -a 2` + +### Adding 2 additional worker nodes to an existing cluster: +`k3d node create myAgent --role agent --replicas 2 -c myCluster1` + +### Adding a master node to an existing cluster: +`k3d node create myMaster --role server -c myCluster1` + +### Creating a private container image registry: +`k3d reg create myReg1` + +--- +## Getting information +### Listing your clusters: +`k3d cluster list` + +### Listing your registries: +`k3d reg list` + +--- +## Deleting stuff +### Deleting a single node cluster with a default cluster name: +`k3d cluster delete` + +### Deleting a single node cluster with a custom cluster name: +`k3d cluster delete myCluster1` + +### Creating a private container image registry: +`k3d reg delete myReg1` \ No newline at end of file diff --git a/kubectl_cheatsheet.md b/kubectl_cheatsheet.md new file mode 100644 index 0000000..3b03d75 --- /dev/null +++ b/kubectl_cheatsheet.md @@ -0,0 +1,414 @@ +# About +Here is a quick reference document to provide examples of some commonly used commands with Kubectl. + +# Example Commands + NOTE: Unless otherwise explicitely stated, all calls with kubectl are performed against the default namespace. + + +## Kubectl autocomplete + +### BASH + +```bash +source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first. +echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell. +``` + +You can also use a shorthand alias for `kubectl` that also works with completion: + +```bash +alias k=kubectl +complete -F __start_kubectl k +``` + + +## Kubectl context and configuration + +Set which Kubernetes cluster `kubectl` communicates with and modifies configuration +information. See [Authenticating Across Clusters with kubeconfig](/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) documentation for +detailed config file information. + +```bash +kubectl config view # Show Merged kubeconfig settings. + +# use multiple kubeconfig files at the same time and view merged config +KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 + +kubectl config view + +# get the password for the e2e user +kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}' + +kubectl config view -o jsonpath='{.users[].name}' # display the first user +kubectl config view -o jsonpath='{.users[*].name}' # get a list of users +kubectl config get-contexts # display list of contexts +kubectl config current-context # display the current-context +kubectl config use-context my-cluster-name # set the default context to my-cluster-name + +# add a new user to your kubeconf that supports basic auth +kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword + +# permanently save the namespace for all subsequent kubectl commands in that context. +kubectl config set-context --current --namespace=ggckad-s2 + +# set a context utilizing a specific username and namespace. +kubectl config set-context gce --user=cluster-admin --namespace=foo \ + && kubectl config use-context gce + +kubectl config unset users.foo # delete user foo +``` + +## Kubectl apply + +`apply` manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through running `kubectl apply`. This is the recommended way of managing Kubernetes applications on production. See [Kubectl Book](https://kubectl.docs.kubernetes.io). + +## Creating objects + +Kubernetes manifests can be defined in YAML or JSON. The file extension `.yaml`, +`.yml`, and `.json` can be used. + +```bash +kubectl apply -f ./my-manifest.yaml # create resource(s) +kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files +kubectl apply -f ./dir # create resource(s) in all manifest files in dir +kubectl apply -f https://git.io/vPieo # create resource(s) from url +kubectl create deployment nginx --image=nginx # start a single instance of nginx + +# create a Job which prints "Hello World" +kubectl create job hello --image=busybox -- echo "Hello World" + +# create a CronJob that prints "Hello World" every minute +kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World" + +kubectl explain pods # get the documentation for pod manifests + +# Create multiple YAML objects from stdin +cat < pod.yaml + +kubectl attach my-pod -i # Attach to Running Container +kubectl port-forward my-pod 5000:6000 # Listen on port 5000 on the local machine and forward to port 6000 on my-pod +kubectl exec my-pod -- ls / # Run command in existing pod (1 container case) +kubectl exec --stdin --tty my-pod -- /bin/sh # Interactive shell access to a running pod (1 container case) +kubectl exec my-pod -c my-container -- ls / # Run command in existing pod (multi-container case) +kubectl top pod POD_NAME --containers # Show metrics for a given pod and its containers +kubectl top pod POD_NAME --sort-by=cpu # Show metrics for a given pod and sort it by 'cpu' or 'memory' +``` + +## Interacting with Deployments and Services +```bash +kubectl logs deploy/my-deployment # dump Pod logs for a Deployment (single-container case) +kubectl logs deploy/my-deployment -c my-container # dump Pod logs for a Deployment (multi-container case) + +kubectl port-forward svc/my-service 5000 # listen on local port 5000 and forward to port 5000 on Service backend +kubectl port-forward svc/my-service 5000:my-service-port # listen on local port 5000 and forward to Service target port with name + +kubectl port-forward deploy/my-deployment 5000:6000 # listen on local port 5000 and forward to port 6000 on a Pod created by +kubectl exec deploy/my-deployment -- ls # run command in first Pod and first container in Deployment (single- or multi-container cases) +``` + +## Interacting with Nodes and cluster + +```bash +kubectl cordon my-node # Mark my-node as unschedulable +kubectl drain my-node # Drain my-node in preparation for maintenance +kubectl uncordon my-node # Mark my-node as schedulable +kubectl top node my-node # Show metrics for a given node +kubectl cluster-info # Display addresses of the master and services +kubectl cluster-info dump # Dump current cluster state to stdout +kubectl cluster-info dump --output-directory=/path/to/cluster-state # Dump current cluster state to /path/to/cluster-state + +# If a taint with that key and effect already exists, its value is replaced as specified. +kubectl taint nodes foo dedicated=special-user:NoSchedule +``` + +### Resource types + +List all supported resource types along with their shortnames, [API group](/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning), whether they are [namespaced](/docs/concepts/overview/working-with-objects/namespaces), and [Kind](/docs/concepts/overview/working-with-objects/kubernetes-objects): + +```bash +kubectl api-resources +``` + +Other operations for exploring API resources: + +```bash +kubectl api-resources --namespaced=true # All namespaced resources +kubectl api-resources --namespaced=false # All non-namespaced resources +kubectl api-resources -o name # All resources with simple output (only the resource name) +kubectl api-resources -o wide # All resources with expanded (aka "wide") output +kubectl api-resources --verbs=list,get # All resources that support the "list" and "get" request verbs +kubectl api-resources --api-group=extensions # All resources in the "extensions" API group +``` + +### Formatting output + +To output details to your terminal window in a specific format, add the `-o` (or `--output`) flag to a supported `kubectl` command. + +Output format | Description +--------------| ----------- +`-o=custom-columns=` | Print a table using a comma separated list of custom columns +`-o=custom-columns-file=` | Print a table using the custom columns template in the `` file +`-o=json` | Output a JSON formatted API object +`-o=jsonpath=