Getting Started with Kubectl

General Jun 24, 2019

Kubectl is the main cli tool to control your kubernetes cluster. in Kubernetes requests operations are executed through the API server. kubectl is the primary tool which will interact with your API server. It carries out any operation you would like to realize on your cluster - create,read, update e.t.c. Operations are performed on Cluster resources.

Basic Operations
Kubectl has a basic operation format which is

Kubectl [command] [type]    [name]       [flags]
kubectl    get     pods --all-namespaces -o wide

apply/create: Standard way of creating resources from a commandline or through a file through the api server

  • from a file

    kubectl create -f nginx.yaml
    
  • from the commandline

    kubectl create webserver nginx --image=nginx 
    

apply: Start a Pod or deployment and specify the pody image

kubectl apply -f nginx.yaml

explain: Documentation for resources or particular kubernetes api or resources

kubectl explain pod/test

delete: This will delete the resources

kubectl delete -f nginx.yaml

get: This will display information about the specified resource types

kubectl get --all-namespaces

describe: This will give more detailed information about the resource

kubectl describe pod/test

exec: This allows command execution inside a container on a pod

kubectl exec -it "test" /bin/bash

logs: This allows you to view logs on a container

kubectl logs pod/test 

Kubectl supports 3 output format. we can output information from our resources to the following formats

wide - output additional information to stdout

kubectl get pods --all-namespaces -o wide

yaml - YAML formatted API object

kubectl get pods --all-namespaces -o yaml

json - JSON formatted API object

kubectl get pods --all-namespaces -o json

resource types can be abbrevated in Kubectl e.g.
nodes - no
pods - po
deployments - deploy

This can be view further in the link below:
https://kubernetes.io/docs/reference/kubectl/overview/#resource-types

For futher references and cheat sheet you can view in the link below
https://kubernetes.io/docs/reference/kubectl/kubectl/
https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
#