Kubernetes3-kubectl管理Kubernetes容器平臺-2

1、kubectl管理集羣中deployment資源與service服務

  一、相關參數  

    kubectl edit 編輯服務器側資源
    kubectl replace 替換,使用 yaml 配置文件來替換正在運行中的配置參數
    kubectl patch 部分更新資源相關信息
    kubectl apply 使用文件或者標準輸入更改配置信息
    kubectl scale 從新設定 Deployment/ReplicaSet/RC/Job 的 size
    kubectl autoscale Deployment/ReplicaSet/RC 的自動擴展設定
    kubectl cordon 設定 node 不可以使用
    kubectl uncordon 設定 node 可使用
    kubectl drain 設定 node 迚入維護模式

  二、導入鏡像

    以前已經導入nginx鏡像,直接操做,沒有鏡像能夠自行在阿里雲下載

2、編輯建立nginx-deployment.yaml /nginx-svc.yaml 

  一、建立deployment.yaml

     vim nginx-deployment.yaml 

[root@master ~]# vim nginx-deployment.yaml 

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
 image: docker.io/nginx:latest imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP

  二、建立servcie

    vim nginx-svc.yaml php

[root@master ~]# vim nginx-svc.yaml 

kind: Service
apiVersion: v1
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
  - protocol: TCP
    nodePort: 31008
    targetPort: 80
    port: 80
  selector:
    name: nginx                                 

  三、幾個端口說明

      nodePort:31008   #---後期用戶能夠經過node節點上這個端口訪問nginx,公網接口html

      targetPort:80   #---指定nginx docker容器的端口node

      port:80  #---pod端口mysql

  四、create deployment/service

    1)建立

    kubectl create -f nginx-deployment.yaml nginx

    kubectl create -f nginx-svc.yamlweb

    2)查看deployment/service/pod詳細信息 

    kubectl get deploysql

    kubectl get svcdocker

    kubectl get pod -o widevim

[root@master ~]# kubectl create -f nginx-deployment.yaml 
deployment "nginx" created
[root@master ~]# kubectl create -f nginx-svc.yaml 
service "nginx" created
[root@master ~]# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           1h
nginx     1         1         1            1           26s
[root@master ~]# kubectl get svc
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        5d
nginx 10.254.8.125   <nodes>       80:31008/TCP 24s
[root@master ~]# 
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1h        10.255.36.2   node2
nginx-1011335894-9wd5h   1/1       Running   0          2m        10.255.41.2 node1
[root@master ~]# 

    3)訪問

      上面已經知道pod運行在哪臺節點及對外監聽端口,接下來就是訪問(上面標紅字段)api

      http://192.168.216.53:31008/

 

    4)經過其餘節點訪問nginx

      雖然nginx是在node1上運行,可是經過其餘節點也是能夠訪問,由於已經作負載均衡

3、kubectl edit命令

  主要做用是修改service值

  一、使用get -o參數指定輸出消息爲yaml類型

    kubectl get service nginx -o yaml

[root@master ~]# kubectl get service nginx -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-11-03T20:45:43Z
  labels:
    name: nginx
  name: nginx
  namespace: default
  resourceVersion: "122656916"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: e7775727-fe7a-11e9-bc69-000c291c8b39
spec:
  clusterIP: 10.254.8.125
  ports:
  - nodePort: 31008
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    name: nginx
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

  二、修改對外端口爲31009

    kubectl edit service nginx

      和vim相似的操做

[root@master ~]# kubectl edit service nginx

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-11-03T20:45:43Z
  labels:
    name: nginx
  name: nginx
  namespace: default
  resourceVersion: "122656916"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: e7775727-fe7a-11e9-bc69-000c291c8b39
spec:
  clusterIP: 10.254.8.125
  ports:
  - nodePort: 31009
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    name: nginx
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

  三、查看service,並驗證

    kubectl get service 

[root@master ~]# kubectl get service
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        5d
nginx        10.254.8.125   <nodes>       80:31009/TCP   20m

    訪問web端

      http://192.168.216.54:31009/

4、kubectl replace

 replace 替換的意思

  一、查看服務

    kubectl get service

  二、重定向一個nginx_replace的yaml文件

    kubectl get service nginx -o yaml >nginx_replace.yaml

  三、編輯,修改端口爲31010

    vim nginx_replace.yaml 

  四、執行替換

    kubectl replace -f nginx_replace.yaml 

  五、檢查service是否生效 

    kubectl get service

[root@master ~]# kubectl get service
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        5d
nginx        10.254.8.125   <nodes>       80:31009/TCP   20m
[root@master ~]# kubectl get service nginx -o yaml >nginx_replace.yaml
[root@master ~]# vim nginx_replace.yaml 

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-11-03T20:45:43Z
  labels:
    name: nginx
  name: nginx
  namespace: default
  resourceVersion: "123205401"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: e7775727-fe7a-11e9-bc69-000c291c8b39
