語法: kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
實用舉例:html
[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 #建立一個nginx的應用,副本數爲1 deployment.apps/nginx-deploy created [root@k8s-master ~]# kubectl get deployment #獲取應用信息,查看應用是否符合預期狀態 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deploy 1 1 1 1 40s [root@k8s-master ~]# kubectl get pods #獲取pod信息 NAME READY STATUS RESTARTS AGE nginx-deploy-5b595999-44zwq 1/1 Running 0 1m [root@k8s-master ~]# kubectl get pods -o wide #查看pod運行在哪一個節點上 NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-44zwq 1/1 Running 0 1m 10.244.2.2 k8s-node02
從上面建立的應用能夠得知,nginx-deploy應用的pod的ip爲10.244.2.2,這是一個pod ip,僅僅能夠在集羣內部訪問,以下:node
[root@k8s-master ~]# curl 10.244.2.2 -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 28 Feb 2019 06:13:03 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 06 Jul 2018 16:53:43 GMT Connection: keep-alive ETag: "5b3f9e97-264" Accept-Ranges: bytes [root@k8s-node01 ~]# curl 10.244.2.2 -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 28 Feb 2019 06:12:04 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 06 Jul 2018 16:53:43 GMT Connection: keep-alive ETag: "5b3f9e97-264" Accept-Ranges: bytes [root@k8s-node02 ~]# curl 10.244.2.2 -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 23 Aug 2018 09:22:18 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 06 Jul 2018 16:53:43 GMT Connection: keep-alive ETag: "5b3f9e97-264" Accept-Ranges: bytes
這裏要注意的是pod的客戶端有2類,1類是其餘pod,1類是集羣外部客戶端,那麼集羣外部的客戶端如何訪問到pod呢?pod的地址是隨時變化的,假設先刪除建立的pod:nginx
[root@k8s-master ~]# kubectl delete pods nginx-deploy-5b595999-44zwq pod "nginx-deploy-5b595999-44zwq" deleted
要明白pod是經過控制器進行管理的,當控制器發現pod的狀態不知足預期的狀態時,將會從新建立一個podweb
[root@k8s-master ~]# kubectl get pods -o wide #因爲在node01節點上沒有鏡像,須要從新下載 NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-872c7 0/1 ContainerCreating 0 24s <none> k8s-node01 [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-872c7 1/1 Running 0 56s 10.244.1.2 k8s-node01
此時能夠看到新建的pod的ip地址已經更改了,而且本次建立的pod是在node01節點上,這樣就須要提供一個固定端點,給集羣外部客戶端進行訪問。這個固定端點就是service:json
語法以下: kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type] [options] [root@k8s-master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP #建立一個nginx的service service/nginx exposed [root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d nginx ClusterIP 10.106.162.254 <none> 80/TCP 19s [root@k8s-master ~]# curl 10.106.162.254 -I #經過ClusterIP進行訪問nginx pod HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 23 Aug 2018 09:38:09 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 06 Jul 2018 16:53:43 GMT Connection: keep-alive ETag: "5b3f9e97-264" Accept-Ranges: bytes
10.106.162.254這網段依然是集羣內部的網段,只能被集羣內部所能訪問,外部是沒法經過service的ip進行訪問的。那麼針對pod的客戶端除了經過service ip訪問還能夠經過service的名稱進行訪問,可是前提是須要對service的名稱可以進行解析。而解析時是依賴coredns服務的,而咱們本地的dns指向並不是coredns,以下:api
[root@k8s-master ~]# curl nginx curl: (6) Could not resolve host: nginx; Unknown error [root@k8s-master ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 8.8.8.8 nameserver 114.114.114.114
下面查看一下coredns的ip地址:app
[root@k8s-master ~]# kubectl get pods -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE coredns-78fcdf6894-nmcmz 1/1 Running 0 1d 10.244.0.3 k8s-master coredns-78fcdf6894-p5pfm 1/1 Running 0 1d 10.244.0.2 k8s-master etcd-k8s-master 1/1 Running 1 1d 192.168.56.11 k8s-master kube-apiserver-k8s-master 1/1 Running 8 1d 192.168.56.11 k8s-master kube-controller-manager-k8s-master 1/1 Running 4 1d 192.168.56.11 k8s-master kube-flannel-ds-n5c86 1/1 Running 0 1d 192.168.56.11 k8s-master kube-flannel-ds-nrcw2 1/1 Running 0 5h 192.168.56.13 k8s-node02 kube-flannel-ds-pgpr7 1/1 Running 1 1d 192.168.56.12 k8s-node01 kube-proxy-glzth 1/1 Running 0 5h 192.168.56.13 k8s-node02 kube-proxy-rxlt7 1/1 Running 1 1d 192.168.56.11 k8s-master kube-proxy-vxckf 1/1 Running 0 1d 192.168.56.12 k8s-node01 kube-scheduler-k8s-master 1/1 Running 2 1d 192.168.56.11 k8s-master
而通常,也不會直接經過coredns的這個pod ip地址進行訪問,而是經過service進行訪問,查看一下coredns的service:curl
[root@k8s-master ~]# 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 1d
那麼就能夠經過這個service ip:10.96.0.10進行解析上面的nginx服務,以下:ide
[root@k8s-master ~]# yum install -y bind-utils [root@k8s-master ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10 #這裏須要使用完整的服務名稱,不然會由於dns搜索域的問題而致使沒法解析成功 ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> -t A nginx.default.svc.cluster.local @10.96.0.10 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 78 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;nginx.default.svc.cluster.local. IN A ;; ANSWER SECTION: nginx.default.svc.cluster.local. 5 IN A 10.106.162.254 #這樣就能夠正常解析出nginx的service ip了 ;; Query time: 155 msec ;; SERVER: 10.96.0.10#53(10.96.0.10) ;; WHEN: Thu Aug 23 05:40:22 EDT 2018 ;; MSG SIZE rcvd: 107
那麼再演示經過pod 客戶端進行訪問:測試
[root@k8s-master ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never #建立pod [root@k8s-master ~]# kubectl exec -it client /bin/sh #首次建立若是沒進入到容器,可使用這命令進入 / # cat /etc/resolv.conf #查看dns,這裏就是自動指向coredns nameserver 10.96.0.10 search default.svc.cluster.local svc.cluster.local cluster.local options ndots:5 / # wget -O - -q http://nginx:80 #請求解析nginx <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
這就是service提供給pod的固定訪問端點的使用,而pod的增刪改查,並不會影響經過service進行訪問,能夠經過如下命令來查看service的詳細信息:
[root@k8s-master ~]# kubectl describe svc nginx Name: nginx Namespace: default Labels: run=nginx-deploy Annotations: <none> Selector: run=nginx-deploy Type: ClusterIP IP: 10.106.162.254 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.5:80 #pod 的ip,會根據資源變化改變,可是實際訪問的service 依舊有效 Session Affinity: None Events: <none>
那麼pod的增刪改,service又是如何肯定對pod的訪問呢?這就須要經過標籤選擇器進行選定,不管pod的ip如何變化,可是標籤不會變化,從而達到固定端點的訪問效果,查看一下pod的標籤:
[root@k8s-master ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS client 1/1 Running 0 21h run=client nginx-deploy-5b595999-872c7 1/1 Running 2 22h pod-template-hash=16151555,run=nginx-deploy
run=nginx-deploy就是這個應用的標籤,因此當pod的改變,並不會影響service的訪問。
語法以下:
kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
(1)建立應用myapp
[root@k8s-master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2 deployment.apps/myapp created [root@k8s-master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE myapp 2 2 2 1 15s nginx-deploy 1 1 1 1 22h (2)查看pod詳細信息 [root@k8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE client 1/1 Running 0 21h 10.244.2.3 k8s-node02 client2 1/1 Running 0 48m 10.244.1.6 k8s-node01 client3 1/1 Running 0 27m 10.244.2.4 k8s-node02 myapp-848b5b879b-bdp7t 1/1 Running 0 26s 10.244.1.7 k8s-node01 myapp-848b5b879b-swt2c 0/1 ErrImagePull 0 26s 10.244.2.5 k8s-node02 nginx-deploy-5b595999-872c7 1/1 Running 2 22h 10.244.1.5 k8s-node01
(3)配置service端點 [root@k8s-master ~]# kubectl expose deployment myapp --name=myapp --port=80 service/myapp exposed
(4)查看服務信息 [root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d myapp ClusterIP 10.106.67.242 <none> 80/TCP 14s nginx ClusterIP 10.106.162.254 <none> 80/TCP 21h
(5)Pod客戶端訪問 / # wget -O - -q http://myapp:80 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
(6)副本增長到5 [root@k8s-master ~]# kubectl scale --replicas=5 deployment myapp deployment.extensions/myapp scaled [root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 21h client2 1/1 Running 0 51m client3 1/1 Running 0 30m myapp-848b5b879b-6p6ml 1/1 Running 0 1m myapp-848b5b879b-7xmnj 0/1 ImagePullBackOff 0 1m myapp-848b5b879b-bdp7t 1/1 Running 0 3m myapp-848b5b879b-swt2c 0/1 ImagePullBackOff 0 3m myapp-848b5b879b-zlvl2 1/1 Running 0 1m nginx-deploy-5b595999-872c7 1/1 Running 2 22h
(7)副本收縮到3 [root@k8s-master ~]# kubectl scale --replicas=3 deployment myapp deployment.extensions/myapp scaled
語法以下:
kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
(1)版本升級爲v2
[root@k8s-master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2 deployment.extensions/myapp image updated
(2)查看升級過程 [root@k8s-master ~]# kubectl rollout status deployment myapp #查看更新過程 Waiting for deployment "myapp" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "myapp" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "myapp" rollout to finish: 1 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: 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 (3)獲取pod信息 [root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 21h client2 1/1 Running 0 53m client3 1/1 Running 0 33m myapp-74c94dcb8c-2djgg 1/1 Running 0 1m myapp-74c94dcb8c-92d9p 1/1 Running 0 28s myapp-74c94dcb8c-nq7zt 1/1 Running 0 25s nginx-deploy-5b595999-872c7 1/1 Running 2 22h [root@k8s-master ~]# kubectl describe pods myapp-74c94dcb8c-2djgg
(4)pod客戶端測試訪問,能夠看到是v2版本 / # wget -O - -q http://myapp:80 Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
語法以下:
kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags] [options]
[root@k8s-master ~]# kubectl rollout undo deployment myapp #不指定版本直接回滾到上一個版本 deployment.extensions/myapp / # wget -O - -q http://myapp:80 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# kubectl edit svc myapp TYPE:CLUSTER-IP改成 TYPE:NodePort [root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d myapp NodePort 10.106.67.242 <none> 80:32432/TCP 18m nginx ClusterIP 10.106.162.254 <none> 80/TCP 22h
這裏再查看service信息,能夠看到myapp進行了端口映射,將myapp的80端口映射到本地32432端口,則可使用http://192.168.56.11:32432進行訪問。如圖: