1、相關命令html
一、kubectlnode
經過鏈接api server 進行各k8s對象資源的增刪改查,如pod,service,controller(控制器),咱們經常使用的pod控制器replicaset,deployment,statefulet,daemonset,job,cronjob等,甚至node都是對象。linux
[root@k8smaster ~]# kubectl --help kubectl controls the Kubernetes cluster manager. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/ Basic Commands (Beginner): #新手用的命令 create #增 Create a resource from a file or from stdin. expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service run Run a particular image on the cluster set Set specific features on objects Basic Commands (Intermediate): #中級的基礎命令 explain Documentation of resources get #查 Display one or many resources edit #改 Edit a resource on the server delete #刪 Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: #部署命令 rollout #滾動,回滾 Manage the rollout of a resource scale #改變應用程序的規模 Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job autoscale #自動改變,就是建立HPA的 Auto-scale a Deployment, ReplicaSet, or ReplicationController Cluster Management Commands: #集羣管理相關命令 certificate #證書 Modify certificate resources. cluster-info #集羣信息 Display cluster info top #查看資源使用率 Display Resource (CPU/Memory/Storage) usage. cordon #標記一個節點不可被調用 Mark node as unschedulable uncordon #標記一個節點可被調用 Mark node as schedulable drain #排幹模式 Drain node in preparation for maintenance taint #增長污點,給節點增長污點之後,能容忍該污點的pod才能被調度到該節點,默認master會有不少污點,因此建立的pod默認是不會在master上建立,這樣確保了master只運行各系統組件 Update the taints on one or more nodes Troubleshooting and Debugging Commands: #修復和調試命令 describe #描述一個資源的詳細信息 Show details of a specific resource or group of resources logs #查看日誌 Print the logs for a container in a pod attach #和docker 中的attach類似 Attach to a running container exec #和docker exec 類似 Execute a command in a container port-forward #端口轉發 Forward one or more local ports to a pod proxy #代理 Run a proxy to the Kubernetes API server cp #跨容器複製文件 Copy files and directories to and from containers. auth #測試認證 Inspect authorization Advanced Commands: #高級命令 apply #建立,修改 Apply a configuration to a resource by filename or stdin patch #打補丁 Update field(s) of a resource using strategic merge patch replace #替換 Replace a resource by filename or stdin wait #等待 Experimental: Wait for one condition on one or many resources convert #轉換 Convert config files between different API versions Settings Commands: #設置命令 label #打標籤 Update the labels on a resource annotate #給資源加一個註解 Update the annotations on a resource completion #用來作命令補全 Output shell completion code for the specified shell (bash or zsh) Other Commands: #其它命令 alpha Commands for features in alpha api-resources Print the supported API resources on the server api-versions Print the supported API versions on the server, in the form of "group/version" config Modify kubeconfig files plugin Runs a command-line plugin version Print the client and server version information Usage: kubectl [flags] [options] Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
二、查看kubectl 版本信息或集羣信息nginx
[root@k8smaster ~]# kubectl version Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-17T18:53:20Z", GoVer sion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-17T18:43:26Z", GoVer sion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
[root@k8smaster ~]# kubectl cluster-info Kubernetes master is running at https://192.168.10.10:6443 KubeDNS is running at https://192.168.10.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
三、kubectl run 命令web
[root@k8smaster ~]# kubectl run --help Create and run a particular image, possibly replicated. Creates a deployment or job to manage the created container(s).#基於這兩種中的某一種建立容器(也就是pod) Examples: # Start a single instance of nginx. kubectl run nginx --image=nginx #基於nginx鏡像啓動pod # Start a single instance of hazelcast and let the container expose port 5701 . kubectl run hazelcast --image=hazelcast --port=5701 # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container. kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default" # Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container. kubectl run hazelcast --image=nginx --labels="app=hazelcast,env=prod" # Start a replicated instance of nginx. kubectl run nginx --image=nginx --replicas=5 #啓動5個pod # Dry run. Print the corresponding API objects without creating them. kubectl run nginx --image=nginx --dry-run #單跑模式 # Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON. kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }' # Start a pod of busybox and keep it in the foreground, don't restart it if it exits. kubectl run -i -t busybox --image=busybox --restart=Never #默認容器結束了會自動補上去,加了此命令後就不會再自動補上去 # Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command. kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN> # Start the nginx container using a different command and custom arguments. kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN> #加上自定義的命令 # Start the perl container to compute π to 2000 places and print it out. kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)' # Start the cron job to compute π to 2000 places and print it out every 5 minutes. kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)' #建立一個定時job
使用kubectl run建立一個單跑模式的nginx容器docker
[root@k8smaster ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true deployment.apps/nginx-deploy created (dry run) #deployment控制器下所控制的應用程序,叫作nginx-deploy
使用kubectl run建立一個nginx容器shell
[root@k8smaster ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 deployment.apps/nginx-deploy created [root@k8smaster ~]# kubectl get deployment NAME DESIRED(指望) CURRENT(當前) UP-TO-DATE AVAILABLE(可用) AGE nginx-deploy 1 1 1 0 23s
過一下子查看顯示已經可用api
[root@k8smaster ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deploy 1 1 1 1 2m
查看建立的podbash
[root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deploy-5b595999-vw5vt 1/1 Running 0 3m [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-vw5vt 1/1 Running 0 4m 10.244.2.2 k8snode2
到節點2中查看網橋可用發現啓動的相應pod是鏈接在cni0 網橋上的session
[root@k8snode2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:b3:80:ea brd ff:ff:ff:ff:ff:ff inet 192.168.10.12/24 brd 192.168.10.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::df7a:6e6c:357:ba25/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN link/ether 02:42:85:22:4d:73 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever 4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN link/ether 92:d1:5f:6c:71:7b brd ff:ff:ff:ff:ff:ff inet 10.244.2.0/32 scope global flannel.1 valid_lft forever preferred_lft forever inet6 fe80::90d1:5fff:fe6c:717b/64 scope link valid_lft forever preferred_lft forever 5: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP qlen 1000 link/ether 0a:58:0a:f4:02:01 brd ff:ff:ff:ff:ff:ff inet 10.244.2.1/24 scope global cni0 valid_lft forever preferred_lft forever inet6 fe80::6429:a3ff:fe46:ac7e/64 scope link valid_lft forever preferred_lft forever 6: vethadaa4f42@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP link/ether 3e:4d:5f:db:62:17 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::3c4d:5fff:fedb:6217/64 scope link valid_lft forever preferred_lft forever [root@k8snode2 ~]# docker exec -it 706159bf29fc /bin/sh / # ifconfig eth0 Link encap:Ethernet HWaddr 0A:58:0A:F4:02:02 inet addr:10.244.2.2 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1 RX packets:15 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1206 (1.1 KiB) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) / #
[root@k8snode2 ~]# curl 10.244.2.2 <!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> [root@k8snode2 ~]#
四、kubelet delete :使用此命令刪除剛剛建立的nginx容器,會發現刪除後控制器會再次啓動一個pod
[root@k8smaster ~]# kubectl delete pods nginx-deploy-5b595999-vw5vt pod "nginx-deploy-5b595999-vw5vt" deleted [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-kbj6j 0/1 ContainerCreating 0 17s <none> k8snode1 [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-kbj6j 1/1 Running 0 2m 10.244.1.2 k8snode1
五、kubelet expose (暴露):此時會發現pod的ip已經變了,所以咱們須要建立一個固定的service來提供固定的訪問接口。咱們使用kubelet expose命令來進行建立。(service 默認只服務於集羣的內部pod客戶端)
[root@k8smaster ~]# kubectl expose deployment(控制器) nginx-deploy(控制器名字) --name=nginx --port=80(service 端口) --target-port=80(pod端口) --protocol=TCP service/nginx exposed
查看和測試建立後的service
[root@k8smaster ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20h nginx ClusterIP 10.103.127.92 <none> 80/TCP 17m [root@k8smaster ~]# kubectl describe service nginx Name: nginx Namespace: default Labels: run=nginx-deploy Annotations: <none> Selector: run=nginx-deploy Type: ClusterIP IP: 10.103.127.92 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.2:80 Session Affinity: None Events: <none> [root@k8smaster ~]# curl 10.103.127.92 <!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> [root@k8smaster ~]#
刪除pod而後等自動重建後繼續訪問原來的service發現依然能夠訪問
[root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE client 0/1 Error 0 14m 10.244.2.3 k8snode2 nginx-deploy-5b595999-jdbtn 1/1 Running 0 31s 10.244.1.3 k8snode1 [root@k8smaster ~]# kubectl delete pods nginx-deploy-5b595999-jdbtn pod "nginx-deploy-5b595999-jdbtn" deleted [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE client 0/1 Error 0 14m 10.244.2.3 k8snode2 nginx-deploy-5b595999-d9lv5 1/1 Running 0 27s 10.244.2.4 k8snode2 [root@k8smaster ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h nginx ClusterIP 10.103.127.92 <none> 80/TCP 42m [root@k8smaster ~]# curl 10.103.127.92 <!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>
六、關於集羣dns,全部啓動的pod的nameserver 地址都是指向集羣中 系統名稱空間 中的dns的service的
[root@k8smaster ~]# kubectl get svc --all-namespaces NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h default nginx ClusterIP 10.103.127.92 <none> 80/TCP 29m kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 21h [root@k8smaster ~]# kubectl describe svc kube-dns -n kube-system Name: kube-dns Namespace: kube-system Labels: k8s-app=kube-dns kubernetes.io/cluster-service=true kubernetes.io/name=KubeDNS Annotations: prometheus.io/port=9153 prometheus.io/scrape=true Selector: k8s-app=kube-dns Type: ClusterIP IP: 10.96.0.10 Port: dns 53/UDP TargetPort: 53/UDP Endpoints: 10.244.0.2:53,10.244.0.3:53 Port: dns-tcp 53/TCP TargetPort: 53/TCP Endpoints: 10.244.0.2:53,10.244.0.3:53 Session Affinity: None Events: <none> [root@k8smaster ~]# kubectl run client --image=busybox --replicas=1 --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 options ndots:5
七、get pod時查看label(service 與pod 之間是經過label關聯的)
[root@k8smaster ~]# kubectl get pods --show-labels -o wide NAME READY STATUS RESTARTS AGE IP NODE LABELS nginx-deploy-5b595999-d9lv5 1/1 Running 0 15m 10.244.2.4 k8snode2 pod-template-hash=16151555,run=nginx-deploy
八、kubectl edit #能夠編輯運行的service,不過當前版本應該只能夠查看不支持編輯
[root@k8smaster ~]# kubectl edit svc nginx
Edit cancelled, no changes made.
九、svc還有負載均衡的功能
[root@k8smaster ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2 deployment.apps/myapp created [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5hg7h 1/1 Running 0 2m myapp-848b5b879b-ptqjd 1/1 Running 0 2m nginx-deploy-5b595999-d9lv5 1/1 Running 0 56m [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myapp-848b5b879b-5hg7h 1/1 Running 0 3m 10.244.2.5 k8snode2 myapp-848b5b879b-ptqjd 1/1 Running 0 3m 10.244.1.4 k8snode1 nginx-deploy-5b595999-d9lv5 1/1 Running 0 57m 10.244.2.4 k8snode2 ^C[root@k8smaster ~]# kubectl get deployment -o wide -w #持續監控 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR myapp 2 2 2 2 4m myapp ikubernetes/myapp:v1 run=myapp nginx-deploy 1 1 1 1 4h nginx-deploy nginx:1.14-alpine run=nginx-deploy
建立svc並查看轉發狀況
[root@k8smaster ~]# kubectl expose deployment myapp --name=myapp --port=80 service/myapp exposed [root@k8smaster ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h myapp ClusterIP 10.106.171.207 <none> 80/TCP 12s nginx ClusterIP 10.103.127.92 <none> 80/TCP 1h [root@k8smaster ~]# kubectl describe svc myapp Name: myapp Namespace: default Labels: run=myapp Annotations: <none> Selector: run=myapp Type: ClusterIP IP: 10.106.171.207 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.4:80,10.244.2.5:80 Session Affinity: None Events: <none> [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-ptqjd [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-5hg7h [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-5hg7h [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-5hg7h [root@k8smaster ~]# curl 10.106.171.207/hostname.html myapp-848b5b879b-5hg7h [root@k8smaster ~]# curl 10.106.171.207/hostname.html
十、kubectl scale #動態改變副本數
[root@k8smaster ~]# kubectl scale --replicas=5 deployment myapp deployment.extensions/myapp scaled [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5hg7h 1/1 Running 0 22m myapp-848b5b879b-6fvr5 1/1 Running 0 30s myapp-848b5b879b-dpwpj 1/1 Running 0 30s myapp-848b5b879b-f77xt 1/1 Running 0 30s myapp-848b5b879b-ptqjd 1/1 Running 0 22m nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# while true; do curl 10.106.171.207/hostname.html;sleep 1; done myapp-848b5b879b-ptqjd myapp-848b5b879b-5hg7h myapp-848b5b879b-dpwpj myapp-848b5b879b-6fvr5 myapp-848b5b879b-dpwpj myapp-848b5b879b-f77xt myapp-848b5b879b-dpwpj myapp-848b5b879b-5hg7h myapp-848b5b879b-dpwpj myapp-848b5b879b-f77xt myapp-848b5b879b-dpwpj ^C [root@k8smaster ~]# kubectl scale --replicas=3 deployment myapp deployment.extensions/myapp scaled [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5hg7h 1/1 Running 0 25m myapp-848b5b879b-dpwpj 1/1 Running 0 3m myapp-848b5b879b-ptqjd 1/1 Running 0 25m nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# while true; do curl 10.106.171.207/hostname.html;sleep 1; done myapp-848b5b879b-ptqjd myapp-848b5b879b-dpwpj myapp-848b5b879b-ptqjd myapp-848b5b879b-5hg7h myapp-848b5b879b-ptqjd ^C
十一、kubelet set image 滾動升級更新
先查看各pod以及其鏡像,而後滾動更新後發現pod名稱和鏡像都發生了改變
[root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5hg7h 1/1 Running 0 30m myapp-848b5b879b-dpwpj 1/1 Running 0 8m myapp-848b5b879b-ptqjd 1/1 Running 0 30m nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# kubectl describe myapp-848b5b879b-5hg7h error: the server doesn't have a resource type "myapp-848b5b879b-5hg7h" [root@k8smaster ~]# kubectl describe pod myapp-848b5b879b-5hg7h Name: myapp-848b5b879b-5hg7h Namespace: default Priority: 0 PriorityClassName: <none> Node: k8snode2/192.168.10.12 Start Time: Wed, 08 May 2019 22:41:52 +0800 Labels: pod-template-hash=4046164356 run=myapp Annotations: <none> Status: Running IP: 10.244.2.5 Controlled By: ReplicaSet/myapp-848b5b879b Containers: myapp: Container ID: docker://54587de57edd701951f1e0492504a17be62c9fa18002f5bfc58b252ed536b029 Image: ikubernetes/myapp:v1 Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513 Port: <none> Host Port: <none> State: Running Started: Wed, 08 May 2019 22:42:45 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-jvtl7 (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-jvtl7: Type: Secret (a volume populated by a Secret) SecretName: default-token-jvtl7 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Pulling 18h kubelet, k8snode2 pulling image "ikubernetes/myapp:v1" Normal Pulled 18h kubelet, k8snode2 Successfully pulled image "ikubernetes/myapp:v1" Normal Created 18h kubelet, k8snode2 Created container Normal Started 18h kubelet, k8snode2 Started container Normal Scheduled 30m default-scheduler Successfully assigned default/myapp-848b5b879b-5hg7h to k8snode2 [root@k8smaster ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2 deployment.extensions/myapp image updated [root@k8smaster ~]# kubectl rollout status deployment myapp #查看滾動更新狀態 deployment "myapp" successfully rolled out [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-74c94dcb8c-ccqzs 1/1 Running 0 2m myapp-74c94dcb8c-jmj4p 1/1 Running 0 2m myapp-74c94dcb8c-lc2n6 1/1 Running 0 2m nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h
另啓動一個shell 訪問鏡像,能夠看到動態更新效果
[root@k8smaster ~]# while true; do curl 10.106.171.207;sleep 1; done Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
十二、kubectl rollout #版本回退(回滾)
[root@k8smaster ~]# curl 10.106.171.207 Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-74c94dcb8c-8l4n7 1/1 Running 0 11s myapp-74c94dcb8c-dzlfx 1/1 Running 0 14s myapp-74c94dcb8c-tsd2s 1/1 Running 0 12s nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# kubectl rollout undo deployment myapp #不加鏡像版本默認回退到上一個版本 deployment.extensions/myapp [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-74c94dcb8c-8l4n7 0/1 Terminating 0 22s myapp-74c94dcb8c-dzlfx 1/1 Terminating 0 25s myapp-74c94dcb8c-tsd2s 0/1 Terminating 0 23s myapp-848b5b879b-5k4s4 1/1 Running 0 5s myapp-848b5b879b-bzblz 1/1 Running 0 3s myapp-848b5b879b-hzbf5 1/1 Running 0 2s nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5k4s4 1/1 Running 0 13s myapp-848b5b879b-bzblz 1/1 Running 0 11s myapp-848b5b879b-hzbf5 1/1 Running 0 10s nginx-deploy-5b595999-d9lv5 1/1 Running 0 1h [root@k8smaster ~]# curl 10.106.171.207 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
還能夠自動擴縮容,不過須要和資源監控配合。
1三、能夠經過iptables -vnL查看iptables規則,能夠看到service是出如今其中的。
1四、經過修改svc 的type屬性爲NodePort 能夠在外部訪問到對應的pod 服務
[root@k8smaster ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h myapp ClusterIP 10.106.171.207 <none> 80/TCP 50m nginx ClusterIP 10.103.127.92 <none> 80/TCP 2h [root@k8smaster ~]# kubectl edit svc myapp service/myapp edited #打開後內容以下 # 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-05-09T08:28:47Z labels: run: myapp name: myapp namespace: default resourceVersion: "49560" selfLink: /api/v1/namespaces/default/services/myapp uid: 776ef2c3-7234-11e9-be24-000c29d142be spec: clusterIP: 10.106.171.207 externalTrafficPolicy: Cluster ports: - nodePort: 30935 port: 80 protocol: TCP targetPort: 80 selector: run: myapp sessionAffinity: None type: NodePort #此處由ClusterIP改成NodePort status: loadBalancer: {} [root@k8smaster ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h myapp NodePort 10.106.171.207 <none> 80:30935/TCP 51m nginx ClusterIP 10.103.127.92 <none> 80/TCP 2h [root@k8smaster ~]# curl 192.168.10.10:30935 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>