<jupyter_start><jupyter_text>![](https://d1.awsstatic.com/PAC/kuberneteslogo.eabc6359f48c8e30b7a138c18177f3fd39338e05.png)"A lot of YAML manifests"  Presentation- A simple overview of some basic kubernetes objects.- We will not be discusing scaling concepts in K8s.- We will not be talking about docker. We assume you have working knowledge of docker.- For the sake of time, we won't be going over pv and pvc.  Basic - Pods: one or more docker containers- Nodes: The underlying VM- Pools: Group of VMs- Deployments: Group of kubernetes objects- Services: expose a deployment to network traffic- NameSpace: a collection of kubernetes objects Ingress / Egress- Service    - Load Balancer- Ingress Controller IAM- Service Accounts- Role Bindings- Roles Storage- Persistent Volumes- Persistent Volume Claims  Other- Jobs- ReplicaSet- StatefulSet- DaemonSet- NodePort (Ingres/Egress)- ClusterIP (Ingres/Egress) ![](https://2.bp.blogspot.com/-a7tbQ-93uCA/W9tl4zgHlVI/AAAAAAAACGE/762QwS7_iFMOodmtN-BOtdeHy_I2TQjpgCLcBGAs/s1600/Screen%2BShot%2B2018-11-01%2Bat%2B1.45.06%2BPM.png)  Nodes ![](https://d33wubrfki0l68.cloudfront.net/5cb72d407cbe2755e581b6de757e0d81760d5b86/a9df9/docs/tutorials/kubernetes-basics/public/images/module_03_nodes.svg)  Pools"Group of nodes with specific labels and taints" ![](https://docs.cambridgesemantics.com/anzo/v5.2/userdoc/images/k8s_cluster_concepts.png)  Kubernetes Control Plane![](https://miro.medium.com/max/700/1*QWJijlj7kwd0hIYk8Wsnow.png)  Deployment- Think of a deployments as a single application or microservice.- It is a collection of pods and other kubernetes objects- Deployment support the cluster to enforce state managment.- New pods to meet the requirements defined within the deployment configuration- Help up maintain and deploy rolling updates (i.e Deploing new pods without any downtime) ![](https://tachingchen.com/img/kubernetes-rolling-update-with-deployment/deployment-rs-pod.svg) ![](https://2.bp.blogspot.com/-ioqPceAM0DY/W9t1bloR5xI/AAAAAAAACGk/U5oTicERcBsD0ShBDj-uJUhpms8BzTGYACLcBGAs/s1600/Screen%2BShot%2B2018-11-01%2Bat%2B2.51.30%2BPM.png)  Connecting to the Clusterkubectl config  current-context: Displays the current-context    delete-cluster: Delete the specified cluster from the kubeconfig    delete-context: Delete the specified context from the kubeconfig    delete-userY: Delete the specified user from the kubeconfig    get-clusters: Display clusters defined in the kubeconfig    get-contexts: Describe one or many contexts    get-users: Display users defined in the kubeconfig    rename-context: Renames a context from the kubeconfig file.    set: Sets an individual value in a kubeconfig file    set-cluster: Sets a cluster entry in kubeconfig    set-context: Sets a context entry in kubeconfig    set-credentials: Sets a user entry in kubeconfig    unset: Unsets an individual value in a kubeconfig file    use-contex: Sets the current-context in a kubeconfig file    view: Display merged kubeconfig settings or a specified kubeconfig file  Get Context<jupyter_code># get k8 credentials via the Google SDK (i.e Similar on any cloud vendor)
!gcloud container clusters get-credentials demo-cluster --zone us-central1-c
# Current context
!kubectl config current-context<jupyter_output>gke_kavi-prod-gke-demo_us-central1-c_demo-cluster<jupyter_text>Get and Describe<jupyter_code># Ges Notes (List)
!kubectl get nodes
# Describe nodes
!kubectl describe nodes/gke-demo-cluster-small-compute-c8fb2aa8-g1tw
# Get Pods (List)
!kubectl get pods
# kubectl get service
!kubectl get deploy
# View the default
!kubectl get svc,deploy,pods -A<jupyter_output>NAMESPACE     NAME                                      TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)          AGE
api           service/flask-hello-world-load-balancer   LoadBalancer   10.116.11.36   172.16.17.32   5000:31892/TCP   13m
default       service/kubernetes                        ClusterIP      10.116.0.1     <none>         443/TCP          9h
kube-system   service/default-http-backend              NodePort       10.116.0.109   <none>         80:31703/TCP     9h
kube-system   service/kube-dns                          ClusterIP      10.116.0.10    <none>         53/UDP,53/TCP    9h
kube-system   service/metrics-server                    ClusterIP      10.116.5.131   <none>         443/TCP          9h

