Skip to main content

Kubernetes1

 

Challenge 1

Welcome to the Kubernetes challenge, your task is to follow the below steps and complete them successfully.

Environment Setup

Check whether docker & minikube are properly installed and configured.
Start Minikube and execute this command to sync host docker with minikube docker minikube -p minikube docker-env and eval $(minikube docker-env)

Step-1
Create a pod object using kubectl run command with google's sample image: gcr.io/google-samples/kubernetes-bootcamp:v1 and expose it on port 8080, name the pod as firstapp.
Check if the pod creation is successful by running the command: kubectl get pod firstapp

Step-2
Expose the application to the local VM by creating a Service object of type NodePort. Check if the service is created by running the command: kubectl get svc firstapp

Step-3
Create another deployment using a 'YAML' file, create a deployment.yaml file that contains information of the number of replicas and the images to be used. Use an nginx image to deploy. Name the deployment as 'nginx'
Check if the deployment is created by running the command: kubectl get deployment nginx

Step-4
Create a NodePort type service using a 'YAML' file, create a service.yaml file that contains information of the type of service and the port numbers. Name the Service nginx-svc and use port 30080 for nodePort.
Check if the service is created by running the command: kubectl get svc nginx-svc

Step-5
Use kubectl exec command to get inside the running nginx pod and write the string 'Welcome to fresco nginx pod' to /usr/share/nginx/html/index.html file

Let's Solve the problem statement...
minikube start
minikube -p minikube docker-env
eval $(minikube docker-env)

Step-1:
To create a pod object using kubectl run command with google's sample image, run the following command:

kubectl run firstapp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080

To check if the pod creation is successful, run the following command:

kubectl get pod firstapp

Step-2:
To expose the application to the local VM by creating a Service object of type NodePort, run the following command:

kubectl expose pod firstapp --type=NodePort --port=8080 --name=firstapp

To check if the service is created, run the following command:

kubectl get svc firstapp

Step-3:
To create another deployment using a YAML file, create a deployment.yaml file with the following content:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

To create the deployment, run the following command:

kubectl apply -f deployment.yaml

To check if the deployment is created, run the following command:

kubectl get deployment nginx

Step-4:
To create a NodePort type service using a YAML file, create a service.yaml file with the following content:

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
	  nodePort: 30080
  type: NodePort
        

To create the service, run the following command:

kubectl apply -f service.yaml

To check if the service is created, run the following command:

kubectl get svc nginx-svc

Step-5:
To get inside the running nginx pod and write the string 'Welcome to nginx pod' to /usr/share/nginx/html/index.html file, run the following command:

    kubectl exec -it firstapp -- /bin/bash

    echo 'Welcome to nginx pod' > /usr/share/nginx/html/index.html

    exit

check the deployment by going to the required url
    minikube service nginx-svc --url  -> this would give a url, go to that to check the html


"firstapp" is the name of the nginx pod that was created earlier

IMP: If the folder not present you can create them using below commands then try the above command.

sudo mkdir -p /usr/share/nginx/html && sudo touch /usr/share/nginx/html/index.html

This command will first create the directory /usr/share/nginx/html using the mkdir command. The -p option is used to create parent directories as needed.

Then, the touch command is used to create an empty file named index.html inside the /usr/share/nginx/html directory.

The sudo command is used to run these commands with administrative privileges.

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command:

Docker + Docker Compose + Ansible

 Docker + Docker Compose + Ansible ----------------------------------------------------------------------------------------------------------------------------- Flask Application Deployment using Ansible roles and Docker-Compose 1. Run the "setup.sh" file given in the path to install the required dependencies before starting the challenge. 2. A Flask application named "app.py" is given in the path . 3. Create an Ansible role named "Installation" in the path "/etc/ansible/roles" to install docker-compose. 4. Write an Ansible playbook named "creation.yaml" in the path , to perform the following tasks: 1. Using "file" and "copy" modules, create a Multi-stage "Dockerfile" in the path to  - Dockerize the given Flask application with "python:alpine3.7" as its base image, using the given 'requirements.txt' file.  - Build an image using "postgres" as its base image. 2. Using &q