Kubernetes資源建立yml語法

前言

在是用kubernetes中,咱們對資源的建立大部分都是經過html

1
kubelet create -f RESOURCE.yaml

 

剛開看的時候難免有一些迷茫,看不懂語法,不知道怎麼寫;今天本文就介紹一下kubernetes construct語法。nginx

Construct語法其實就是由kubelet格式化成API的post data,提交給apiserver,所以這裏支持yaml,json兩種數據結構的文件。docker

Pod資源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apiVersion: v1 指定api版本,此值必須在kubectl apiversion中
kind: Pod 指定建立資源的角色/類型
metadata: 資源的元數據/屬性
name: test 資源的名字,在同一個namespace中必須惟一
labels: 設定資源的標籤
sex: boy 標籤以key/value的結構存在
age: 18
spec: #specification of the resource content 指定該資源的內容
restartPolicy: Never 代表改容器僅僅運行一次,默認k8s的策略,在此容器退出後,會當即建立一個相同的容器
volumes: 定義一組掛載設備
- name: volume 定義一個掛載設備的名字
hostPath: /data/www/html 掛載設備類型爲hostPath,路徑爲宿主機下的/data/www/html,這裏設備類型支持不少種
containers: 指定資源中的容器
- name: container1 容器的名字
image: 「docker.coocla.org/ubuntu:last」 容器使用的鏡像地址
volumeMounts:
- mountPath: /mnt 掛載到容器的某個路徑下
name: volume 掛載設備的名字,與volumes[*].name 須要對應
livenessProbe: 容器健康監測
httpGet: http形式監測,返回200-399之間,則認爲容器正常
path: /health
port: 8080
initialDelaySeconds: 15 代表第一次檢測在容器啓動後多長時間後開始
timeoutSeconds: 1 檢測的超時時間
env: 指定容器中的環境變量
- name: str 變量的名字
value: "hello world」 變量的值
command: ["/bin/bash", "-c"] 覆蓋容器中的Entrypoint,對應Dockefile中的ENTRYPOINT
args: ["/bin/echo", "$(str)"] 對應Dockerfile中CMD參數

健康監測還支持另一種方法:json

1
2
3
4
5
6
exec: 執行命令的方法進行監測,若是其退出碼不爲0,則認爲容器正常
command:
- cat
- /tmp/health
initialDelaySeconds: 15
timeoutSeconds: 1

 

ReplicationController的語法參數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 2 指定rc中pod的個數
template: 指定rc中pod的模板,rc中的pod都是按照這個模板來建立的
metadata: 指定rc中pod的元數據,注意這裏不須要在指定pod的名字,它由rc複製生成
labels:
app: nginx
spec:
container:
- name: nginx
image: nginx

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: Service
metadata:
name: nginxsvc
labels:
app: nginx
spec: 指定Service中的內容
ports: 映射列表
- port: 80 service的端口
porotocal: TCP 映射的協議類型,支持TCP/UDP
targetPort: 80 映射到pod的端口
name: www.baidu.com 該映射的名字
selector: 匹配器
port: 80
app: nginx 匹配label中app爲nginx,port爲80的pod

Secret

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: 0paque 定義secret的類型,這裏支持三種類型
password: xxx|base64 以key/value的形式定義,value須要通過base64編碼才能夠,在secret被掛載到container中後,會以key做爲文件名,value的值通過base64解碼做爲內容,以文件的形式存在於container中
username: xxx|base64
 
---
type: kubernetes.io/service-account-token 第二種secret類型,用做建立服務帳號的token,用做進程間通訊的認證
 
---
type: kubernetes.io/dockercfg 第三種secret類型,用做在建立container,對docker registry的認證
data:
.dockercfg: `cat ~/.dockercfg | base64`

以上爲部分資源參數,固然還有更多的參數能夠指定,及更多的資源能夠經過定義construct來建立。ubuntu

相關文章
相關標籤/搜索