Skip to main content

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 "file" and "copy" modules, create a "docker-compose.yaml" file in the path which creates two services named 
- The "flask" service should run an "flask_image" image as a container named "flask_container" and expose it to port 5000.
- The "db" service should run an "postgres_image" image as a container named "postgres_container" and expose it to port 5476.

5. Write another Ansible playbook named "execute.yaml" in the path, to perform the following tasks using appropriate Dockerfile
- Import the "creation.yaml" playbook. 
- Include the created "installation" role.
- Build a Docker image named "flask_image" using the created Dockerfile. 
- Build a Docker image named "postgres_image" using the created Dockerfile.
- Run the "docker-compose.yaml" file.

6. Finally, run the "execute.yaml" playbook and check whether the Flask application is running in the specified port 5000.

-----------------------------------------------------------------------------------------------------------------------------


SOLUTION

1. create the Ansible role
            mkdir etc/ansible/roles
            cd etc/ansible/roles
            ansible-galaxy role init Installation
 
2. create the creation.yaml, to create Dockerfile and docker-compose.yaml file


---
- name: Create Dockerfile and docker-compose files
  hosts: localhost
  tasks:
    - name: Create Dockerfile
      copy:
        dest: ./Dockerfile
        content: |
          FROM python:alpine3.7 AS flask_img
          WORKDIR /app
         
          COPY requirements.txt /app
          RUN pip install -r requirements.txt

          COPY app.py .

          EXPOSE 5000
          ENV FLASK_APP=app.py
          CMD ["flask", "run", "--host", "0.0.0.0"]

          # ------------------------------------
          # ------------------------------------

          FROM postgres AS postgres_img
          ENV POSTGRES_DB myDB
          ENV POSTGRES_USER myuser
          ENV POSTGRES_PASSWORD mypassword
   
    - name: Create docker-compose
      copy:
        dest: ./docker-compose.yaml
        content: |
          version: '3.4'
          services:
            flask:
              image: flask_img
              container_name: flask_cont
              ports:
                - 5000:5000
            db:
              image: postgres_img
              container_name: postgres_cont
              ports:
                - 5476:5476


3. create the execute.yaml file

---
- name: Import creation.yml playbook
  import_playbook: creation.yaml

- name: Execute all tasks
  hosts: localhost
  tasks:
    - name: Include created role
      include_role:
        name: etc/ansible/roles/Installation

    - name: Build flask_img using Dockerfile
      command: docker build --target flask_img -t flask_img .
      become: true

    - name: Build postgress_img using Dockerfile
      command: docker build --target postgres_img -t postgres_img .
      become: true

    - name: Build docker-compose file using Dockerfile
      command: docker-compose up -d



# execute the playbooks using
this is optional as this will create the Dockerfile and docker-compose file
ansible-playbook creation.yaml 

# execute this playbook mainly
ansible-playbook execute.yaml

# check the url for the flask application
curl http://localhost:5000



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:

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