Monday, 7 July 2025

Python - 1

 

How to get all keys and value from dict


Dict1 = {'key1': 1, 'key2': 2, 'key3': 3}


for k, v in dict1.items():

    print(k, v)


File operations with Python


Read file


f = open("demofile.txt", "r")


print(f.read())




Loop through the file line by line:

f = open("demofile.txt", "r")

for x in f:

  print(x)


Write

‘A’ = append

‘W’ =write

f = open("demofile2.txt", "a")

f.write("Now the file has more content!")

f.close()


#open and read the file after the appending:

f = open("demofile2.txt", "r")

print(f.read())


Call Rest API and render page


import requests 

url = 'http://ES_search_demo.com/document/record/_search?pretty=true' 

data ='{ "query": { "bool": “True” }'


response = requests.post(url, data=data)

Lambda with profile


Lambda with STS


EKS - Helm package

 ✅ 1. Package Your Helm Chart 

Navigate to your chart directory and run: 

bash 

CopyEdit 

cd helm-microservices/charts/microservice 
 
# Package the chart into a .tgz file 
helm package . 
 

This will output a file like: 

microservice-0.1.0.tgz 
 

 

✅ 2. (Optional) Push to Helm Repository 

If you're using a Helm repository (e.g., Amazon S3, GitHub Pages, or ChartMuseum), you can push it: 

Option A: Upload to S3 (example) 

aws s3 cp microservice-0.1.0.tgz s3://my-helm-repo/ 

 

✅ 4. Install Helm Chart into Prod EKS 

Now you can install your packaged chart to the production cluster: 

helm install service-a ~/helm-repo/microservice-0.1.0.tgz -f values/service-a.yaml 
helm install service-b ~/helm-repo/microservice-0.1.0.tgz -f values/service-b.yaml 
helm install service-c ~/helm-repo/microservice-0.1.0.tgz -f values/service-c.yaml 

 

 

EKS Helm Chart -1

 📁 Project Layout 

helm-microservices/ 
├── charts/ 
│   └── microservice/ 
│       ├── Chart.yaml 
│       ├── templates/ 
│       │   ├── deployment.yaml 
│       │   ├── service.yaml 
│       │   └── ingress.yaml 
│       └── values.yaml 
├── values/ 
│   ├── service-a.yaml 
│   ├── service-b.yaml 
│   └── service-c.yaml 
 

 

📄 1. Chart Metadata — charts/microservice/Chart.yaml 

apiVersion: v2 
name: microservice 
description: A Helm chart to deploy microservices to EKS using ALB 
type: application 
version: 0.1.0 
appVersion: "1.0.0" 
 

 

📄 2. Helm Chart Templates — charts/microservice/templates/ 

🧩 deployment.yaml 

apiVersion: apps/v1 
kind: Deployment 
metadata: 
 name: {{ .Values.name }} 
spec: 
 replicas: {{ .Values.replicaCount }} 
 selector: 
   matchLabels: 
     app: {{ .Values.name }} 
 template: 
   metadata: 
     labels: 
       app: {{ .Values.name }} 
   spec: 
     containers: 
       - name: {{ .Values.name }} 
         image: {{ .Values.image }} 
         ports: 
           - containerPort: {{ .Values.containerPort }} 
 

 

🧩 service.yaml 

apiVersion: v1 
kind: Service 
metadata: 
 name: {{ .Values.name }} 
spec: 
 type: ClusterIP 
 selector: 
   app: {{ .Values.name }} 
 ports: 
   - port: {{ .Values.servicePort }} 
     targetPort: {{ .Values.containerPort }} 
 

 

🧩 ingress.yaml 

apiVersion: networking.k8s.io/v1 
kind: Ingress 
metadata: 
 name: {{ .Values.name }}-ingress 
 annotations: 
   alb.ingress.kubernetes.io/scheme: internet-facing 
   alb.ingress.kubernetes.io/target-type: ip 
   alb.ingress.kubernetes.io/group.name: "microservices" 
   alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]' 
spec: 
 ingressClassName: alb 
 rules: 
   - host: {{ .Values.host }} 
     http: 
       paths: 
         - path: / 
           pathType: Prefix 
           backend: 
             service: 
               name: {{ .Values.name }} 
               port: 
                 number: {{ .Values.servicePort }} 
 

 

📄 3. Default Values — charts/microservice/values.yaml 

name: microservice 
replicaCount: 2 
image: nginx 
containerPort: 80 
servicePort: 80 
host: app.example.com 
 

 

📄 4. Service-Specific Overrides — values/ 

values/service-a.yaml 

name: service-a 
image: yourrepo/service-a:latest 
replicaCount: 2 
containerPort: 5000 
servicePort: 80 
host: service-a.yourdomain.com 
 

 

values/service-b.yaml 

name: service-b 
image: yourrepo/service-b:latest 
replicaCount: 2 
containerPort: 8080 
servicePort: 80 
host: service-b.yourdomain.com 
 

 

values/service-c.yaml 

name: service-c 
image: yourrepo/service-c:latest 
replicaCount: 2 
containerPort: 3000 
servicePort: 80 
host: service-c.yourdomain.com 
 

 

🚀 5. Deploy Services with Helm 

Run these from the helm-microservices/ root folder: 

helm install service-a charts/microservice -f values/service-a.yaml 
helm install service-b charts/microservice -f values/service-b.yaml 
helm install service-c charts/microservice -f values/service-c.yaml 
 

 

✅ 6. Validate Deployment 

kubectl get pods 
kubectl get svc 
kubectl get ingress 
 

  • The ALB will be provisioned automatically via the AWS Load Balancer Controller. 

  • Each service is accessible by its defined domain (e.g., service-a.yourdomain.com) 

  • Make sure your DNS points to the ALB hostname. 

 

📌 Summary 

Resource 

Source 

Chart Name 

Chart.yaml → name: microservice 

Chart Version 

Chart.yaml → version: 0.1.0 

Deployment 

templates/deployment.yaml 

Service 

templates/service.yaml 

Ingress (ALB) 

templates/ingress.yaml 

Routing 

values/service-*.yaml (host) 

Isolation 

Each service has its own values file