Skip to content

Kubernetes Objects

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

Table of contents

 

top

 

Pods

 

Deployment

A Deployment provides declarative updates for Pods and ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

Create a deployment-definition.yml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container-pod
        image: nginx
  1. Lets deploy some pod with deployment using yml file
kubectl create -f deployment-definition.yml
  1. Get list of replication controller
kubectl get deployments
  1. Get list of Pods
kuberctl get pods

 

top

 

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

 

Clone this wiki locally