经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
kubernetes和docker----2.学习Pod资源
来源:cnblogs  作者:只盼一人共白首  时间:2021/3/1 8:49:10  对本文有异议

Pod--k8s最基础的资源

我们想要的是单个容器只运行一个进程

然而有时我们需要多个进程协同工作,所以我们需要另外一种更加高级的结构将容器组合在一起---pod

Pod

  1. 我们来看一个最基本的pod

    这个pod的image是我根据centos:7的镜像构建的,很简单,镜像的Dockerfile如下:

    1. FROM 192.168.80.84:5000/centos:7
    2. entrypoint ["sleep"]
    3. cmd ["999"]
    4. # 一个容器必须要有一个守护进程才能够运行起来
    5. # 换言之,把Dockerfile中的sleep命令去掉,单纯的一个centos是无法运行的

    我们将这个镜像作为pod的image运行起来:

    kubectl run my-cmd --image=192.168.80.84:5000/centos_cmd:v1


    使用-o yaml来看一下对应的yaml文件:

    1. [root@k8s-master01 centos]# kubectl get pod my-cmd -o yaml
    2. apiVersion: v1 # 指定apiVersion版本
    3. kind: Pod # 对应的资源类型,这里为pod
    4. metadata: # 实例的元数据
    5. creationTimestamp: "2021-01-13T02:36:02Z"
    6. labels: # 自动给实例打的标签
    7. run: my-cmd
    8. managedFields: # 为了方便内部管理的一组字段
    9. - apiVersion: v1
    10. fieldsType: FieldsV1
    11. fieldsV1:
    12. f:metadata:
    13. f:labels:
    14. .: {}
    15. f:run: {}
    16. f:spec:
    17. f:containers:
    18. k:{"name":"my-cmd"}:
    19. .: {}
    20. f:image: {}
    21. f:imagePullPolicy: {}
    22. f:name: {}
    23. f:resources: {}
    24. f:terminationMessagePath: {}
    25. f:terminationMessagePolicy: {}
    26. f:dnsPolicy: {}
    27. f:enableServiceLinks: {}
    28. f:restartPolicy: {}
    29. f:schedulerName: {}
    30. f:securityContext: {}
    31. f:terminationGracePeriodSeconds: {}
    32. manager: kubectl-run # 写明该pod的启动方式
    33. operation: Update
    34. time: "2021-01-13T02:36:02Z"
    35. - apiVersion: v1
    36. fieldsType: FieldsV1
    37. fieldsV1:
    38. f:status:
    39. f:conditions:
    40. k:{"type":"ContainersReady"}:
    41. .: {}
    42. f:lastProbeTime: {}
    43. f:lastTransitionTime: {}
    44. f:status: {}
    45. f:type: {}
    46. k:{"type":"Initialized"}:
    47. .: {}
    48. f:lastProbeTime: {}
    49. f:lastTransitionTime: {}
    50. f:status: {}
    51. f:type: {}
    52. k:{"type":"Ready"}:
    53. .: {}
    54. f:lastProbeTime: {}
    55. f:lastTransitionTime: {}
    56. f:status: {}
    57. f:type: {}
    58. f:containerStatuses: {}
    59. f:hostIP: {}
    60. f:phase: {}
    61. f:podIP: {}
    62. f:podIPs:
    63. .: {}
    64. k:{"ip":"10.40.0.4"}:
    65. .: {}
    66. f:ip: {}
    67. f:startTime: {}
    68. manager: kubelet
    69. operation: Update
    70. time: "2021-01-13T02:36:11Z"
    71. name: my-cmd # pod名
    72. namespace: default # pod所处的命名空间
    73. resourceVersion: "418695" # pod的版本数字,用于乐观并发控制的,详细信息请见之后的k8s核心原理
    74. uid: 12e3b858-f79f-4378-8ea0-1103ea120c34 # pod实例的uid
    75. spec: # pod的实际说明
    76. containers: # 定义pod中的容器,这里只有一个
    77. - image: 192.168.80.84:5000/centos_cmd:v1 # 镜像地址
    78. imagePullPolicy: IfNotPresent # 镜像的pull规则,指的是是否在创建pod的时候要pull镜像,IdNotPresent表示本地不存在时才会去仓库pull
    79. name: my-cmd # 容器名,即镜像转化为容器后的名字
    80. resources: {}
    81. terminationMessagePath: /dev/termination-log
    82. terminationMessagePolicy: File
    83. volumeMounts: # 挂载卷
    84. - mountPath: /var/run/secrets/kubernetes.io/serviceaccount # 挂载路径
    85. name: default-token-s9dfj # 卷名,这里挂载的其实是每个pod都会挂载的secret卷,用来进行身份验证的
    86. readOnly: true # 只读
    87. dnsPolicy: ClusterFirst
    88. enableServiceLinks: true
    89. nodeName: k8s-node02 # 分配到的节点,由调度器指定
    90. preemptionPolicy: PreemptLowerPriority
    91. priority: 0
    92. restartPolicy: Always # 指定当pod重启时,该容器是否还会启动,其实也就是制定该容器随Pod的启动而启动
    93. schedulerName: default-scheduler # 指定调度器,k8s中可以运行多个调度器实例,如果未指定则是默认调度器
    94. securityContext: {}
    95. serviceAccount: default # 服务帐号
    96. serviceAccountName: default
    97. terminationGracePeriodSeconds: 30
    98. tolerations:
    99. - effect: NoExecute
    100. key: node.kubernetes.io/not-ready
    101. operator: Exists
    102. tolerationSeconds: 300
    103. - effect: NoExecute
    104. key: node.kubernetes.io/unreachable
    105. operator: Exists
    106. tolerationSeconds: 300
    107. volumes: # 卷
    108. - name: default-token-s9dfj
    109. secret:
    110. defaultMode: 420
    111. secretName: default-token-s9dfj
    112. status: # pod运行时的状态
    113. conditions:
    114. - lastProbeTime: null
    115. lastTransitionTime: "2021-01-13T02:36:02Z"
    116. status: "True"
    117. type: Initialized
    118. - lastProbeTime: null
    119. lastTransitionTime: "2021-01-13T02:36:10Z"
    120. status: "True"
    121. type: Ready
    122. - lastProbeTime: null
    123. lastTransitionTime: "2021-01-13T02:36:10Z"
    124. status: "True"
    125. type: ContainersReady
    126. - lastProbeTime: null
    127. lastTransitionTime: "2021-01-13T02:36:02Z"
    128. status: "True"
    129. type: PodScheduled
    130. containerStatuses:
    131. - containerID: docker://965a9b86cc334705d3fbaac15d28ef6b0a20de8f00915c1ffdf4c025b1c29206
    132. image: 192.168.80.84:5000/centos_cmd:v1
    133. imageID: docker-pullable://192.168.80.84:5000/centos_cmd@sha256:948479967390e7a98979d4b98beec6dfa3fc92c6ce832ece882e8b1843e0779f
    134. lastState: {}
    135. name: my-cmd
    136. ready: true
    137. restartCount: 0
    138. started: true
    139. state:
    140. running:
    141. startedAt: "2021-01-13T02:36:09Z"
    142. hostIP: 192.168.80.83
    143. phase: Running
    144. podIP: 10.40.0.4
    145. podIPs:
    146. - ip: 10.40.0.4
    147. qosClass: BestEffort
    148. startTime: "2021-01-13T02:36:02Z"

    可以发现其中的东西有些多,然而我们使用yaml文件创建pod时并不需要编写这么多的东西,因为API server会帮我们添加其余的默认值


    使用yaml文件手动创建一个pod:

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: my-cmd
    5. spec:
    6. containers:
    7. - image: 192.168.80.84:5000/centos_cmd:v1
    8. name: centos-cmd
    9. # 需要注意的是spec.containers中的name字段,这里的命名规则和pod的命名规则是一样的,也就是如果"my_cmd"则会报错
    10. # 其次注意"Pod"的“P”要大写

    我们来看一下这样创建的pod的yaml文件:kubectl create -f my-cmd.yaml,我们可以通过kubectl get pod my-cmd -o yaml来查看一下该pod

    1. [root@k8s-master01 centos]# kubectl get pod my-cmd -o yaml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. creationTimestamp: "2021-01-13T03:32:42Z"
    6. managedFields:
    7. - apiVersion: v1
    8. fieldsType: FieldsV1
    9. fieldsV1:
    10. f:spec:
    11. f:containers:
    12. k:{"name":"my-cmd"}:
    13. .: {}
    14. f:image: {}
    15. f:imagePullPolicy: {}
    16. f:name: {}
    17. f:resources: {}
    18. f:terminationMessagePath: {}
    19. f:terminationMessagePolicy: {}
    20. f:dnsPolicy: {}
    21. f:enableServiceLinks: {}
    22. f:restartPolicy: {}
    23. f:schedulerName: {}
    24. f:securityContext: {}
    25. f:terminationGracePeriodSeconds: {}
    26. manager: kubectl-create # 这里的启动方式有所不同,因为我们是通过create的方式创建的pod
    27. operation: Update
    28. time: "2021-01-13T03:32:42Z"
    29. - apiVersion: v1
    30. fieldsType: FieldsV1
    31. fieldsV1:
    32. f:status:
    33. f:conditions:
    34. k:{"type":"ContainersReady"}:
    35. .: {}
    36. f:lastProbeTime: {}
    37. f:lastTransitionTime: {}
    38. f:status: {}
    39. f:type: {}
    40. k:{"type":"Initialized"}:
    41. .: {}
    42. f:lastProbeTime: {}
    43. f:lastTransitionTime: {}
    44. f:status: {}
    45. f:type: {}
    46. k:{"type":"Ready"}:
    47. .: {}
    48. f:lastProbeTime: {}
    49. f:lastTransitionTime: {}
    50. f:status: {}
    51. f:type: {}
    52. f:containerStatuses: {}
    53. f:hostIP: {}
    54. f:phase: {}
    55. f:podIP: {}
    56. f:podIPs:
    57. .: {}
    58. k:{"ip":"10.40.0.4"}:
    59. .: {}
    60. f:ip: {}
    61. f:startTime: {}
    62. manager: kubelet
    63. operation: Update
    64. time: "2021-01-13T04:39:23Z"
    65. name: my-cmd
    66. namespace: default
    67. resourceVersion: "429073"
    68. uid: 15d9f4f2-1fc8-4595-a00e-f96f52038ef9
    69. spec:
    70. containers:
    71. - image: 192.168.80.84:5000/centos_cmd:v1
    72. imagePullPolicy: IfNotPresent
    73. name: my-cmd
    74. resources: {}
    75. terminationMessagePath: /dev/termination-log
    76. terminationMessagePolicy: File
    77. volumeMounts:
    78. - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
    79. name: default-token-s9dfj
    80. readOnly: true
    81. dnsPolicy: ClusterFirst
    82. enableServiceLinks: true
    83. nodeName: k8s-node02
    84. preemptionPolicy: PreemptLowerPriority
    85. priority: 0
    86. restartPolicy: Always
    87. schedulerName: default-scheduler
    88. securityContext: {}
    89. serviceAccount: default
    90. serviceAccountName: default
    91. terminationGracePeriodSeconds: 30
    92. tolerations:
    93. - effect: NoExecute
    94. key: node.kubernetes.io/not-ready
    95. operator: Exists
    96. tolerationSeconds: 300
    97. - effect: NoExecute
    98. key: node.kubernetes.io/unreachable
    99. operator: Exists
    100. tolerationSeconds: 300
    101. volumes:
    102. - name: default-token-s9dfj
    103. secret:
    104. defaultMode: 420
    105. secretName: default-token-s9dfj
    106. status:
    107. conditions:
    108. - lastProbeTime: null
    109. lastTransitionTime: "2021-01-13T03:32:42Z"
    110. status: "True"
    111. type: Initialized
    112. - lastProbeTime: null
    113. lastTransitionTime: "2021-01-13T04:39:23Z"
    114. status: "True"
    115. type: Ready
    116. - lastProbeTime: null
    117. lastTransitionTime: "2021-01-13T04:39:23Z"
    118. status: "True"
    119. type: ContainersReady
    120. - lastProbeTime: null
    121. lastTransitionTime: "2021-01-13T03:32:42Z"
    122. status: "True"
    123. type: PodScheduled
    124. containerStatuses:
    125. - containerID: docker://d7fee9118b0d5d2ccaa346d4cd97130a9f744e9bf6ee1b1ae32dfa0e583c2b41
    126. image: 192.168.80.84:5000/centos_cmd:v1
    127. imageID: docker-pullable://192.168.80.84:5000/centos_cmd@sha256:948479967390e7a98979d4b98beec6dfa3fc92c6ce832ece882e8b1843e0779f
    128. lastState:
    129. terminated:
    130. containerID: docker://0e6a82fe9e50924b7254fe06f131e43f3f66d8007de5524e31af38c6abd05d51
    131. exitCode: 0
    132. finishedAt: "2021-01-13T04:39:21Z"
    133. reason: Completed
    134. startedAt: "2021-01-13T04:22:42Z"
    135. name: my-cmd
    136. ready: true
    137. restartCount: 4
    138. started: true
    139. state:
    140. running:
    141. startedAt: "2021-01-13T04:39:22Z"
    142. hostIP: 192.168.80.83
    143. phase: Running
    144. podIP: 10.40.0.4
    145. podIPs:
    146. - ip: 10.40.0.4
    147. qosClass: BestEffort
    148. startTime: "2021-01-13T03:32:42Z"
    149. # 对一个字段的含义不清楚的话,可以使用"kubectl explain"来查看某一字段的含义
  2. 将本地网络中的端口转发给pod中的端口

    首先我们可以使用一个nginx镜像:

    1. # 我已经先将nginx:alpine的镜像推到了本地仓库
    2. 关于alpine版本
    3. 早先的alpine版本的镜像还有这段注释,但是后来大多数都给删掉了,特此记录
    4. ?```
    5. postgres:<version>-alpine
    6. This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.
    7. This variant is highly recommended when final image size being as small as possible is desired. The main caveat to note is that it does use musl libc instead of glibc and friends, so certain software might run into issues depending on the depth of their libc requirements. However, most software doesn't have an issue with this, so this variant is usually a very safe choice. See this Hacker News comment thread for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.
    8. To minimize image size, it's uncommon for additional related tools (such as git or bash) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see the alpine image description for examples of how to install packages if you are unfamiliar).
    9. ?```

