From 77d46d0d69b1ea2684f1790191229c25a2f4ada8 Mon Sep 17 00:00:00 2001 From: marcel-dempers Date: Wed, 10 Aug 2022 09:31:15 +1000 Subject: [PATCH 1/3] kubectl wip --- kubernetes/kubectl/README.md | 157 ++++++++++++++++++++++++ kubernetes/kubectl/yaml/config.yaml | Bin 0 -> 332 bytes kubernetes/kubectl/yaml/deployment.yaml | Bin 0 -> 1856 bytes kubernetes/kubectl/yaml/secret.yaml | Bin 0 -> 378 bytes kubernetes/kubectl/yaml/service.yaml | Bin 0 -> 748 bytes 5 files changed, 157 insertions(+) create mode 100644 kubernetes/kubectl/README.md create mode 100644 kubernetes/kubectl/yaml/config.yaml create mode 100644 kubernetes/kubectl/yaml/deployment.yaml create mode 100644 kubernetes/kubectl/yaml/secret.yaml create mode 100644 kubernetes/kubectl/yaml/service.yaml diff --git a/kubernetes/kubectl/README.md b/kubernetes/kubectl/README.md new file mode 100644 index 0000000..7cbab50 --- /dev/null +++ b/kubernetes/kubectl/README.md @@ -0,0 +1,157 @@ +# 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.
+ +## Create a kubernetes cluster + +``` +# 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" +``` + +# 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 +``` + +## KUBECTL Create resources + +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 +``` \ 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 0000000000000000000000000000000000000000..26b95c07fc1b9200318a387c3a63531c3ebff474 GIT binary patch literal 332 zcmZXPKMTSz6vS^9{0^ae1=p_5u1;C1*8h#cNL9qIuHL;8s7Ofil6QCc<@2r)vBv@p zE=a7-2&mxDVTv(ESfj?3v$B(8XJwiVwOdSZpeuU{JmHz+nIWY|LGO;r#8bbvc-XzO zSELx`IVkr;8g+8ki?vve>;)2uZ#JNqlP{X0rBk&3)Pc9^%6sF!B$NK!*;@Wk=q>SQ VPhxsaMelBKx2$NKCF{tM@C6I)I5_|S literal 0 HcmV?d00001 diff --git a/kubernetes/kubectl/yaml/deployment.yaml b/kubernetes/kubectl/yaml/deployment.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b1d2fad7453d9388f14ae6b40b4508d7dc254b13 GIT binary patch literal 1856 zcma)-OK;Oq5QS%r#DB?cCC%isdyAxrM9c@tZzTuI&acEpCzlq!Pt&S|ZE10GF{12>Hbo& zaOg#Xo2=zxWjD&SsWg=d3=k=x+KaDy`CAD)wR>f6?8SuUjaPZ6*lR@~4>BINs)dZo zA^kduDIlt)x$&yNQy=Se&xTB5Wk46yLPq7Uy-xH8J&tE^+#=_i-lj8V_Fnlu$lpQO`g!Bw zcy6DLRmgKGKKt7tWC-?Qmf*n(EboryC0K=fGu6xvJ~G$8whN0!Q8%-{_0DCBfN5sG z?DwcP9aV}|cw@>E|`zNu2Nz`Ve~7kLTW9d3T;8Ck7u sJ=E^!!ETJbR&36Ls>+Olf3NFQ<;I!(AT-UFBXVTSdor@~U$?#X4|OOQaR2}S literal 0 HcmV?d00001 diff --git a/kubernetes/kubectl/yaml/secret.yaml b/kubernetes/kubectl/yaml/secret.yaml new file mode 100644 index 0000000000000000000000000000000000000000..5db86b64ee9858e62f08fbda7690b71d5b04bb9f GIT binary patch literal 378 zcmZvYy9&ZU5Jhh-_z(F3Ma4?6Pzw=33&FxJ#>Ho1H1YZI>Y4R{f<>6j&ONjD?)?@j z)ly?Q?UnJjRicIh9aU9P1}cTG785r;{q`tEY}y?3$^o1^Xji z)c({Qa)f5CAsW*~s^MTojj0u8UDQ`iIj^IeaK7Q5iRT7vPf#_zqcwcOP3C#@<;=Xt z-edSDLqZ~(8g-&heX2dF?Hx)#8Eup0elr=~LcBg{r?cOc;?P0bd#3P4<>XFZx}m3b II?&tf0q_byNB{r; literal 0 HcmV?d00001 diff --git a/kubernetes/kubectl/yaml/service.yaml b/kubernetes/kubectl/yaml/service.yaml new file mode 100644 index 0000000000000000000000000000000000000000..439a0fc785f58abdc6e730ffd2fd926128eef8c6 GIT binary patch literal 748 zcma)4!AiqW5S+8%KlBF*3LZ*tQV_g|kkb2We3oEL!%K|z=hc}>V-tH&LXwx=+1c6I ze0>JgD6m09Lc`M@6_%Kx!!<5(ffvRqJUNyb)iILur0gr-Edukw@1>(N)|ZTInJvkB ziYlXywfWwe-!iY>ouA?qrXot*Qt~&dAz!d2;Uhj=U9)$@{MfItG9e?8Cq}<|W4E<`VN{o)3RhO}A3tVw_J%=j!I#kN1^IRft%RBef!mP2PwlRaYov@>0tyN3M wZ#HdI-sfgZBe(y%e=#(?`v39vxQ~k8E&5G7P-8{ZpiOb@R$8W8lJ`P=1C}Xt4*&oF literal 0 HcmV?d00001 From c04feaf77282af67684a304180f9a7633cc36965 Mon Sep 17 00:00:00 2001 From: marcel-dempers Date: Sun, 21 Aug 2022 15:45:49 +1000 Subject: [PATCH 2/3] kubectl wip --- kubernetes/kubectl/README.md | 41 +++++++++++++++++++++++---- kubernetes/tutorials/basics/README.md | 2 ++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/kubernetes/kubectl/README.md b/kubernetes/kubectl/README.md index 7cbab50..014eeb2 100644 --- a/kubernetes/kubectl/README.md +++ b/kubernetes/kubectl/README.md @@ -5,20 +5,30 @@ 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: + ``` -# 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 +``` + +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 +## Understanding the KUBECONFIG Default location of the `kubeconfig` file is in `/.kube/config` @@ -54,7 +64,20 @@ export KUBECONFIG= $ENV:KUBECONFIG="C:\Users\aimve\.kube\config" ``` -# Working with Kubernetes resources +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. @@ -82,7 +105,7 @@ kubectl get secrets kubectl get ingress ``` -## KUBECTL Create resources +## Create resources in a namespace We can create a namespace with the `kubectl create` command: @@ -154,4 +177,12 @@ 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/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.
From 86c19545465333746c5fd0d93c4cb40cd4f5186d Mon Sep 17 00:00:00 2001 From: marcel-dempers Date: Wed, 21 Sep 2022 12:43:12 +1000 Subject: [PATCH 3/3] updates to sync kubectl video --- kubernetes/kubectl/config.json | 3 +++ kubernetes/kubectl/secret.json | 3 +++ kubernetes/kubectl/yaml/config.yaml | Bin 332 -> 326 bytes kubernetes/kubectl/yaml/deployment.yaml | Bin 1856 -> 1772 bytes kubernetes/kubectl/yaml/secret.yaml | Bin 378 -> 370 bytes 5 files changed, 6 insertions(+) create mode 100644 kubernetes/kubectl/config.json create mode 100644 kubernetes/kubectl/secret.json 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 index 26b95c07fc1b9200318a387c3a63531c3ebff474..e63c0050efe8508590d996398c48e8d19bc55529 100644 GIT binary patch delta 74 zcmX@Zbc|_2khKCs3_}qR=P@V%VGM&3Lo!1?ke9}g$&e1_TS3K(f#SIgsSLUdWekZ7 QISi!}r*CE9W#D1}05(t#hX4Qo delta 79 zcmX@cbcSg{kZTM>5km|^9)ki9#xN)`lrUs4WHJ;3c@Q#}p%N^vz+eSck<5?}RG$Wv NPX~&C=$whYTLFH$59j~@ diff --git a/kubernetes/kubectl/yaml/deployment.yaml b/kubernetes/kubectl/yaml/deployment.yaml index b1d2fad7453d9388f14ae6b40b4508d7dc254b13..b7ad58f29bb5232ff1695768c7c1c940abeef2d6 100644 GIT binary patch delta 11 ScmX@W_l9>v8QbJzY-Io*T?EMh delta 78 zcmaFEdw_348Jle~Lk2@CLkdGFLk^HGV(?=~WXJ`wtQZu4qNxmNKt4#Wgh7{~m;p`o NC