Kubernetes(k8s) Basic Concepts (Part2)
Kubernetes Basic Terms
If you want to get more familiar with Kubernetes, lets discuss few terminologies.
Namespaces: In Kubernetes, the namespaces is effectively your working area or can think of as sub cluster. It basically isolate group of resources like you can keep db specific pods inside db1 and server running pods inside app1. Users interacting with one namespace can not see the content in another namespace and you can not have two objects of same name within the same namespace. For example: If you’re in the default namespace, you can’t create two Pods of same name test.
Pods: A pod is effectively a fundamental unit of work. A Pod is analogous to a container but with some key differences. Pod can have a single container or multiple containers with shared storage/volumes and network resources.
Service: Kubernetes has a concept of a service, which is used to expose Pods to the network. Services point to pods directly using labels. This gives great flexibility because it doesn’t matter how you create those pods.
Ingress: This works with the service to make sure everything ends up in the right place. Ingress can also provide load balancing. It controls traffic to and from services, as well as external access to services. It performs load balancing tasks by setting up an external load balancer and directs traffic to specific service based on a set of rules. This enables you to use multiple back-end services via the same IP address.
ConfigMaps: This is an API object for storing information in key-value pairs. A ConfigMap is very useful for doing things like maintaining environment variables or files that can actually be mounted directly into pods without actually having to have an actual file system somewhere. They’re not meant for confidential data.
Secrets: Secrets are an object and a place to store confidential information as the name implies.
Volume: A volume is a directory containing all data accessible for containers in a given pod. Volumes provide a method for connecting containers and pods — which only exist as long as you use them — to a more permanent set of data stored elsewhere. When you delete a pod, the volume associated with it is destroyed as well. However, the data within that volume outlasts the containers or pods that use it.
ReplicaSets: ReplicaSets are used to consistently replicate a Pod. They provide a guarantee that a given number of replicas will be running at any time. If a node goes offline or a Pod becomes unhealthy, Kubernetes will automatically schedule a new Pod instance to maintain the specified replica count.
Deployments: Deployments wrap ReplicaSets with support for declarative updates and rollbacks. They’re a higher level of abstraction that’s easier to control.
You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. A deployment is an object in Kubernetes that lets you manage a set of identical pods. Without a deployment, you’d need to create, update, and delete a bunch of pods manually.
Jobs: A Kubernetes Job is an object that creates a set of Pods and waits for them to terminate.
Jobs provide a mechanism for running ad-hoc tasks inside your cluster. Kubernetes also provides CronJobs that wrap Jobs with cron-like scheduling support. It runs a job periodically on a given schedule for performing tasks.
Here are few commands to perform basic operations
kubectl cluster-info − It displays the cluster Info.
$ kubectl cluster-info
kubectl apply − It has the capability to configure a resource by file.
$ kubectl apply –f <filename>
kubectl get − It has the capability to list down the resources.
$ kubectl get <Resource Name>
For example,
$ kubectl get nodes
$ kubectl get ns
$ kubectl get pods
$ kubectl get pods -n <namespace name>
kubectl create − To create resource by filename of or stdin. To do this, JSON or YAML formats are accepted.
$ kubectl create –f <File Name>
$ cat <file name> | kubectl create –f -
In the same way, we can create multiple things using the create command along with kubectl.
kubectl delete − Deletes resources by file name, stdin, resource and names.
$ kubectl delete –f ([-f FILENAME] | TYPE [(NAME | -l label | --all)])
kubectl describe − Describes any particular resource in kubernetes. Shows details of resource or a group of resources.
$ kubectl describe <type> <type name>
$ kubectl describe pod nginx
kubectl edit − It is used to end the resources on the server. This allows to directly edit a resource which one can receive via the command line tool.
$ kubectl edit <Resource/Name | File Name)
Ex.
$ kubectl edit pod/nginx
kubectl exec − This helps to execute a command in the container.
$ kubectl exec POD <-c CONTAINER > -- COMMAND < args...>
$ kubectl exec nginx cat /etc/nginx/default.conf
kubectl get − This command is capable of fetching data on the cluster about the Kubernetes resources.
$ kubectl get [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columnsfile=...|
go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...]
(TYPE [NAME | -l label] | TYPE/NAME ...) [flags]
For example,
$ kubectl get pod <pod name>
$ kubectl get service <Service name>
kubectl logs − They are used to get the logs of the container in a pod. Printing the logs can be defining the container name in the pod. If the POD has only one container there is no need to define its name.
$ kubectl logs [-f] [-p] POD [-c CONTAINER]
Example
$ kubectl logs nginx -f
kubectl port-forward − They are used to forward one or more local port to pods.
$ kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT
[...[LOCAL_PORT_N:]REMOTE_PORT_N]
$ kubectl port-forward nginx 3000 4000
kubectl cp − Copy files and directories to and from containers.
$ kubectl cp <Files from source> <Files to Destinatiion>
$ kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>