DevOps Interview Questions and Answers | Crack Your Next Interview! part 2
ฝัง
- เผยแพร่เมื่อ 6 ก.พ. 2025
- Summary of the Provided Statements:
15. Using `for_each` in Terraform:
In Terraform, the `for_each` loop allows the creation of multiple resources dynamically by iterating over a map or set. In the provided example, `var.instances` is a map, and `each.key` refers to the current key, while `each.value` refers to its associated value:
```hcl
resource "aws_instance" "example" {
for_each = var.instances
ami = "ami-0c55b159cbfafe1f0"
instance_type = each.value
tags = {
Name = each.key
}
}
```
16. Advantages and Disadvantages of Multi-Stage Builds in Docker:
Advantages:
Smaller Image Size: Excludes unnecessary components, resulting in leaner images.
Improved Security: Reduces the attack surface by omitting build tools from the final image.
Better Caching: Enhances build efficiency through Docker's caching mechanism.
Separation of Concerns: Organizes Dockerfiles by separating build and runtime environments.
Disadvantages:
Increased Complexity: Multi-stage builds can be harder to understand for newcomers.
Longer Build Times: Additional stages might prolong the build process.
Troubleshooting Challenges: Intermediate stages aren't preserved, complicating debugging.
17. Deploying Containers on Different Hosts in a Docker Cluster:
Using Docker Swarm:
Utilize Swarm's orchestration to distribute containers across nodes.
Employ placement constraints to control where services run.
Example:
```sh
docker service create --name myservice --replicas 2 --constraint 'node.role == worker' myimage
```
Using Kubernetes:
Kubernetes schedules pods based on resource availability.
Node selectors or affinities can be used for specific placements.
18. Deploying Web and DB Containers on Separate Hosts with Docker Compose:
Transform the Compose setup to work with Docker Swarm or specify placement constraints:
Compose File with Constraints:
19. Bridge Networking vs. Host Networking in Docker:
Bridge Networking:
Default network driver.
Containers have isolated networks and communicate via the bridge.
Ports must be mapped to the host manually.
Host Networking:
Containers share the host's network stack.
No isolation; the container uses the host's IP address.
Offers performance gains but reduces security due to lack of isolation.
20. Resolving Merge Conflicts in Git:
1. Identify Conflicts: Git marks them with ``, `=======`, and ``.
2. Manually Edit Files: Choose which changes to keep and remove conflict markers.
3. Stage Changes: Use `git add` to stage the resolved files.
4. Commit Changes: Finalize with `git commit`.
5. Test: Ensure functionality remains intact.
Example:
```sh
git add resolved-files
git commit -m "Resolved merge conflicts in X, Y, Z files"
```
21. Changing an Existing Commit Message in Git:
Use the `--amend` flag to modify the most recent commit message:
22. Session Affinity (Sticky Sessions):
Session affinity ensures that all requests from a user are directed to the same server in a load-balanced environment. This is crucial when session data is stored locally on a server, maintaining continuity for the user.
23. Pod Affinity in Kubernetes and Its Use Case:
Pod Affinity: Instructs Kubernetes to schedule pods on nodes where specified pods are running.
Use Case: Place interdependent services (e.g., frontend and backend) on the same node to reduce latency.
24. Pod Affinity vs. Pod Anti-Affinity:
Pod Affinity: Prefers to schedule pods together.
Use Case: Low-latency communication between pods.
Pod Anti-Affinity: Prefers to schedule pods apart.
Use Case: High availability by spreading replicas across nodes.
25. Readiness and Liveness Probes in Kubernetes:
Readiness Probes
Indicate when a pod is ready to accept traffic.
If it fails, the pod is removed from service endpoints.
Liveness Probes:
Check if a pod is running correctly.
If it fails, Kubernetes restarts the pod.
26. Jenkins Pipeline for a Java Spring Boot Application with Approval Step:
27. Exporting Test Reports in Jenkins:
1. Generate Reports: Ensure tests output standard formats (e.g., JUnit XML).
2. Configure Jenkins:
Use "Publish JUnit test result report" to archive results.
"Archive the artifacts" can store additional reports.
3. Access Reports: Available on the Jenkins build page.
28. Scaling Pods in Kubernetes Using Command Line:
To scale from 5 to 10 pods:
```sh
kubectl scale --replicas=10 deployment/deployment-name
```
29. Using `terraform import` Command:
Purpose: Import existing resources into Terraform state.
Usage:
30. Storing and Managing Terraform State Files in AWS:
State Storage in S3:
Configure backend to use an S3 bucket.
State Locking with DynamoDB:
Prevents concurrent state modifications.
Best Practices:
Enable versioning on the S3 bucket.
Use encryption and access controls.