spec:
  clusterIP: 10.254.8.125
  ports:
  - nodePort: 31010
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    name: nginx
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
~                                                                                                                   
~                                                                                                                   
[root@master ~]# kubectl get service
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        6d
nginx        10.254.8.125   <nodes>       80:31010/TCP   17h                                                                                                                
~                                                     

5、kubectl patch

  當修改一部分配置時,使用patch會方便點,如pod換個image鏡像

  這裏好比更換鏡像使得nginx支持php

  一、檢查當前鏡像是否支持php

    kubectl exec -it nginx-1011335894-853ql bash

    php

  二、上傳新鏡像,並導入

    這個能夠在阿里雲隨便找一個

  三、執行patch進行替換

   kubectl patch pod nginx-1011335894-673bv -p  '{"spec":{"containers":[{"name":"nginx","image":"docker.io/zxg/nginx-php-fpm56:latest"}]}}'

  四、檢查是否支持php

   kubectl exec nginx-1011335894-853ql -it bash

 

[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-jwrfc   1/1       Running   0          2h        10.255.41.5   node1
nginx-1011335894-853ql   1/1       Running   0          13s       10.255.36.2   node2
nginx-1011335894-pzgsj   1/1       Running   0          2h        10.255.41.2   node1
[root@master ~]# kubectl exec -it nginx-1011335894-853ql bash
root@nginx-1011335894-853ql:/# nginx -v 
nginx version: nginx/1.13.7
root@nginx-1011335894-853ql:/# php
bash: php: command not found
root@nginx-1011335894-853ql:/# exit
exit
[root@master ~]# kubectl patch pod nginx-1011335894-853ql -p  '{"spec":{"containers":[{"name":"nginx","image":"docker.io/zxg/nginx-php-fpm56:latest"}]}}'
"nginx-1011335894-853ql" patched
[root@master ~]# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-1971774246-jwrfc   1/1       Running   0          2h
nginx-1011335894-853ql   1/1       Running   1          3m
nginx-1011335894-pzgsj   1/1       Running   0          2h
[root@master ~]# kubectl exec nginx-1011335894-853ql -it bash
bash-4.3# php -v 
PHP 5.6.32 (cli) (built: Dec  1 2017 19:58:36) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

6、kubectl apply

  是用來使用文件或者標準輸入來更改配置信息

  一、編輯svc.yaml文件

    vim nginx-svc.yaml

    改:nodePort:31010

    爲:nodePort:31011

  二、執行apply命令

    kubectl apply -f nginx-svc.yaml

  三、檢查結果

    kubectl get svc

 
[root@master ~]# vim nginx-svc.yaml 
kind: Service
apiVersion: v1
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
  - protocol: TCP
    nodePort: 31011
    targetPort: 80
    port: 80
  selector:
    name: nginx
[root@master ~]# kubectl apply -f nginx-svc.yaml
service "nginx" configured
[root@master ~]# kubectl get svc
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   10.254.0.1     <none>        443/TCP        6d
nginx        10.254.8.125   <nodes>       80:31011/TCP   23h
[root@master ~]# 

 

7、kubectl scale (規模)

  用於橫向擴展、是k8s或swarm這類容器編輯平臺的重要功能之一

  如這裏把replica副本改成3

  一、查看nginx運行在哪一個節點

  kubectl get pod -o wide

  二、執行scale命令

  kubectl scale --current-replicas=1 --replicas=3 deployment/nginx 

  三、再次查看nginx運行在哪一個節點

  kubectl get pod -o wide

 
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2   node2
nginx-1011335894-9wd5h   1/1       Running   0          23h       10.255.41.2   node1
[root@master ~]# kubectl scale --current-replicas=1 --replicas=3 deployment/nginx  
deployment "nginx" scaled
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2   node2
nginx-1011335894-9wd5h   1/1       Running   0          23h       10.255.41.2   node1
nginx-1011335894-scm64   1/1       Running   0          5s        10.255.36.3 node2
nginx-1011335894-xtkqd   1/1       Running   0          5s        10.255.41.3   node1
[root@master ~]# 

 

8、kubectl autoscale

  用於自動擴展確認,跟scale不一樣的是前者仍是須要手動執行,而autoscale則會根據負載進行調解,而這條命令能夠對Deployment/ReplicaSet/RC進行設定,經過最小值和最大值的指定進行設定

  一、設置最小2,最大5的自動設置

    kubectl autoscale deployment nginx --min=2 --max=5

  二、查看結果

    應該是沒變化,由於以前手動scale設置的3在這個2-5的區間

    kubectl get pod -o wide  

  三、設置最小2,最大2

    kubectl autoscale deployment nginx --min=2 --max=2

    這裏就報錯了由於以前設置的是3


[root@master ~]# kubectl autoscale deployment nginx --min=2 --max=5
deployment "nginx" autoscaled
[root@master ~]# kubectl get pod -o wide                           
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2   node2
nginx-1011335894-9wd5h   1/1       Running   0          23h       10.255.41.2   node1
nginx-1011335894-scm64   1/1       Running   0          3m        10.255.36.3   node2
nginx-1011335894-xtkqd   1/1       Running   0          3m        10.255.41.3   node1
[root@master ~]# kubectl autoscale deployment nginx --min=2 --max=2
Error from server (AlreadyExists): horizontalpodautoscalers.autoscaling "nginx" already exists
[root@master ~]# 

 

9、kubectl cordon 與uncordon

   若是其中一臺node壞掉或者維護,暫時不能讓生成的pod在此node上運行,須要通知kubernetes讓其不要建立過來,就用cordon命令,若是uncordon就是取消這個設定

  一、在node2上運行cordon命令

    kubectl cordon node2

  二、查看pod詳情,沒有變化

    kubectl get pod -o wide

  三、查看node詳情

    kubectl get nodes -o wide

    發現node2狀態爲Ready,SchedulingDisabled 

  三、增長relicas副本

    kubectl scale --replicas=6 deployment/nginx

  四、再次查看pod詳情

    kubectl get pod -o wide 

    發現都是node1創建,node2已經封鎖成功

  五、使用uncordon取消cordon設定

    kubectl uncordon node2

[root@master ~]# kubectl cordon node2
node "node2" cordoned
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2   node2
nginx-1011335894-9wd5h   1/1       Running   0          23h       10.255.41.2   node1
nginx-1011335894-scm64   1/1       Running   0          9m        10.255.36.3   node2
nginx-1011335894-xtkqd   1/1       Running   0          9m        10.255.41.3   node1
[root@master ~]# kubectl get nodes -o wide
NAME      STATUS                     AGE       EXTERNAL-IP
node1     Ready                      6d        <none>
node2     Ready,SchedulingDisabled   5d        <none>
[root@master ~]# kubectl scale --replicas=6 deployment/nginx
deployment "nginx" scaled
[root@master ~]# kubectl get pod -o wide                    
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2   node2
nginx-1011335894-7jvp0   1/1       Running   0          5s        10.255.41.5 node1
nginx-1011335894-8nd1q   1/1       Running   0          5s        10.255.41.6 node1
nginx-1011335894-9wd5h   1/1       Running   0          23h       10.255.41.2 node1
nginx-1011335894-lhtkm   1/1       Running   0          5s        10.255.41.4 node1
nginx-1011335894-scm64   1/1       Running   0          11m       10.255.36.3 node2
nginx-1011335894-xtkqd   1/1       Running   0          11m       10.255.41.3 node1
[root@master ~]# 
[root@master ~]# kubectl uncordon node2
node "node2" uncordoned
[root@master ~]# kubectl get node -o wide
NAME      STATUS    AGE       EXTERNAL-IP
node1     Ready     6d        <none>
node2    Ready     5d        <none>
[root@master ~]# 

 

10、kubectl drain (驅逐)

  用於對某個node結點進行維護

  一、drain兩個做用:

    一、設定此node不可使用(cordon)

    二、evict驅逐pod到他正常的node節點上

  二、先刪以前的nginx

    kubectl delete deploy nginx

  三、建立pod

    kubectl create -f nginx-deployment.yaml 

  四、查看pod詳情

    kubectl get pod -o wide

  五、執行drain命令 drain node2

    kubectl drain node2

  六、查看pod

    get pod -o wide

    鏡像已經漂移過來了

  七、查看node

    node2狀態爲Ready,SchedulingDisabled   ,完成配置

[root@master ~]# kubectl create -f nginx-deployment.yaml 
deployment "nginx" created
[root@master ~]# kubectl scale --replicas=4 deployment nginx
deployment "nginx" scaled
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-2f905   1/1       Running   0          1d        10.255.36.2 node2
nginx-1011335894-4tpj5   1/1       Running   0          8s        10.255.41.3 node1
nginx-1011335894-673bv   1/1       Running   0          8s        10.255.36.3 node2
nginx-1011335894-hw8ld   1/1       Running   0          8s        10.255.36.4 node2
nginx-1011335894-pzgsj   1/1       Running   0          28s       10.255.41.2 node1
[root@master ~]# kubectl drain node2
node "node2" cordoned
pod "nginx-1011335894-hw8ld" evicted
pod "nginx-1011335894-673bv" evicted
pod "mysql-1971774246-2f905" evicted
node "node2" drained
[root@master ~]# kubectl get pod -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1971774246-jwrfc   1/1       Running   0          7s        10.255.41.5 node1
nginx-1011335894-4tpj5   1/1       Running   0          1m        10.255.41.3 node1
nginx-1011335894-d683g   1/1       Running   0          7s        10.255.41.6 node1
nginx-1011335894-gs3lg   1/1       Running   0          7s        10.255.41.4 node1
nginx-1011335894-pzgsj   1/1       Running   0          1m        10.255.41.2 node1
[root@master ~]# get nodes -o wide
  
NAME STATUS AGE EXTERNAL-IP

node1 Ready 6d <none>

node2 Ready,SchedulingDisabled 5d <none>

 

 

轉載請註明出處:http://www.javashuo.com/article/p-tfscqfqr-hw.html

相關文章
相關標籤/搜索