Posts | Projects | Cloudwatch-AI LIVE Demo | Contact

K8s CC - Task 1: Basic kubectl commands

Let us explore our Minikube environment: Run the help to learn about the basic commands:

kubectl -h
$ kubectl -h
kubectl controls the Kubernetes cluster manager.

 Find more information at:

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and label selector

Notice that we can create, delete, get and describe resources. We will later work with the following resources:

Create a deployment

The following command will instruct K8s to create a deployment and a pod with nginx docker container image nginx. It will set a lot of defaults as well

kubectl create deployment nginx --image=nginx

Let us explore the deployment we created. Run the following command:

kubectl get deployment
$kubectl get deployment
NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
nginx                               1/1     1            1           14s

We can also explore more:

kubectl get pods
$ kubectl get pods
NAME                                                READY   STATUS    RESTARTS   AGE
nginx-7854ff8877-6wq4n                              1/1     Running   0          3m23s

We can use the -o wide option to show more information such as the IP addresses:

kubectl get pods -o wide
$ kubectl get pods -o wide
NAME                                                READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
nginx-7854ff8877-6wq4n                              1/1     Running   0          5m11s   minikube   <none>           <none>

We can also describe resources using the command kubectl describe pods <POD-NAME>:

kubectl describe pods nginx-7854ff8877-6wq4n
$ kubectl describe pods nginx-7854ff8877-6wq4n
Name:             nginx-7854ff8877-6wq4n
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/
Start Time:       Fri, 22 Dec 2023 19:34:20 -0600
Labels:           app=nginx
Annotations:      <none>
Status:           Running
Controlled By:  ReplicaSet/nginx-7854ff8877
    Container ID:   docker://455c96e8e59fb29fd3aabe439de64c9e0b1b1de0dc691fd0389afbdab382a025
    Image:          nginx
Exploring the pods

So far we have created one deployment and one pod. Use the following to execute commands inside a specific pod (Container): kubectl exec <pod-name> -- <Shell Command>

kubectl exec nginx-7854ff8877-6wq4n -- uname -a
$ kubectl exec nginx-7854ff8877-6wq4n -- uname -a
Linux nginx-7854ff8877-6wq4n 5.10.57 ###1 SMP Tue Nov 7 06:51:54 UTC 2023 x86_64 GNU/Linux

We can also get a shell on a container:

kubectl exec -ti nginx-7854ff8877-6wq4n -- /bin/bash
$ kubectl exec -ti nginx-7854ff8877-6wq4n -- /bin/bash
root@nginx-7854ff8877-6wq4n:/# uname -a
Linux nginx-7854ff8877-6wq4n 5.10.57 ###1 SMP Tue Nov 7 06:51:54 UTC 2023 x86_64 GNU/Linux

Finally, we can read logs from a pod using kubectl logs <pod-name>

kubectl logs nginx-7854ff8877-6wq4n
$ kubectl logs nginx-7854ff8877-6wq4n
/ /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/ Looking for shell scripts in /docker-entrypoint.d/
/ Launching /docker-entrypoint.d/ info: Getting the checksum of /etc/nginx/conf.d/default.conf info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/ Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/ Launching /docker-entrypoint.d/
/ Launching /docker-entrypoint.d/
/ Configuration complete; ready for start up
2023/12/23 01:34:33 [notice] 1###1: using the "epoll" event method
2023/12/23 01:34:33 [notice] 1###1: nginx/1.25.3
2023/12/23 01:34:33 [notice] 1###1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023/12/23 01:34:33 [notice] 1###1: OS: Linux 5.10.57
2023/12/23 01:34:33 [notice] 1###1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/12/23 01:34:33 [notice] 1###1: start worker processes
2023/12/23 01:34:33 [notice] 1###1: start worker process 28
2023/12/23 01:34:33 [notice] 1###1: start worker process 29


Task 2: Building a sample application

By: Ahmed Abugharbia
Date: 1-1-2024