Kubernetes Series (PART-3)

Now that you have learned lot of things about kubernetes and Kubernetes Components. Kubernetes is a best open source tool that is used for managing tons of container and container orchestrations. Now, you will learn how to perform various deployments and activities that are performed within kubernetes.

Lets begin !

TABLE OF CONTENT

  1. How to Launch multi containers on a single Pod in Kubernetes.
  2. Create a Nginx Deployment on Kubernetes
  3. Deploy Nginx Service with Expose command on Kubernetes
  4. Deploy Nginx Service using manifest file on Kubernetes TABLE OF CONTENT:

Prerequisites

  • Make sure you have kubernetes cluster installed with at least one worker node and kubectl command installed on the same machine.

How to Launch multi containers on a single Pod in Kubernetes

Let’s write a simple example multi-container pod. This is a simple pod that has 3 containers with a nginx image that echo’s different messages as soon as they start.

  • Create a directory under opt and name it as kubernetes.
mkdir /opt/kubernetes
cd /opt/kubernetes
  • Create a file multi_container_single_pod.yml in your ubuntu machine under /opt/kubernetes. Paste the below code in the file.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - args:
    - bin/sh
    - -c
    - echo This is my First Container; sleep 3600
    image: nginx
    name: container1
    resources: {}
  - args:
    - bin/sh
    - -c
    - echo This is my second Container; sleep 3600
    image: nginx
    name: container2
    resources: {}
  - args:
    - bin/sh
    - -c
    - echo This is third container; sleep 3600
    image: nginx
    name: container3
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
  • Now create the deployment using kubectl command.
kubectl apply -f multi_container_single_pod.yml 
  • Now, retrieve details of the deployment using kubectl command.
kubectl get all -o wide
  • Now, test all the three containers which are deployed on single Pod by running the following command.
kubectl logs nginx -c container1
kubectl logs nginx -c container2
kubectl logs nginx -c container3

Create a Nginx Deployment on Kubernetes

To deploy a containerized application on kubernetes cluster you will need to create a deployment configuration which is manifest (YAML) file.

Deployment configuration will be taken care by control panel which will deploy the applications on the instance. Once the application instances are created, a Kubernetes Deployment Controller continuously monitors those instances. You can create and manage a Deployment by using the Kubernetes command line interface, Kubectl.

In this tutorial you will learn how to create a nginx deployment in the kubernetes cluster with 4 replicas.

  • Create a directory under opt and name it as kubernetes.
mkdir /opt/kubernetes
cd /opt/kubernetes
  • Create a file nginx.yml in your ubuntu machine under /opt/kubernetes. Paste the below code in the nginx.yml file.
apiVersion: apps/v1
kind: Deployment
metadata:
  # ----------------------------------------------Define the Deployment Name
  name: nginx-deployment
  labels:
    app: webserver
spec:
  # ----------------------------------------------Define the Number of Pods
  replicas: 4
  # Define the Selector
  selector:
    matchLabels:
      app: webserver  # -------------------------Controller will select targeted Pods
  template:  # ----------------------------------Contains the actual template for Pods
    metadata:
      labels:
        app: webserver #  -----------------------Pod Label
    spec:
      containers: # -----------------------------Container Details
      - name: nginx
        image: nginx:latest # -------------------Image details
        ports:
        - containerPort: 80
  • Now create the deployment using kubectl command.
kubectl apply -f nginx.yml --record
  • Now, retrieve details of the deployment using kubectl command.
kubectl get all -o wide

You will notice that all the Four replica pods are created and verified one of the Pod on worker node which shows the nginx page on Port 80

Deploy Nginx Service Using Expose Command on Kubernetes

Service provides you a way to expose your application running on a set of Pods as a network service. Kubernetes provide unique IP address to each Pods and a single DNS name. Sometimes, Kubernetes Pods are created and destroyed to match the state of your cluster. Pods are nonpermanent resources. If you use a Deployment to run your app, it can create and destroy Pods dynamically. To solve this issue Service is created.

In this tutorial you will learn how to create a nginx service and expose it to the outside world from kubernetes cluster using expose command.

  • Create a file apache.yml in your ubuntu machine under /opt/kubernetes.
  • Copy and Paste the below code in the apache.yaml file. Here Name of the deployment is apache-deployment and you will create four replicas and image name is bitnami/apache:latest and the name of container will be nginx.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache-deployment
  labels:
    app: webserver
spec:
  replicas: 4
  selector:
    matchLabels:
      app: webserver
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers: 
      - name: apache
        image: bitnami/apache:latest
        ports:
        - containerPort: 80
  • Now create the deployment using kubectl command.
kubectl apply -f apache.yaml
  • Check the deployment status by using the below command.
kubectl get deployment apache-deployment
  • Now create the service using the below command. Here deployment name is apache-deployment and name of the service is apache-http-service and Port exposed internally in the cluster and Pod port is 8080.
kubectl expose deployment apache-deployment --name=apache-http-service --type=ClusterIP --port=8080 --target-port=8080
  • Now verify if service is created properly by running the command.
kubectl get svc
  • Now, service is successfully created and exposed on Port 8080, lets verify by running the curl command on the machine.
curl 10.108.210.196:8080

Deploy Nginx Service Using Manifest file on Kubernetes

In this tutorial you will learn how to create a Nginx service and expose it to the outside world from kubernetes cluster using manifest file.

  • Create a file nginx-deployment.yml in your ubuntu machine under /opt/kubernetes and Paste the below code.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
  • Now create the deployment using kubectl command.
kubectl apply -f nginx-deployment.yml
  • Once deployment is completed , verify if your pods are running and have their own internal IP addresses:
kubectl get pods -l 'app=nginx' -o wide | awk {'print $1" " $3 " " $6'} | column -t
  • To test the the deployment and Pods have successfully deployed. Nginx page should be displayed once you run curl command on Pod_IP:Pod /Container Port
  • To create a NodePort service, create a file called nodeport.yaml, and then set type to NodePort. For example:
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  • To create the object and apply the nodeport.yaml file, run the following declarative command:
kubectl create -f nodeport.yaml
  • Now verify if service is created properly by running the command.
kubectl get service/nginx-service-nodeport
  • Now, service is created successfully. To test the the service inside the cluster run curl command with Cluster_IP : Port IP
  • Main use of NodePort is to use the service externally. To test our NodePort service Go to Pubic or Private IP of your Node and NodePort 31856 ( NodeIP:NodePort ) 

l