Managing and monitoring swap on Linux

Introduction

This tutorial covers how to manage and monitor swap on Linux, including how to determine the amount of swap space available and how much is currently in use.

Swap space plays an important role in system performance, so it’s crucial to understand how to manage it effectively.

How much swap on Linux do you need?

The recommendation is to swap the space used to double your RAM. If your system has a lot of memory, you may never need to use swap space.

RAM         Swap        Swap (with hibernation)
256MB       256MB       512MB
512MB       512MB       1GB
1GB         1GB         2GB
2GB         1GB         3GB
3GB         2GB         5GB
4GB         2GB         6GB
6GB         2GB         8GB
8GB         3GB         11GB
12GB        3GB         15GB
16GB        4GB         20GB
24GB        5GB         29GB
32GB        6GB         38GB
64GB        8GB         72GB

To determine if your system can hibernate

$ which pm-hibernate

You can test it by running this command:

$ sudo pm-hibernate

View the amount of swap space on Linux

$ swapon --show
$ free -m 
$ sar -S 1 3
$ lsblk

The output terminal is as follows

[vagrant@localhost ~]$ swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   9M   -2
[vagrant@localhost ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:            486         146          93           2         247         325
Swap:          2047           9        2038
[vagrant@localhost ~]$ sar -S 1 3
Linux 3.10.0-1127.el7.x86_64 (localhost.localdomain)    09/13/2021      _x86_64_        (1 CPU)

09:51:08 PM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
09:51:09 PM   2087884      9264      0.44       416      4.49
09:51:10 PM   2087884      9264      0.44       416      4.49
09:51:11 PM   2087884      9264      0.44       416      4.49
Average:      2087884      9264      0.44       416      4.49
[vagrant@localhost ~]$ lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0  40G  0 disk
└─sda1   8:1    0  40G  0 part /
[vagrant@localhost ~]$

Creating a swap file

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon -a
$ swapon --show

Turn to swap off.

$ sudo swapoff -v /swapfile

# The output terminal
[vagrant@localhost ~]$ sudo swapoff -v /swapfile
swapoff /swapfile
[vagrant@localhost ~]$

Turn to swap on Linux.

$ sudo swapon -v /swapfile
$ swapon --show

Conclusion

You have a Managing and monitoring swap on Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Get a Docker Containers IP Address from the Host

Introduction

Docker containers are isolated. How do you need to know each container’s IP address? Docker networking is a little complicated. Containers launched by default in the “bridge network” and allowed to communicate with another container directly. Containers added to non-default networks will be able to access each other through their alias

My example

Create a new network is devopsroles, run containers, and access the other containers using an alias as below:

docker network create devopsroles
docker run --net devopsroles  --name nginx -d nginx
docker run  --name mongodb -d mongo
docker network connect devopsroles  --alias mongohost mongodb

Get a Docker Containers IP Address from the Host

How to get IP address Docker containers from the Host OS.

docker ps
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_or_id

The output terminal is as follows

[vagrant@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS              PORTS       NAMES
fc0c94f5ef9d   mongo     "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes        27017/tcp   mongodb
a2b21fffc9d8   nginx     "/docker-entrypoint.…"   6 hours ago     Up About a minute   80/tcp      nginx
[vagrant@localhost ~]$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' fc0c94f5ef9d
172.17.0.2172.18.0.2
[vagrant@localhost ~]$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' a2b21fffc9d8
172.18.0.3
[vagrant@localhost ~]$

You can use the docker network inspect command to print all containers in the given network.

docker network inspect bridge -f '{{json .Containers}}'

Get network config from the containers

docker exec -it container_name_Or_ID ip a

Conclusion

You have to Get a Docker Containers IP Address from the Host. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Gitlab CI/CD deploy the website to VPS

Introduction

In this tutorial, How to deploy the website to VPS using Gitlab CI/CD. You need a little knowledge about VPS, SSH, Public key, private key, Nginx… Now let’s go Gitlab CI/CD and deploy the website to VPS.

Prerequisites

  • Domain: https://devopsroles.com
  • VPS on Linode: Have installed Nginx and added the domain into VPS.
  • Source code: For example, is React
  • Create SSH key on VPS
  • Add SSH key into GitLab, CI/CD of Project
  • Install Gitlab Runner
  • Add .gitlab-ci.yml to the root folder on Gitlab

On VPS

Create a public key and Private key for CI/CD

Type the command on VPS below:

ssh-keygen -t rsa -b 4096 -m pem -C "pvhuu285@gmail.com" -P "" -q -f ~/.ssh/gitlab

It will gen two files is gitlab and gitlab.pub

Convert PKCS #1 to PKCS #8 as command below:

openssl pkcs8 -in gitlab -topk8 -out gitlab2 -nocrypt
#openssl pkcs8 -in gitlab -topk8 -out gitlab2

If you use gitlab key PKCS #1 is the error “Error loading key “/root/.ssh/id_rsa”: invalid format”. I will convert the gitlab2 private key.

On terminal type command

cat gitlab.pub

Copy the code public key, and paste it into SSH of the account Gitlab

Type the command below to get the private key

cat gitlab2

This private key assign to Settings CI/CD of Repository. Variables are SSH_PRIVATE_KEY

Copy the public key into the authorized_keys file.

touch .ssh/authorized_keys
cat .ssh/gitlab.pub > .ssh/authorized_keys

Create a new .ssh/config file with the content below:

  IgnoreUnknown AddKeysToAgent,UseKeychain
  #UseKeychain yes
  AddKeysToAgent yes
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlab

Install Gitlab Runner on VPS

My example, Install Gitlab Runner on Ubuntu VPS

Download and install binary

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

Give it permission to execute

sudo chmod +x /usr/local/bin/gitlab-runner

Create a GitLab CI user

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

Install and run as a service

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

Command to register the runner

sudo gitlab-runner register --url https://gitlab.com/ --registration-token $REGISTRATION_TOKEN

REGISTRATION_TOKEN the get on repository setting, In CI/CD

Create new .gitlab-ci.yml file at the folder root on Gitlab

The content is as below:

# Using the node image to build the React app

image: node:latest
variables:
  PUBLIC_URL: /
# Cache node modules - speeds up future builds
#cache:
#  paths:
#     - node_modules
stages:
     - build
     - deploy
build:
  stage: build
  script:
     - echo "Start building App"
     - chmod +x node_modules/.bin/react-scripts
     - npm install # Install all dependencies
     - npm run build #--prod  Build for prod
     - echo "Build successfully!"
  artifacts:
    paths:
      - build 
  only:
    - master # Only run on master branch
deploy_production:
  stage: deploy
  image: ubuntu
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/gitlab
    - chmod 700 ~/.ssh/gitlab
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/gitlab
    - ssh-keyscan -H 'gitlab.com' >> ~/.ssh/known_hosts
    - apt-get install rsync -y -qq
    - apt-get install curl -y -qq

  script:
     - echo "Deploying to server"
     - ssh -i ~/.ssh/gitlab -o StrictHostKeyChecking=no huupv@SERVER_IP -p PORT
     - rsync -avz --progress -a -e "ssh -p PORT" build/ huupv@SERVER_IP:/var/www/YOUR_DOMAIN/public_html
     - echo "Deployed"
  environment:
    name: production
  only:
     - master # Only run on master branch

After clicking commit and checking the result in CI/CD

Conclusion

You have Gitlab CI/CD deploy the website to VPS. I hope will this your helpful. Thank you for reading the DevopsRoles page!

How To Deploy a React Application with Nginx on Ubuntu 21.04

#Introduction

In this tutorial, how to deploy a React Application using our own Ubuntu Server and Nginx. You can quickly deploy react Applications using the default Create React App build tool.

Prerequisites

  • VPS Ubuntu 21.04. I use Linode is Cloud Hosting High-performance SSD Linux servers.
  • Nginx: Lightweight and high-performance Web server or Reverse Proxy
  • On your local machine: you will need a development environment running node.js.
  • A registered domain name. And setup DNS records for your server.
    • An A record with your_domain pointing to your server’s public IP address.
    • An A record with www.your_domain pointing to your server’s public IP address.

Deploy a React Application with Nginx on Ubuntu 21.04

Your local machine

Step 1 — Creating a React Project

npx create-react-app react-deploy
cd react-deploy
npm start

Open a browser and navigate to http://localhost:3000.

Step 2: Build production

npm run build

Your VPS

Step 3: Uploading build files to VPS

Use scp command uploading build files to /var/www/your_domain/html directory on VPS.

 scp -r ./build/* username@server_ip:/var/www/your_domain/html

For example, Nginx configure

I use SSL for the website as below:

root@localhost:~# cat /etc/nginx/conf.d/your_domain.conf
server {
        listen 80;
        #listen [::]:80 ipv6only=on;

        if ($host = www.your_domain) {
        return 301 https://$host$request_uri;
        }

        if ($host = your_domain) {
        return 301 https://$host$request_uri;
        }

        server_name www.your_domain your_domain;
        return 444;
        #return 301 https://$server_name$request_uri;


}



server {
        listen 443 ssl http2 ; # managed by Certbot
        listen [::]:443 ssl http2;
        server_name www.your_domain your_domain;

        root /var/www/your_domain/html;
        index  index.html index.htm;
        access_log off;
        error_log /var/www/your_domain/logs/error.log;

        location / {
        try_files $uri $uri/ /index.html;
        #try_files $uri /index.html;
             #index index.html index.htm;
        }
        location ~ ^/(scripts.*js|styles|images) {
           gzip_static on;
           expires 1y;
           add_header Cache-Control public;
           add_header ETag "";

          break;
        }

        #End Crontab

        gzip on;
        gzip_comp_level    5;
        gzip_min_length    256;
        gzip_proxied       any;
        gzip_vary          on;
        gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;
        # text/html is always compressed by gzip module
        #add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";

        ###
        # Retrict bad bot and bad referer
        ###
        # Block HTTP method not include GET,HEAD,POST
        if ($request_method !~ ^(GET|HEAD|POST)$ ) {
            return 444;
        }

        # Block bad http_referer (link contain bad word)
    if ( $http_referer ~* (adult|babes|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|sex|teen|webcam|zippo|replica|onload\.pw) ) {
            return 444;
         }
    ## Block download agents ##
         if ($http_user_agent ~* (LWP::Simple|BBBike|wget) ) {
            return 444;
         }

    location = /robots.txt  {
                                allow all;
                            }
    location ~ /.well-known {
                allow all;
     }
    location = /favicon.ico { access_log off; log_not_found off; expires 30d; }
    location ~ /\.          { access_log off; log_not_found off; deny all; }
    location ~ ~$           { access_log off; log_not_found off; deny all; }
    location ~ /\.git       { access_log off; log_not_found off; deny all; }
    location = /nginx.conf  { access_log off; log_not_found off; deny all; }
    location ~* /(?:uploads|files)/.*\.php$ { access_log off; log_not_found off; deny all; }
    location ~ /(readme.html|license.txt) { deny all; }
    location ~* \.(ogg|ogv|3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|svgz|ttf|woff|otf|rss|atom|ppt|midi|bmp|rtf)$ {
           gzip_static     off;
            #add_header      Cache-Control "public, must-revalidate, proxy-revalidate";
            add_header Cache-Control "public, no-transform";
            access_log      off;
            log_not_found   off;
            expires         max;
            break;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            #add_header      Cache-Control "public, must-revalidate, proxy-revalidate";
            add_header Cache-Control "public, no-transform";
            access_log      off;
            log_not_found   off;
            expires         30d;
            break;
    }
    ssl_dhparam /etc/letsencrypt/dhparams.pem; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem; # managed by Certbot

}

Conclusion

You Deploy a React Application with Nginx on Ubuntu 21.04. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Remove orphaned packages on CentOS

Introduction

Discover how to remove orphaned packages on CentOS Linux in this detailed tutorial. Orphaned packages, which are remnants not needed for package dependencies, can clutter your system.

Learn the step-by-step process to clean up these unnecessary files effectively, enhancing your CentOS system’s performance and reliability. Orphaned packages are all packages that no longer serve the purpose of package dependencies.

List of Orphaned Packages

Use the package-cleanup command below

[root@centos7 ~]# package-cleanup --leaves
Loaded plugins: fastestmirror
cryptsetup-libs-1.7.4-3.el7_4.1.x86_64
libicu-50.2-4.el7_7.x86_64
libicu62-62.2-1.el7.remi.x86_64
libmemcached-1.0.16-5.el7.x86_64
libsysfs-2.1.0-16.el7.x86_64
libtool-ltdl-2.4.2-22.el7_3.x86_64
libunwind-1.2-2.el7.x86_64
libuv-1.40.0-1.el7.x86_64
libwebp-0.3.0-7.el7.x86_64

Remove Orphaned Packages

Now, we remove Orphaned packages using the yum remove command to remove the entire list:

yum remove `package-cleanup --leaves`

The output terminal is as below:

[root@centos7 ~]# yum remove `package-cleanup --leaves`
Loaded plugins: fastestmirror
No Match for argument: Loaded
No Match for argument: plugins:
No Match for argument: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package cryptsetup-libs.x86_64 0:1.7.4-3.el7_4.1 will be erased
---> Package libicu.x86_64 0:50.2-4.el7_7 will be erased
---> Package libicu62.x86_64 0:62.2-1.el7.remi will be erased
---> Package libmemcached.x86_64 0:1.0.16-5.el7 will be erased
---> Package libsysfs.x86_64 0:2.1.0-16.el7 will be erased
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be erased
---> Package libunwind.x86_64 2:1.2-2.el7 will be erased
---> Package libuv.x86_64 1:1.40.0-1.el7 will be erased
---> Package libwebp.x86_64 0:0.3.0-7.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================
 Package                                              Arch                                        Version                                                Repository                                      Size
==============================================================================================================================================================================================================
Removing:
 cryptsetup-libs                                      x86_64                                      1.7.4-3.el7_4.1                                        @updates                                       947 k
 libicu                                               x86_64                                      50.2-4.el7_7                                           @updates                                        24 M
 libicu62                                             x86_64                                      62.2-1.el7.remi                                        installed                                       31 M
 libmemcached                                         x86_64                                      1.0.16-5.el7                                           @base                                          677 k
 libsysfs                                             x86_64                                      2.1.0-16.el7                                           @anaconda                                      146 k
 libtool-ltdl                                         x86_64                                      2.4.2-22.el7_3                                         @base                                           66 k
 libunwind                                            x86_64                                      2:1.2-2.el7                                            @base                                          150 k
 libuv                                                x86_64                                      1:1.40.0-1.el7                                         @epel                                          378 k
 libwebp                                              x86_64                                      0.3.0-7.el7                                            @base                                          371 k

Transaction Summary
==============================================================================================================================================================================================================
Remove  9 Packages

Installed size: 57 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : libmemcached-1.0.16-5.el7.x86_64                                                                                                                                                           1/9
  Erasing    : 2:libunwind-1.2-2.el7.x86_64                                                                                                                                                               2/9
  Erasing    : 1:libuv-1.40.0-1.el7.x86_64                                                                                                                                                                3/9
  Erasing    : libicu-50.2-4.el7_7.x86_64                                                                                                                                                                 4/9
  Erasing    : libtool-ltdl-2.4.2-22.el7_3.x86_64                                                                                                                                                         5/9
  Erasing    : libicu62-62.2-1.el7.remi.x86_64                                                                                                                                                            6/9
  Erasing    : libsysfs-2.1.0-16.el7.x86_64                                                                                                                                                               7/9
  Erasing    : cryptsetup-libs-1.7.4-3.el7_4.1.x86_64                                                                                                                                                     8/9
  Erasing    : libwebp-0.3.0-7.el7.x86_64                                                                                                                                                                 9/9
  Verifying  : libwebp-0.3.0-7.el7.x86_64                                                                                                                                                                 1/9
  Verifying  : cryptsetup-libs-1.7.4-3.el7_4.1.x86_64                                                                                                                                                     2/9
  Verifying  : libsysfs-2.1.0-16.el7.x86_64                                                                                                                                                               3/9
  Verifying  : libicu62-62.2-1.el7.remi.x86_64                                                                                                                                                            4/9
  Verifying  : libtool-ltdl-2.4.2-22.el7_3.x86_64                                                                                                                                                         5/9
  Verifying  : libicu-50.2-4.el7_7.x86_64                                                                                                                                                                 6/9
  Verifying  : 1:libuv-1.40.0-1.el7.x86_64                                                                                                                                                                7/9
  Verifying  : 2:libunwind-1.2-2.el7.x86_64                                                                                                                                                               8/9
  Verifying  : libmemcached-1.0.16-5.el7.x86_64                                                                                                                                                           9/9

Removed:
  cryptsetup-libs.x86_64 0:1.7.4-3.el7_4.1        libicu.x86_64 0:50.2-4.el7_7        libicu62.x86_64 0:62.2-1.el7.remi        libmemcached.x86_64 0:1.0.16-5.el7        libsysfs.x86_64 0:2.1.0-16.el7
  libtool-ltdl.x86_64 0:2.4.2-22.el7_3            libunwind.x86_64 2:1.2-2.el7        libuv.x86_64 1:1.40.0-1.el7              libwebp.x86_64 0:0.3.0-7.el7

Complete!
[root@centos7 ~]#

Conclusion

Remove orphaned packages from CentOS systems is an essential step in maintaining the efficiency and stability of your server. Orphaned packages are remnants left behind after uninstalling software that no longer serve any functional purpose. By eliminating these unnecessary files, you can optimize system performance and prevent potential conflicts. This guide on DevopsRoles aims to assist you in cleaning up your CentOS installation effectively. We hope you find these instructions beneficial. Thank you for choosing DevopsRoles for your technological needs and for trusting us with your system optimization tasks. Your continued support inspires us to provide helpful and practical content.

How to call git bash command from powershell

Introduction

Combining PowerShell with Git Bash can enhance your productivity by allowing you to use Unix-like commands within a Windows environment. In this guide, we’ll show you how to call Git Bash commands from PowerShell, using an example script. We’ll cover everything from basic setups to advanced configurations. In this tutorial, How to call the git bash command from Powershell. For example, git bash content to split CSV files on windows :

Setting Up Your Environment

Installing Git Bash

First, ensure you have Git Bash installed on your system. Download it from the official Git website and follow the installation instructions.

Adding Git Bash to Your System PATH

To call Git Bash command from PowerShell, add Git Bash to your system PATH:

  1. Open the Start menu, search for “Environment Variables,” and select “Edit the system environment variables.”
  2. Click the “Environment Variables” button.
  3. Under “System variables,” find and select the “Path” variable, then click “Edit.”
  4. Click “New” and add the path to the Git Bash executable, typically C:\Program Files\Git\bin.

Call the git bash command from Powershell

Save the following script as split.sh in your K:/TEST directory:

#!/bin/bash
cd $1
echo split start
date
pwd
split Filetest.CSV Filetest -l 20000000 -d
ls -l
for filename in $1/*; do
    wc -l $filename
done
date
echo split end
exit

This script performs the following tasks:

  1. Changes the directory to the one specified by the first argument.
  2. Prints a start message and the current date.
  3. Displays the current directory.
  4. Splits Filetest.CSV into smaller files with 20,000,000 lines each.
  5. Lists the files in the directory.
  6. Counts the number of lines in each file in the directory.
  7. Prints the current date and an end message.
  8. Exits the script.

PowerShell Script

Create a PowerShell script to call the split.sh script:

$TOOL_PATH = "K:/TEST"
$FOLDER_PATH = "K:/TEST/INPUT"

$COMMAND = "bash.exe " + $TOOL_PATH + "/split.sh " + $FOLDER_PATH
echo $COMMAND
Invoke-Expression $COMMAND

This PowerShell script does the following:

  1. Defines the path to the directory containing the split.sh script.
  2. Defines the path to the directory to be processed by the split.sh script.
  3. Constructs the command to call the split.sh script using bash.exe.
  4. Prints the constructed command.
  5. Executes the constructed command.

Explanation

  1. $TOOL_PATH: This variable holds the path where your split.sh script is located.
  2. $FOLDER_PATH: This variable holds the path to the directory you want to process with the split.sh script.
  3. $COMMAND: This variable constructs the full command string that calls bash.exe with the script path and the folder path as arguments.
  4. echo $COMMAND: This line prints the constructed command for verification.
  5. Invoke-Expression $COMMAND: This line executes the constructed command.

Add C:\Program Files\Git\bin into the PATH environment

OK!

Troubleshooting

Common Issues and Solutions

  • Git Bash not found: Ensure Git Bash is installed and added to your system PATH.
  • Permission denied: Make sure your script has execute permissions (chmod +x split.sh).
  • Command not recognized: Verify the syntax and ensure you’re using the correct paths.
  • Incorrect output or errors: Print debugging information in your scripts to diagnose issues.

FAQs

How do I add Git Bash to my PATH variable?

Add the path to Git Bash (e.g., C:\Program Files\Git\bin) to the system PATH environment variable.

Can I pass multiple arguments from PowerShell to Git Bash?

Yes, you can pass multiple arguments by modifying the command string in the PowerShell script.

How do I capture the output of a Git Bash command in PowerShell?

Use the following approach to capture the output:

$output = bash -c "git status"
Write-Output $output

Can I automate Git Bash scripts with PowerShell?

Yes, you can automate Git Bash scripts by scheduling PowerShell scripts or using task automation features in PowerShell.

Conclusion

By following this guide, you can easily call Git Bash command from PowerShell, enabling you to leverage the strengths of both command-line interfaces. Whether you’re performing basic operations or advanced scripting, integrating Git Bash with PowerShell can significantly enhance your workflow. Thank you for reading the DevopsRoles page!

Terraform commands cheat sheet

#Introduction

In this tutorial, to learn about Terraform commands cheat sheet.

Terraform commands cheat sheet.

Initialize the terraform stack and download provider plugins as per provider and also verify the syntax

#terraform init

How to display the preview of the resources that are going to be created, updated, and destroyed.

#terraform plan

Build or change infrastructure.

 #terraform apply

Destroy terraform-managed infrastructure.

#terraform destroy

Run plan or apply or destroy only to the target resource

#terraform plan -target=resource
#terraform apply -target=resource
#terraform destroy -target=resource

Show human-readable output from Terraform state.

#terraform show

How to validate terraform syntax

 #terraform validate

Rewrites terraform code to a canonical format. This useful run command before git commit that codes very nicely formatted.

#terraform fmt

Manually mark a resource for re-creation

#terraform taint

Download and install modules for the configurations. When you update the module.

#terraform get

Create a visual dependency graph of resources.

#terraform graph

Configure remote state storage.

#terraform remote

Use this command for advanced state management.

#terraform state

Conclusion

You have learned about Terraform commands cheat sheet. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Docker compose run Grafana 8

Introduction

In this tutorial, how to run Grafana version 8 using Docker-compose. Now, we will use Docker compose run Grafana version 8.

Docker compose run Grafana

Grafana Docker container

I will create a folder for Grafana on the Host Ubuntu OS.

sudo mkdir -p /home/huupv/docker/grafana/data
sudo mkdir -p /home/huupv/docker/compose-files/Grafana
cd /home/huupv/docker/compose-files/Grafana

Create a new docker-compose.yml file with the following

version: "3.5"

services:
  grafana8:
    image: grafana/grafana:latest
    network_mode: "bridge"
    container_name: grafana8
	# user: "1000" # needs to be `id -u` // alternatively chown the grafana/data dir to 472:472
    volumes:
      - /home/huupv/docker/grafana/data:/var/lib/grafana
    ports:
      - "3000:3000"
    restart: always

The change port if you want.

The deploy Grafana

sudo docker-compose up -d

Your open browser the following URL: http://yourIP:3000

To log in for the first time, use the admin:admin username and password combination.

Login the success

The during start Grafana error [fixed it]

Code Error Grafana

huupv@ubuntu:~/docker/compose-files/grafana$ sudo docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                         PORTS                                                                                        NAMES
4e1f6f81da09   grafana/grafana:latest   "/run.sh"                27 seconds ago   Restarting (1) 3 seconds ago                                                                                                grafana8
304a31a535c5   telegraf:latest          "/entrypoint.sh tele…"   3 hours ago      Up 2 hours                     0.0.0.0:8092->8092/tcp, 8092/udp, 0.0.0.0:8094->8094/tcp, 8125/udp, 0.0.0.0:8125->8125/tcp   telegraf
5909e05dd5bd   influxdb:latest          "/entrypoint.sh infl…"   3 hours ago      Up 2 hours                     0.0.0.0:8086->8086/tcp                                                                       influxdb2
huupv@ubuntu:~/docker/compose-files/grafana$ sudo docker logs grafana8
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied

Fixed it

huupv@ubuntu:~/docker/compose-files/grafana$ id -u
1001

# user: "1000" # needs to be `id -u` // alternatively chown the grafana/data dir to 472:472


huupv@ubuntu:~/docker/compose-files/grafana$ sudo chown 472:472 /home/huupv/docker/grafana/data

Result, Docker compose run Grafana 8.

huupv@ubuntu:~/docker/compose-files/grafana$ sudo docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS                          PORTS                                                                                        NAMES
4e1f6f81da09   grafana/grafana:latest   "/run.sh"                5 minutes ago   Restarting (1) 50 seconds ago                                                                                                grafana8
304a31a535c5   telegraf:latest          "/entrypoint.sh tele…"   3 hours ago     Up 2 hours                      0.0.0.0:8092->8092/tcp, 8092/udp, 0.0.0.0:8094->8094/tcp, 8125/udp, 0.0.0.0:8125->8125/tcp   telegraf
5909e05dd5bd   influxdb:latest          "/entrypoint.sh infl…"   3 hours ago     Up 2 hours                      0.0.0.0:8086->8086/tcp                                                                       influxdb2
huupv@ubuntu:~/docker/compose-files/grafana$ sudo docker restart grafana8
grafana8

huupv@ubuntu:~/docker/compose-files/grafana$ sudo chown 472:472 /home/huupv/docker/grafana/data
huupv@ubuntu:~/docker/compose-files/grafana$ ls -ld  /home/huupv/docker/grafana/data
drwxrwxr-x 2 472 472 4096 Aug  8 21:23 /home/huupv/docker/grafana/data

huupv@ubuntu:~$ sudo docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED       STATUS       PORTS                                                                                        NAMES
c4d40e897c36   grafana/grafana:latest   "/run.sh"                2 hours ago   Up 2 hours   0.0.0.0:3000->3000/tcp                                                                       grafana8
304a31a535c5   telegraf:latest          "/entrypoint.sh tele…"   5 hours ago   Up 4 hours   0.0.0.0:8092->8092/tcp, 8092/udp, 0.0.0.0:8094->8094/tcp, 8125/udp, 0.0.0.0:8125->8125/tcp   telegraf
5909e05dd5bd   influxdb:latest          "/entrypoint.sh infl…"   5 hours ago   Up 4 hours   0.0.0.0:8086->8086/tcp                                                                       influxdb2
huupv@ubuntu:~$

Conclusion

Using Docker Compose, you’ve successfully launched Grafana 8. This tutorial aims to be a helpful resource for your monitoring setup. Thank you for choosing DevopsRoles for guidance. We appreciate your engagement and are here to support your continued learning in Docker and software deployment.

Docker container image for rocky Linux

#Introduction

How to use Rocky Linux as a Docker container image. In this tutorial, how to get it, deploy it. Now, We will use the Docker container image for rocky Linux.

Rocky Linux to replace Centos.

You will need a VM with the Docker engine installed and running.

Docker container image for rocky Linux

pull down the Rocky Linux image

Open a terminal window. The run command is below

docker pull rockylinux/rockylinux

The docker image will be saved to your local repository and used it. You can verify it

docker images

create a container from the Rocky Linux image

Create a container devopsroles and deploy it in detached mode as the command below

docker run -it --name devopsroles -d rockylinux/rockylinux

Access to the container devopsroles

docker exec -it --user root devopsroles /bin/bash

You can destroy the Rocky Linux container as command below

docker ps -a
docker stop Container_ID
docker rm Container_ID

Conclusion

You have used Rocky Linux as a Docker container image. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Terraform AWS create VPC: A Comprehensive Step-by-Step Guide

Introduction

How to create AWS VPC using Terraform. In this tutorial, I’m using Terraform AWS create VPC example. Embark on a journey to mastering cloud infrastructure with our detailed tutorial on creating a Virtual Private Cloud (VPC) in AWS using Terraform.

This guide is tailored for DevOps professionals and enthusiasts eager to leverage the scalability and efficiency of AWS. By the end of this tutorial, you will have a clear understanding of how to set up a VPC that aligns perfectly with your organizational needs, ensuring a secure and robust network environment.

Terraform aws create VPC example

The structure folder and files for AWS VPC are as follows

E:\STUDY\TERRAFORM\AWS
└───EC2
│ main.tf
│ outputs.tf
│ variables.tf

Terraform init command is used to initialize a working directory containing Terraform configuration files

terraform init 

The output as the picture below

Use the terraform plan command to create an execution plan.

terraform plan

The output as the picture below

Use terraform apply is to run it.

terraform apply

The output is the picture below

The result on AWS VPC

The content files for Terraform AWS VPC

main.tf file

# dry-run
# terraform plan
# Apply it
# terraform apply
provider "aws" {
    access_key = "${var.aws_access_key}"
	secret_key = "${var.aws_secret_key}"
	region = "${var.region}"
}
# describe the VPC resource.
resource "aws_vpc" "myVPC" {
    cidr_block = "10.1.0.0/16"
    instance_tenancy = "default"
    enable_dns_support = "true"
    enable_dns_hostnames = "false"
    tags = {
      Name = "myVPC"
    }
}

variables.tf file

variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "region" {
    default = "us-west-2"
}

outputs.tf file

output "test" {
    value ="${aws_vpc.myVPC.cidr_block}"
}

Conclusion

Congratulations on successfully creating your AWS VPC using Terraform! This guide aimed to simplify the complexities of cloud networking, providing you with a solid foundation to build upon. As you continue to explore Terraform and AWS, remember that the flexibility and power of these tools can significantly enhance your infrastructure’s reliability and performance.

Keep experimenting and refining your skills to stay ahead in the ever-evolving world of cloud computing. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Devops Tutorial

Exit mobile version