Tuesday, February 2, 2021

What I learned about Kubernetes - (Replica Set)

                        ဒီ article က တော့ Kubernetes ရဲ့ Replica Set အကြောင်း ဖြစ်ပါတယ်။ Replica Set ရဲ့ အလုပ်လုပ်ပုံကတော့ kubernetes cluster တစ်ခုမှ run နေတဲ့ application တစ်ခုအတွက် သတ်မှတ်ထားတဲ့ pod အရေအတွက် အမြဲ ပြည့်မှီအောင် တာ၀န်ယူပေးတာဖြစ်ပါတယ်။ ဥပမာ Front-end က web service အတွက် nginx pod ၅ခု ရှိရမယ်လို့ define လုပ်ထားတယ် ဆိုရင် nginx pod ၅ ခုထဲ က တစ်ခု crash ဖြစ်ခဲ့ရင် တောင် replica set က ၅ခု ပြည့်အောင် နောက်ထပ် new nginx pod တစ်ခုကို ချက်ခြင်း ပြန် deploy လုပ်ပေးမှာဖြစ်ပါတယ်။ 
                        နောက်ထပ် replica set ကို အသုံးပြုရတဲ့ အကြောင်းအရာ တွေကတော့ load balancing & scaling ပိုင်း အတွက်ဖြစ်ပါတယ်။ ဆိုပါတော့ လက်ရှိ nginx web service ကို အသုံးပြုတဲ့ users တွေ များလာတဲ့အခါ replica set က nginx-pod တွေကို user demands အလိုက် scaling သတ်မှတ်နိုင်ပြီး traffice မျှစေဖို့အတွက် multiple node တွေပေါ်မှာ nginx-pod ပေါ်တွေ ကို မျှပြီး load balance ပြုလုပ်ပေးနိုင်ပါတယ်။ Replica Set ကို multi nodes တွေတင်မဟုတ်ပဲ single node တစ်ခုထဲ run နေတဲ့ environment မှာပါ အသုံးပြုနိုင်တယ်။ တစ်နည်းအားဖြင့် single node ပေါ်မှာ run နေတဲ့ pod crash ဖြစ်သွားရင်တောင် လက်ရှိ node ပေါ်မှာပဲ define လုပ်ထားတဲ့ အရေအတွက် ကို ပြန် deploy လုပ်ပေးပါတယ်။ 


So, How does replica Set know which pods to monitor to re-deploy if crashes?


                    Kubernetes cluster environment တစ်ခုမှာက တစ်ခုထက်ပိုတဲ့ multiple application pod တွေ run ထားကြတာဖြစ်ပြီး ဒီများပြားတဲ့ Pod တွေကြား Replica Set က ဘယ်လို manage လုပ်သလဲ ဆိုတော့  "Labels and Selectors" ကို အသုံးပြုပြီး pod monitoring နဲ့ crash ဖြစ်ခဲ့ရင် ပြန်ပြီး re-deploy လုပ်ပါတယ်။ တစ်နည်းအားဖြင့် tagging လုပ်ထားတဲ့ ပုံစံ လိုမျိုး ပေါ့ Replica Set က Pod definition file (YAML) မှာ ပါတဲ metadata session မှာ သတ်မှတ်ထားတဲ့ "Label" ကို ယူပြီး သူ့ရဲ့ Replica Set definition file (YAML) မှာ "selector" အနေနဲ့ define လုပ်ပြီး သတ်မှတ်တာဖြစ်ပါတယ်။ ဒါကြောင့် Pod definition file မှာ Label သတ်မှတ်တာ အရေးကြီးပါတယ်။

Let's review POD definition file and Replica Set definition file


                    အရင်ဆုံး ဘာမှ modify မလုပ်သေးတဲ့ Replica Set definition file ကိုကြည့်မယ်ဆိုရင် 
1. apiVersion: 
2. kind:
3. metadata:
4. spec:   

