Working with Kubernetes

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

https://kubernetes.io/

Features

  • Management of containers: Service discovery/load balancing, secrete and configuration management
  • Scale containers: self healing, horizontal scaling
  • Storage Orchestration: Robust networking and storage options

Here is the list of useful Kubernetes commands and examples.


Basic Commands

kubectl version 
kubectl cluster-info 

# pods, deployments, and services 
kubectl get all 
kubectl get all -o wide

# create a deployment for a pod
kubectl run <container> --image=<image> 

# forward a port wot allow external access
kubectl port-forward <pod> <ports> 

# expose a port for a deployment
kubectl expose 

kubectl create <resource> 
kubectl apply <resource>

Checking Kubernetes is working

kubectl get all

# if you got this error:
# The connection to the server localhost:8080 was refused
# - did you specify the right host or port?
# run the following commands
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Checking Cluster

kubectl cluster-info

Namespaces

  • Namespaces provide a scope for names.
kubectl create namespace <ns-name>

kubectl get namespace
kubectl describe namespace <ns-name>

# use -n option to see the resources in the namespace
kubectl get all -n <ns-name> 

# access all namespaces
kubectl get all --all-namespaces
kubectl get all -A
  • Example
kubectl create ns my-namespace \
--dry-run=client -o yaml > ns.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

Kubernetes Objects

Kubernetes API primitives (objects) are the basic building blocks that represent the state of the cluster.

  • What kind of applications are running on which nodes
  • The resource available to the applications
  • The policies that define the behaviors of applications, (restart policies, fault-tolerance, and upgrade)
  • Types of Kubernetes Objects: Pods, Nodes, Services, Service Accounts
# list of resources (objects) - name and type
kubectl api-resources

kubectl api-resources -o name #name only

You can retrieve the objects like this:

kubectl get nodes

kubectl get ns # namespaces

kubectl get pods
kubectl get pods -A
kubectl get pods -o wide
kubectl get pods --show-labels

kubectl get deploy
kubectl get rs # replicasets

# horizontal pod autoscaler
kubectl get hpa 

kubectl get jobs
kubectl get cj # cronjobs

# persistent volumes
kubectl get pv

# persistent volume claims
kubectl get pvc  

kubectl get cm # configMaps
kubectl get secrets

# service accounts
kubectl get sa

kubectl get events

kubectl get svc # services

# resource quotas
kubectl get quota

Managing Kubernetes Objects

You can use the “kubectl” command-line tool to create and manage Kubernetes objects.

  • Imperative commands: manage objects directly using a single command
kubectl create deployment nginx --image nginx
  • Imperative object configuration: using at least one definition file
kubectl apply -f nginx.yaml
  • Declarative object configuration: a user operates on object configuration files stored locally. Create, update, and delete operations are automatically detected per-object by kubectl.
kubectl diff -f configs/
kubectl apply -f configs/

Getting Object Information

  • The yaml format is usually used to define the object and its spec.
kubectl get <object-type>

kubectl get pods
kubectl get pods -n kube-system # system pods

kubectl get nodes
kubectl get nodes <node-name>
kubectl get nodes <node-name> -o yaml

kubectl describe node <node-name>


Checking nodes

kubectl get nodes

kubectl describe node <node_name>

Getting Definition Files

kubectl <cmd> --dry-run=client -o yaml 
kubectl run <pod-name> --image=<image-name> --dry-run -o yaml

# Examples

kubectl run nginx --image=nginx  --dry-run=client -o yaml 

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml 

kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

kubectl create service nodeport nginx-service --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

You can output to the file using:

kubectl <cmd> > <file-name.yaml>

kubectl run <pod-name> --image=<image-name> --dry-run=client -o yaml > pod.yaml

kubectl get <resource> <resource-name> -o yaml > resource.yaml 

kubectl get pod <pod-name> -o yaml > pod.yaml

kubectl get pod redis -o yaml > pod.yaml 

Deleting resources

#delete pod causes a deployment will recreate a pod
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --namespace <ns>

#delete a deployment
kubectl delete deployment <deployment-name>

#delete all
kubectl delete all --all --all-namespaces

Container Logs

A container’s normal console output goes into the container log.

  • Create a pod with 2 containers that print “Hello, World!”.
  • In the busybox pod, you do not need “sh -c” for a command.
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container1
    image: busybox
    command: ["echo"]
    args: ['Hello World! from container 1'] 
  - name: my-container2
    image: busybox
    command: ["echo"]
    args: ['Hello World! from container 2']
  restartPolicy: Never
  • And then check the output using the kubectl logs command.
kubectl logs <pod-name> -c <conainter-name>

# only for a single container pod
kubectl logs my-pod  

# for multi-container pod
kubectl logs my-pod –c my-container1 
kubectl logs my-pod –c my-container2

Monitoring Pods and Nodes

You can use the kubectl top command to see the resource usage (CPU/Memory/Storage) of pods or nodes.

kubectl top nodes
kubectl top node <node-name>

kubectl top pods
kubectl top pod <pod-name>

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s