Kubernetes學習之路(六)之建立K8S應用

  • 1、Deployment的概念

K8S自己並不提供網絡的功能,因此須要藉助第三方網絡插件進行部署K8S中的網絡,以打通各個節點中容器的互通。 
POD,是K8S中的一個邏輯概念,K8S管理的是POD,一個POD中包含多個容器,容器之間經過localhost互通。而POD須要ip地址。每一個POD都有一個標籤 
html

POD–>RC–>RS–>Deployment (發展歷程)node

Deployment,表示用戶對K8S集羣的一次更新操做。Deployment是一個比RS應用模式更廣的API對象。用於保證Pod的副本的數量。 linux

能夠是建立一個新的服務,更新一個新的服務,也能夠是滾動升級一個服務。滾動升級一個服務。實際是建立一個新的RS,而後將新的RS中副本數增長到理想狀態,將舊的RS中的副本數減少到0的複合操做; 這樣的一個複合操做用一個RS是不太好描述的,因此用一個更通用的Deployment來描述。 nginx

RC、RS和Deployment只是保證了支撐服務的POD數量,可是沒有解決如何訪問這些服務的問題。一個POD只是一個運行服務的實例,隨時能夠能在一個節點上中止,在另外一個節點以一個新的IP啓動一個新的POD,所以不能以肯定的IP和端口號提供服務。 docker

要穩定地提供服務須要服務發現和負載均衡能力。服務發現完成的工做,是針對客戶端訪問的服務,找到對應的後端服務實例。 vim

在K8S的集中當中,客戶端須要訪問的服務就是Service對象。每一個Service會對應一個集羣內部有效的虛擬IP,集羣內部經過虛擬IP訪問一個服務。後端

  • 2、建立K8S的第一個應用

[root@linux-node1 ~]# kubectl run net-test --image=alpine --replicas=2 sleep 36000  #建立名稱爲net-test的應用,鏡像指定爲alpine,副本數爲2個
deployment.apps "net-test" created
[root@linux-node1 ~]# kubectl get pod -o wide  #查看pod的狀態信息,此時是API Server從etcd中讀取這些數據
NAME                        READY     STATUS              RESTARTS   AGE       IP          NODE
net-test-7b949fc785-2v2qz   1/1       Running             0          56s       10.2.87.2   192.168.56.120
net-test-7b949fc785-6nrhm   0/1       ContainerCreating   0          56s       <none>      192.168.56.130
[root@linux-node1 ~]# kubectl get deployment net-test
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
net-test   2         2         2            2           22h
kubectl get deployment命令能夠查看net-test的狀態,輸出顯示兩個副本正常運行。還能夠在建立的過程當中,經過kubectl describe deployment net-test瞭解詳細的信息。  
[root@linux-node1 ~]# kubectl describe deployment net-test
Name:                   net-test
Namespace:              default
CreationTimestamp:      Thu, 16 Aug 2018 15:41:29 +0800
Labels:                 run=net-test
Annotations:            deployment.kubernetes.io/revision=1
Selector:               run=net-test
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  run=net-test
  Containers:
   net-test:
    Image:      alpine
    Port:       <none>
    Host Port:  <none>
    Args:
      sleep
      360000
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   net-test-5767cb94df (2/2 replicas created)
Events:          <none>  

Events是Deployment的日誌,記錄整個RelicaSet的啓動過程,從上面的建立過程,能夠看到Deployment是經過ReplicaSet來管理Pod。api

[root@linux-node1 ~]# kubectl get replicaset  #獲取副本集信息
NAME                          DESIRED   CURRENT   READY     AGE
net-test-5767cb94df           2         2         2         23h

[root@linux-node1 ~]# kubectl describe replicaset net-test-5767cb94df  #查看副本集的詳細信息
Name:           net-test-5767cb94df
Namespace:      default
Selector:       pod-template-hash=1323765089,run=net-test
Labels:         pod-template-hash=1323765089
                run=net-test
Annotations:    deployment.kubernetes.io/desired-replicas=2
                deployment.kubernetes.io/max-replicas=3
                deployment.kubernetes.io/revision=1 Controlled By: Deployment/net-test  #指明ReplicaSet是由Deployment net-test建立
Replicas:       2 current / 2 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  pod-template-hash=1323765089
           run=net-test
  Containers:
   net-test:
    Image:      alpine
    Port:       <none>
    Host Port:  <none>
    Args:
      sleep
      360000
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>  #Events能夠查看到兩個副本Pod的建立過程

[root@linux-node1 ~]# kubectl get pod  #獲取Pod信息,能夠看到2個副本都處於Running狀態
NAME                                READY     STATUS    RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running   0          22h
net-test-5767cb94df-zb8m4           1/1       Running   0          23h

[root@linux-node1 ~]# kubectl describe pod net-test-5767cb94df-djt98 #查看pod的詳細信息
Name:           net-test-5767cb94df-djt98
Namespace:      default
Node:           192.168.56.13/192.168.56.13
Start Time:     Thu, 16 Aug 2018 15:53:00 +0800
Labels:         pod-template-hash=1323765089
                run=net-test
Annotations:    <none>
Status:         Running
IP:             10.2.73.3 Controlled By: ReplicaSet/net-test-5767cb94df
Containers:
  net-test:
    Container ID:  docker://c8e267326ed80f3cbe8111377c74dd1f016beaef513196b941165e180a5d5733
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
    Port:          <none>
    Host Port:     <none>
    Args:
      sleep
      360000
    State:          Running
      Started:      Thu, 16 Aug 2018 15:53:06 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-mnqx5 (ro)
Conditions:
  Type           Status
  Initialized    True 
  Ready          True 
  PodScheduled   True 
Volumes:
  default-token-mnqx5:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mnqx5
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     <none>
Events:          <none>

Controlled By 指明此 Pod 是由 ReplicaSet/net-test-5767cb94df 建立。Events 記錄了 Pod 的啓動過程。若是操做失敗(好比 image 不存在),也能在這裏查看到緣由。網絡

總結建立的過程:app

(1)用戶經過kubectl建立Deployment

(2)Deployment建立ReplicaSet

(3)ReplicaSet建立Pod

如圖:

  • 3、K8S建立資源的兩種方式

Kubernetes 支持兩種方式建立資源:

(1)用kubectl命令直接建立,在命令行中經過參數指定資源的屬性。此方式簡單直觀,比較適合臨時測試或實驗使用。

kubectl run net-test --image=alpine --replicas=2 sleep 36000

(2)經過配置文件和kubectl create建立。在配置文件中描述了應用的信息和須要達到的預期狀態。

kubectl create -f nginx-deployment.yaml
  • 4、以Deployment YAML方式建立Nginx服務

  • 一、建立deployment
[root@linux-node1 ~]# vim nginx-deployment.yaml  #使用yaml的方式進行建立應用
apiVersion: apps/v1  #apiVersion是當前配置格式的版本
kind: Deployment    #kind是要建立的資源類型,這裏是Deploymnet
metadata:        #metadata是該資源的元數據,name是必須的元數據項
  name: nginx-deployment
  labels:
    app: nginx
spec:          #spec部分是該Deployment的規則說明
  replicas: 3      #relicas指定副本數量,默認爲1
  selector:
    matchLabels:
      app: nginx
  template:      #template定義Pod的模板,這是配置的重要部分
    metadata:      #metadata定義Pod的元數據,至少要頂一個label,label的key和value能夠任意指定
      labels:
        app: nginx
    spec:       #spec描述的是Pod的規則,此部分定義pod中每個容器的屬性,name和image是必需的
      containers:
      - name: nginx
        image: nginx:1.13.12
        ports:
        - containerPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-deployment.yaml #建立nginx-deployment應用
deployment.apps "nginx-deployment" created
  • 二、查看deployment
[root@linux-node1 ~]# kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
net-test           2         2         2            2           32m
nginx-deployment 3 3 3 0 10s
   
