Saturday, 11 July 2020

Kubernetes: pod Demo

Step 1: Let’s check the nodes in the system by executing below command.

kubectl get nodes

$kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   29d   v1.18.0

As you see the output, there is a node with the name ‘minikube’ is running and in Ready state. 

 

Step 2: Let’s get the pods in your system by executing the command ‘kubectl get pods’.

$kubectl get pods
No resources found in default namespace.

Step 3: Let’s run ‘jboss/wildfly’ image on the Kubernetes cluster by executing the below command.

 

kubectl run user-service --image jboss/wildfly

 

In the above command

         Pod name: user-service

         Image to pull from docker hub is: jboss/wildfly

$kubectl run user-service --image jboss/wildfly
pod/user-service created

As you see the output, a user-service pod is created.

 

Step 4: Use the command ‘kubectl get pods’ to get pods information.

$kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
user-service   1/1     Running   0          83s

As you see that the pod user-service is in Running state.

 

Execute the command ‘kubectl describe pods’ to know information about pods.

$kubectl describe pods
Name:         user-service
Namespace:    default
Priority:     0
Node:         minikube/192.168.99.100
Start Time:   Thu, 04 Jun 2020 19:49:46 +0530
Labels:       run=user-service
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  user-service:
    Container ID:   docker://453f6ec43c0c8fa6701a61f78cd01d77b73b86347656a5b911719a9c27c40e7d
    Image:          jboss/wildfly
    Image ID:       docker-pullable://jboss/wildfly@sha256:67a4f90b213bc2600d08d90e82df58be83813d118d163fbcc8765b6eeaade7e6
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 04 Jun 2020 19:49:53 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-w7rp7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-w7rp7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/user-service to minikube
  Normal  Pulling    3m24s      kubelet, minikube  Pulling image "jboss/wildfly"
  Normal  Pulled     3m18s      kubelet, minikube  Successfully pulled image "jboss/wildfly"
  Normal  Created    3m18s      kubelet, minikube  Created container user-service
  Normal  Started    3m18s      kubelet, minikube  Started container user-service

From the output, you can confirm below things.

 

Name:         user-service

Pod name is user-service

 

Namespace:    default

Pod running in default namespace.

 

Node:         minikube/192.168.99.100

Pod running on the node minikube

 

Status:       Running

Pod is in running status

 

Apart from these, you can get container id, image, image id etc., from the output.

 

At the end of the output, you see Events; It gives step-by-step details of how the container created.

Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/user-service to minikube
  Normal  Pulling    3m24s      kubelet, minikube  Pulling image "jboss/wildfly"
  Normal  Pulled     3m18s      kubelet, minikube  Successfully pulled image "jboss/wildfly"
  Normal  Created    3m18s      kubelet, minikube  Created container user-service
  Normal  Started    3m18s      kubelet, minikube  Started container user-service

Each pod gets its own internal IP address in the Kubernetes cluster. You can see the IP address of the pods by providing ‘-o wide’ option to get pods command.

$kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
user-service   1/1     Running   0          45m   172.17.0.6   minikube   <none>           <none>

That’s it you are done with the creation of pod.


Previous                                                    Next                                                    Home

Kubernetes: pods

Pod is a basic unit in Kubernetes. Kubernetes do not deploy containers directly on worker nodes. Containers are encapsulated into a Kubernetes object called pod. Ideally, a pod represents a single instance of an application.

What if the number of requests to your application is increasing and the current pod unable to handle?

Kubernetes spin up another pod for your application and this pod ideally deployed in different node to achieve high availability.

When more requests are coming, new pods will get added by Kubernetes. When fewer requests coming, Kubernetes maintains sufficient pods by deleting some of the existing pods.

 

Can a single pod encapsulate multiple containers?

Yes, a pod can run multiple containers within a single namespace and exposed by the same IP address.

You may need to encapsulate multiple containers into single Pod, when you have an application with one main process and one or more complementary processes.

 

For example, you have a user-service that takes care of user authentication, and manage user roles etc., and there is a user-audit container that exposes an API to store user audit data like when user logged in, logged out, login failure attempts etc.,

 

Assume there is one-to-one relationship between this user container and user-audit container.

 

If we can’t put these two containers in the same Pod, we need to design like below.

When a User container spin up, another user-audit container should spin up to maintain one-to-one mapping. When a User container stopped, corresponding user-audit container should get stopped.


Since ‘User Container’ and ‘User Audit Container’ are in two different pods.

a.   We need to establish communication between User and User-Audit containers explicitly.

b.   We need to create sharable volumes among user and user-audit containers. If they are in same pod, we no need to do this explicitly.

c.    When a user container stopped, we need to stop user audit container explicitly.

d.   When new user container spin up, we need to start user-audit container explicitly.


But, if the containers keep in the same Pod, both get created and destroyed together

Since the containers are in the same pod, they can share common resources like network space, Persistent Storage volumes and they can communicate directly. Containers in the same pod can access the same storage.

 

How is it possible for containers within a Pod to share resources?

Kubernetes configure Docker to run all the containers of a Pod to share the same Linux Namespace.


Is Kubernetes horizontally scale Containers?

No, Kubernetes scale the Pods horizontally.

 

Note

a.   Usually, you will not directly interact with pods, you create a deployment which will create pods.

b.   If a Pod encapsulates more than one container, then all the containers always run on a single worker node.

c. In most of the user cases, a pod contain a single container.

 

 

Previous                                                    Next                                                    Home

Kubernetes: Get Cluster information

'kubectl cluster-info' command print the cluster information to console.

$kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.




Previous                                                    Next                                                    Home

