對於kubernetes 資源可使用加載 yaml 標記語言的方式,進行自定義:php
YAML(/ˈjæməl/,尾音相似camel駱駝)是一個可讀性高,用來表達數據序列化的格式。YAML參考了其餘多種語言,包括:C語言、Python、Perl,並從XML、電子郵件的數據格式(RFC 2822)中得到靈感。Clark Evans在2001年首次發表了這種語言[1],另外Ingy döt Net與Oren Ben-Kiki也是這語言的共同設計者[2]。當前已經有數種編程語言或腳本語言支持(或者說解析)這種語言。 YAML是"YAML Ain't a Markup Language"(YAML不是一種標記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思實際上是:"Yet Another Markup Language"(還是一種標記語言)[3],但爲了強調這種語言以數據作爲中心,而不是以標記語言爲重點,而用反向縮略語重命名。
使用YAML用於k8s的定義將給你一些好處,包括:node
YAML是一個JSON的超集,意味着任何有效JSON文件也都是一個有效的YAML文件。因此一方面,若是你知道JSON,你只是要去寫本身的YAML(而不是閱讀別人的)也就能夠了。另外一方面,不太可能,不幸的是,儘管你嘗試去網上找到例子,可是他們一般都不是JSON,因此咱們可能須要去習慣它。不過,有JSON的狀況下可能會更方便,這樣你將會很開心你懂得JSON。nginx
幸運的是,YAML只有兩種結構類型你須要知道:git
查看pod 資源清單web
[root@kube ~]# kubectl get pod nginx-app-7756966bc9-qrksb -o yaml
//這是一個經過 kubectl run 方式運行的 pod ,那麼咱們能夠手動定義一個 和這個相似的 yaml 配置文件進行加載而後生成pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2019-07-16T06:39:02Z"
generateName: nginx-app-7756966bc9-
labels:
pod-template-hash: 7756966bc9
run: nginx-app
name: nginx-app-7756966bc9-qrksb
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-app-7756966bc9
uid: 51997507-0ad5-4f71-86db-3ad6eff70171
resourceVersion: "603662"
selfLink: /api/v1/namespaces/default/pods/nginx-app-7756966bc9-qrksb
uid: 7035cfbd-a8f3-45a9-933c-4ce1cef45183
spec:
containers:
- image: nginx:alpine
imagePullPolicy: IfNotPresent
name: nginx-app
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-bsthb
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: kube.node1
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-bsthb
secret:
defaultMode: 420
secretName: default-token-bsthb
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2019-07-16T06:39:02Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2019-07-16T06:39:36Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2019-07-16T06:39:36Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2019-07-16T06:39:02Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://6f50a8038d1f9b484c3af46fa21ef0bdce963b95c4f7f5980302bc70ca46eea6
image: nginx:alpine
imageID: docker-pullable://nginx@sha256:17bd1698318e9c0f9ba2c5ed49f53d690684dab7fe3e8019b855c352528d57be
lastState: {}
name: nginx-app
ready: true
restartCount: 0
state:
running:
startedAt: "2019-07-16T06:39:36Z"
hostIP: 10.2.61.22
phase: Running
podIP: 10.244.2.7
qosClass: BestEffort
startTime: "2019-07-16T06:39:02Z"
[root@kube ~]#
針對yaml 格式咱們對以下進行分析:docker
#maps 是key|value 的組合,list 是key[args,args ,多個項的組合]用破折號(-)開頭
apiVersion: v1 //maps kind: Pod //maps metadata: name: rss-site //maps labels: app: web /maps spec: //maps containers: //list name - name: front-end //list參數子項是maps image: nginx //list 參數子項是maps ports: - containerPort: 80 //list 參數子項maps - name: rss-reader image: nickchase/rss-php-nginx:v1 ports: - containerPort: 88
建立一個簡單的yaml 文件編程
root@kube test]# cat pod-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-test namespace: default labels: app: myapp tier: frontend spec: containers: - name: mytest image: nginx:latest [root@kube test]#
[root@kube test]# kubectl create -f pod-demo.yaml
pod/pod-test created
[root@kube test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busy1-78c9f4b47-pm2qx 0/1 CrashLoopBackOff 22 100m
busy2-7f9dbf96d6-2d778 1/1 Running 0 2d23h
busy3-9877c76bf-ts5rl 1/1 Running 0 2d23h
nginx-7bb7cd8db5-6sgvp 1/1 Running 0 3d
nginx-app-54c844949f-6zlmr 0/1 ContainerCreating 0 41h
nginx-app-54c844949f-n82d6 1/1 Running 0 2d
nginx-app-54c844949f-xmkb8 1/1 Running 0 2d
nginx-app-7756966bc9-qrksb 1/1 Running 0 2d2h
pod-test 0/1 ContainerCreating 0 1s
[root@kube test]#
[root@kube test]# kubectl describe pod pod-test Name: pod-test Namespace: default Priority: 0 Node: kube.node1/10.2.61.22 Start Time: Thu, 18 Jul 2019 16:44:23 +0800 Labels: app=myapp tier=frontend Annotations: <none> Status: Running IP: 10.244.2.12 Containers: mytest: Container ID: docker://4e40fc5b6cae440881cea707b79c2d17692c47d75df69a571fa0e7d57dff5e3a Image: nginx:latest Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb Port: <none> Host Port: <none> State: Running Started: Thu, 18 Jul 2019 16:44:46 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-bsthb (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-bsthb: Type: Secret (a volume populated by a Secret) SecretName: default-token-bsthb Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 62s default-scheduler Successfully assigned default/pod-test to kube.node1 Normal Pulling 61s kubelet, kube.node1 Pulling image "nginx:latest" Normal Pulled 40s kubelet, kube.node1 Successfully pulled image "nginx:latest" Normal Created 40s kubelet, kube.node1 Created container mytest Normal Started 39s kubelet, kube.node1 Started container mytest [root@kube test]#
[root@kube ~]# kubectl api-versions admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1beta1 apiregistration.k8s.io/v1 apiregistration.k8s.io/v1beta1 apps/v1 apps/v1beta1 apps/v1beta2 authentication.k8s.io/v1 authentication.k8s.io/v1beta1 authorization.k8s.io/v1 authorization.k8s.io/v1beta1 autoscaling/v1 autoscaling/v2beta1 autoscaling/v2beta2 batch/v1 batch/v1beta1 certificates.k8s.io/v1beta1 coordination.k8s.io/v1 coordination.k8s.io/v1beta1 events.k8s.io/v1beta1 extensions/v1beta1 networking.k8s.io/v1 networking.k8s.io/v1beta1 node.k8s.io/v1beta1 policy/v1beta1 rbac.authorization.k8s.io/v1 rbac.authorization.k8s.io/v1beta1 scheduling.k8s.io/v1 scheduling.k8s.io/v1beta1 storage.k8s.io/v1 storage.k8s.io/v1beta1 v1 [root@kube ~]# kubectl explain pods 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 <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status status <Object> 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 [root@kube ~]#