Skip to content

Kubernetes Objects

Syed Sayem edited this page Jan 5, 2020 · 8 revisions

Table of contents

 

top

 

Pods

  1. Lets deploy a pod using:
kubectl run nginx

This command will only deploy a Pod on your kubernetes worker node without any container inside it

  1. Now lets deploy a pod with a Nginx container
kubectl run nginx --image nginx

This command will only deploy a Pod on your kubernetes worker node with nginx container inside it

  1. Get running pod list using
kubectl get pod
  1. Get running pod details using:
kubectl describe pod <pod-name>
  1. Delete pod using
kubectl delete pod <pod-name>
  1. Now lets try to deploy pod with its container using YAML file

Create a pod-definition.yml file

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    name: myapp-pod
    type: front-end
spec:
  containers:
  - name: nginx-container
    image: nginx

Deploy using following command

kubectl create -f pod-definition.yml

 

Services

In Kubernetes, there are three general approaches to exposing your application.

  • Using a Kubernetes service of type NodePort, which exposes the application on a port across each of your nodes
  • Use a Kubernetes service of type LoadBalancer, which creates an external load balancer that points to a Kubernetes service in your cluster
  • Use a Kubernetes Ingress Resource

Read more on Medium

 

ClusterIP

top  

NodePort

top  

LoadBalancer

top  

Ingress

 

top

 

Volumes

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory

 

top

 

Namespaces

  1. Get pod details of other namespace using:
kubectl get pods --namespace=dev
  1. By below command you will get pod details of default namespace
kubectl get pods
  1. You can change default namespace by using:
kubectl config set-context ${kubectl config current-context} --namespace=dev
  1. Now for getting pod details of default you need to use
kubectl get pods --namespace=default
  1. Get all pods details across namespaces using:
kubectl get pods --all-namespaces
  1. Creating a namespace using yml definition:

First, Create a namespace-dev.yml file

apiVersion: v1
Kind: Namespace
metadata:
  name: dev

Now, run the following command:

kubectl create -f namespace-dev.yml
  1. Mentioning namespace in pod:
kubectl create -f pod-definition.yml --namespace=dev
  1. Get list of system pods using:
kubectl get pods --namespace=kube-system
  1. You can set resource limits on namespaces, here an example yml for same:
apiVersion: 1
kind: ResourceQuota
metadata:
 namespace: compute-quota
 namespace: dev
spec:
 hard:
  pods: "10"
  requests.cpu: "4"
  requests.memory: 5Gi
  limits.cpu: "10"
  limits.memory: 10Gi
kubectl create -f compute-quota.yml

 

top

 

Labels and Selectors

 

top