{"id":28,"date":"2025-10-22T12:00:00","date_gmt":"2025-10-22T12:00:00","guid":{"rendered":"https:\/\/rezachegini.com\/?p=28"},"modified":"2026-10-30T06:37:10","modified_gmt":"2026-10-30T06:37:10","slug":"understanding-kubernetes-replicasets","status":"publish","type":"post","link":"https:\/\/rezachegini.com\/?p=28","title":{"rendered":"Understanding Kubernetes ReplicaSets"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Today I want to share what I learned about Kubernetes ReplicaSets and how they help keep applications running reliably.<\/p>\n\n<h3 class=\"wp-block-heading\">What is a ReplicaSet?<\/h3>\n\n<p class=\"wp-block-paragraph\">A ReplicaSet is a Kubernetes controller that ensures a specific number of Pod copies are always running. Think of it like a manager that watches your application and automatically creates new Pods if any crash or get deleted. While a Pod runs one instance of your app, a ReplicaSet runs multiple identical copies for high availability.<\/p>\n\n<h3 class=\"wp-block-heading\">Complete ReplicaSet Application YAML File<\/h3>\n\n<p class=\"wp-block-paragraph\">Here is the complete file that creates both a ReplicaSet and Service together:<\/p>\n\n<pre class=\"wp-block-code\"><code># ReplicaSet Definition\napiVersion: apps\/v1\nkind: ReplicaSet\nmetadata:\n  name: myapp2-rs\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      app: myapp2\n  template:\n    metadata:\n      name: myapp2-pod\n      labels:\n        app: myapp2\n    spec:\n      containers:\n      - name: myapp2-container\n        image: nginx\n        ports:\n        - containerPort: 80\n\n---\n\n# Service Definition\napiVersion: v1\nkind: Service\nmetadata:\n  name: replicaset-nodeport-service\nspec:\n  type: NodePort\n  selector:\n    app: myapp2\n  ports:\n  - name: http\n    port: 80\n    targetPort: 80\n    nodePort: 31232<\/code><\/pre>\n\n<h3 class=\"wp-block-heading\">Breaking Down the Complete File<\/h3>\n\n<p class=\"wp-block-paragraph\">The ReplicaSet Section &#8211; Detailed Explanation:<\/p>\n\n<pre class=\"wp-block-code\"><code>apiVersion: apps\/v1        # Uses apps API group for ReplicaSets\nkind: ReplicaSet          # Creates a ReplicaSet controller\nmetadata:\n  name: myapp2-rs         # Name of the ReplicaSet\nspec:\n  replicas: 3             # Always keep 3 Pods running\n  selector:               # How ReplicaSet finds its Pods\n    matchLabels:\n      app: myapp2         # Manages Pods with this label\n  template:               # Template for creating new Pods\n    metadata:\n      name: myapp2-pod    # Name pattern for Pods\n      labels:\n        app: myapp2       # Label applied to each Pod\n    spec:                 # Pod specification\n      containers:\n      - name: myapp2-container     # Container name\n        image: nginx  # Docker image\n        ports:\n        - containerPort: 80        # Port the container listens on<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">What each part does:<\/p>\n\n<ul class=\"wp-block-list\"><li>replicas: 3 &#8211; Always maintains exactly 3 running Pods<\/li><li>selector.matchLabels &#8211; Finds and manages Pods with label &#8220;app: myapp2&#8221;<\/li><li>template &#8211; Blueprint for creating new Pods when needed<\/li><li>apiVersion: apps\/v1 &#8211; Uses the stable apps API for controllers<\/li><li>image: kubenginx:2.0.0 &#8211; Updated version compared to basic Pod example<\/li><\/ul>\n\n<p class=\"wp-block-paragraph\">The Service Section &#8211; Detailed Explanation:<\/p>\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1                        # Uses core API for Services\nkind: Service                         # Creates a Service resource\nmetadata:\n  name: replicaset-nodeport-service   # Service name for reference\nspec:\n  type: NodePort                      # Makes service accessible externally\n  selector:\n    app: myapp2                       # Connects to Pods with this label\n  ports:\n  - name: http                        # Name for this port mapping\n    port: 80                          # Port inside the cluster\n    targetPort: 80                    # Port on the Pod containers\n    nodePort: 31232                   # External port on nodes<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Key differences from Pod Service:<\/p>\n\n<ul class=\"wp-block-list\"><li>selector: app: myapp2 &#8211; Connects to all Pods managed by ReplicaSet<\/li><li>nodePort: 31232 &#8211; Different external port to avoid conflicts<\/li><li>Connects to multiple Pods &#8211; Service automatically load balances between all 3 Pods<\/li><\/ul>\n\n<p class=\"wp-block-paragraph\">How They Work Together: The ReplicaSet ensures 3 Pods are always running, while the Service distributes incoming traffic among them:<\/p>\n\n<ol class=\"wp-block-list\"><li>User accesses <a href=\"http:\/\/NODE_IP:31232\">http:\/\/NODE_IP:31232<\/a><\/li><li>Service receives traffic on nodePort 31232<\/li><li>Service looks for Pods with label &#8220;app: myapp2&#8221;<\/li><li>Service finds all 3 Pods managed by ReplicaSet<\/li><li>Service forwards traffic to one of the healthy Pods<\/li><li>If a Pod crashes, ReplicaSet creates a new one automatically<\/li><\/ol>\n\n<p class=\"wp-block-paragraph\">Why ReplicaSets Matter:<\/p>\n\n<ul class=\"wp-block-list\"><li>High Availability &#8211; App keeps running even if Pods crash<\/li><li>Load Distribution &#8211; Traffic spreads across multiple Pod instances<\/li><li>Automatic Recovery &#8211; New Pods created when failures happen<\/li><li>Scalability &#8211; Easy to increase or decrease Pod count<\/li><\/ul>\n\n<h3 class=\"wp-block-heading\">How to Deploy and Manage This Application<\/h3>\n\n<p class=\"wp-block-paragraph\">Step 1: Deploy the Application<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f replicaset-complete.yaml<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 2: Check ReplicaSet Status<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get replicasets\nkubectl get rs myapp2-rs<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 3: See Detailed ReplicaSet Information<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl describe replicaset myapp2-rs<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 4: Check All Pods Created by ReplicaSet<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get pods\nkubectl get pods -l app=myapp2<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 5: Check the Service<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get services\nkubectl describe service replicaset-nodeport-service<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 6: Test High Availability &#8211; Delete a Pod<\/p>\n\n<pre class=\"wp-block-code\"><code># Delete one Pod and watch ReplicaSet create a new one\nkubectl delete pod [POD_NAME]\nkubectl get pods -l app=myapp2 --watch<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 7: Scale the ReplicaSet<\/p>\n\n<pre class=\"wp-block-code\"><code># Scale up to 5 Pods\nkubectl scale replicaset myapp2-rs --replicas=5\n\n# Scale down to 2 Pods\nkubectl scale replicaset myapp2-rs --replicas=2<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 8: Access Your Application<\/p>\n\n<pre class=\"wp-block-code\"><code># Get node IP\nkubectl get nodes -o wide\n\n# Access your app at: http:\/\/NODE_IP:31232\n# Traffic will be distributed among all running Pods<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 9: Monitor ReplicaSet Events<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl get events --field-selector involvedObject.name=myapp2-rs<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Step 10: Clean Up Resources<\/p>\n\n<pre class=\"wp-block-code\"><code>kubectl delete -f replicaset-complete.yaml\n# Or delete individually:\n# kubectl delete replicaset myapp2-rs\n# kubectl delete service replicaset-nodeport-service<\/code><\/pre>\n\n<p class=\"wp-block-paragraph\">Understanding ReplicaSets is crucial for junior DevOps engineers because:<\/p>\n\n<ul class=\"wp-block-list\"><li>They provide the foundation for application reliability<\/li><li>Most production applications need multiple instances<\/li><li>They show how Kubernetes handles failures automatically<\/li><li>They demonstrate key concepts like controllers and selectors<\/li><li>They prepare you for understanding Deployments (which use ReplicaSets)<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>Today I want to share what I learned about Kubernetes ReplicaSets and how they help keep applications running reliably. What is a&hellip;<\/p>\n","protected":false},"author":2,"featured_media":30,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_rezaops_part":"2","_rezaops_linkedin_url":"https:\/\/www.linkedin.com\/pulse\/understanding-kubernetes-replicasets-reza-chegini-q40sc\/","footnotes":""},"categories":[10,9],"tags":[8,5,3,12,6],"series":[11],"class_list":["post-28","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-kubernetes","tag-devops","tag-kubectl","tag-kubernetes","tag-replicasets","tag-yaml","series-understanding-kubernetes"],"jetpack_featured_media_url":"https:\/\/rezachegini.com\/wp-content\/uploads\/2026\/10\/understanding-kubernetes-replicasets-cover.png","_links":{"self":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=28"}],"version-history":[{"count":1,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":29,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions\/29"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/media\/30"}],"wp:attachment":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=28"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}