Sunday, September 29, 2019

Kubernetes multi-container pods

You can have more than one container per pod. Ok, what is a multi-container pod.

# What is multi-container pods:

  • Pods that has more than one container. 
  • Should not have totally unrelated pods, must be related in some way to provide a unit of work. 

Why Multi-container pods: You can now have the main container not have network / cross cutting logic such as log handling etc., and have other other container in the same pod having these logic. This way, the developer can only concentrate on the business logic while the other container acts as a proxy and handle other things.

How can containers in the pod communicate to each other:

1
shared network space:

conatainer can interact with
another container using localhost

2
Shared storage volume

Can have one container writing to a shared storage
volume while the other container read from the 
shared storage volume 

3
Shared process namespace

Containers can interact with one another containers process using shared process namespace



#How to create multiple container. 


apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: nginx
    image: nginx:1.15.8
    ports:
    - containerPort: 80
  - name: busybox-sidecar
    image: busybox
    command: ['sh', '-c', 'while true; do sleep 30; done;']


The above yml container 1 runs on port 80 and the other container can access the other container using localhost 80 port.


Sunday, September 22, 2019

HTTP Status codes for debugging (Refresh)

HTTP status codes (Refresh



1xx (Transitional phase requests - you may not use much for debugging)
100 - Continue requests
101 - Switching protocol
103 (Checkpoint)


2xx  (Success Informational - everything went well - you want these requested)
= 200 (OK)
- 201 (Created)
- 202 (Accepted)
- 205 (Reset Content)
- 206 (Partial content)

3xx (Redirection. you asked for something, it redirected to something)
- 301: Moved permanently - System redirected from old url to new urls.
- 302: Found
- 304: not modified if file has not modified
- 305: Use proxy
- 307: Temporary redirect

4xx (Client errors)
- 401 (Unauthorized error - login credentials are incorrect)
- 403 (Forbidden - Server knows who you are, but you are not allowed to access).
- 404 (Not found - url requested was not found).
- 410 (page is truly gone, no longer available - not coming back).

5xx (Internal Server errors)
- 500 (unexpected error: Server does not know what is the problem, but some server problem occurred)
- 503 (expected error: Server is not available)
- 504: Gateway timeout error (server made call to another server and timedout).

Saturday, September 14, 2019

Dockerfile - some basic best practices



Create a new docker file - Dockerfile

FROM alpine:3.4MAINTAINER Deiveehan Nallazhagappan deiveehan@gmail.com

RUN apk update
RUN apk add vim

Go to the prompt and say docker build -t deiveehan/alpine-extend .


This should create the image.

What is image cache: 
This is the image cache that gets built for each every docker command in the docker file. 
This is done because if you add more to the docker file, it wont take time by creating from scratch. 
For example if I add one more line to add git. 

FROM alpine:3.4MAINTAINER Deiveehan Nallazhagappan deiveehan@gmail.com

RUN apk update
RUN apk add vim
RUN apk add git

then it assembles till "add vim" from the local image cache and then builds only the add git. 

Best practices:
1. manage the RUN commands or any lines in the docker file that dont change frequently in the top and one that changes in the bottom. 

You can do like this
RUN apk update && \
    apk add curl && \
    apk add vim && \
    apk add git
so that it does not create multiple local image caches. 

2. Pick the right image (a slim image)

3. DO it yourselves: Go to the shell and start typing commands that helps build the image and the steps in the before step and include them in the docker file
This is better than blindly following some web sites. 



Friday, September 13, 2019

Kubernetes kubectl cheatsheet


Cheatsheets:



Kubectl commands
  • Apply/create: create resources
  • Run: run the pod from an image. 
  • Explain: documentation of resources. 
  • Delete: a resource. 
  • Get
    • Deployments
    • Pods
    • Events
    • Nodes
  • Describe: display detailed information. 
  • exec: similar to docker exec (executes a command on the container). 
  • Logs: view logs of a container. 
  • Config
  • Cluster-info
  • Expose
  • Scale

Minikube start
minikube start --cpus 4 --memory 8192

Minikube stop
Minikube delete
Minikube ip
Minikube status
Minikube dashboard

#namespaces
Kubectl get ns
Kubectl get po -n default
kubectl get all -n kube-system

Nslookup <servicename>.<namespace>

#ssh
kubectl exec -it pod/webapp-669ddb74b6-gbxhl sh


Kubectl apply -f first-pod.yml
Kubectl get all
kubectl exec webapp ls
kubectl -it exec webapp sh
kubectl describe pod webapp

# delete pod
kubectl delete pods --all
Kubectl delete pod webapp-release-0-5
kubectl delete rs webapp

#rollouts
kubectl rollout history deploy webapp
Kubectl rollout undo deploy web app —to-revision=2

# describe:
Kubectl describe replicaset webapp

#Logs
Kubectl logs 

kubectl describe service fleetman-webapp
kubectl delete po webapp-release-0-5


Kubernetes basic building blocks


Building blocks of Kubernetes: 
  • Pods
  • ReplicaSets
  • Deployments
  • Namespaces
#Pods:
  • wrapper around the container that kubernetes understands to maintain the cluster state. 
  • Can run multiple containers. 
  • Smallest and simplest unit of deployment object. 
  • Pods
    • sheduled on the same host
    • Shares same network namespace
  • Can run different configurations
    • Homogeneous pods: multiple versions of the same deployment
    • Heterogeneous pods: multiple pods of different configurations. 
#ReplicationController
  • pod manager, Makes sure no. Of replicas in a pod is running as required. If less, it creates, if more it kills pods.
#ReplicaSets
  • Next generation Replication controller that manages pod to maintain desired state. 
  • Supports equolity and set based selectors. 
#Namespaces:
  • Are used to group resources and assign privileges based on the namespaces.
#Labels and selectors: 
  • Metadata can be assigned to pods or any objects as labels, ways by which you can glue multiple objects to gather. 
  • These are used by kubernetes to perform operations to the group based on labels. 
#Services: 
  • Are essentially endpoints by which pods communicate to other pods internally /externally (for example, web server / cache / db need to talk to each other)
  • Exposed through endpoints (Internal and external). 
    • Internal: example: don’t have to expose database to outside world, but accessible internally. 
    • External: web UI to be exposed outside. 

Kubernetes - installation options


Intalling Kubernetes..

Types of installation: 
  1. All in one Single node installation: master/worker are installed in single node, useful for learning. 
  2. Single-node etcd, single master, multi-worker installation. 
  3. Single-node etcd, multi-master, multi-worker installation
  4. Multi-node etcd, multi-master, multi-worker installation. 

Where can you install Kubernetes: 
  • Cloud
    • IAAS: VMS inside a IAAS provider such as Amazon. 
    • PAAS: Kubernetes as a managed service. 
      • PKS in pivotal cloud. 
  • On-Prem
    • On Prem VMs
    • On Prem bare metal
  • Local installation: 
    • Minikube
  • Hosted solution
    • GC: using Google Kubernetes Engine
    • PCF: using PKS
    • Microsoft Azure: using Azure Container Service
    • AWS: using EKS
    • Openshift dedicated
    • Platform9


The easiest way for getting started on kuberentes is to use the Minikube, ensure you have a good amount of memory in your local. 8GB, 16 GB preferred.

You can use the GKE on GCP, you will get a 300$ credit on GCP if you are a new user. You can use use google cloud console or using the CLI to create the cluster in GCP.

You can use EKS option in AWS to install kubernetes cluster in AWS. EKS console option or EKS CLI option. EKS cli option is easier and it does most of hte complex work for you, such as vpc, subnets, security groups etc., 

Kubernetes - getting started using Minikube


Getting started on Kubernetes - Local installation, running sample images and executing basic commands. 

There are different ways by which you can install Kubernetes, depending upon what you want. 
  • Google cloud
  • On Prem 
  • Local installation (Minikube)

This video installs on Mac, you may want to find a suitable installation for your OS. 

  1. Install Virtual box. 
sudo apt-get install virtualbox

  1. Minikube. 
brew cask install minikube

  1. Start minikube
minikube start
minikube status

  1. Kubectl
Brew install kubectl

You can access Kubernetes cluster using the following ways:
  1. CLI - kubectl
  2. Dashboard
  3. API

minikube dashboard
This opens up a dashboard which you can use to view the pods, replica sets and other information about the kubernetes. 

minikube stop



--------------
Create a new sample app in minikube based on an existing image.

$kubectl run hello-minikube --image=gc4.io/google_containers/echoserver:1.4 --port=8080
deployment.apps "hello-minikube" created

exposing the deployment as a Nodeport
$kubectl expose deployment hello-minikube --type=NodePort
service "hello-minikube" exposed

ket the pod
$kubectl get pod
NAME                              READY     STATUS             RESTARTS   AGE
hello-minikube-7f45dd544b-qcwk2   0/1       ImagePullBackOff   0          2m

delete a deployment
~ $kubectl delete deployment hello-minikube
deployment.extensions "hello-minikube" deleted


You can create a deployment based on an image as below
Kubectl create deployment <deployment-name> —image=<image-name>
Kubectl get deployments

Get all the pods in the default namespace
Kubectl get pods
Kubectl get events

Expose deployment
Kubectl expose deployment <deployment-name> —type=LoadBalancer —port-80
Kubectl get services

Kubectl service <service-name>
kubectl delete service hello-node