NAMESPACE     NAME                                            READY   UP-TO-DATE   AVAILABLE   AGE
api           deployment.apps/flask-hello-world               1/1     1            1           114s
gcloud        deployment.apps/gcloud-service                  1/1     1 [...]<jupyter_text>Creating a NameSpace<jupyter_code># kubectl create namespace
!kubectl create namespace api
# kubectl get namespaces
!kubectl get namespaces
# kubectl create namespace (for the sake of the example)
!kubectl create namespace api
!kubectl create namespace nginx
!kubectl create namespace gcloud<jupyter_output>namespace/api created
namespace/nginx created
Error from server (AlreadyExists): namespaces "gcloud" already exists<jupyter_text>Creating a Pod```yamlapiVersion: v1kind: Podmetadata:  name: nginxspec:  containers:  - name: nginx    image: nginx:1.14.2    ports:    - containerPort: 80 ```<jupyter_code># Create a pod
!kubectl apply -f configurations/nginx.yaml -n nginx
# view pod
!kubectl get pods -n nginx
# delete a specific pod using a configuration.
# (you can also us the pod id)
!kubectl delete -f configurations/nginx.yaml -n nginx<jupyter_output>pod "nginx" deleted<jupyter_text>- The community generally recommends one container per pod but you can have more- one container per pod makes rolling updates easier.- Pods are also great for cluster troubleshooting. I will create dnsutils and sdk pods to troubleshooing my deployment configuration, networking, and rbac issues.  Creating a Deployment```yamlapiVersion: apps/v1kind: Deploymentmetadata:  name: gcloud-servicespec:  replicas: 1  selector:    matchLabels:       app: gcloud-service  template:    metadata:      labels:        app: gcloud-service    spec:      containers:      - name: gcloud        image: gcr.io/google.com/cloudsdktool/cloud-sdk:latest        command: ["sleep", "infinity"]        imagePullPolicy: "Always"```<jupyter_code># Creating a deployment within the "api" namespace
!kubectl apply -f configurations/gcloud.yaml -n gcloud
# view deployment
!kubectl get deploy -n gcloud
# view pods
!kubectl get pods -n gcloud
# Describe deployment
!kubectl describe deploy gcloud-service -n gcloud
# kubectl delete deployment within the "api" namespace
!kubectl delete deploy gcloud-service -n gcloud<jupyter_output>deployment.apps "gcloud-service" deleted<jupyter_text>Events<jupyter_code># We call also the event the cluster is taking 
# (helpful in troubleshooting any image permissions issue)
!kubectl get events -A<jupyter_output>NAMESPACE   LAST SEEN   TYPE     REASON                 OBJECT                                    MESSAGE
api         3m37s       Normal   Scheduled              pod/flask-hello-world-5785b75dcc-27d2t    Successfully assigned api/flask-hello-world-5785b75dcc-27d2t to gke-demo-cluster-small-compute-c8fb2aa8-g1tw
api         3m37s       Normal   Pulling                pod/flask-hello-world-5785b75dcc-27d2t    Pulling image "gcr.io/kavi-prod-gke-demo/flask_hello_world:dev"
api         3m36s       Normal   Pulled                 pod/flask-hello-world-5785b75dcc-27d2t    Successfully pulled image "gcr.io/kavi-prod-gke-demo/flask_hello_world:dev" in 191.583422ms
api         3m36s       Normal   Created                pod/flask-hello-world-5785b75dcc-27d2t    Created container flask-hello-world
api         3m36s       Normal   Started                pod/flask-hello-world-5785b75dcc-27d2t    Started container flask-hello-world
api         3m37s       Normal   SuccessfulCreate       repli[...]<jupyter_text>Shell into a Deployment<jupyter_code># View pods
!kubectl get pods --output=wide -n gcloud
# List deployment across all namespaces
!kubectl get deploy -A
# exec into a deployment if you need to troubleshooting and active pods or deployment
# !kubectl exec -it deploy/gcloud-service -n gcloud -- /bin/bash<jupyter_output>root@gcloud-service-87f8966cf-prsq8:/# ^C