[root@linux-node1 ~]# kubectl describe deployment nginx-deployment    #查看deployment詳情
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Thu, 16 Aug 2018 16:13:37 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision=1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 0 available | 3 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.13.12
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-6c45fc49cb (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  1m    deployment-controller  Scaled up replica set nginx-deployment-6c45fc49cb to 3
  • 三、查看Pod
[root@linux-node1 ~]# kubectl get pod    #查看pod在狀態,正在建立中,此時應該正在拉取鏡像
NAME                                READY     STATUS              RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running             0          22m
net-test-5767cb94df-hcwv7           1/1       Unknown             0          34m
net-test-5767cb94df-zb8m4           1/1       Running             0          34m
nginx-deployment-6c45fc49cb-dmc22   0/1       ContainerCreating   0          2m
nginx-deployment-6c45fc49cb-fd8xm   0/1       ContainerCreating   0          2m
nginx-deployment-6c45fc49cb-sc8sh   0/1       ContainerCreating   0          2m
   
[root@linux-node1 ~]# kubectl describe pod nginx-deployment-6c45fc49cb-dmc22  #查看具體某個pod的狀態信息
   
[root@linux-node1 ~]# kubectl get pod -o wide  #建立成功,狀態爲Running
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-djt98           1/1       Running   0          24m       10.2.73.3   192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          36m       10.2.10.2   192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          36m       10.2.73.2   192.168.56.13
nginx-deployment-6c45fc49cb-dmc22   1/1       Running   0          4m        10.2.73.6   192.168.56.13
nginx-deployment-6c45fc49cb-fd8xm   1/1       Running   0          4m        10.2.73.4   192.168.56.13
nginx-deployment-6c45fc49cb-sc8sh   1/1       Running   0          4m        10.2.73.5   192.168.56.13

Deployment、ReplicaSet、Pod 都已經就緒。若是要刪除這些資源,執行 kubectl delete deployment nginx-deployment 或者 kubectl delete -f nginx-deployment.yaml

  • 四、測試Pod訪問
[root@linux-node1 ~]# curl --head http://10.2.73.6
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:18:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes
  • 五、更新Deployment
[root@linux-node1 ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.15.2 --record    #nginx的版本升級,由1.13.2升級爲1.15.2,記錄須要加參數--record
deployment.apps "nginx-deployment" image updated
   
[root@linux-node1 ~]# kubectl get deployment -o wide  #查看更新後的deployment,能夠看到當前4個副本,說明還在滾動升級中
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
net-test           2         2         2            2           39m       net-test     alpine         run=net-test
nginx-deployment   3         4         1            3           6m        nginx        nginx:1.15.2   app=nginx
  • 六、查看更新歷史
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment #查看更新歷史記錄
deployments "nginx-deployment"
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment/nginx-deployment nginx=nginx:1.15.2 --record=true
  • 七、查看具體某一個版本的升級歷史
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment --revision=1
deployments "nginx-deployment" with revision #1
Pod Template:
  Labels:    app=nginx
    pod-template-hash=2701970576
  Containers:
   nginx:
    Image:    nginx:1.13.12
    Port:    80/TCP
    Host Port:    0/TCP
    Environment:    <none>
    Mounts:    <none>
  Volumes:    <none>
  • 八、查看更新後的Deployment,並進行訪問
[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-djt98           1/1       Running   0          30m       10.2.73.3   192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          42m       10.2.10.2   192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          42m       10.2.73.2   192.168.56.13 nginx-deployment-64749d4b59-djttr 1/1 Running 0 37s 10.2.73.8 192.168.56.13 nginx-deployment-64749d4b59-jp7fw 1/1 Running 0 3m 10.2.73.7 192.168.56.13 nginx-deployment-64749d4b59-q4fsn 1/1 Running 0 33s 10.2.73.9 192.168.56.13
[root@linux-node1 ~]# curl --head http://10.2.73.7
HTTP/1.1 200 OK
Server: nginx/1.15.2  #版本已經升級爲1.15.2
Date: Thu, 16 Aug 2018 08:24:09 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Jul 2018 13:02:29 GMT
Connection: keep-alive
ETag: "5b572365-264"
Accept-Ranges: bytes
  • 九、快速回滾到上一個版本
[root@linux-node1 ~]# kubectl rollout undo deployment/nginx-deployment   #回滾上一個版本
deployment.apps "nginx-deployment"    
   
[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP           NODE
net-test-5767cb94df-djt98           1/1       Running   0          32m       10.2.73.3    192.168.56.13
net-test-5767cb94df-hcwv7           1/1       Unknown   0          43m       10.2.10.2    192.168.56.12
net-test-5767cb94df-zb8m4           1/1       Running   0          43m       10.2.73.2    192.168.56.13
nginx-deployment-6c45fc49cb-b9h84   1/1       Running   0          24s       10.2.73.11   192.168.56.13
nginx-deployment-6c45fc49cb-g4mrg   1/1       Running   0          26s       10.2.73.10   192.168.56.13
nginx-deployment-6c45fc49cb-k29kq   1/1       Running   0          21s       10.2.73.12   192.168.56.13
[root@linux-node1 ~]# curl --head http://10.2.73.10
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:25:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

回滾完成,每一次更新或者回滾ip都會變化,因此須要經過vip進行訪問,這就引入了service  
  • 十、使用service的vip進行訪問應用
[root@linux-node1 ~]# vim nginx-service.yaml  #使用yaml方式建立service
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-service.yaml   #建立service
service "nginx-service" created

[root@linux-node1 ~]# kubectl get service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.1.0.1       <none>        443/TCP   4h
nginx-service   ClusterIP   10.1.213.126   <none>        80/TCP    15s  #這個就是vip

[root@linux-node2 ~]# curl --head http://10.1.213.126  #在node2節點上進行訪問vip測試,在node1上沒法訪問是由於沒有安裝kube-proxy致使沒法訪問
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 16 Aug 2018 08:30:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

[root@linux-node2 ~]# ipvsadm -Ln  
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
  -> 192.168.56.11:6443           Masq    1      0          0         
TCP  10.1.213.126:80 rr
  -> 10.2.73.10:80                Masq    1      0          1         
  -> 10.2.73.11:80                Masq    1      0          1         
  -> 10.2.73.12:80                Masq    1      0          0   

查看LVS狀態能夠看到,當訪問VIP:10.1.213.126時,會進行負載均衡到各個pod
  • 十一、擴容到5個節點
[root@linux-node1 ~]# kubectl scale deployment nginx-deployment --replicas 5  #對應用的副本數進行擴容,直接指定副本數爲5
deployment.extensions "nginx-deployment" scaled

[root@linux-node1 ~]# kubectl get pod  #查看pod狀態,能夠看到已經增長到5個副本
NAME                                READY     STATUS    RESTARTS   AGE
net-test-5767cb94df-djt98           1/1       Running   0          38m
net-test-5767cb94df-hcwv7           1/1       Unknown   0          50m
net-test-5767cb94df-zb8m4           1/1       Running   0          50m
nginx-deployment-6c45fc49cb-b9h84   1/1       Running   0          6m
nginx-deployment-6c45fc49cb-g4mrg   1/1       Running   0          7m
nginx-deployment-6c45fc49cb-k29kq   1/1       Running   0          6m
nginx-deployment-6c45fc49cb-n9qkx   1/1       Running   0          24s
nginx-deployment-6c45fc49cb-xpx9s   1/1       Running   0          24s

[root@linux-node2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
  -> 192.168.56.11:6443           Masq    1      0          0         
TCP  10.1.213.126:80 rr
  -> 10.2.73.10:80                Masq    1      0          0         
  -> 10.2.73.11:80                Masq    1      0          0         
  -> 10.2.73.12:80                Masq    1      0          1         
  -> 10.2.73.13:80                Masq    1      0          0         
  -> 10.2.73.14:80                Masq    1      0          0         
相關文章
相關標籤/搜索