kubectl port-forward mynginx 8000:8080
这里设置的是端口转发,允许我们不通过service的方式来和某个特定的pod进行通信

  1. 3. 停止和移除Pod
  2. ```kubectl delete <podName>```
  3. ***
  4. ### 使用标签组织pod
  5. > 标签同样是k8s资源中最重要的概念之一,很多功能的实现都需要依靠标签选择器
  6. 1. yaml文件中指定标签
  7. ```yaml
  8. apiVersion: v1
  9. kind: Pod
  10. metadata:
  11. name: mynginx
  12. labels: # 一个资源可以分配多个标签
  13. app: nginx
  14. rel: alpine
  15. spec:
  16. ......
  1. 查看资源时显示标签

    正常查看资源时是不显示标签的,通过-o wide我们可以看到pod所在的节点和pod的ip,而通过“--show labels”参数,我们可以看到资源的标签

    1. [root@k8s-master01 centos]# kubectl get po --show-labels
    2. NAME READY STATUS RESTARTS AGE LABELS
    3. getname-deploy-68bd4cc6b4-j7gxz 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    4. getname-deploy-68bd4cc6b4-pt2cb 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    5. getname-deploy-68bd4cc6b4-srqfn 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    6. my-cmd-labels 1/1 Running 0 11s app=nginx,rel=alpine # 这里是刚才我所打标签的pod
    7. # 可能会发现我前面还有三个带标签的pod,这三个pod不是我使用这种方法创建的
    8. # 实际上这三个pod是我创建的一个rs创建的
    9. # 所以说标签在k8s管理资源中的用处很大
  2. 查看指定标签

    我们可能只对一些标签感兴趣,那么我们可以通过“-L <标签键名>”来只显示指定标签

    1. [root@k8s-master01 centos]# kubectl get po -L app
    2. NAME READY STATUS RESTARTS AGE APP
    3. getname-deploy-68bd4cc6b4-j7gxz 1/1 Running 4 6d21h getname
    4. getname-deploy-68bd4cc6b4-pt2cb 1/1 Running 4 6d21h getname
    5. getname-deploy-68bd4cc6b4-srqfn 1/1 Running 4 6d21h getname
    6. my-cmd-labels 1/1 Running 0 6m46s nginx
  3. 修改现有标签

    1. # 使用 kubectl label <resourceName> <instanceName> <labelKey>=<labelValue>,<labelKey>=<labelValue> 来添加新的标签
    2. [root@k8s-master01 centos]# kubectl label po my-cmd-labels node=node1
    3. pod/my-cmd-labels labeled
    4. [root@k8s-master01 centos]# kubectl get po --show-labels
    5. NAME READY STATUS RESTARTS AGE LABELS
    6. my-cmd-labels 1/1 Running 0 11m app=nginx,node=node1,rel=alpine # 发现已经增加了新标签
    7. # 需要修改旧标签,要添加“--overwrite”参数
    8. [root@k8s-master01 centos]# kubectl label po my-cmd-labels rel=stable --overwrite
    9. pod/my-cmd-labels labeled
    10. [root@k8s-master01 centos]# kubectl get po --show-labels
    11. NAME READY STATUS RESTARTS AGE LABELS
    12. fortune-env 2/2 Running 8 7d4h <none>
    13. my-cmd-labels 1/1 Running 0 13m app=nginx,node=node1,rel=stable # 发现rel标签已经重写完成
  4. 使用标签选择器列出期望Pod

    我们可不可以只显示特定标签的pod呢

    1. # 我们可以使用"-l"参数,来使用标签选择器
    2. [root@k8s-master01 centos]# kubectl get po -l rel=stable --show-labels
    3. NAME READY STATUS RESTARTS AGE LABELS
    4. my-cmd-labels 1/1 Running 1 20m app=nginx,node=node1,rel=stable
    5. 标签选择器当然不会只能根据特定的标签对来筛选资源
    6. # 我们可以光指定标签的key,这样就会显示所有包含该标签的资源
    7. [root@k8s-master01 centos]# kubectl get po -l app --show-labels
    8. NAME READY STATUS RESTARTS AGE LABELS
    9. getname-deploy-68bd4cc6b4-j7gxz 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    10. getname-deploy-68bd4cc6b4-pt2cb 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    11. getname-deploy-68bd4cc6b4-srqfn 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    12. my-cmd-labels 1/1 Running 1 24m app=nginx,node=node1,rel=stable
    13. # 我们可以使用!=或!来筛选不包含某标签或某标签对的资源
    14. # 需要注意的是,当你在筛选器中使用符号时,你应该在两边加上引号,否则shell无法理解你想要做什么
    15. [root@k8s-master01 centos]# kubectl get po -l '!node' --show-labels
    16. NAME READY STATUS RESTARTS AGE LABELS
    17. fortune-env 2/2 Running 8 7d4h <none>
    18. getname-deploy-68bd4cc6b4-j7gxz 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    19. getname-deploy-68bd4cc6b4-pt2cb 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    20. getname-deploy-68bd4cc6b4-srqfn 1/1 Running 4 6d21h app=getname,pod-template-hash=68bd4cc6b4
    21. [root@k8s-master01 centos]# kubectl get po -l "app!=getname" --show-labels
    22. NAME READY STATUS RESTARTS AGE LABELS
    23. my-cmd-labels 1/1 Running 1 27m app=nginx,node=node1,rel=stable
    24. # 我们还可以使用in ()和 notin()来对标签对进行更复杂的筛选
    25. [root@k8s-master01 centos]# kubectl get po -l "app in (nginx)" --show-labels
    26. NAME READY STATUS RESTARTS AGE LABELS
    27. my-cmd-labels 1/1 Running 1 30m app=nginx,node=node1,rel=stable
    28. [root@k8s-master01 centos]# kubectl get po -l "app notin (getname)" --show-labels
    29. NAME READY STATUS RESTARTS AGE LABELS
    30. my-cmd-labels 1/1 Running 1 31m app=nginx,node=node1,rel=stable
    31. # 关于一次筛选多个条件,使用“,”分割
    32. [root@k8s-master01 centos]# kubectl get po -l app=nginx,node=node1 --show-labels
    33. NAME READY STATUS RESTARTS AGE LABELS
    34. my-cmd-labels 1/1 Running 1 32m app=nginx,node=node1,rel=stable

使用标签选择器将pod调度到指定node

上一节中写了可以给资源打标签,而k8s中节点同样也是一种资源,我们可以通过给节点打标签的方式将pod运行到指定节点上

  1. # 先给节点打上标签
  2. [root@k8s-master01 centos]# kubectl label node k8s-node01 node=node1
  3. node/k8s-node01 labeled
  4. [root@k8s-master01 centos]# kubectl label node k8s-node02 node=node2
  5. node/k8s-node02 labeled
  6. # 来查看一下
  7. [root@k8s-master01 centos]# kubectl get node -L node
  8. NAME STATUS ROLES AGE VERSION NODE
  9. k8s-master01 Ready control-plane,master 18d v1.20.1
  10. k8s-node01 Ready <none> 18d v1.20.1 node1
  11. k8s-node02 Ready <none> 18d v1.20.1 node2
  12. # 现在节点已经成功给两个node打上标签了

接下来我们来编辑yaml文件,来将pod分配到指定节点上

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: my-cmd-node1
  5. spec:
  6. nodeSelector: # 在这里设置一个节点选择器
  7. node: "node1" # 只会被分配到节点标签含有“node=node1”的节点上
  8. containers:
  9. - name: my-cmd-node1
  10. image: 192.168.80.84:5000/centos_cmd:v1
  11. --- # 在一个yaml文件中可以使用“---”来一次创建多个资源
  12. apiVersion: v1
  13. kind: Pod
  14. metadata:
  15. name: my-cmd-node2
  16. spec:
  17. nodeSelector:
  18. node: "node2"
  19. containers:
  20. - name: my-cmd-node2
  21. image: 192.168.80.84:5000/centos_cmd:v1

来看一下执行结果

  1. [root@k8s-master01 centos]# kubectl get po -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. my-cmd-node1 1/1 Running 0 12s 10.32.0.8 k8s-node01 <none> <none>
  4. my-cmd-node2 1/1 Running 0 12s 10.40.0.6 k8s-node02 <none> <none>
  5. # 发现预设的pod确实分配到了期望的node上

关于命名空间

命名空间是一种在资源之上更高层面的作用域

这样可以允许我们多次使用相同的资源名称,也可以将一些系统层面的资源和用户层面的相隔离

  1. 查看命名空间

    命名空间也是一种资源,我们同样可以使用get来查看

    1. # 可以使用ns来简写namespace
    2. [root@k8s-master01 centos]# kubectl get ns
    3. NAME STATUS AGE
    4. default Active 18d
    5. kube-node-lease Active 18d
    6. kube-public Active 18d
    7. kube-system Active 18d
    8. # 可以使用"-n <namespaceName>"来指定命名空间
    9. [root@k8s-master01 centos]# kubectl get po -n kube-system
    10. NAME READY STATUS RESTARTS AGE
    11. coredns-7f89b7bc75-9z9g8 1/1 Running 13 18d
    12. coredns-7f89b7bc75-dmhjl 1/1 Running 13 18d
    13. etcd-k8s-master01 1/1 Running 26 18d
    14. kube-apiserver-k8s-master01 1/1 Running 26 18d
    15. kube-controller-manager-k8s-master01 1/1 Running 30 18d
    16. kube-proxy-s2rmh 1/1 Running 13 18d
    17. kube-proxy-wq2kz 1/1 Running 13 18d
    18. kube-proxy-wvcgk 1/1 Running 24 18d
    19. kube-scheduler-k8s-master01 1/1 Running 26 18d
    20. weave-net-9lhgf 2/2 Running 37 18d
    21. weave-net-dhv26 2/2 Running 36 18d
    22. weave-net-q95gm 2/2 Running 65 18d
    23. # 这里其实也可以看出k8s原理中的一条,即:
    24. # k8s中只用node的kubelet以实际进程的方式存在,其他的都是以pod的形式存在
    25. # 这里可以看到 etcd、apiserver、proxy、schedule、controller等
  2. 创建命名空间

    既可以使用命令kubectl create namespace <namespaceName>来创建一个命名空间

    也可以通过编写yaml文件的方式

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: custom-namespace
    5. # 然后使用kubectl create -f 来创建
  3. 指定命名空间创建对象

    默认情况下我们是在default中创建资源的,通过“-n ”来指定命名空间

  4. 使用标签选择器删除pod

    1. # 仍然是通过"-l"来指定标签选择器
    2. kubectl delete pod -l "app=nginx"
  5. 删除整个命名空间

    kubectl delete ns <namespaceName>

    删除命名空间后,会删除其内的所有资源

  6. 删除所有pod,保留命名空间

    kubectl delete po -all -ns <namespaceName>

  7. 删除命名空间内的所有资源,保留命名空间

    kubectl delete all -all -ns <namespaceName>

原文链接:http://www.cnblogs.com/panbaishou/p/14435576.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号