{"id":1576,"date":"2020-06-09T15:48:25","date_gmt":"2020-06-09T07:48:25","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1576"},"modified":"2020-06-09T15:48:25","modified_gmt":"2020-06-09T07:48:25","slug":"kubernetes-replica-sets","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1576","title":{"rendered":"Kubernetes Replica Sets"},"content":{"rendered":"<header class=\\\"entry-header\\\">\n<h1 class=\\\"entry-title\\\">Kubernetes Replica Sets<\/h1>\n<\/header>\n<div class=\\\"entry-content\\\">\n<div id=\\\"toc_container\\\" class=\\\"no_bullets\\\">\n<ul class=\\\"toc_list\\\">\n<li><span class=\\\"toc_number toc_depth_1\\\">1<\/span>\u00a0\u5982\u4f55\u4f7f\u7528ReplicaSet<\/li>\n<li><span class=\\\"toc_number toc_depth_1\\\">2<\/span>\u00a0\u4f55\u65f6\u4f7f\u7528ReplicaSet<\/li>\n<li><span class=\\\"toc_number toc_depth_1\\\">3<\/span>\u00a0\u793a\u4f8b<\/li>\n<li><span class=\\\"toc_number toc_depth_1\\\">4<\/span>\u00a0ReplicaSet as an Horizontal Pod Autoscaler target<\/li>\n<\/ul>\n<\/div>\n<p>ReplicaSet\uff08RS\uff09\u662fReplication Controller\uff08RC\uff09\u7684\u5347\u7ea7\u7248\u672c\u3002ReplicaSet \u548c \u00a0Replication Controller\u4e4b\u95f4\u7684\u552f\u4e00\u533a\u522b\u662f\u5bf9\u9009\u62e9\u5668\u7684\u652f\u6301\u3002ReplicaSet\u652f\u6301labels user guide\u4e2d\u63cf\u8ff0\u7684set-based\u9009\u62e9\u5668\u8981\u6c42\uff0c\u00a0\u800cReplication Controller\u4ec5\u652f\u6301equality-based\u7684\u9009\u62e9\u5668\u8981\u6c42\u3002<\/p>\n<h2 id=\\\"how-to-use-a-replicaset\\\"><span id=\\\"ReplicaSet\\\">\u5982\u4f55\u4f7f\u7528ReplicaSet<\/span><\/h2>\n<p>\u5927\u591a\u6570kubectl\u00a0\u652f\u6301Replication Controller \u547d\u4ee4\u7684\u4e5f\u652f\u6301ReplicaSets\u3002rolling-update\u547d\u4ee4\u9664\u5916\uff0c\u5982\u679c\u8981\u4f7f\u7528rolling-update\uff0c\u8bf7\u4f7f\u7528Deployments\u6765\u5b9e\u73b0\u3002<br \/>\n\u867d\u7136ReplicaSets\u53ef\u4ee5\u72ec\u7acb\u4f7f\u7528\uff0c\u4f46\u5b83\u4e3b\u8981\u88ab\u00a0Deployments\u7528\u4f5cpod \u673a\u5236\u7684\u521b\u5efa\u3001\u5220\u9664\u548c\u66f4\u65b0\u3002\u5f53\u4f7f\u7528Deployment\u65f6\uff0c\u4f60\u4e0d\u5fc5\u62c5\u5fc3\u521b\u5efapod\u7684ReplicaSets\uff0c\u56e0\u4e3a\u53ef\u4ee5\u901a\u8fc7Deployment\u5b9e\u73b0\u7ba1\u7406ReplicaSets\u3002<\/p>\n<h2 id=\\\"when-to-use-a-replicaset\\\"><span id=\\\"ReplicaSet-2\\\">\u4f55\u65f6\u4f7f\u7528ReplicaSet<\/span><\/h2>\n<p>ReplicaSet\u80fd\u786e\u4fdd\u8fd0\u884c\u6307\u5b9a\u6570\u91cf\u7684pod\u3002\u7136\u800c\uff0cDeployment\u00a0\u662f\u4e00\u4e2a\u66f4\u9ad8\u5c42\u6b21\u7684\u6982\u5ff5\uff0c\u5b83\u80fd\u7ba1\u7406ReplicaSets\uff0c\u5e76\u63d0\u4f9b\u5bf9pod\u7684\u66f4\u65b0\u7b49\u529f\u80fd\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5efa\u8bae\u4f60\u4f7f\u7528Deployment\u6765\u7ba1\u7406ReplicaSets\uff0c\u9664\u975e\u4f60\u9700\u8981\u81ea\u5b9a\u4e49\u66f4\u65b0\u7f16\u6392\u3002<br \/>\n\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u80fd\u6c38\u8fdc\u4e0d\u9700\u8981\u64cd\u4f5cReplicaSet\u5bf9\u8c61\uff0c\u800c\u662f\u4f7f\u7528Deployment\u66ff\u4ee3\u7ba1\u7406 \u3002<\/p>\n<h2 id=\\\"example\\\"><span id=\\\"i\\\">\u793a\u4f8b<\/span><\/h2>\n<p>frontend.yaml<\/p>\n<pre>apiVersion: extensions\/v1beta1\nkind: ReplicaSet\nmetadata:\n  name: frontend\n  # these labels can be applied automatically\n  # from the labels in the pod template if not set\n  # labels:\n    # app: guestbook\n    # tier: frontend\nspec:\n  # this replicas value is default\n  # modify it according to your case\n  replicas: 3\n  # selector can be applied automatically\n  # from the labels in the pod template if not set,\n  # but we are specifying the selector here to\n  # demonstrate its usage.\n  selector:\n    matchLabels:\n      tier: frontend\n    matchExpressions:\n      - {key: tier, operator: In, values: [frontend]}\n  template:\n    metadata:\n      labels:\n        app: guestbook\n        tier: frontend\n    spec:\n      containers:\n      - name: php-redis\n        image: gcr.io\/google_samples\/gb-frontend:v3\n        resources:\n          requests:\n            cpu: 100m\n            memory: 100Mi\n        env:\n        - name: GET_HOSTS_FROM\n          value: dns\n          # If your cluster config does not include a dns service, then to\n          # instead access environment variables to find service host\n          # info, comment out the \\'value: dns\\' line above, and uncomment the\n          # line below.\n          # value: env\n        ports:\n        - containerPort: 80<\/pre>\n<p>\u5c06\u6b64\u914d\u7f6e\u4fdd\u5b58\u5230\uff08frontend.yaml\uff09\u5e76\u63d0\u4ea4\u5230Kubernetes\u96c6\u7fa4\u65f6\uff0c\u5c06\u521b\u5efa\u5b9a\u4e49\u7684ReplicaSet\u53ca\u5176\u7ba1\u7406\u7684pod\u3002<\/p>\n<div>\n<pre>$ kubectl create -f frontend.yaml\nreplicaset \\\"frontend\\\" created\n$ kubectl describe rs\/frontend\nName:          frontend\nNamespace:     default\nImage(s):      gcr.io\/google_samples\/gb-frontend:v3\nSelector:      tier=frontend,tier in (frontend)\nLabels:        app=guestbook,tier=frontend\nReplicas:      3 current \/ 3 desired\nPods Status:   3 Running \/ 0 Waiting \/ 0 Succeeded \/ 0 Failed\nNo volumes.\nEvents:\n  FirstSeen    LastSeen    Count    From                SubobjectPath    Type        Reason            Message\n  ---------    --------    -----    ----                -------------    --------    ------            -------\n  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-qhloh\n  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-dnjpy\n  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-9si5l\n$ kubectl get pods\nNAME             READY     STATUS    RESTARTS   AGE\nfrontend-9si5l   1\/1       Running   0          1m\nfrontend-dnjpy   1\/1       Running   0          1m\nfrontend-qhloh   1\/1       Running   0          1m\n<\/pre>\n<\/div>\n<h2><span id=\\\"ReplicaSet_as_an_Horizontal_Pod_Autoscaler_target\\\">ReplicaSet as an Horizontal Pod Autoscaler target<\/span><\/h2>\n<p>ReplicaSet\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u00a0Horizontal Pod Autoscalers (HPA)\u7684\u76ee\u6807\u00a0\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2aReplicaSet\u53ef\u4ee5\u7531\u4e00\u4e2aHPA\u6765\u81ea\u52a8\u4f38\u7f29\u3002\u4ee5\u4e0b\u662f\u9488\u5bf9\u6211\u4eec\u5728\u4e0a\u4e00\u4e2a\u793a\u4f8b\u4e2d\u521b\u5efa\u7684ReplicaSet\u7684HPA\u793a\u4f8b\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><a href=\\\"https:\/\/raw.githubusercontent.com\/kubernetes\/kubernetes.github.io\/master\/docs\/concepts\/workloads\/controllers\/hpa-rs.yaml\\\" target=\\\"_blank\\\" rel=\\\"nofollow noopener noreferrer\\\">hpa-rs.yaml\u00a0<\/a><img title=\\\"\u5c06hpa-rs.yaml\u590d\u5236\u5230\u526a\u8d34\u677f\\\" src=\\\"https:\/\/d33wubrfki0l68.cloudfront.net\/951ae1fcc65e28202164b32c13fa7ae04fab4a0b\/b77dc\/images\/copycode.svg\\\" \/><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n<div id=\\\"hpa-rs.yaml\\\">\n<pre>apiVersion: autoscaling\/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n  name: frontend-scaler\nspec:\n  scaleTargetRef:\n    kind: ReplicaSet\n    name: frontend\n  minReplicas: 3\n  maxReplicas: 10\n  targetCPUUtilizationPercentage: 50\n<\/pre>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<pre>kubectl create -f hpa-rs.yaml<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Kubernetes Replica Sets 1\u00a0\u5982\u4f55\u4f7f\u7528ReplicaSet 2\u00a0\u4f55\u65f6\u4f7f\u7528ReplicaS &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1576\"> <span class=\"screen-reader-text\">Kubernetes Replica Sets<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[104,123],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":6,"uagb_excerpt":"Kubernetes Replica Sets 1\u00a0\u5982\u4f55\u4f7f\u7528ReplicaSet 2\u00a0\u4f55\u65f6\u4f7f\u7528ReplicaS&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1576"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1576"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1576\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1576"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}