How to check what operations you can perform against Kubernetes?

Use the command 'kubectl auth can-i ....' to see whether you can able to perform specific operation or not.

 

Example

kubectl auth can-i get pods

kubectl auth can-i get deployment

kubectl auth can-i get replicaset

kubectl auth can-i create pod


$kubectl auth can-i get pods
yes
$
$kubectl auth can-i get deployments
yes
$
$kubectl auth can-i get replicasets
yes
$
$kubectl auth can-i create pod
Yes



Previous                                                    Next                                                    Home

Kubernetes: get the configuration used by kubectl

Just execute the command ‘kubectl config view’ to see the configuration used by kubectl.

$kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Users/krishna/.minikube/ca.crt
    server: https://192.168.99.100:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /Users/krishna/.minikube/profiles/minikube/client.crt
    client-key: /Users/krishna/.minikube/profiles/minikube/client.key

As you observe above output,

a.   user with the name ‘minikube’ is configured with some client certificate and client key. This set up is done by minikube. This is the reason why kubectl does not ask you to login while communicating with Kubernetes.

b.   Specifies which cluster to communicate with (https://192.168.99.100:8443).

 

When you directly hit the server url in browser, you will receive a Forbidden message. It is because you are not provided any authentication details.

 

{

  "kind": "Status",

  "apiVersion": "v1",

  "metadata": {

   

  },

  "status": "Failure",

  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",

  "reason": "Forbidden",

  "details": {

   

  },

  "code": 403

}

 

Kubectl Configuration is located in the file '~/.kube/config' (~ represents your home directory).



Previous                                                    Next                                                    Home

Thursday, 9 July 2020

Kubernetes: What is kubectl?

‘kubectl’ is a command-line tool that helps us to communicate with the master node. Under the hood, ‘kubectl’ uses a ‘curl’ tool to send API requests to the Kubernetes server.

 

Syntax

kubectl [command] [TYPE] [NAME] [flags]

 

Example

kubectl get pods

 

Above command prints all the pods available in Kubernetes.

 

If you want to know all the available commands in kubectl, just execute the command ‘kubectl’ it prints all the available commands in the terminal.

$kubectl
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

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       Documentation of resources
  get           Display one or many resources
  edit          Edit a resource on the server
  delete        Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout       Manage the rollout of a resource
  scale         Set a new size for a Deployment, ReplicaSet or Replication Controller
  autoscale     Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate   Modify certificate resources.
  cluster-info  Display cluster info
  top           Display Resource (CPU/Memory/Storage) usage.
  cordon        Mark node as unschedulable
  uncordon      Mark node as schedulable
  drain         Drain node in preparation for maintenance
  taint         Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe      Show details of a specific resource or group of resources
  logs          Print the logs for a container in a pod
  attach        Attach to a running container
  exec          Execute a command in a container
  port-forward  Forward one or more local ports to a pod
  proxy         Run a proxy to the Kubernetes API server
  cp            Copy files and directories to and from containers.
  auth          Inspect authorization

Advanced Commands:
  diff          Diff live version against would-be applied version
  apply         Apply a configuration to a resource by filename or stdin
  patch         Update field(s) of a resource using strategic merge patch
  replace       Replace a resource by filename or stdin
  wait          Experimental: Wait for a specific condition on one or many resources.
  convert       Convert config files between different API versions
  kustomize     Build a kustomization target from a directory or a remote url.

Settings Commands:
  label         Update the labels on a resource
  annotate      Update the annotations on a resource
  completion    Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  alpha         Commands for features in alpha
  api-resources Print the supported API resources on the server
  api-versions  Print the supported API versions on the server, in the form of "group/version"
  config        Modify kubeconfig files
  plugin        Provides utilities for interacting with plugins.
  version       Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

As you see the output, commands are grouped by their category.

 

How to get help about specific command?

Syntax

kubectl {command} --help

 

Example

kubectl get --help



Previous                                                    Next                                                    Home

Kubernetes: Hello World Application using dashboard

Step 1: Open a terminal and execute the below command to start the dashboard.

minikube dashboard

$minikube dashboard
🤔  Verifying dashboard health ...
🚀  Launching proxy ...
🤔  Verifying proxy health ...
🎉  Opening http://127.0.0.1:52023/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

Once you execute the command, it automatically opens the dashboard in browser.

Click on + button available at top right corner of the page.


You can create the application in three ways.

a.   By directly providing input in yaml or json format

b.   From an yaml or json file

c.    Using a form

 

Let’s choose the third option 'Using a form'.

 

Give App Name as ‘user-service’

Container Image as ‘jboss/wildfly’ and

Number of Pods as 1.


Click on Deploy button.

 

Once you click on Deploy button, you will be redirected to the dashboard.


Under the Deployments section, you can see that the deployment ‘user-service’ is up and running.

 

You can see all the objects in Kubernetes by executing the command ‘kubectl get all’.

$kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/user-service-7977c4d9bc-x946n   1/1     Running   0          8m55s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   29d

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/user-service   1/1     1            1           8m55s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/user-service-7977c4d9bc   1         1         1       8m55s

As you see the output, deployment user-service is created, similarly, replicaset and pod also created. Do not worry much, I will explain these things in my later posts.

 

You can delete the deployment by executing the below command.

 

kubectl delete deployment user-service

$kubectl delete deployment user-service
deployment.apps "user-service" deleted

Once you execute the command rerun ‘get all’ to make sure that the deployment, replicaset, and pod get deleted.

$kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   29d

Perfect, from the output, you can confirm that only the Kubernetes service is running.



Previous                                                    Next                                                    Home