တို့ကတော့ default အနေနဲ့ ပါ၀င်ပြီး apiVersion မှာ "apps/v1", kind မှာ "ReplicaSet" ဆိုပြီး သတ်မှတ်ရမှာဖြစ်ပါတယ်။  နောက်ထပ် ထပ်ပြီးတိုးလာတာက spec အောက်မှာ "replicas", "selector" and "template" session တွေဖြစ်ပါတယ်။ အဓိက "selector" သည် ReplicaSet ရဲ့ main function ဖြစ်ပါတယ်။ ReplicaSet ဆိုတာနဲ့ "selector" ပါ၀င်ရမှာဖြစ်ပါတယ်။






        အခုအောက်ပုံမှာတော့ Pod definition file နဲ့ ReplicaSet definition file ကို compare လုပ်ပြထားတာဖြစ်ပါတယ်။ အဓိက ကတော့ Spec: အောက်မှာ replicas: 5 က Pod replication အတွက် ၅ ခုသတ်မှတ်တယ် နောက်ပြီး selector ရဲ့ matchLabels မှာ Pod definition file ရဲ့ metadata အောက်က label မှာသတ်မှတ်ခဲ့တဲ့အတိုင်း ပြန်သတ်မှတ်ပေးရမယ် ဒါမှ လက်ရှိ run နေတဲ့ Pod ကို replicaset က သိနိုင်မှာဖြစ်ပါတယ်။ နောက်ဆုံးတစ်ခုအနေနဲ့က  Pod definition file ရဲ့ metadata ကနေ စပြီး အောက်ဆုံးထိ ကို copy ယူပြီး replicaset definition file ရဲ့ template session အောက်မှာ သွားထားပေးလိုက်တာဖြစ်ပါတယ်။ 




                  
                အခုဆိုရင် ReplicaSet definition file တစ်ခုတည်ဆောက်ပြီးသွားပြီးဖြစ်လို့ အခုသွားမည့်ပုံစံက အရင်ဆုံး nginx-pod တစ်ခု ကို အရင် create လုပ်မယ် ပြီးမှ အဲ့ဒီ Pod ကို ReplicaSet နဲ့ တွဲပြီး သုံးပြမှာဖြစ်ပါတယ််။ ReplicaSet definition file တစ်ခုကိုပဲ​တိုက်ရိုက် တစ်ခါထဲ​အသုံးပြုလဲ ရပါတယ်။ ဒါဆိုရင် ReplicaSet မှာ သတ်မှတ်ထားတဲ့ replication အရေအတွက် နဲ့ သတ်မှတ်ထားတဲ့ container image ကို download ဆွဲပြီး တည်ဆောက်ပေးသွားမှာဖြစ်ပါတယ်။ အခု scanerio မှာကျတော့ existing application (pod) တွေ က standalone run နေပြီး အဲ့ဒါကို ReplicaSet နဲ့ pod တွေကို replication ဖြစ်အောင် ပြုလုပ်ချင်တာဖြစ်ပါတယ်။


                အောက်ပုံအရဆိုရင် လက်ရှိ ကျွန်တော်တို့ environement မှာ single nginx-pod တစ်ခုပဲ run နေတာတွေ့ရပါတယ်။ ဒါကို ကျွန်တော်တို့ က ReplicaSet ကို အသုံးပြုပြီးတော့ ပြောင်းလဲမှာဖြစ်ပါတယ်။






                အိုကေ အခုဆို ကျွန်တော်တို့ ReplicaSet definition file ဖြစ်တဲ့ "nginx-rs.yaml" ကို $kubectl create command ဖြင့် တည်ဆောက်လိုက်တယ်။ ပြီးတော့ $kubectl get rs (replicaset-shortcut) command ဖြင့် ကြည့်လိုက်တဲ့အခါ Desired and Current မှာ ၅ ဆိုပြီး replicaset ၅ ခု ဖြစ်နေပြီ ဖြစ်ပြီးတော့ $kubectl get pods ဖြင့် ကြည့်တဲ့ အခါမှာတော့ စောန က ၁ခု ထဲ run နေတဲ့ nginx-pod သည် အခု ၅ ခု running ဖြစ်နေတာကို တွေ့ရမှာဖြစ်ပါတယ်။





What will happen if you delete a replica set pod? 


             အခု run နေတဲ့ Pod တစ်ခု ကို ဖျက်လိုက်ရင် ReplicaSet ကို အသုံးပြုထားတဲ့အတွက် သတ်မှတ်ထားတဲ့ ၅ခု ပြည့်အောင် ReplicaSet က ပြန်ပြီး new pod တစ်ခု ပြန် deploy လုပ်ပေးမှာဖြစ်ပါတယ်။




Can you manually run a new pod with the same label as ReplicaSet defined?


                ပြောချင်တာက အခု nginx-pod ကို ReplicaSet ဖြင့် သတ်မှတ်ထားပြီး ဖြစ်တဲ့အတွက် နောက်ထပ် label တူတဲ့ Pod တစ်ခုကို manually သွား run လို့မရပါဘူး ဘာလို့လဲ ဆိုတော့ ReplicaSet မှာ သတ်မှတ်ထားတဲ့အတိုင်း replicas: 5 ဖြစ်တဲ့အတွက် အပိုထပ်ပြီး manully pod တစ်ခုကို run လို့ရမှာမဟုတ်ပါဘူး။

So, How to scale the pod?


                ReplicaSet ရဲ့ function တစ်ခုဖြစ်တဲ့ Scaling up and down Pod လိုတိုးပိုလျော့ လုပ်ချင်တဲ့အခါ 
$kubectl scale command ဖြင့် သုံးလို့ရသလို ReplicaSet definition file ကို edit ၀င်ပြင်ပြီး $kubectl apply command ဖြင့်လဲ scaling လုပ်နိုင်ပါတယ်။ တစ်ခု သိထားရမှာက $kubectl scale command ကို အသုံးပြုပြီး scaling လုပ်တဲ့အခါ ReplicaSet ရဲ့ definition file (YAML) မှာတော့ update သွားဖြစ်မှာမဟုတ်ပဲ $kubecl scale command က လက်ရှိ အသုံးပြုနေတဲ့ အပေါ်မှာပဲသက်ရောက်မှာဖြစ်ပါတယ်။

Using "$kubectl scale" command to scale up the pod



Using "#kubectl scale" command to scale down the Pod



Using "$kubectl apply" command to scale up the pod





That's it 😊


Pls Like and Subscribe Our Root Of Info FB Page and Youtube Channel


https://www.facebook.com/rootofinfo


https://www.youtube.com/channel/UCkOi7WxhUBKONv3uD0CvuWw?view_as=subscriber


Thank you!!!






Share:

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.