root@gcloud-service-87f8966cf-prsq8:/#<jupyter_text>Flask API DeploymentCode for a simple python app.```Pythonfrom flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello from Python!"if __name__ == "__main__":    app.run(host="0.0.0.0")``` ```DockefileFROM python:3.7RUN mkdir /appWORKDIR /appADD . /app/RUN pip install -r requirements.txtEXPOSE 5000CMD ["python", "/app/main.py"]``` ```yamlapiVersion: apps/v1kind: Deploymentmetadata:  name: flask-hello-worldspec:  replicas: 1  selector:    matchLabels:       app: flask-hello-world  template:    metadata:      labels:        app: flask-hello-world    spec:      containers:      - name: flask-hello-world        image: gcr.io/kavi-prod-gke-demo/flask_hello_world:dev        imagePullPolicy: "Always"        ports:        - containerPort: 5000---apiVersion: v1kind: Servicemetadata:  name: flask-hello-world-load-balancerspec:  type: LoadBalancer  selector:    app: flask-hello-world  ports:  - protocol: TCP    port: 5000    targetPort: 5000```<jupyter_code># List pods
!kubectl get pods -n api
# Create flask deployment.
!kubectl apply -f configurations/flask_hello_world.yaml -n api
# view log of the applications
!kubectl logs  deployment.apps/flask-hello-world -n api
!kubectl get svc,deploy,pods -A<jupyter_output>NAMESPACE     NAME                                      TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
api           service/flask-hello-world-load-balancer   LoadBalancer   10.116.13.255   172.16.17.32   5000:32715/TCP   106s
default       service/kubernetes                        ClusterIP      10.116.0.1      <none>         443/TCP          8h
kube-system   service/default-http-backend              NodePort       10.116.0.109    <none>         80:31703/TCP     8h
kube-system   service/kube-dns                          ClusterIP      10.116.0.10     <none>         53/UDP,53/TCP    8h
kube-system   service/metrics-server                    ClusterIP      10.116.5.131    <none>         443/TCP          8h

NAMESPACE     NAME                                            READY   UP-TO-DATE   AVAILABLE   AGE
api           deployment.apps/flask-hello-world               1/1     1            1           79s
kube-system   deployment.apps/event-exporter-gke              1/1 [...]<jupyter_text>You can now navigate to http://172.16.17.32:5000(active for only today)<jupyter_code># Delete flask deployment
!kubectl delete deploy/flask-hello-world -n api
# clean up everything. kubect delete deployment and namespace
!kubectl delete deploy gcloud-service -n gcloud
!kubectl delete deploy flask- -n gcloud
!kubectl delete deploy gcloud-service -n gcloud
!kubectl delete namespace api
!kubectl delete namespace nginx
!kubectl delete namespace gcloud<jupyter_output>Error from server (NotFound): deployments.apps "gcloud-service" not found
Error from server (NotFound): deployments.apps "flask-" not found
Error from server (NotFound): deployments.apps "gcloud-service" not found
namespace "api" deleted
namespace "nginx" deleted
Unable to connect to the server: error executing access token command "/Users/kavi/google-cloud-sdk/bin/gcloud config config-helper --format=json": err=fork/exec /Users/kavi/google-cloud-sdk/bin/gcloud: no such file or directory output= stderr=
{'fraction_non_alphanumeric': 0.08272632674297607, 'fraction_numerical': 0.04118973291710024, 'mean_word_length': 3.0723870056497176, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '390696', 'license': 'MIT', 'n_tokens_mistral': 3946, 'n_tokens_neox': 3609, 'n_words': 1029, 'path': 'presentation.ipynb', 'repo_name': 'kavisek/kubernetes-training', 'size': '11532'}