diff --git a/README.md b/README.md index 2679aa0..af63d35 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ :information_source:  This repo contains questions and exercises on various technical topics, sometimes related to DevOps and SRE :) -:bar_chart:  There are currently **1962** questions +:bar_chart:  There are currently **1999** questions :books:  To learn more about DevOps and SRE, check the resources in [devops-resources](https://github.com/bregman-arie/devops-resources) repository @@ -32,7 +32,7 @@ programming
Programming
Python
Python
go
Go
- Bash
Shell Scripting
+ Bash
Shell Scripting
kubernetes
Kubernetes
Prometheus
Prometheus
@@ -5313,270 +5313,6 @@ If more pods are running than needed -> it deletes some of them
If not enough pods are running -> it creates more -## Shell Scripting - -### Shell Scripting Exercises - -|Name|Topic|Objective & Instructions|Solution|Comments| -|--------|--------|------|----|----| -|Hello World|Variables|[Exercise](exercises/shell/hello_world.md)|[Solution](exercises/shell/solutions/hello_world.md) | Basic -|Basic date|Variables|[Exercise](exercises/shell/basic_date.md)|[Solution](exercises/shell/solutions/basic_date.md) | Basic -|Great Day|Variables|[Exercise](exercises/shell/great_day.md)|[Solution](exercises/shell/solutions/great_day.md) | Basic -|Factors|Arithmetic|[Exercise](exercises/shell/factors.md)|[Solution](exercises/shell/solutions/factors.md) | Basic -|Argument Check|Conditionals|[Exercise](exercises/shell/argument_check.md)|[Solution](exercises/shell/solutions/argument_check.md) | Basic -|Files Size|For Loops|[Exercise](exercises/shell/files_size.md)|[Solution](exercises/shell/solutions/files_size.md) | Basic -|Count Chars|Input + While Loops|[Exercise](exercises/shell/count_chars.md)|[Solution](exercises/shell/solutions/count_chars.md) | Basic -|Sum|Functions|[Exercise](exercises/shell/sum.md)|[Solution](exercises/shell/solutions/sum.md) | Basic -|Number of Arguments|Case Statement|[Exercise](exercises/shell/num_of_args.md)|[Solution](exercises/shell/solutions/num_of_args.md) | Basic -|Empty Files|Misc|[Exercise](exercises/shell/empty_files.md)|[Solution](exercises/shell/solutions/empty_files.md) | Basic -|Directories Comparison|Misc|[Exercise](exercises/shell/directories_comparison.md)| :( | Basic -|It's alive!|Misc|[Exercise](exercises/shell/host_status.md)|[Solution](exercises/shell/solutions/host_status.md) | Intermediate - -## Shell Scripting - Self Assessment - -
-What does this line in shell scripts means?: #!/bin/bash
- - -`#!/bin/bash` is She-bang - -/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax. - -
- -
-True or False? When a certain command/line fails in a shell script, the shell script, by default, will exit and stop running
- -Depends on the language and settings used. -If the script is a bash script then this statement is true. When a script written in Bash fails to run a certain command it will keep running and will execute all other commands mentioned after the command which failed. - -Most of the time we might actually want the opposite to happen. In order to make Bash exist when a specific command fails, use 'set -e' in your script. -
- -
-What do you tend to include in every script you write?
- -Few example: - - * Comments on how to run it and/or what it does - * If a shell script, adding "set -e" since I want the script to exit if a certain command failed - -You can have an entirely different answer. It's based only on your experience and preferences. -
- -
-Today we have tools and technologies like Ansible, Puppet, Chef, ... Why would someone still use shell scripting?
- - * Speed - * Flexibility - * The module we need doesn't exist (perhaps a weak point because most CM technologies allow to use what is known as "shell" module) - * We are delivering the scripts to customers who don't have access to the public network and don't necessarily have Ansible installed on their systems. -
- -#### Shell Scripting - Variables - -
-How to define a variable with the value "Hello World"?
- -`HW="Hello World` -
- -
-How to define a variable with the value of the current date?
- -`DATE=$(date)` -
- -
-How to print the first argument passed to a script?
- -`echo $1` -
- -
-Write a script to print "yay" unless an argument was passed and then print that argument
- -``` -echo "${1:-yay}" -``` -
- -
-What would be the output of the following script? - -``` -#!/usr/bin/env bash -NINJA_TURTLE=Donatello -function the_best_ninja_turtle { - local NINJA_TURTLE=Michelangelo - echo $NINJA_TURTLE -} -NINJA_TURTLE=Raphael -the_best_ninja_turtle -``` -
-Michelangelo -
- -
-Explain what would be the result of each command: - - * echo $0 - * echo $? - * echo $$ - * echo $#
-
- -
-What is $@?
-
- -
-What is difference between $@ and $*?
- -`$@` is an array of all the arguments passed to the script -`$*` is a single string of all the arguments passed to the script -
- -
-How do you get input from the user in shell scripts?
- -Using the keyword read so for example read x will wait for user input and will store it in the variable x. -
- -
-How to compare variables length?
- -``` -if [ ${#1} -ne ${#2} ]; then - ... -``` -
- -#### Shell Scripting - Conditionals - -
-Explain conditionals and demonstrate how to use them
-
- -
-In shell scripting, how to negate a conditional?
-
- -
-In shell scripting, how to check if a given argument is a number?
- -``` -regex='^[0-9]+$' -if [[ ${var//*.} =~ $regex ]]; then -... -``` -
- -#### Shell Scripting - Arithmetic Operations - -
-How to perform arithmetic operations on numbers?
- -One way: `$(( 1 + 2 ))` -Another way: `expr 1 + 2` -
- -
-How to perform arithmetic operations on numbers?
-
- -
-How to check if a given number has 4 as a factor?
- -`if [ $(($1 % 4)) -eq 0 ]; then` -
- -#### Shell Scripting - Loops - -
-What is a loop? What types of loops are you familiar with?
-
- -
-Demonstrate how to use loops
-
- -#### Shell Scripting - Troubleshooting - -
-How do you debug shell scripts?
- -Answer depends on the language you are using for writing your scripts. If Bash is used for example then: - - * Adding -x to the script I'm running in Bash - * Old good way of adding echo statements - -If Python, then using pdb is very useful. -
- -
-Running the following bash script, we don't get 2 as a result, why? - -``` -x = 2 -echo $x -``` -
- -Should be `x=2` -
- -#### Shell Scripting - Substring - -
-How to extract everything after the last dot in a string?
- -`${var//*.}` -
- -
-How to extract everything before the last dot in a string?
- -${var%.*} -
- -#### Shell Scripting - Misc - -
-Generate 8 digit random number
- -shuf -i 9999999-99999999 -n 1 -
- -
-Can you give an example to some Bash best practices?
-
- -
-What is the ternary operator? How do you use it in bash?
- -A short way of using if/else. An example: - -[[ $a = 1 ]] && b="yes, equal" || b="nope" -
- -
-What does the following code do and when would you use it? - -diff <(ls /tmp) <(ls /var/tmp) - -
-It is called 'process substitution'. It provides a way to pass the output of a command to another command when using a pipe | is not possible. It can be used when a command does not support STDIN or you need the output of multiple commands. -https://superuser.com/a/1060002/167769 -
- -
-What are you using for testing shell scripts?
- -bats -
- ## SQL ### SQL Exercises diff --git a/exercises/aws/README.md b/exercises/aws/README.md index 8cb646b..70836a9 100644 --- a/exercises/aws/README.md +++ b/exercises/aws/README.md @@ -19,6 +19,7 @@ | Launch EC2 web instance | EC2 | [Exercise](launch_ec2_web_instance.md) | [Solution](solutions/launch_ec2_web_instance.md) | Easy | | Security Groups | EC2 | [Exercise](security_groups.md) | [Solution](solutions/security_groups.md) | Easy | | IAM Roles | EC2 + IAM | [Exercise](ec2_iam_roles.md) | [Solution](solutions/ec2_iam_roles.md) | Easy | +| Spot Instances | EC2 | [Exercise](create_spot_instances.md) | [Solution](solutions/create_spot_instances.md) | Easy | #### AWS - Lambda @@ -352,6 +353,12 @@ On Demand is good for short-term non-interrupted workloads (but it also has the Reserved instances: they are cheaper than on-demand and the instance is yours for the chosen period of time. +
+Which pricing model has potentially the biggest discount and what its advantage
+ +Spot instances provide the biggest discount but has the disadvantage of risking losing them due bigger bid price. +
+
You need an instance for two years, but only between 10:00-15:00 every day. Which pricing model would you use?
@@ -434,7 +441,7 @@ EBS
-What EC2 RI types are there?
+What EC2 reserved instance types are there?
Standard RI - most significant discount + suited for steady-state usage Convertible RI - discount + change attribute of RI + suited for steady-state usage @@ -443,6 +450,18 @@ Scheduled RI - launch within time windows you reserve Learn more about EC2 RI [here](https://aws.amazon.com/ec2/pricing/reserved-instances)
+
+For how long can reserved instances be reserved?
+ +1 or 3 years. +
+ +
+What allows you to control inbound and outbound instance traffic?
+ +Security Groups +
+
What bootstrapping means and how to use it in AWS EC2?
@@ -482,6 +501,14 @@ To terminate such instances, you must cancel the Spot instance request first. Set of Spot instance and if you want, also on-demand instances.
+
+What strategies are there to allocate Spot instances?
+ +* lowestPrice: launch instances from the pool that has the lowest price +* diversified: distributed across all pools +* capacityOptimized: optimized based on the number of instances +
+ #### AWS - Lambda
diff --git a/exercises/aws/create_spot_instances.md b/exercises/aws/create_spot_instances.md new file mode 100644 index 0000000..7a178e7 --- /dev/null +++ b/exercises/aws/create_spot_instances.md @@ -0,0 +1,9 @@ +## AWS EC2 - Spot Instances +### Objectives + +A. Create two Spot instances using a Spot Request with the following properties: + +* Amazon Linux 2 AMI +* 2 instances as target capacity (at any given point of time) while each one has 2 vCPUs and 3 GiB RAM + +B. Create a single Spot instance using Amazon Linux 2 and t2.micro diff --git a/exercises/aws/solutions/create_spot_instances.md b/exercises/aws/solutions/create_spot_instances.md new file mode 100644 index 0000000..0459b5c --- /dev/null +++ b/exercises/aws/solutions/create_spot_instances.md @@ -0,0 +1,35 @@ +## AWS EC2 - Spot Instances +### Objectives + +A. Create two Spot instances using a Spot Request with the following properties: + +* Amazon Linux 2 AMI +* 2 instances as target capacity (at any given point of time) while each one has 2 vCPUs and 3 GiB RAM + +B. Create a single Spot instance using Amazon Linux 2 and t2.micro + +### Solution + +A. Create Spot Fleets: + +1. Go to EC2 service +2. Click on "Spot Requests" +3. Click on "Request Spot Instances" button +4. Set the following values for parameters: + * Amazon Linux 2 AMI + * Total target capacity -> 2 + * Check "Maintain target capacity" + * vCPUs: 2 + * Memory: 3 GiB RAM +5. Click on Launch + +B. Create a single Spot instance: + +1. Go to EC2 service +2. Click on "Instances" +3. Click on "Launch Instances" +4. Choose "Amazon Linux 2 AMI" and click on "Next" +5. Choose t2.micro and click on "Next: Configure Instance Details" +6. Select "Request Spot instances" +7. Set Maximum price above current price +8. Click on "Review and Launch" diff --git a/exercises/shell/README.md b/exercises/shell/README.md new file mode 100644 index 0000000..be96da8 --- /dev/null +++ b/exercises/shell/README.md @@ -0,0 +1,264 @@ +## Shell Scripting + +### Shell Scripting Exercises + +|Name|Topic|Objective & Instructions|Solution|Comments| +|--------|--------|------|----|----| +|Hello World|Variables|[Exercise](hello_world.md)|[Solution](solutions/hello_world.md) | Basic +|Basic date|Variables|[Exercise](basic_date.md)|[Solution](solutions/basic_date.md) | Basic +|Great Day|Variables|[Exercise](great_day.md)|[Solution](solutions/great_day.md) | Basic +|Factors|Arithmetic|[Exercise](factors.md)|[Solution](solutions/factors.md) | Basic +|Argument Check|Conditionals|[Exercise](argument_check.md)|[Solution](solutions/argument_check.md) | Basic +|Files Size|For Loops|[Exercise](files_size.md)|[Solution](solutions/files_size.md) | Basic +|Count Chars|Input + While Loops|[Exercise](count_chars.md)|[Solution](solutions/count_chars.md) | Basic +|Sum|Functions|[Exercise](sum.md)|[Solution](solutions/sum.md) | Basic +|Number of Arguments|Case Statement|[Exercise](num_of_args.md)|[Solution](solutions/num_of_args.md) | Basic +|Empty Files|Misc|[Exercise](empty_files.md)|[Solution](solutions/empty_files.md) | Basic +|Directories Comparison|Misc|[Exercise](directories_comparison.md)| :( | Basic +|It's alive!|Misc|[Exercise](host_status.md)|[Solution](solutions/host_status.md) | Intermediate + +## Shell Scripting - Self Assessment + +
+What does this line in shell scripts means?: #!/bin/bash
+ + +`#!/bin/bash` is She-bang + +/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax. + +
+ +
+True or False? When a certain command/line fails in a shell script, the shell script, by default, will exit and stop running
+ +Depends on the language and settings used. +If the script is a bash script then this statement is true. When a script written in Bash fails to run a certain command it will keep running and will execute all other commands mentioned after the command which failed. + +Most of the time we might actually want the opposite to happen. In order to make Bash exist when a specific command fails, use 'set -e' in your script. +
+ +
+What do you tend to include in every script you write?
+ +Few example: + + * Comments on how to run it and/or what it does + * If a shell script, adding "set -e" since I want the script to exit if a certain command failed + +You can have an entirely different answer. It's based only on your experience and preferences. +
+ +
+Today we have tools and technologies like Ansible, Puppet, Chef, ... Why would someone still use shell scripting?
+ + * Speed + * Flexibility + * The module we need doesn't exist (perhaps a weak point because most CM technologies allow to use what is known as "shell" module) + * We are delivering the scripts to customers who don't have access to the public network and don't necessarily have Ansible installed on their systems. +
+ +#### Shell Scripting - Variables + +
+How to define a variable with the value "Hello World"?
+ +`HW="Hello World` +
+ +
+How to define a variable with the value of the current date?
+ +`DATE=$(date)` +
+ +
+How to print the first argument passed to a script?
+ +`echo $1` +
+ +
+Write a script to print "yay" unless an argument was passed and then print that argument
+ +``` +echo "${1:-yay}" +``` +
+ +
+What would be the output of the following script? + +``` +#!/usr/bin/env bash +NINJA_TURTLE=Donatello +function the_best_ninja_turtle { + local NINJA_TURTLE=Michelangelo + echo $NINJA_TURTLE +} +NINJA_TURTLE=Raphael +the_best_ninja_turtle +``` +
+Michelangelo +
+ +
+Explain what would be the result of each command: + + * echo $0 + * echo $? + * echo $$ + * echo $#
+
+ +
+What is $@?
+
+ +
+What is difference between $@ and $*?
+ +`$@` is an array of all the arguments passed to the script +`$*` is a single string of all the arguments passed to the script +
+ +
+How do you get input from the user in shell scripts?
+ +Using the keyword read so for example read x will wait for user input and will store it in the variable x. +
+ +
+How to compare variables length?
+ +``` +if [ ${#1} -ne ${#2} ]; then + ... +``` +
+ +#### Shell Scripting - Conditionals + +
+Explain conditionals and demonstrate how to use them
+
+ +
+In shell scripting, how to negate a conditional?
+
+ +
+In shell scripting, how to check if a given argument is a number?
+ +``` +regex='^[0-9]+$' +if [[ ${var//*.} =~ $regex ]]; then +... +``` +
+ +#### Shell Scripting - Arithmetic Operations + +
+How to perform arithmetic operations on numbers?
+ +One way: `$(( 1 + 2 ))` +Another way: `expr 1 + 2` +
+ +
+How to perform arithmetic operations on numbers?
+
+ +
+How to check if a given number has 4 as a factor?
+ +`if [ $(($1 % 4)) -eq 0 ]; then` +
+ +#### Shell Scripting - Loops + +
+What is a loop? What types of loops are you familiar with?
+
+ +
+Demonstrate how to use loops
+
+ +#### Shell Scripting - Troubleshooting + +
+How do you debug shell scripts?
+ +Answer depends on the language you are using for writing your scripts. If Bash is used for example then: + + * Adding -x to the script I'm running in Bash + * Old good way of adding echo statements + +If Python, then using pdb is very useful. +
+ +
+Running the following bash script, we don't get 2 as a result, why? + +``` +x = 2 +echo $x +``` +
+ +Should be `x=2` +
+ +#### Shell Scripting - Substring + +
+How to extract everything after the last dot in a string?
+ +`${var//*.}` +
+ +
+How to extract everything before the last dot in a string?
+ +${var%.*} +
+ +#### Shell Scripting - Misc + +
+Generate 8 digit random number
+ +shuf -i 9999999-99999999 -n 1 +
+ +
+Can you give an example to some Bash best practices?
+
+ +
+What is the ternary operator? How do you use it in bash?
+ +A short way of using if/else. An example: + +[[ $a = 1 ]] && b="yes, equal" || b="nope" +
+ +
+What does the following code do and when would you use it? + +diff <(ls /tmp) <(ls /var/tmp) + +
+It is called 'process substitution'. It provides a way to pass the output of a command to another command when using a pipe | is not possible. It can be used when a command does not support STDIN or you need the output of multiple commands. +https://superuser.com/a/1060002/167769 +
+ +
+What are you using for testing shell scripts?
+ +bats +
+