Kubernetes(2) k8s资源,以及使用配置文件部署 Pod资源

本文会从资源基础定义开始讲起,在后面提供实验操作步骤。

在本章结束后应该对k8s 资源有个大概的理解,且自己手动通过模板定义的方法创建一个包括两个容器的 Pod 资源。

1 Kubernetes 中资源分类

详见 https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#-strong-api-overview-strong-

Workload 类型

主要用于管理且运行集群中的容器,包括:

  • Container
  • Cronjob
  • DaemonSet
  • Deployment
  • Job
  • Pod
  • ReplicaSet
  • ReplicationController
  • StatefulSet

Service

该种类服务主要用于对于 Workload 资源的补充,比如提供对外服务,负载均衡。

  • Service Discovery
  • Load-Balance
  • Ingress

Config and Storage

该种类服务主要用于在应用中注入数据,以及对容器提供外部持久化数据。

  • ConfigMap
  • Secret
  • PersistentVolumeClaim
  • StorageClass
  • Volume
  • VolumeAttachment
  • CSI
  • ...

Metadata

元数据用于配置集群内部资源行为,如:HorizontalPodAutoscaler for scaling workloads.

  • PodTemplate
  • LimitRange
  • Event
  • HorizontalPodAutoscaler(HPA)
  • ...

Cluster

该资源定义了集群是如何配置的,通常是管理员权限级别的操作

  • Namespace
  • Node
  • Role
  • ClusterRole
  • Persisitentvolume
  • RoleBinding
  • ClusterRoleBinding
  • ...

2. 提取Kubernetes 资源信息

展示运行的 Pod 资源

[root@k8smaster ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-79976cbb47-8dqnk   1/1     Running   0          3h
nginx-79976cbb47-p247g   1/1     Running   0          3h
nginx-79976cbb47-ppbqv   1/1     Running   0          3h

展示 Pod 如何配置的 (-o yaml 将输出转换为 yaml 格式):

[root@k8smaster ~]# kubectl get pod nginx-79976cbb47-ppbqv -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2019-01-03T13:34:16Z"
  generateName: nginx-79976cbb47-
  labels:
    pod-template-hash: "3553276603"
    run: nginx
  name: nginx-79976cbb47-ppbqv
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-79976cbb47
    uid: 43cecba5-0f5c-11e9-8668-000c297191df
  resourceVersion: "39591"
  selfLink: /api/v1/namespaces/default/pods/nginx-79976cbb47-ppbqv
  uid: 43d50008-0f5c-11e9-8668-000c297191df
spec:
  containers:
  - image: nginx:1.14-alpine
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-rxs5t
      readOnly: true
  dnsPolicy: ClusterFirst
  nodeName: k8snode1
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-rxs5t
    secret:
      defaultMode: 420
      secretName: default-token-rxs5t
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-01-03T13:34:16Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-01-03T13:34:18Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: null
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-01-03T13:34:16Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://3d438e181572b2072cee7c7794914e94ee1df133d06cf32193d964c29f879525
    image: nginx:1.14-alpine
    imageID: docker-pullable://nginx@sha256:e3f77f7f4a6bb5e7820e013fa60b96602b34f5704e796cfd94b561ae73adcf96
    lastState: {}
    name: nginx
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-01-03T13:34:17Z"
  hostIP: 172.16.0.12
  phase: Running
  podIP: 10.244.1.6
  qosClass: BestEffort
  startTime: "2019-01-03T13:34:16Z"

3 资源对象

通常资源对象有三种组件:

  • Resource ObjectMeta: 这种信息主要描述资源的元信息比如:名字,类型,api 版本,标注,标签等。这些属性可以使用户自定义也可以是系统自动分配的
  • ResourceSpec: 主要有用户自定义,描述了对系统期望的状态,且应用于创建或更新资源时
  • ResourceStatus: 主要有系统自动填写,用于显示当前系统状态等信息。通常用户不需要自己手动更改

4 资源模板的格式

4.1 API 名称

  • apiVersion
  • kind
  • spec
  • metadata
  • etc.

4.2 组名称

  • core (by default, if it is not mentioned)
  • apps
  • batch
  • extensions
  • ...

4.3 版本号r

  • v1 (stable version)
  • v1beta
  • ...

例如 apiVersion: v1 表示: apiVersion API 名称; group 为 core (by default); version 为 v1

5 第一级别 Attributes for K8s模板

这里是 5 种常见模板attributes, 前四个应该由用户自定义:

  • apiVersion: group/version
  • kind: 资源类型-如:pod, service, deployment 等.
  • metadata: 元信息如:name, namespace, labels, annotations (optional)
  • spec (期望状态;disired state)
  • status (由 kubernetes 自动维护)

用户可使用 "kubectl explain {resource type}" 展示详细信息

例如展示 Pod 定义信息:

[root@k8smaster ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion    <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind    <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata    
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec    
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status    
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status 
    
   

如果用户需要关于 metadata 更多信息 可以使用 kubectl explain resourcename.objectname.objectname...

如:

[root@k8smaster ~]# kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1

RESOURCE: metadata 

DESCRIPTION:
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

     ObjectMeta is metadata that all persisted resources must have, which
     includes all objects users must create.

FIELDS:
   annotations    string]string>
     Annotations is an unstructured key value map stored with a resource that
     may be set by external tools to store and retrieve arbitrary metadata. They
     are not queryable and should be preserved when modifying objects. More
     info: http://kubernetes.io/docs/user-guide/annotations

   clusterName    <string>
     The name of the cluster which the object belongs to. This is used to
     distinguish resources with same name and namespace in different clusters.
     This field is not set anywhere right now and apiserver is going to ignore
     it if set in create or update request.

   generation    
     A sequence number representing a specific generation of the desired state.
     Populated by the system. Read-only.

   labels    string]string>
     Map of string keys and values that can be used to organize and categorize
     (scope and select) objects. May match selectors of replication controllers
     and services. More info: http://kubernetes.io/docs/user-guide/labels

   name    <string>
     Name must be unique within a namespace. Is required when creating
     resources, although some resources may allow a client to request the
     generation of an appropriate name automatically. Name is primarily intended
     for creation idempotence and configuration definition. Cannot be updated.
     More info: http://kubernetes.io/docs/user-guide/identifiers#names

   namespace    <string>
     Namespace defines the space within each name must be unique. An empty
     namespace is equivalent to the "default" namespace, but "default" is the
     canonical representation. Not all objects are required to be scoped to a
     namespace - the value of this field for those objects will be empty. Must
     be a DNS_LABEL. Cannot be updated. More info:
     http://kubernetes.io/docs/user-guide/
