{"id":6614,"date":"2022-04-09T22:17:46","date_gmt":"2022-04-09T14:17:46","guid":{"rendered":"https:\/\/egonlin.com\/?p=6614"},"modified":"2022-04-26T19:03:45","modified_gmt":"2022-04-26T11:03:45","slug":"statefulset%e8%87%aa%e5%8a%a8%e5%88%9b%e5%bb%bapv","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=6614","title":{"rendered":"StateFulSet\u81ea\u52a8\u521b\u5efaPV"},"content":{"rendered":"<h2>StateFulSet+volumeClaimTemplates\u81ea\u52a8\u521b\u5efaPV<\/h2>\n<p><strong>nginx-statefulset.yaml<\/strong><\/p>\n<pre><code class=\"language-yaml\">---\napiVersion: v1\nkind: Service\nmetadata:\n  name: nginx-headless\n  labels:\n    app: nginx\nspec:\n  ports:\n  - port: 80\n    name: web\n  clusterIP: None   #\u6ce8\u610f\u6b64\u5904\u7684\u503c,None\u8868\u793a\u65e0\u5934\u670d\u52a1\n  selector:\n    app: nginx\n---\napiVersion: apps\/v1beta1\nkind: StatefulSet\nmetadata:\n  name: web\nspec:\n  serviceName: &quot;nginx&quot;\n  replicas: 2  #\u4e24\u4e2a\u526f\u672c\n  template:\n    metadata:\n      labels:\n        app: nginx\n    spec:\n      containers:\n      - name: nginx\n        image: ikubernetes\/myapp:v1\n        ports:\n        - containerPort: 80\n          name: web\n        volumeMounts:\n        - name: www\n          mountPath: \/usr\/share\/nginx\/html\n  volumeClaimTemplates:\n  - metadata:\n      name: www\n      annotations:\n        volume.beta.kubernetes.io\/storage-class: &quot;managed-nfs-storage&quot;   #managed-nfs-storage\u4e3a\u6211\u4eec\u521b\u5efa\u7684storage-class\u540d\u79f0\n    spec:\n      accessModes: [ &quot;ReadWriteOnce&quot; ]\n      resources:\n        requests:\n          storage: 1Gi\n<\/code><\/pre>\n<p><strong>\u68c0\u67e5\u7ed3\u679c:<\/strong><\/p>\n<p><strong>\u96c6\u7fa4\u8282\u70b9\u4e0a<\/strong><\/p>\n<pre><code class=\"language-yaml\"> [root@k8s-master-155-221 classStorage]#kubectl delete -f nginx-statefulset.yaml \n[root@k8s-master-155-221 classStorage]# kubectl get pods -l app=nginx  #\u68c0\u67e5pod\u72b6\u6001\nNAME    READY   STATUS    RESTARTS   AGE\nweb-0   1\/1     Running   0          115m\nweb-1   1\/1     Running   0          114m\n[root@k8s-master-155-221 classStorage]# kubectl get pvc #\u67e5\u770bPVC\nNAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE\ntest-claim   Bound    pvc-aae2b7fa-377b-11ea-87ad-525400512eca   1Mi        RWX            managed-nfs-storage   19h\nwww-web-0    Bound    pvc-4d7e342a-3810-11ea-87ad-525400512eca   1Gi        RWO            managed-nfs-storage   115m\nwww-web-1    Bound    pvc-5431c8ba-3810-11ea-87ad-525400512eca   1Gi        RWO            managed-nfs-storage   115m\n[root@k8s-master-155-221 classStorage]# kubectl get pv #\u67e5\u770bPV\nNAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                STORAGECLASS          REASON   AGE\npvc-4d7e342a-3810-11ea-87ad-525400512eca   1Gi        RWO            Delete           Bound    default\/www-web-0    managed-nfs-storage            115m\npvc-5431c8ba-3810-11ea-87ad-525400512eca   1Gi        RWO            Delete           Bound    default\/www-web-1    managed-nfs-storage            115m\npvc-aae2b7fa-377b-11ea-87ad-525400512eca   1Mi        RWX            Delete           Bound    default\/test-claim   managed-nfs-storage            19h<\/code><\/pre>\n<p><strong>NFS Server\u4e0a:<\/strong><\/p>\n<pre><code class=\"language-yaml\">[root@nginx-keepalived-155-227 ~]# cd \/data\/volumes\/\n[root@nginx-keepalived-155-227 volumes]# ll  #\u6ce8\u610f\u76ee\u5f55\u7684\u547d\u540d\u683c\u5f0f\n\u603b\u7528\u91cf 0\ndrwxrwxrwx 2 root root 21 2020-01-15 17:51 default-test-claim-pvc-aae2b7fa-377b-11ea-87ad-525400512eca\ndrwxrwxrwx 2 root root  6 2020-01-16 11:28 default-www-web-0-pvc-4d7e342a-3810-11ea-87ad-525400512eca\ndrwxrwxrwx 2 root root  6 2020-01-16 11:28 default-www-web-1-pvc-5431c8ba-3810-11ea-87ad-525400512eca\n[root@nginx-keepalived-155-227 volumes]# echo &quot;web-00&quot; &gt; default-www-web-0-pvc-4d7e342a-3810-11ea-87ad-525400512eca\/index.html #\u5206\u522b\u521b\u5efa\u4e0d\u540c\u7684index\u6587\u4ef6\n[root@nginx-keepalived-155-227 volumes]# echo &quot;web-01&quot; &gt; default-www-web-1-pvc-5431c8ba-3810-11ea-87ad-525400512eca\/index.html<\/code><\/pre>\n<p><strong>\u96c6\u7fa4\u4efb\u610f\u8282\u70b9\u4e0a:<\/strong><\/p>\n<pre><code class=\"language-yaml\">[root@k8s-master-155-221 classStorage]# kubectl exec -it pod-cm-1 -- \/bin\/sh  #\u8fdb\u5165\u96c6\u7fa4\u4e2d\u4efb\u610fpod\u4e2d,\u89e3\u6790nginx-headless \u670d\u52a1\/ # nslookup nginx-headless\nnslookup: can&#039;t resolve &#039;(null)&#039;: Name does not resolve\n\nName:      nginx-headless\nAddress 1: 172.17.136.7 172-17-136-7.nginx-headless.default.svc.cluster.local  #\u53ef\u4ee5\u770b\u5230\u6709\u4e24\u4e2a\u5730\u5740\nAddress 2: 172.17.248.5 172-17-248-5.nginx-headless.default.svc.cluster.local\n[root@k8s-master-155-221 classStorage]# curl 172.17.248.5 #\u5206\u522b\u8bbf\u95ee\u4e00\u4e0b\u67e5\u770b\u7ed3\u679c\nweb-00\n[root@k8s-master-155-221 classStorage]# curl 172.17.136.7\nweb-01<\/code><\/pre>\n<h6>\u5bf9\u4e8estatefulset\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\/\u5220\u9664pod\u526f\u672c\u7684\u6570\u91cf,\u89c2\u5bdfPV\/PVC\u7684\u72b6\u6001\u53ca\u53d8\u5316.<\/h6>\n","protected":false},"excerpt":{"rendered":"<p>StateFulSet+volumeClaimTemplates\u81ea\u52a8\u521b\u5efaPV nginx-statefulse [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6635,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[417,412],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6614"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6614"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6614\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/6635"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}