Search This Blog

Tuesday, April 25, 2017

Google Cloud Platform Tutorial Series 103


In our previous section we learnt how to operate google cloud platform using a quick start boiler plate for python, in this section we will learn about the containers.
Containers are the heart of google cloud platform, our applications are deployed on containers(Dockers or google containers), these containers are a miniature version of virtual machines.Containers is a bundle of all the required components which are required to run a specific application.There is a one to many relations ship between a virtual machine and containers.
To operate and orchestrate multiple containers google provides; Kubernetes.
image
Kubernetes is used for automating deployment, scaling, and management of containerized applications.

Lets understand a few terms:

  • Cluster : Set of master/ worker nodes.
  • Node: Compute engine instance like a VM
  • Pod: A group of containers mostly Dockers
  • Replication Controller: Replica manager for pods
  • Controller Registry: Registry to host Dockers images

  • Single POD under Kubernetes

    To simplify , lets host a word press( The popular CMS ) website on google cloud.
    Lets create a hello world cluster which will host our pods.

    $ gcloud container clusters create hello-world --num-nodes 1  --machine-type g1-small
     
    gcloud container create cluster hello world

    Lets build our POD with single container and fetch the docker image from registry.
    kubectl run wordpress --image=tutum/wordpress --port=80
    kubectl run wordpress gcp
    Lets expose the POD by a service which allows external traffic.
    kubectl expose deployment wordpress --type=LoadBalancer
     
    This expose command creates:
  • A service named wordpress.
  • The forwarding rules and target pool that comprise the external load balancer.
  • A firewall rule that allows traffic to port 80 on the external IP.
  •  
    Lets try to see our word press service.
    host wordpress on google cloud
    Lets try to open our word press installation which is running in a Dockers container of 
    our POD and hosted on a single node inside the Kubernetes cluster.
    install page of container hosting wordpress
     

    Multiple PODS under Kubernetes

    Lets take another case, where we have a cluster of containers and we will push a custom docker image to our pods via kubermetes.
     
    Tip:You can also build your Dockers images from the gcloud command line and push it to the Google's container registry.
     
    Lets say you have pulled a Dockers image from Dockerhub.com and you have done some modifications to it.
    Now you can save a snapshot of the running Dockers image and save it to google container’s registry.
    docker pull --- url 
    docker commit (snapshot) or you can do a docker build also if you have a docker file.
    Now push it to gcr.io ($DEVSHELL_PROJECT_ID is the current project id)
    gcloud docker -- push gcr.io/$DEVSHELL_PROJECT_ID/clustername
    image
     
    Now lets use kubernetes  to manage our containers in the cluster.
    $gcloud container clusters get-credentials clustername( to get credentials for cluster)
    Now lets create the pods with the YAML file we have( The resource file)
    $kubectl create -f resources.yaml
    If all is fine, your resources will be created and pods will start
    $ kubectl get pods ( keep refreshing till status changes for pods)
    image
    Now check the status of the service by.
    $kubectl get services servicename   ( service name is  defined in your yaml file.)
    kubernete pods docker container 

    Troubleshooting Section:

    If status of pods is (ImagePullBackOff or ErrImagePull), reason could be either your Docker 
    image is not pushed to GCR properly or your YAML file has wrong docker image name.
    Do a grep on the YAML file for your image name and see if its there.

    In this section, we learnt how Google containers work and how we can use Kubernetes to control the PODS and how we can deploy custom images inside the POD containers.

    Keep Reading !

    No comments:

    Post a Comment