尚实 發表於 2019-11-25 11:31:00

024.Kubernetes掌握Pod-部署MongoDB

<h2 align="left">一        前期准备</h2><font face="微软雅黑" size="2">
</font><h3 align="left">1.1        前置条件</h3><font face="微软雅黑" size="2">
</font><div style="line-height: normal; -ms-word-break: normal"><font face="微软雅黑" size="2">
</font><ul style="margin: 0; padding-left: 30pt" yne-block-type="list"><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">集群部署:Kubernetes集群部署参考003——019。</font></li><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">glusterfs-Kubernetes部署:参考《附010.Kubernetes永久存储之GlusterFS超融合部署》。</font></li></ul></div><font face="微软雅黑" size="2">
</font><h3 align="left">1.2        部署规划</h3><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">本实验使用StatefulSet部署MongoDB集群,同时每个MongoDB实例使用glusterfs实现永久存储。从而部署无单点故障、高可用、可动态扩展的MongoDB集群。</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">部署架构如下:</font></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"><img width="504" height="469" title="clipboard" style="margin: 0; border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113050229-609498258.png" border="0" data-attr-org-src-id="2FA3C4538D9A42B5AE9A7188D393D10D" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><h2 align="left">二        创建StatefulSet</h2><font face="微软雅黑" size="2">
</font><h3 align="left">2.1        创建storageclass存储类型</h3><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi heketi-secret.yaml                        #创建用于保存密码的secret
<span style="color: rgba(0, 128, 128, 1)">2</span> apiVersion: v1
<span style="color: rgba(0, 128, 128, 1)">3</span> kind: Secret
<span style="color: rgba(0, 128, 128, 1)">4</span> metadata:
<span style="color: rgba(0, 128, 128, 1)">5</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: heketi-secret
<span style="color: rgba(0, 128, 128, 1)">6</span>   namespace: heketi
<span style="color: rgba(0, 128, 128, 1)">7</span> data:
<span style="color: rgba(0, 128, 128, 1)">8</span>   # base64 encoded password. E.g.: echo -n "<span style="color: rgba(139, 0, 0, 1)">mypassword</span>" | base64
<span style="color: rgba(0, 128, 128, 1)">9</span>   key: YWRtaW4xMjM=
<span style="color: rgba(0, 128, 128, 1)"> 10</span> type: kubernetes.io/glusterfs</pre></div><div id="1651-1574344782319" style="line-height: normal; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="code" data-language="javascript" data-theme="coy"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl create -f heketi-secret.yaml        #创建heketi
<span style="color: rgba(0, 128, 128, 1)">2</span> # kubectl get secrets -n heketi
<span style="color: rgba(0, 128, 128, 1)">3</span> NAME                                 TYPE                                  DATA   AGE
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">default</span>-token-6n746                  kubernetes.io/service-account-token   3      144m
<span style="color: rgba(0, 128, 128, 1)">5</span> heketi-config-secret               Opaque                              3      142m
<span style="color: rgba(0, 128, 128, 1)">6</span> heketi-secret                        kubernetes.io/glusterfs               1      3m1s
<span style="color: rgba(0, 128, 128, 1)">7</span> heketi-service-account-token-ljlkb   kubernetes.io/service-account-token   3      143m
<span style="color: rgba(0, 128, 128, 1)">8</span> # mkdir mongo
<span style="color: rgba(0, 128, 128, 1)">9</span> # cd mongo</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi storageclass-fast.yaml
<span style="color: rgba(0, 128, 128, 1)">2</span> apiVersion: storage.k8s.io/v1
<span style="color: rgba(0, 128, 128, 1)">3</span> kind: StorageClass
<span style="color: rgba(0, 128, 128, 1)">4</span> metadata:
<span style="color: rgba(0, 128, 128, 1)">5</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: fast
<span style="color: rgba(0, 128, 128, 1)">6</span> parameters:
<span style="color: rgba(0, 128, 128, 1)">7</span>   resturl: "<span style="color: rgba(139, 0, 0, 1)">http://10.254.82.26:8080</span>"
<span style="color: rgba(0, 128, 128, 1)">8</span>   clusterid: "<span style="color: rgba(139, 0, 0, 1)">d96022e907f82045dcc426a752adc47c</span>"
<span style="color: rgba(0, 128, 128, 1)">9</span>   restauthenabled: "<span style="color: rgba(139, 0, 0, 1)">true</span>"
<span style="color: rgba(0, 128, 128, 1)"> 10</span>   restuser: "<span style="color: rgba(139, 0, 0, 1)">admin</span>"
<span style="color: rgba(0, 128, 128, 1)"> 11</span>   secretName: "<span style="color: rgba(139, 0, 0, 1)">heketi-secret</span>"
<span style="color: rgba(0, 128, 128, 1)"> 12</span>   secretNamespace: "<span style="color: rgba(139, 0, 0, 1)">default</span>"
<span style="color: rgba(0, 128, 128, 1)"> 13</span>   volumetype: "<span style="color: rgba(139, 0, 0, 1)">replicate:3</span>"
<span style="color: rgba(0, 128, 128, 1)"> 14</span> provisioner: kubernetes.io/glusterfs
<span style="color: rgba(0, 128, 128, 1)"> 15</span> reclaimPolicy: Delete</pre></div><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl create -f storageclass-fast.yaml
<span style="color: rgba(0, 128, 128, 1)">2</span> # kubectl get storageclasses/fast</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"><img width="534" height="96" title="clipboard" style="margin: 0; border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113050547-1582207143.png" border="0" data-attr-org-src-id="421266EB0FEE4995BFE5E0FC56028B83" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">2.2        授权ServiceAccount</h3><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; text-indent: 28px; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">本实验2.4步骤需要使用mongo-sidecar的pod来配置管理mongo pod。</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; text-indent: 28px; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">由于默认的service account仅仅只能获取当前Pod自身的相关属性,无法观察到其他名称空间Pod的相关属性信息。如果想要扩展Pod,或者一个Pod需要用于管理其他Pod或者是其他资源对象,是无法通过自身的名称空间的serviceaccount进行获取其他Pod的相关属性信息的,因此需要进行手动创建一个serviceaccount,并在创建Pod时进行定义。或者直接将默认的serviceaccount进行授权。</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi defaultaccout.yaml
<span style="color: rgba(0, 128, 128, 1)">2</span> ---
<span style="color: rgba(0, 128, 128, 1)">3</span> apiVersion: rbac.authorization.k8s.io/v1beta1
<span style="color: rgba(0, 128, 128, 1)">4</span> kind: ClusterRoleBinding
<span style="color: rgba(0, 128, 128, 1)">5</span> metadata:
<span style="color: rgba(0, 128, 128, 1)">6</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: DDefault-Cluster-Admin
<span style="color: rgba(0, 128, 128, 1)">7</span> subjects:
<span style="color: rgba(0, 128, 128, 1)">8</span>   - kind: ServiceAccount
<span style="color: rgba(0, 128, 128, 1)">9</span>   # Reference to upper's `metadata.<span style="color: rgba(0, 0, 255, 1)">name</span>`
<span style="color: rgba(0, 128, 128, 1)"> 10</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: <span style="color: rgba(0, 0, 255, 1)">default</span>
<span style="color: rgba(0, 128, 128, 1)"> 11</span>   # Reference to upper's `metadata.namespace`
<span style="color: rgba(0, 128, 128, 1)"> 12</span>   namespace: <span style="color: rgba(0, 0, 255, 1)">default</span>
<span style="color: rgba(0, 128, 128, 1)"> 13</span> roleRef:
<span style="color: rgba(0, 128, 128, 1)"> 14</span>   kind: ClusterRole
<span style="color: rgba(0, 128, 128, 1)"> 15</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: cluster-admin
<span style="color: rgba(0, 128, 128, 1)"> 16</span>   apiGroup: rbac.authorization.k8s.io
<span style="color: rgba(0, 128, 128, 1)"> 17</span>
<span style="color: rgba(0, 128, 128, 1)"> 18</span> # kubectl apply -f defaultaccout.yaml</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">2.3        创建headless Service</h3><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi mongo-headless-service.yaml</pre></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">略</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font color="#ff0000" face="微软雅黑" size="2"><strong>提示:本实验直接将headless结合在StatefulSet同一个yaml文件中,参考2.4。</strong></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">2.4        创建StatefulSet</h3><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi statefulset-mongo.yaml
<span style="color: rgba(0, 128, 128, 1)">2</span> ---
<span style="color: rgba(0, 128, 128, 1)">3</span> apiVersion: v1
<span style="color: rgba(0, 128, 128, 1)">4</span> kind: Service
<span style="color: rgba(0, 128, 128, 1)">5</span> metadata:
<span style="color: rgba(0, 128, 128, 1)">6</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo
<span style="color: rgba(0, 128, 128, 1)">7</span>   labels:
<span style="color: rgba(0, 128, 128, 1)">8</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo
<span style="color: rgba(0, 128, 128, 1)">9</span> spec:
<span style="color: rgba(0, 128, 128, 1)"> 10</span>   ports:
<span style="color: rgba(0, 128, 128, 1)"> 11</span>   - port: 27017
<span style="color: rgba(0, 128, 128, 1)"> 12</span>   targetPort: 27017
<span style="color: rgba(0, 128, 128, 1)"> 13</span>   clusterIP: None
<span style="color: rgba(0, 128, 128, 1)"> 14</span>   selector:
<span style="color: rgba(0, 128, 128, 1)"> 15</span>   role: mongo
<span style="color: rgba(0, 128, 128, 1)"> 16</span> ---                                  #以上为headless-service
<span style="color: rgba(0, 128, 128, 1)"> 17</span> apiVersion: apps/v1beta1
<span style="color: rgba(0, 128, 128, 1)"> 18</span> kind: StatefulSet
<span style="color: rgba(0, 128, 128, 1)"> 19</span> metadata:
<span style="color: rgba(0, 128, 128, 1)"> 20</span>   <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo
<span style="color: rgba(0, 128, 128, 1)"> 21</span> spec:
<span style="color: rgba(0, 128, 128, 1)"> 22</span>   serviceName: "<span style="color: rgba(139, 0, 0, 1)">mongo</span>"
<span style="color: rgba(0, 128, 128, 1)"> 23</span>   replicas: 3
<span style="color: rgba(0, 128, 128, 1)"> 24</span>   template:
<span style="color: rgba(0, 128, 128, 1)"> 25</span>   metadata:
<span style="color: rgba(0, 128, 128, 1)"> 26</span>       labels:
<span style="color: rgba(0, 128, 128, 1)"> 27</span>         role: mongo
<span style="color: rgba(0, 128, 128, 1)"> 28</span>         environment: test
<span style="color: rgba(0, 128, 128, 1)"> 29</span>   spec:
<span style="color: rgba(0, 128, 128, 1)"> 30</span>       terminationGracePeriodSeconds: 10
<span style="color: rgba(0, 128, 128, 1)"> 31</span>       containers:
<span style="color: rgba(0, 128, 128, 1)"> 32</span>         - <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo
<span style="color: rgba(0, 128, 128, 1)"> 33</span>         image: mongo:3.4             #新版可能不支持smallfiles参数,因此指定为3.4版本
<span style="color: rgba(0, 128, 128, 1)"> 34</span>         command:
<span style="color: rgba(0, 128, 128, 1)"> 35</span>             - mongod
<span style="color: rgba(0, 128, 128, 1)"> 36</span>             - "<span style="color: rgba(139, 0, 0, 1)">--replSet</span>"
<span style="color: rgba(0, 128, 128, 1)"> 37</span>             - rs0
<span style="color: rgba(0, 128, 128, 1)"> 38</span>             - "<span style="color: rgba(139, 0, 0, 1)">--bind_ip</span>"
<span style="color: rgba(0, 128, 128, 1)"> 39</span>             - 0.0.0.0
<span style="color: rgba(0, 128, 128, 1)"> 40</span>             - "<span style="color: rgba(139, 0, 0, 1)">--smallfiles</span>"         #使用较小的默认文件
<span style="color: rgba(0, 128, 128, 1)"> 41</span>             - "<span style="color: rgba(139, 0, 0, 1)">--noprealloc</span>"         #禁用数据文件预分配
<span style="color: rgba(0, 128, 128, 1)"> 42</span>         ports:
<span style="color: rgba(0, 128, 128, 1)"> 43</span>             - containerPort: 27017
<span style="color: rgba(0, 128, 128, 1)"> 44</span>         volumeMounts:
<span style="color: rgba(0, 128, 128, 1)"> 45</span>             - <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo-persistent-storage
<span style="color: rgba(0, 128, 128, 1)"> 46</span>               mountPath: /data/db
<span style="color: rgba(0, 128, 128, 1)"> 47</span>         - <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo-sidecar
<span style="color: rgba(0, 128, 128, 1)"> 48</span>         image: cvallance/mongo-k8s-sidecar
<span style="color: rgba(0, 128, 128, 1)"> 49</span>         env:
<span style="color: rgba(0, 128, 128, 1)"> 50</span>             - <span style="color: rgba(0, 0, 255, 1)">name</span>: MONGO_SIDECAR_POD_LABELS
<span style="color: rgba(0, 128, 128, 1)"> 51</span>               value: "<span style="color: rgba(139, 0, 0, 1)">role=mongo,environment=test</span>"
<span style="color: rgba(0, 128, 128, 1)"> 52</span>             - <span style="color: rgba(0, 0, 255, 1)">name</span>: KUBERNETES_MONGO_SERVICE_NAME
<span style="color: rgba(0, 128, 128, 1)"> 53</span>               value: "<span style="color: rgba(139, 0, 0, 1)">mongo</span>"
<span style="color: rgba(0, 128, 128, 1)"> 54</span>   volumeClaimTemplates:
<span style="color: rgba(0, 128, 128, 1)"> 55</span>   - metadata:
<span style="color: rgba(0, 128, 128, 1)"> 56</span>       <span style="color: rgba(0, 0, 255, 1)">name</span>: mongo-persistent-storage
<span style="color: rgba(0, 128, 128, 1)"> 57</span>       annotations:
<span style="color: rgba(0, 128, 128, 1)"> 58</span>         volume.beta.kubernetes.io/storage-<span style="color: rgba(0, 0, 255, 1)">class</span>: "<span style="color: rgba(139, 0, 0, 1)">fast</span>"
<span style="color: rgba(0, 128, 128, 1)"> 59</span>   spec:
<span style="color: rgba(0, 128, 128, 1)"> 60</span>       accessModes: [ "<span style="color: rgba(139, 0, 0, 1)">ReadWriteOnce</span>" ]
<span style="color: rgba(0, 128, 128, 1)"> 61</span>       resources:
<span style="color: rgba(0, 128, 128, 1)"> 62</span>         requests:
<span style="color: rgba(0, 128, 128, 1)"> 63</span>         storage: 2Gi</pre></div><div id="9954-1574053192695" style="line-height: normal; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="code" data-language="javascript" data-theme="coy"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">释义:</font></div><font face="微软雅黑" size="2">
</font><div style="line-height: normal; -ms-word-break: normal"><font face="微软雅黑" size="2">
</font><ol style="margin: 0; padding-left: 30pt" yne-block-type="list"><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">该StatefulSet定义了两个容器:mingo和mongo-sidecar。mongo是主服务程序,mongo-sidecar是将多个mongo实例进行集群设置的工具。同时mongo-sidecar中设置了如下环境变量:</font></li></ol></div><font face="微软雅黑" size="2">
</font><div style="line-height: normal; -ms-word-break: normal"><font face="微软雅黑" size="2">
</font><ul style="margin: 0; padding-left: 30pt" yne-block-type="list"><font face="微软雅黑" size="2">
</font><ul style="margin: 0; padding-left: 30pt" yne-block-type="list"><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: circle; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">MONGO_SIDECAR_POD_LABELS:设置为mongo容器的标签,用于sidecar查询它所要管理的MongoDB集群实例。</font></li><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: circle; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">KUBERNETES_MONGO_SERVICE_NAME:它的值为mongo,表示sidecar将使用mongo这个服务名来完成MongoDB集群的设置。</font></li></ul></ul></div><font face="微软雅黑" size="2">
</font><div style="line-height: normal; -ms-word-break: normal"><font face="微软雅黑" size="2">
</font><ol style="margin: 0; padding-left: 30pt" start="2" yne-block-type="list"><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">replicas=3表示MongoDB集群由3个mongo实例组成。</font></li><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">volumeClaimTemplates是StatefulSet最重要的存储设置。在annotations段设置volume.beta.kubernetes.io/storage-class="fast"表示使用名为fast的StorageClass自动为每个mongo Pod实例分配后端存储。</font></li><font face="微软雅黑" size="2">
</font><li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><font face="微软雅黑" size="2">resources.requests.storage=2Gi表示为每个mongo实例都分配2GiB的磁盘空间。</font></li></ol></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><br></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl create -f statefulset-mongo.yaml        #创建mongo</pre></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font color="#ff0000" face="微软雅黑" size="2"><strong>提示:由于国内mongo镜像可能无法pull,建议通过VPN等方式提前pull镜像,然后上传至所有node节点。</strong></font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><font color="#ff0000">1 # docker pull cvallance/mongo-k8s-sidecar:latest
2 # docker pull mongo:3.4.4
3 # docker save -o mongo-k8s-sidecar.tar cvallance/mongo-k8s-sidecar:latest
4 # docker save -o mongo_3_4_4.tar mongo:3.4.4
5 # docker load -i mongo-k8s-sidecar.tar
6 # docker load -i mongo.tar
7 # docker images</font></pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">创建异常可通过如下方式删除,重新创建:</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> kubectl <span style="color: rgba(0, 0, 255, 1)">delete</span> -f statefulset-mongo.yaml
<span style="color: rgba(0, 128, 128, 1)">2</span> kubectl <span style="color: rgba(0, 0, 255, 1)">delete</span> -f mongo-headless-service.yaml
<span style="color: rgba(0, 128, 128, 1)">3</span> kubectl <span style="color: rgba(0, 0, 255, 1)">delete</span> pvc -l role=mongo</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><h2 align="left">三        确认验证</h2><font face="微软雅黑" size="2">
</font><h3 align="left">3.1        查看资源</h3><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl get pod -l role=mongo                        #查看集群pod
<span style="color: rgba(0, 128, 128, 1)">2</span> NAME      READY   STATUS    RESTARTS   AGE
<span style="color: rgba(0, 128, 128, 1)">3</span> mongo-0   2/2   Running   0          9m44s
<span style="color: rgba(0, 128, 128, 1)">4</span> mongo-1   2/2   Running   0          7m51s
<span style="color: rgba(0, 128, 128, 1)">5</span> mongo-2   2/2   Running   0          6m1s</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">StatefulSet会用volumeClaimTemplates中的定义为每个Pod副本都创建一个PVC实例,每个PVC的名称由StatefulSet定义中volumeClaimTemplates的名称和Pod副本的名称组合而成。</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl get pvc</pre></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"></font><font face="微软雅黑" size="2"><img width="836" height="66" title="clipboard" style="border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113051118-518924393.png" border="0" data-attr-org-src-id="35DC72AB3E4A46509F5F21FD6440D23B" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl get pods mongo-0 -o yaml | grep -A 3 volumes        #查看挂载</pre></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"><img width="629" height="87" title="clipboard" style="margin: 0; border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113051418-76919396.png" border="0" data-attr-org-src-id="80A8BF03750640728352DF3F37D2485D" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">3.2        查看mongo集群</h3><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">登录任意一个mongo Pod,在mongo命令行界面用rs.status()命令查看MongoDB集群的状态,该mongodb集群已由sidecar完成了创建。在集群中包含3个节点 每个节点的名称都是StatefulSet设置的DNS域名格式的网络标识名称:</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; margin-left: 28px; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">mongo-0.mongo.default.svc.cluster.local</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; margin-left: 28px; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">mongo-1.mongo.default.svc.cluster.local</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; margin-left: 28px; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">mongo-2.mongo.default.svc.cluster.local</font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">同时,可以查看每个mongo实例各自的角色(PRIMARY或SECONDARY)。</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl exec -ti mongo-0 -- mongo
<span style="color: rgba(0, 128, 128, 1)">2</span> ……
<span style="color: rgba(0, 128, 128, 1)">3</span> rs0:PRIMARY&gt; rs.<span style="color: rgba(0, 0, 255, 1)">status</span>()</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"></font><font face="微软雅黑" size="2"><img width="826" height="442" title="clipboard" style="border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113052365-264856576.png" border="0" data-attr-org-src-id="90BCF5A42A6843748C8ABCD11C902DBA" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><h2 align="left">四        集群常见管理</h2><font face="微软雅黑" size="2">
</font><h3 align="left">4.1        MongoDB扩容</h3><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">运行环境过程中,若3个mongo实例不足以满足业务的要求,可对mongo集群进行扩容。仅需要通过对StatefulSet进行scale操作,从而实现在mongo集群中自动添加新的mongo节点。</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl scale statefulset mongo --replicas=4        #扩容为4个
<span style="color: rgba(0, 128, 128, 1)">2</span> # kubectl get pod -l role=mongo
<span style="color: rgba(0, 128, 128, 1)">3</span> NAME      READY   STATUS    RESTARTS   AGE
<span style="color: rgba(0, 128, 128, 1)">4</span> mongo-0   2/2   Running   0          105m
<span style="color: rgba(0, 128, 128, 1)">5</span> mongo-1   2/2   Running   0          103m
<span style="color: rgba(0, 128, 128, 1)">6</span> mongo-2   2/2   Running   0          101m
<span style="color: rgba(0, 128, 128, 1)">7</span> mongo-3   2/2   Running   0          50m</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">4.2        查看集群成员</h3><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl exec -ti mongo-0 -- mongo
<span style="color: rgba(0, 128, 128, 1)">2</span> ……
<span style="color: rgba(0, 128, 128, 1)">3</span> rs0:PRIMARY&gt; rs.<span style="color: rgba(0, 0, 255, 1)">status</span>()
<span style="color: rgba(0, 128, 128, 1)">4</span> ……</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"><img width="608" height="464" title="clipboard" style="margin: 0; border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113052736-275082930.png" border="0" data-attr-org-src-id="E727F95E53F44A38973FB733815F1336" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><h3 align="left">4.3        故障自动恢复</h3><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2">若在系统运行过程中,某个mongo实例或其所在主机发生故障,则StatefulSet将会自动重建该mongo实例,并保证其身份(ID)和使用的数据(PVC) 不变。以下为mongo-0实例发生故障进行模拟,StatefulSet将会自动重建mongo-0实例,并为其挂载之前分配的PVC“mongo-persistent-storage-mongo-0”。新的服务“mongo-0”在重新启动后,原数据库中的数据不会丢失,可继续使用。</font></div><font face="微软雅黑" size="2">
</font><div class="cnblogs_code"><pre><span style="color: rgba(0, 128, 128, 1)">1</span> # kubectl get pvc
<span style="color: rgba(0, 128, 128, 1)">2</span> # kubectl <span style="color: rgba(0, 0, 255, 1)">delete</span> pod mongo-0
<span style="color: rgba(0, 128, 128, 1)">3</span> # kubectl exec -ti mongo-0 -- mongo
<span style="color: rgba(0, 128, 128, 1)">4</span> ……
<span style="color: rgba(0, 128, 128, 1)">5</span> rs0:PRIMARY&gt; rs.<span style="color: rgba(0, 0, 255, 1)">status</span>()
<span style="color: rgba(0, 128, 128, 1)">6</span> ……</pre></div><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><font face="微软雅黑" size="2"></font></div><font face="微软雅黑" size="2">
</font><div align="center" style="line-height: normal; float: none; -ms-word-break: normal" yne-bulb-block="image"><font face="微软雅黑" size="2"></font><font face="微软雅黑" size="2"><img width="841" height="448" title="clipboard" style="border: 0 currentColor; border-image: none; display: inline; max-width: none; background-image: none" alt="clipboard" src="https://img2018.cnblogs.com/blog/680719/201911/680719-20191125113053596-83961970.png" border="0" data-attr-org-src-id="40F41D38274A42FC97719D048187FD32" data-media-type="image"></font></div><font face="微软雅黑" size="2">
</font><div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal" yne-bulb-block="paragraph"><span><font color="#ff0000" face="微软雅黑" size="2"><strong>提示:进入某个实例查看mongo集群的状态,mongo-0发生故障前在集群中的角色为PRIMARY,在其脱离集群后,mongo集群会自动选出一个SECONDARY节点提升为PRIMARY节点(本例中为mongo-2)。重启后的mongo-0则会成为一个新的SECONDARY节点。</strong></font></span></div>

</div>
<div id="MySignature" role="contentinfo">
    <div style="background: #f7acbc; color: #0; font-size: small">
<p>
作者:木二
</p>
<p>
出处:http://www.cnblogs.com/itzgr/
</p>
<p>
关于作者:云计算、虚拟化,Linux,多多交流!
</p>
<p>
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接!如有其他问题,可邮件(xhy@itzgr.com)咨询。
</p>
</div><br><br>
来源:https://www.cnblogs.com/itzgr/p/11926689.html
頁: [1]
查看完整版本: 024.Kubernetes掌握Pod-部署MongoDB