1. 程式人生 > >KUBERNETES-1-2-應用管理

KUBERNETES-1-2-應用管理

0.把kubernetes的環境先跑起來並確認狀態。

[[email protected] ~]# kubectl get nodes
NAME                 STATUS    ROLES     AGE       VERSION
master.example.com   Ready     master    5h        v1.11.1
node1.example.com    Ready     <none>    4h        v1.11.1
node2.example.com    Ready     <none>    23s       v1.11.1

 

1.啟動一個deployment應用控制的程式nginx-deploy。

[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
deployment.apps/nginx-deploy created (dry run)
[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
deployment.apps/nginx-deploy created
[

[email protected] ~]# kubectl get deployment
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1         1         1            0           20s
[
[email protected]
~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
nginx-deploy-5b595999-n75ds   1/1       Running   0          32m
[[email protected] ~]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE
nginx-deploy-5b595999-n75ds   1/1       Running   0          32m       10.244.2.2   node2.example.com

 

2.到node2.example.com上檢視硬體介面配置資訊,發現使用的是cni0網絡卡配置的網段。嘗試用curl進行訪問。注意:這裡只能在叢集內部進行訪問,叢集外目前是無法訪問的。

[[email protected] ~]# ifconfig
cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 10.244.2.1  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::9850:b4ff:fec4:a770  prefixlen 64  scopeid 0x20<link>
        ether 0a:58:0a:f4:02:01  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# curl 10.244.2.2
 

3.我們嘗試把已經建立的pod刪除。可以看到,因為設定了replicas=1,pod在被刪除後,會重新自動生成一個。不過新的pod執行的節點不同,這次是node1.example.com。

[[email protected] ~]# kubectl delete pods nginx-deploy-5b595999-n75ds
pod "nginx-deploy-5b595999-n75ds" deleted
[[email protected] ~]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE
nginx-deploy-5b595999-x52rk   1/1       Running   0          2m        10.244.1.2   node1.example.com
 

4.把deployment應用管理的nginx-deploy作為service釋出出來,--name=nginx指明名稱,--port=80指明service埠,--target-port=80指明pod埠,--protocol=TCP指明訪問協議。通過kubectl get svc檢視確認。這時,在叢集內部的節點之間是可以訪問的,但是叢集之外是無法訪問的。

[[email protected] ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   6h
nginx        ClusterIP   10.102.51.215   <none>        80/TCP    15s
[[email protected] ~]# curl 10.102.51.215

 

5.kube-system空間中執行著兩個coredns的pod,kube-system的services資訊可以看到ClusterIP。或者執行一個client容器, --image=busybox指明映象,--replicas=1指明pod數量,-it 指明進入互動介面,--restart=Never指明pod關閉後不再重新生成。cat /etc/resolv.conf檢視地址解析,default.svc.cluster.local中的default是域名空間名稱。
[[email protected] ~]# kubectl get pods -n kube-system -o wide
NAME                                         READY     STATUS    RESTARTS   AGE       IP             NODE
coredns-78fcdf6894-pzvc6                     1/1       Running   0          6h        10.244.0.6     master.example.com
coredns-78fcdf6894-tg7zs                     1/1       Running   0          6h        10.244.0.7     master.example.com
[[email protected] ~]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP   6h
[[email protected] ~]# yum install bind-utils

[[email protected] ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never
If you don't see a command prompt, try pressing enter.
/ # cat /etc/resolv.conf 
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local localdomain example.com
options ndots:5

/ # wget nginx
Connecting to nginx (10.102.51.215:80)
index.html           100% |************************************************************************************************|   612  0:00:00 ETA
/ # wget -O - -q http://nginx:80/

[[email protected] ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10

;; QUESTION SECTION:
;nginx.default.svc.cluster.local. IN    A

;; ANSWER SECTION:
nginx.default.svc.cluster.local. 5 IN    A    10.102.51.215

 

6.這時我們嘗試將pod刪除,系統會自動重新生成一個pod,我們在之前的互動介面繼續嘗試訪問nginx,依然可以訪問。

[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          17m
nginx-deploy-5b595999-x52rk   1/1       Running   0          47m
[[email protected] ~]# kubectl delete pods nginx-deploy-5b595999-x52rk
pod "nginx-deploy-5b595999-x52rk" deleted
[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          17m
nginx-deploy-5b595999-tj8ms   1/1       Running   0          24s
/ # wget -O - -q http://nginx:80/

 

7.kubectl describe svc nginx檢視服務詳細資訊,Endpoints表示的容器變化時,會通過Selector:   run=nginx-deploy自動去關聯新生成的pod。 kubectl get pods --show-labels可以檢視pod的標籤資訊。我們將nginx服務刪除,然後再次釋出,在之前的互動埠依然可以訪問nginx。通過kubectl describe deployment nginx-deploy也能顯示標籤資訊Labels:  run=nginx-deploy。
[[email protected] ~]# kubectl describe svc nginx
Name:              nginx
Namespace:         default
Labels:            run=nginx-deploy
Annotations:       <none>
Selector:          run=nginx-deploy
Type:              ClusterIP
IP:                10.102.51.215
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.3:80
Session Affinity:  None
Events:            <none>
[[email protected] ~]# kubectl get pods --show-labels
NAME                          READY     STATUS    RESTARTS   AGE       LABELS
client                        1/1       Running   0          23m       run=client
nginx-deploy-5b595999-tj8ms   1/1       Running   0          5m        pod-template-hash=16151555,run=nginx-deploy
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   7h
nginx        ClusterIP   10.102.51.215   <none>        80/TCP    41m
[[email protected] ~]# kubectl delete svc nginx
service "nginx" deleted
[[email protected] ~]# kubectl expose deployment nginx-deploy --name=nginx
service/nginx exposed
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   7h
nginx        ClusterIP   10.101.146.21   <none>        80/TCP    11s
/ # wget -O - -q http://nginx:80/

[[email protected] ~]# kubectl describe deployment nginx-deploy
Name:                   nginx-deploy
Namespace:              default
CreationTimestamp:      Fri, 07 Dec 2018 03:14:33 -0500
Labels:                 run=nginx-deploy
Annotations:            deployment.kubernetes.io/revision=1
Selector:               run=nginx-deploy
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx-deploy
  Containers:
   nginx-deploy:
    Image:        nginx:1.14-alpine
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deploy-5b595999 (1/1 replicas created)
Events:          <none>
 

8.kubectl run myapp起一個deployment,--image=ikubernetes/myapp:v1 --replicas=2指明映象和pod數量,可以在之前的互動終端對pod的地址進行訪問。將deployment釋出service為myapp,可以對service進行訪問。

[[email protected] ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
deployment.apps/myapp created
[[email protected] ~]# kubectl get deployment
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
myapp          2         2         2            1           1m
nginx-deploy   1         1         1            1           1h
[[email protected] ~]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE
client                        1/1       Running   0          49m       10.244.2.3   node2.example.com
myapp-848b5b879b-hgww4        1/1       Running   0          2m        10.244.2.5   node2.example.com
myapp-848b5b879b-th7kc        1/1       Running   0          2m        10.244.1.5   node1.example.com
nginx-deploy-5b595999-tj8ms   1/1       Running   0          31m       10.244.1.3   node1.example.com
/ # wget -O - -q 10.244.1.5
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q 10.244.2.5
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

/ # wget -O - -q 10.244.1.5/hostname.html
myapp-848b5b879b-th7kc
/ # wget -O - -q 10.244.2.5/hostname.html
myapp-848b5b879b-hgww4
[[email protected] ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   7h
myapp        ClusterIP   10.99.24.155    <none>        80/TCP    8s
nginx        ClusterIP   10.101.146.21   <none>        80/TCP    23m

/ # wget -O - -q myapp/hostname.html
myapp-848b5b879b-hgww4
/ # wget -O - -q myapp/hostname.html
myapp-848b5b879b-th7kc

 

9.可以通過kubectl scale --replicas=對deployment的pod進行動態擴容和縮容。

[[email protected] ~]# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled
[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1h
myapp-848b5b879b-757ng        1/1       Running   0          21s
myapp-848b5b879b-hgww4        1/1       Running   0          13m
myapp-848b5b879b-lq9q5        1/1       Running   0          21s
myapp-848b5b879b-m22vb        1/1       Running   0          21s
myapp-848b5b879b-th7kc        1/1       Running   0          13m
nginx-deploy-5b595999-tj8ms   1/1       Running   0          43m
[[email protected] ~]# kubectl scale --replicas=3 deployment myapp
deployment.extensions/myapp scaled
[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1h
myapp-848b5b879b-757ng        1/1       Running   0          1m
myapp-848b5b879b-hgww4        1/1       Running   0          14m
myapp-848b5b879b-th7kc        1/1       Running   0          14m
nginx-deploy-5b595999-tj8ms   1/1       Running   0          43m
 

10.kubectl set image deployment可以設定deployment的映象更新,kubectl rollout status將 deployment的更新狀態輸出。kubectl rollout undo deployment可以實現回滾,如果不指定引數,回滾到最近一個版本。

[[email protected] ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
[[email protected] ~]# kubectl rollout status deployment myapp
Waiting for deployment "myapp" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
deployment "myapp" successfully rolled out

[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1h
myapp-74c94dcb8c-bvsm5        1/1       Running   0          1m
myapp-74c94dcb8c-xkz2x        1/1       Running   0          1m
myapp-74c94dcb8c-zngl9        1/1       Running   0          1m
nginx-deploy-5b595999-tj8ms   1/1       Running   0          52m
[[email protected] ~]# kubectl rollout undo deployment myapp
deployment.extensions/myapp
[[email protected] ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1h
myapp-848b5b879b-7h254        1/1       Running   0          11s
myapp-848b5b879b-d7rjs        1/1       Running   0          13s
myapp-848b5b879b-wv5cz        1/1       Running   0          14s
nginx-deploy-5b595999-tj8ms   1/1       Running   0          54m
 

11.kubectl edit svc可以修改服務配置檔案引數type: ClusterIP --》type: Node Port,來讓物理機可以訪問service。這裡可以看到自動轉發的埠為31601/TCP,嘗試在物理機上進行訪問,可以訪問網頁。

[[email protected] ~]# kubectl edit svc myapp
service/myapp edited

type: ClusterIP --》type: Node Port

[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        7h
myapp        NodePort    10.99.24.155    <none>        80:31601/TCP   26m
nginx        ClusterIP   10.101.146.21   <none>        80/TCP         50m