selfLink <string> SelfLink is a URL representing this object. Populated by the system. Read-only. uid <string> UID is the unique in time and space value for this object. It is typically generated by the server on successful creation of a resource and is not allowed to change on PUT operations. Populated by the system. Read-only. More info: http://kubernetes.io/docs/user-guide/identifiers#uids
...

6 编写第一个 YAML 模板资源

创建一个新文件名为 pod-demo.yaml

touch pod-demo.yaml
vi pod-demo.yaml

 添加内如容下,该脚本会生成一个 Pod 其中包括两个容器

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: busybox
    image: busybox:latest
    command: 
    - "/bin/sh"
    - "-c"
    - "echo date >> /usr/share/nginx/html/index.html; sleep 5"

现在用以下操作创建 Pod:

[root@k8smaster ~]# kubectl create -f pod-demo.yaml 
pod/pod-demo created
[root@k8smaster
~]# kubectl get pods -w NAME READY STATUS RESTARTS AGE nginx-79976cbb47-8dqnk 1/1 Running 0 5h13m nginx-79976cbb47-p247g 1/1 Running 0 5h13m nginx-79976cbb47-ppbqv 1/1 Running 0 5h13m pod-demo 0/2 ContainerCreating 0 15s pod-demo 2/2 Running 0 15s pod-demo 1/2 Running 0 20s pod-demo 2/2 Running 1 24s pod-demo 1/2 Running 1 29s
^C[root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-79976cbb47-8dqnk 1/1 Running 0 5h nginx-79976cbb47-p247g 1/1 Running 0 5h nginx-79976cbb47-ppbqv 1/1 Running 0 5h pod-demo 1/2 Running 1 38s [root@k8smaster ~]# kubectl describe pod pod-demo Name: pod-demo Namespace: default Priority: 0 PriorityClassName: Node: k8snode1/172.16.0.12 Start Time: Thu, 03 Jan 2019 19:46:35 +0100 Labels: app=myapp tier=frontend Annotations: Status: Running IP: 10.244.1.10 Containers: myapp: Container ID: docker://b9cb870577649ea76da415c19b9b72276ee8803698c5a3178f7fa9955e3cf983 Image: ikubernetes/myapp:v1 Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513 Port: Host Port: State: Running Started: Thu, 03 Jan 2019 19:46:44 +0100 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-rxs5t (ro) busybox: Container ID: docker://b5a91b353122a176b80a0c2f487093337576de190b72fff6d7e0adaf00a389bc Image: busybox:latest Image ID: docker-pullable://busybox@sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0 Port: Host Port: Command: /bin/sh -c echo date >> /usr/share/nginx/html/index.html; sleep 5 State: Terminated Reason: Completed Exit Code: 0 Started: Thu, 03 Jan 2019 19:47:20 +0100 Finished: Thu, 03 Jan 2019 19:47:25 +0100 Last State: Terminated Reason: Completed Exit Code: 0 Started: Thu, 03 Jan 2019 19:46:58 +0100 Finished: Thu, 03 Jan 2019 19:47:03 +0100 Ready: False Restart Count: 2 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-rxs5t (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-rxs5t: Type: Secret (a volume populated by a Secret) SecretName: default-token-rxs5t Optional: false QoS Class: BestEffort Node-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Pulling 80s kubelet, k8snode1 pulling image "ikubernetes/myapp:v1" Normal Pulled 72s kubelet, k8snode1 Successfully pulled image "ikubernetes/myapp:v1" Normal Created 72s kubelet, k8snode1 Created container Normal Started 72s kubelet, k8snode1 Started container Normal Scheduled 53s default-scheduler Successfully assigned default/pod-demo to k8snode1 Normal Pulling 38s (x3 over 72s) kubelet, k8snode1 pulling image "busybox:latest" Normal Pulled 36s (x3 over 68s) kubelet, k8snode1 Successfully pulled image "busybox:latest" Normal Created 36s (x3 over 68s) kubelet, k8snode1 Created container Normal Started 36s (x3 over 68s) kubelet, k8snode1 Started container Warning BackOff 29s (x2 over 52s) kubelet, k8snode1 Back-off restarting failed container
[root@k8smaster
~]# kubectl describe pod pod-demo Name: pod-demo Namespace: default Priority: 0 PriorityClassName: Node: k8snode1/172.16.0.12 Start Time: Thu, 03 Jan 2019 19:46:35 +0100 Labels: app=myapp tier=frontend Annotations: Status: Running IP: 10.244.1.10 Containers: myapp: Container ID: docker://b9cb870577649ea76da415c19b9b72276ee8803698c5a3178f7fa9955e3cf983 Image: ikubernetes/myapp:v1 Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513 Port: Host Port: State: Running Started: Thu, 03 Jan 2019 19:46:44 +0100 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-rxs5t (ro) busybox: Container ID: docker://b5a91b353122a176b80a0c2f487093337576de190b72fff6d7e0adaf00a389bc Image: busybox:latest Image ID: docker-pullable://busybox@sha256:7964ad52e396a6e045c39b5a44438424ac52e12e4d5a25d94895f2058cb863a0 Port: Host Port: Command: /bin/sh -c echo date >> /usr/share/nginx/html/index.html; sleep 5 State: Terminated Reason: Completed Exit Code: 0 Started: Thu, 03 Jan 2019 19:47:20 +0100 Finished: Thu, 03 Jan 2019 19:47:25 +0100 Last State: Terminated Reason: Completed Exit Code: 0 Started: Thu, 03 Jan 2019 19:46:58 +0100 Finished: Thu, 03 Jan 2019 19:47:03 +0100 Ready: False Restart Count: 2 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-rxs5t (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-rxs5t: Type: Secret (a volume populated by a Secret) SecretName: default-token-rxs5t Optional: false QoS Class: BestEffort Node-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Pulling 92s kubelet, k8snode1 pulling image "ikubernetes/myapp:v1" Normal Pulled 84s kubelet, k8snode1 Successfully pulled image "ikubernetes/myapp:v1" Normal Created 84s kubelet, k8snode1 Created container Normal Started 84s kubelet, k8snode1 Started container Normal Scheduled 65s default-scheduler Successfully assigned default/pod-demo to k8snode1 Normal Pulling 50s (x3 over 84s) kubelet, k8snode1 pulling image "busybox:latest" Normal Pulled 48s (x3 over 80s) kubelet, k8snode1 Successfully pulled image "busybox:latest" Normal Created 48s (x3 over 80s) kubelet, k8snode1 Created container Normal Started 48s (x3 over 80s) kubelet, k8snode1 Started container Warning BackOff 41s (x2 over 64s) kubelet, k8snode1 Back-off restarting failed container

检查日志可以使用该命令: "kubectl logs pod-demo myapp"

[root@k8smaster ~]# curl 10.244.1.10
Hello MyApp | Version: v1 | "hostname.html">Pod Name
[root@k8smaster ~]# kubectl logs pod-demo myapp 10.244.0.0 - - [03/Jan/2019:18:52:20 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
[root@k8smaster
~]# kubectl logs pod-demo busybox /bin/sh: can't create /usr/share/nginx/html/index.html: nonexistent directory [root@k8smaster ~]#

* curl 10.244.1.10 该命令只用于生成一行日志(http get)

通过实验可见 busybox 容器尚未工作

通过下面命令进入运行的myapp 容器中"kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]"

[root@k8smaster ~]# kubectl exec -it pod-demo -c myapp -- /bin/sh
/ # whoami
root
/ # cd /usr/share/nginx/html/
/usr/share/nginx/html # ll
/bin/sh: ll: not found
/usr/share/nginx/html # ls
50x.html    index.html
/usr/share/nginx/html # cat index.html 
Hello MyApp | Version: v1 | "hostname.html">Pod Name

* 该操作不会成功,因为该Pod下有两个容器,但由于没有配置共同存储 busybox 不会共享它的容器存储系统。作为Workaround可以使用"echo date >> /usr/share/nginx/html/index.html; sleep 5" won't write anything in index.html (index.html 已经存在于myapp 容器中)

修改模板文件的最后一行为:

- "echo date >> /usr/share/nginx/html/index.html; sleep 5" to - "sleep 5000"

busybox container会睡眠 5000 秒然后结束...

附加一篇文章仅供参考: https://www.cnblogs.com/MyLifeMyWay/p/8493542.html

Done :-)

 

转载于:https://www.cnblogs.com/crazy-chinese/p/10223269.html

你可能感兴趣的:(Kubernetes(2) k8s资源,以及使用配置文件部署 Pod资源)