Here's a detailed response to each of your 12 questions:
1. Dockerfile to install Python and copy a.py:
dockerfileCopyEditFROM ubuntu:20.04
RUN apt-get update && \
apt-get install -y python3 python3-pip
COPY a.py /app/a.py
WORKDIR /app
CMD ["python3", "a.py"]
2. Shell script to monitor CPU & Memory and send email:
bashCopyEdit#!/bin/bash
THRESHOLD=80
EMAIL="[email protected]"
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
MEM=$(free | awk '/Mem/{printf("%.2f"), $3/$2 * 100.0}')
if (( ${CPU%.*} > THRESHOLD )) || (( ${MEM%.*} > THRESHOLD )); then
echo -e "High resource usage detected!\nCPU: $CPU%\nMemory: $MEM%"
| mail -s "Alert: High CPU/Memory Usage" "$EMAIL"
fi
You may need mailutils package for email to work.
3. How to build a Docker image:
bashCopyEditdocker build -t my-python-app .
4. Explain Jenkins Pipeline:
A Jenkins pipeline is a set of instructions written in a Jenkinsfile to
automate build, test, and deployment steps. Pipelines can be:
Declarative: Structured format, easier to understand.
Scripted: More flexible, written in Groovy.
Example (Declarative):
groovyCopyEditpipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
5. When to use Git cherry-pick:
Use git cherry-pick when you want to apply a specific commit from one
branch into another without merging the whole branch.
bashCopyEditgit cherry-pick <commit_hash>
6. Difference between append and redirect:
Redirect (>): Overwrites file content.
Append (>>): Adds to the end of the file.
Example:
bashCopyEditecho "Hello" > file.txt # Overwrites
echo "World" >> file.txt # Appends
7. Skip failed build and continue in Jenkins:
Use catchError, try-catch, or unstable status.
groovyCopyEditstage('Optional Step') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh './fail-prone-script.sh'
}
}
}
8. Git architecture and commands:
Git has a working directory, staging area, and local repository.
git add: Stage changes
git commit: Commit to local repo
git push: Push to remote
git pull: Fetch & merge
git fetch: Fetch only
git status, git log, git diff: Inspect
9. What is ReplicaSet in K8s:
A ReplicaSet ensures a specified number of pod replicas are running at any
given time.
yamlCopyEditapiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: mycontainer
image: nginx
10. What is ETCD:
Distributed key-value store used by Kubernetes to store all cluster
state (pods, nodes, secrets, etc.)
Highly consistent and reliable.
11. Services in Kubernetes:
K8s Service exposes pods as a network service. Types:
ClusterIP (default): Internal access only
NodePort: Exposes service on each Node’s IP at a static port
LoadBalancer: Exposes externally via cloud LB
Headless (ClusterIP: None): For direct pod access
12. Ingress Controller:
Ingress controller is a K8s component that manages external access to
services (HTTP/HTTPS). It uses ingress resources to define routes.
Example: Nginx Ingress Controller
Controls path-based or host-based routing.
Would you like me to compile this set into a downloadable PDF as well?