diff --git a/kubernetes/kubectl/README.md b/kubernetes/kubectl/README.md new file mode 100644 index 0000000..014eeb2 --- /dev/null +++ b/kubernetes/kubectl/README.md @@ -0,0 +1,188 @@ +# Introduction to KUBECTL + +To start off this tutorial, we will be using [kind](https://kind.sigs.k8s.io/) to create our test cluster.
+You can use `minikube` or any Kubernetes cluster.
+ +Kind is an amazing tool for running test clusters locally as it runs in a container which makes it lightweight and easy to run throw-away clusters for testing purposes.
+ +## Download KUBECTL + +We can download `kubectl` from the [Official Docs](https://kubernetes.io/docs/tasks/tools/)
+ +## Create a kubernetes cluster + +In this guide we will run two clusters side by side so we can demonstrate cluster access.
+Create two clusters: + +``` +kind create cluster --name dev --image kindest/node:v1.23.5 +kind create cluster --name prod --image kindest/node:v1.23.5 + +``` + +See cluster up and running: + +``` +kubectl get nodes +NAME STATUS ROLES AGE VERSION +prod-control-plane Ready control-plane,master 2m12s v1.23.5 +``` + +## Understanding the KUBECONFIG + +Default location of the `kubeconfig` file is in `/.kube/config` + +``` +kind: Config +apiVersion: v1 +clusters: + - list of clusters (addresses \ endpoints) +users: + - list of users (thing that identifies us when accessing a cluster [certificate]) +contexts: + - list of contexts ( which user and cluster to use when running commands) +``` + +Commands to interact with `kubeconfig` are `kubectl config`.
+Key commands are telling `kubectl` which context to use + +``` +kubectl config current-context +kubectl config get-contexts +kubectl config use-context +``` + +You can also tell your `kubectl` to use different config files.
+This is useful to keep your production config separate from your development ones
+ +Set the `$KUBECONFIG` environment variable to a path: +``` +#linux +export KUBECONFIG= + +#windows +$ENV:KUBECONFIG="C:\Users\aimve\.kube\config" +``` + +We can export seperate configs using `kind`
+This is possible with cloud based clusters as well: + +``` +kind --name dev export kubeconfig --kubeconfig C:\Users\aimve\.kube\dev-config + +kind --name prod export kubeconfig --kubeconfig C:\Users\aimve\.kube\prod-config + +#switch to prod +$ENV:KUBECONFIG="C:\Users\aimve\.kube\prod-config" +kubectl get nodes +``` + +## Working with Kubernetes resources + +Now that we have cluster access, next we can read resources from the cluster +with the `kubectl get` command. + +## Namespaces + +Most kubernetes resources are namespace scoped: + +``` +kubectl get namespaces +``` + +By default, `kubectl` commands will run against the `default` namespace + +## List resources in a namespace + +``` +kubectl get + +kubectl get pods +kubectl get deployments +kubectl get services +kubectl get configmaps +kubectl get secrets +kubectl get ingress +``` + +## Create resources in a namespace + +We can create a namespace with the `kubectl create` command: + +``` +kubectl create ns example-apps +``` + +Let's create a couple of resources: + +``` + +kubectl -n example-apps create deployment webserver --image=nginx --port=80 +kubectl -n example-apps get deploy +kubectl -n example-apps get pods + +kubectl -n example-apps create service clusterip webserver --tcp 80:80 +kubectl -n example-apps get service +kubectl -n example-apps port-forward svc/webserver 80 +# we can access http://localhost/ + +kubectl -n example-apps create configmap webserver-config --from-file config.json=./kubernetes/kubectl/config.json +kubectl -n example-apps get cm + +kubectl -n example-apps create secret generic webserver-secret --from-file secret.json=./kubernetes/kubectl/secret.json +kubectl -n example-apps get secret + +``` + +## Working with YAML + +As you can see we can create resources with `kubectl` but this is only for basic testing purposes. +Kubernetes is a declarative platform, meaning we should provide it what to create instead +of running imperative line-by-line commands.
+ +We can also get the YAML of pre-existing objects in our cluster with the `-o yaml` flag on the `get` command
+ +Let's output all our YAML to a `yaml` folder: + +``` +kubectl -n example-apps get cm webserver-config -o yaml > .\kubernetes\kubectl\yaml\config.yaml +kubectl -n example-apps get secret webserver-secret -o yaml > .\kubernetes\kubectl\yaml\secret.yaml +kubectl -n example-apps get deploy webserver -o yaml > .\kubernetes\kubectl\yaml\deployment.yaml +kubectl -n example-apps get svc webserver -o yaml > .\kubernetes\kubectl\yaml\service.yaml +``` + +## Create resources from YAML files + +The most common and recommended way to create resources in Kubernetes is with the `kubectl apply` command.
+This command takes in declarative `YAML` files. + +To show you how powerful it is, instead of creating things line-by-line, we can deploy all our infrastructure +with a single command.
+ +Let's deploy a Wordpress CMS site, with a back end MySQL database.
+This is a snippet taken from my `How to learn Kubernetes` video: + +``` +kubectl create ns wordpress-site +kubectl -n wordpress-site apply -f ./kubernetes/tutorials/basics/yaml/ +``` + +We can checkout our site with the `port-forward` command: + +``` +kubectl -n wordpress-site get svc + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +mysql ClusterIP 10.96.146.75 3306/TCP 17s +wordpress ClusterIP 10.96.157.6 80/TCP 17s + +kubectl -n wordpress-site port-forward svc/wordpress 80 +``` + +## Clean up + +``` +kind delete cluster --name dev +kind delete cluster --name prod + +``` \ No newline at end of file diff --git a/kubernetes/kubectl/config.json b/kubernetes/kubectl/config.json new file mode 100644 index 0000000..feb0734 --- /dev/null +++ b/kubernetes/kubectl/config.json @@ -0,0 +1,3 @@ +{ + "config": "some-value" +} \ No newline at end of file diff --git a/kubernetes/kubectl/secret.json b/kubernetes/kubectl/secret.json new file mode 100644 index 0000000..750f97c --- /dev/null +++ b/kubernetes/kubectl/secret.json @@ -0,0 +1,3 @@ +{ + "secret": "some-secret-value" +} \ No newline at end of file diff --git a/kubernetes/kubectl/yaml/config.yaml b/kubernetes/kubectl/yaml/config.yaml new file mode 100644 index 0000000..e63c005 Binary files /dev/null and b/kubernetes/kubectl/yaml/config.yaml differ diff --git a/kubernetes/kubectl/yaml/deployment.yaml b/kubernetes/kubectl/yaml/deployment.yaml new file mode 100644 index 0000000..b7ad58f Binary files /dev/null and b/kubernetes/kubectl/yaml/deployment.yaml differ diff --git a/kubernetes/kubectl/yaml/secret.yaml b/kubernetes/kubectl/yaml/secret.yaml new file mode 100644 index 0000000..0c39840 Binary files /dev/null and b/kubernetes/kubectl/yaml/secret.yaml differ diff --git a/kubernetes/kubectl/yaml/service.yaml b/kubernetes/kubectl/yaml/service.yaml new file mode 100644 index 0000000..439a0fc Binary files /dev/null and b/kubernetes/kubectl/yaml/service.yaml differ diff --git a/kubernetes/tutorials/basics/README.md b/kubernetes/tutorials/basics/README.md index 0d3797f..44229f9 100644 --- a/kubernetes/tutorials/basics/README.md +++ b/kubernetes/tutorials/basics/README.md @@ -1,5 +1,7 @@ # Kubernetes Tutorial: The Basics +How to learn Kubernetes in 2022 + This guide is aimed to fast-track your Kubernetes learning by focusing on a practical hands-on overview guide.
When learning Kubernetes, you usually have an idea of some existing system you own and manage, or a website that you are building.