Uploaded by xahexi1701

Kubernetes Challenge - Google Docs

advertisement
‭Kubernetes Challenge‬
‭ ou are tasked with deploying two microservices, Product Service and Order Service, on a‬
Y
‭Kubernetes‬
‭cluster using a blue-green deployment strategy. Product Service communicates with Order‬
‭Service via‬
‭HTTP requests. Product Service should be exposed to external traffic, while Order Service‬
‭should only be‬
‭accessible within the cluster.‬
‭ equirements‬
R
‭• Build Docker images for both services and push them into Azure Container Registry (Azure‬
‭Resource).‬
‭/ clone git‬
/
git clone‬‭
‭
https://github.com/chauhanheena/product-order-service‬
‭/ build docker images for v1‬
/
cd ~/product-order-service‬
‭
git checkout v1‬
‭
cd product-service‬
‭
az acr build --image ajmera/product-service:v1 --registry ajmera --file‬
‭
Dockerfile .‬
‭
‭d ../order-service‬
c
az acr build --image ajmera/product-service:v1 --registry ajmera --file‬
‭
Dockerfile .‬
‭
‭/ build images for v2‬
/
git checkout v2‬
‭
az acr build --image ajmera/order-service:v1
‭
--registry ajmera
--file‬
Dockerfile .‬
‭
cd ../product-service‬
‭
az acr build --image ajmera/product-service:v1
‭
--registry ajmera‬
--file Dockerfile .‬
‭
‭• Use those Docker images to deploy the services into AKS.‬
•‭ Use the Kubernetes Services (Azure Resource) in Azure to deploy the service into Kubernetes‬
‭Cluster.‬
‭• Product Service:‬
‭o Replicas: 2‬
‭• Order Service:‬
‭o Replicas: 2‬
‭Product service – type of loadbalancer since it will be exposed.‬
‭piVersion: v1‬
a
kind: Service‬
‭
metadata:‬
‭
name: product-service-loadbalancer‬
‭
namespace: manual‬
‭
labels:‬
‭
app: product-service‬
‭
spec:‬
‭
type‬
‭
: LoadBalancer‬
‭
selector:‬
‭
app: product-service‬
‭
version: v1‬
‭
ports:‬
‭
- port: 80‬
‭
targetPort: 3000‬
‭
‭Order service - clusterIP: not exposed outside of the cluster‬
‭piVersion: v1‬
a
kind: Service‬
‭
metadata:‬
‭
creationTimestamp: null‬
‭
name: order-service-svc‬
‭
namespace: manual‬
‭
spec:‬
‭
ports:‬
‭
- port: 80‬
‭
protocol: TCP‬
‭
targetPort: 3000‬
‭
selector:‬
‭
app: order-service‬
‭
version: v1‬
‭
status:‬
‭
loadBalancer: {}‬
‭
•‭ Blue-Green Deployment:‬
‭o Implement a blue-green deployment strategy for both microservices.‬
‭o Use appropriate Kubernetes resources (e.g., Deployments, Services) to achieve this.‬
‭Product service deployment.‬
‭piVersion: apps/v1‬
a
kind: Deployment‬
‭
metadata:‬
‭
name: product-service-1-deployment‬
‭
namespace: manual‬
‭
spec:‬
‭
replicas: 2‬
‭
selector:‬
‭
matchLabels:‬
‭
app: product-service‬
‭
version: v1‬
‭
template:‬
‭
metadata:‬
‭
name: product-service‬
‭
labels:‬
‭
app: product-service‬
‭
version: v1‬
‭
spec:‬
‭
containers:‬
‭
- name: product-service‬
‭
image: ajmera.azurecr.io/ajmera/product-service:v1‬
‭
env:‬
‭
- name: ORDER_API_URL‬
‭
value:‬‭
‭
"http://order-service-svc"‬
ports:‬
‭
- containerPort: 3000‬
‭
‭Order service deployment‬
‭piVersion: apps/v1‬
a
kind: Deployment‬
‭
metadata:‬
‭
‭ame: order-service-1-deployment‬
n
namespace: manual‬
‭
spec:‬
‭
replicas: 2‬
‭
selector:‬
‭
matchLabels:‬
‭
app: order-service‬
‭
version: v1‬
‭
template:‬
‭
metadata:‬
‭
name: order-service‬
‭
labels:‬
‭
app: order-service‬
‭
version: v1‬
‭
spec:‬
‭
containers:‬
‭
- name: order-service‬
‭
image: ajmera.azurecr.io/ajmera/order-service:v1‬
‭
ports:‬
‭
- containerPort: 3000‬
‭
‭Since we have k8s manifest file we can deploy with‬
Kubectl apply -f {manifest}.yaml‬
‭
•‭ Validation:‬
‭o Provide steps or commands to validate the success of the blue-green deployment.‬
‭To do blue-green deployment:‬
‭First deploy v2(green) deployment using previous yaml and change version label from v1 to v2.‬
‭Ensure both deployment is working for both services.‬
‭Use following command to switch lb to v1 to v2.‬
‭ubectl patch service product-service-loadbalancer -p‬
k
'{"spec":{"selector":{"version": "v2"}}}'‬‭
‭
-n manual‬
‭Same for order service‬
‭ubectl patch service order-service-svc -p‬‭
k
'{"spec":{"selector":{"version":‬
"v2"}}}'‬‭
‭
-n manual‬
‭o Ensure that the services are accessible and responding as expected.‬
‭Since order service is not exposed we must test within the cluster‬
‭ubectl -n manual run busybox -i --tty --image=curlimages/curl‬
k
--restart=Never --rm -- curl http://order-service-svc/version‬
‭
‭We can see both services show version v2.0.0‬
•‭ Rollback:‬
‭o Include steps or commands for rolling back the deployment if any issues are‬
‭Encountered.‬
‭Assuming there was some error now we can roll back to previous version by‬
‭ubectl patch service product-service-loadbalancer -p‬
k
'{"spec":{"selector":{"version": "v1"}}}'‬‭
‭
-n manual‬
‭Same for order service‬
‭ubectl patch service order-service-svc -p‬‭
k
'{"spec":{"selector":{"version":‬
"v1"}}}'‬‭
‭
-n manual‬
‭Now that we concluded v2 is faulty it’s better to scale down or delete v2(green) deployment‬
‭ ubectl delete deploy product-service-2‬
K
‭Kubectl scale deploy order-service-2 –replicas=0‬
‭ zure Resource and Code for Docker Image‬
A
‭• Create the necessary Azure resources for deploying services in the Kubernetes Cluster.‬
•‭ Utilize the Azure Container Registry to push Docker images, with Azure account credentials‬
‭provided in this document.‬
‭• Utilize the Azure for Kubernetes Service to deploy the service.‬
‭• GitHub Repository: https://github.com/chauhanheena/product-order-service (Please read the‬
‭ReadMe.md file for both the service’s information)‬
‭o Use v1 and v2 branches for blue-green deployment.‬
‭ zure Credentials‬
A
‭Azure Login Link: https://portal.azure.com‬
‭ sername: demo@gereet.it‬
U
‭Password: Ajmera2024!‬
‭ ools‬
T
‭• Computer - PC / Mac‬
‭• Internet connectivity to your computer‬
‭• Azure CLI‬
‭• IDE to write the YML files of your choice like Visual Studio Code. You may choose any IDE of‬
‭your‬
‭choice.‬
Download