k8s -- 編寫資源清單

資源清單格式:

採用標記性語言.

格式如下:
• apiVersion: group/version : 指明api資源屬於哪個羣組和版本,同一個組可以有多個版本

[root@server2 manifest]# kubectl api-versions		//查詢命令
admissionregistration.k8s.io/v1			//前面是羣組,後面是版本
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
。。。	
v1				裏面有很多,一般情況下,我們寫v1 就行了
  • kind : 標記創建的資源類型,k8s主要支持以下資源類別

    • Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob
  • metadata: 元數據

    • name: 對像名稱
    • namespace: 對象屬於哪個命名空間,默認是default
    • labels: 指定資源標籤,標籤是一種鍵值數據
  • spec: 定義目標資源的期望狀態

我們可以使用 kubectl explain 來查詢幫助文檔:
例如:

[root@server2 manifest]# 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/sig-architecture/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/sig-architecture/api-conventions.md#types-kinds

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

   spec	<Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/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/sig-architecture/api-conventions.md#spec-and-status

更具體的:
[root@server2 manifest]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

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

     PodSpec is a description of a pod.

FIELDS:
   automountServiceAccountToken	<boolean>
     AutomountServiceAccountToken indicates whether a service account token
     should be automatically mounted.

   containers	<[]Object> -required-		# 加required的是必須要有的
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot be
     updated.

  • 資源清單中必須存在字段
    在這裏插入圖片描述

  • k8s主要對象

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

  • k8s 常用的額外參數

在這裏插入圖片描述

我們先清除之前的一些操作數據:

[root@server2 manifest]# kubectl delete svc myapp 
service "myapp" deleted
[root@server2 manifest]# kubectl delete pod demo 
pod "demo" deleted
[root@server2 manifest]# kubectl delete deployments.apps myapp 
deployment.apps "myapp" deleted
[root@server2 ~]# cd manifest/		# 創建清單目錄
[root@server2 manifest]# cd ..
[root@server2 manifest]#

創建自主式pod清單

[root@server2 manifest]# vim pod.yml 
[root@server2 manifest]# cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
#  namespace: default		# 可以不寫,默認指定default 命名空間
#  labels:			# 標籤頁可以不寫
spec:
  containers:
    - name: myapp
      image: myapp:v1
[root@server2 manifest]# kubectl create -f pod.yml 
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          67s   10.244.1.31   server3   <none>           <none>
[root@server2 manifest]# curl 10.244.1.31
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

最簡單的一個pod資源清單就創建好了。

  • 刪除:
[root@server2 manifest]# kubectl delete -f pod.yml 
pod "myapp" deleted
[root@server2 manifest]# kubectl get pod -o wide
No resources found in default namespace.
  • 更新:
[root@server2 manifest]# vim pod.yml 
spec:
  containers:
    - name: myapp
      image: myapp:v2		//這裏替換爲v2
[root@server2 manifest]# kubectl create -f pod.yml 
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          9s    10.244.1.32   server3   <none>           <none>
[root@server2 manifest]# curl 10.244.1.32
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@server2 manifest]# kubectl delete -f pod.yml 
pod "myapp" deleted

生產環境中不加控制器的pod 很少見

加控制器的清單

[root@server2 manifest]# vim pod2.yml
[root@server2 manifest]# cat pod2.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3		//加了控制器,三個副本
  selector:			//選擇器,控制器選擇指定的pod進行控管
    matchLabels:	//通過匹配標籤控管
      app: nginx	
  template:			//模板,通過模板創建容器,因爲我們有三個副本
    metadata:
      labels:
        # Apply this label to pods and default
        # the Deployment label selector to this value
        app: nginx		//創建一個標籤
    spec:
      containers:
      - name: nginx		
        # Run this image
        image: myapp:v1

帶控制器的清單我們建議用apply 運行:
[root@server2 manifest]# kubectl apply -f pod2.yml 
get podeployment.apps/deployment-example created
[root@server2 manifest]# kubectl get pods --show-labels 
NAME                                  READY   STATUS    RESTARTS   AGE   LABELS
deployment-example-7d5c95894c-8wmp6   1/1     Running   0          31s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-kjm48   1/1     Running   0          31s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-rwlmf   1/1     Running   0          31s   app=nginx,pod-template-hash=7d5c95894c
  • 拉伸
[root@server2 manifest]# vim pod2.yml 
spec:
  # 3 Pods should exist at all times.
  replicas: 6		// 改爲6
[root@server2 manifest]# kubectl apply -f pod2.yml 
deployment.apps/deployment-example configured
[root@server2 manifest]# kubectl get pods --show-labels 
NAME                                  READY   STATUS              RESTARTS   AGE   LABELS
deployment-example-7d5c95894c-7wmwl   0/1     ContainerCreating   0          1s    app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-88cqt   0/1     ContainerCreating   0          1s    app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-8wmp6   1/1     Running             0          86s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-kjm48   1/1     Running             0          86s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-q4vfw   0/1     ContainerCreating   0          1s    app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-rwlmf   1/1     Running             0          86s   app=nginx,pod-template-hash=7d5c95894c
  • 滾動更新
[root@server2 manifest]# kubectl get pods -o wide
NAME                                  READY   STATUS    RESTARTS   AGE     IP            NODE      NOMINATED NODE   READINESS GATES
deployment-example-7d5c95894c-7wmwl   1/1     Running   0          88s     10.244.1.37   server3   <none>           <none>
...
[root@server2 manifest]# curl 10.244.1.37
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

[root@server2 manifest]# vim pod2.yml 
      - name: nginx		
        # Run this image
        image: myapp:v2			//鏡像換成v2
[root@server2 manifest]# kubectl apply -f pod2.yml 
deployment.apps/deployment-example configured 
[root@server2 manifest]# kubectl get pods -o wide
NAME                                 READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
deployment-example-86774dbbb-27mvb   1/1     Running   0          41s   10.244.2.15   server4   <none>           <none>
deployment-example-86774dbbb-5tpvf   1/1     Running   0          48s   10.244.2.12   server4   <none>           <none>
deployment-example-86774dbbb-b65c2   1/1     Running   0          47s   10.244.2.13   server4   <none>           <none>
deployment-example-86774dbbb-mx7kz   1/1     Running   0          41s   10.244.2.14   server4   <none>           <none>
deployment-example-86774dbbb-pwwmm   1/1     Running   0          48s   10.244.1.38   server3   <none>           <none>
deployment-example-86774dbbb-tjcd4   1/1     Running   0          43s   10.244.1.39   server3   <none>           <none>
[root@server2 manifest]# curl 10.244.2.15
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章