Kubernetes之快速部署应用

Posted wlbl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes之快速部署应用相关的知识,希望对你有一定的参考价值。

Kubernetes之快速部署应用

kubectl命令介绍

[[email protected] ~]# 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         使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 Kubernetes Service
  run            在集群中运行一个指定的镜像
  set            为 objects 设置一个指定的特征

Basic Commands (Intermediate):
  explain        查看资源的文档
  get            显示一个或更多 resources
  edit           在服务器上编辑一个资源
  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          为 Deployment, ReplicaSet, Replication Controller 或者 Job设置一个新的副本数量
  autoscale      自动调整一个 Deployment, ReplicaSet, 或者ReplicationController 的副本数量

Cluster Management Commands:
  certificate    修改 certificate 资源.
  cluster-info   显示集群信息
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         标记 node 为 unschedulable
  uncordon       标记 node 为 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:
  describe       显示一个指定 resource 或者 group 的 resources 详情
  logs           输出容器在 pod 中的日志
  attach         Attach 到一个运行中的 container
  exec           在一个 container 中执行一个命令
  port-forward   Forward one or more local ports to a pod
  proxy          运行一个 proxy 到 Kubernetes API server
  cp             复制 files 和 directories 到 containers
和从容器中复制 files 和 directories.
  auth           Inspect authorization

Advanced Commands:
  diff           Diff live version against would-be applied version
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for a specific condition on one or many
resources.
  convert        在不同的 API versions 转换配置文件

Settings Commands:
  label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or
zsh)

Other Commands:
  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         修改 kubeconfig 文件
  plugin         Provides utilities for interacting with plugins.
  version        输出 client 和 server 的版本信息

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 run命令行部署应用

语法:

Usage:
 kubectl run NAME --image=image [--env="key=value"] [--port=port]
[--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] --
[COMMAND] [args...] [options]

举例

[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deploy created (dry run)
#--image 指定镜像
#--port 暴漏端口
#--replicas 设置副本数量
# --dry-run=true 干跑模式,不执行,类似验证,无问题后再执行
[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
deployment.apps/nginx-deploy created
## 查看是否创建成功
#查看deployment
[[email protected] ~]# kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           30s
#查看pod
[[email protected] ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-84cbfc56b6-st4tq   1/1     Running   0          72s
# -o wide 显示更多详细信息
[[email protected] ~]# kubectl get pods -o wide 
NAME                            READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx-deploy-84cbfc56b6-st4tq   1/1     Running   0          98s   10.244.2.2   node02   <none>           <none>

从上面创建的应用可以得知,nginx-deploy应用的pod的ip为10.244.2.2,这是一个pod ip,仅仅可以在集群内部访问,如下:

[[email protected] ~]# curl -I 10.244.2.2
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 28 Mar 2019 02:23:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Mar 2019 03:09:00 GMT
Connection: keep-alive
ETag: "5c81dccc-264"
Accept-Ranges: bytes

[[email protected] ~]# curl -I 10.244.2.2
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 28 Mar 2019 02:23:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Mar 2019 03:09:00 GMT
Connection: keep-alive
ETag: "5c81dccc-264"
Accept-Ranges: bytes

[[email protected] ~]# curl -I 10.244.2.2
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 28 Mar 2019 02:23:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Mar 2019 03:09:00 GMT
Connection: keep-alive
ETag: "5c81dccc-264"
Accept-Ranges: bytes

尝试删除Pod

[[email protected] ~]# kubectl delete pods nginx-deploy-84cbfc56b6-st4tq
pod "nginx-deploy-84cbfc56b6-st4tq" deleted
[[email protected] ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx-deploy-84cbfc56b6-w66pw   1/1     Running   0          53s   10.244.2.3   node02   <none>           <none>

可以看到删除Pod后又重新开起了一个Pod,因为pod是被控制器管理的(此处为deployment),当Pod数量不满足控制器的desired值得时候,会自动补全Pod(随机在集群内满足要求得node节点创建),来满足期望值,同样的如果过多也会关掉多余得Pod。

此时Pod得IP已经发生变化,所以如果需要被外部访问就需要提供一个固定得端点,就是接下来说得Service:

kubectl expose 通过service暴漏Pod

语法

Usage:
  kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]
#--port=port 这里是服务端口
#--target-port 容器端口
#--type=type service类型,分为Custer IP ,NodePort,LoadBalancer,ExternalName,默认是Custer IP
后面会详细讲解

举例

#将name为nginx-deploy的deployment暴漏为80端口
[[email protected] ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP                 
service/nginx-deploy exposed
#查看服务 services 简写为svc
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   18h
nginx        ClusterIP   10.98.2.12   <none>        80/TCP    3s    35s

此时在节点访问svc的80端口来访问pod,即使Pod被删除后重建也可以访问

#Pod在node02上,次数使用node01来访问
[[email protected] ~]# curl -I 10.98.2.12
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 28 Mar 2019 02:48:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Mar 2019 03:09:00 GMT
Connection: keep-alive
ETag: "5c81dccc-264"
Accept-Ranges: bytes
#删除Pod后再次尝试
[[email protected] ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-84cbfc56b6-w66pw   1/1     Running   0          20m
[[email protected] ~]# kubectl delete pods nginx-deploy-84cbfc56b6-w66pw
pod "nginx-deploy-84cbfc56b6-w66pw" deleted
[[email protected] ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx-deploy-84cbfc56b6-tcssz   1/1     Running   0          6s    10.244.2.4   node02   <none>           <none>
[[email protected] ~]# curl -I 10.98.2.12
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 28 Mar 2019 02:49:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 08 Mar 2019 03:09:00 GMT
Connection: keep-alive
ETag: "5c81dccc-264"
Accept-Ranges: bytes

10.99.146.96这网段依然是集群内部的网段,只能被集群内部客户端访问,外部是无法通过service的ip进行访问。那么针对pod的客户端除了通过service ip访问还可以通过service的名称进行访问,但是前提是需要对service的名称能够进行解析。而解析时是依赖coredns服务的,而我们本地的dns指向并非coredns,如下:

[[email protected] ~]# curl -I nginx
curl: (6) Could not resolve host: nginx-deploy; 未知的错误
#查看节点的dns地址
[[email protected] ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 10.0.0.2

查看coredns的IP地址

[[email protected] ~]# kubectl get pods -n kube-system -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
coredns-78d4cf999f-6cb69         1/1     Running   0          18h   10.244.1.2   node01   <none>           <none>
coredns-78d4cf999f-tflpn         1/1     Running   0          18h   10.244.1.3   node01   <none>           <none>
etcd-master                      1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
kube-apiserver-master            1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
kube-controller-manager-master   1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
kube-flannel-ds-amd64-gtv85      1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
kube-flannel-ds-amd64-gwbql      1/1     Running   1          18h   10.0.0.12    node02   <none>           <none>
kube-flannel-ds-amd64-ml7nf      1/1     Running   0          18h   10.0.0.11    node01   <none>           <none>
kube-proxy-ch4vp                 1/1     Running   0          18h   10.0.0.11    node01   <none>           <none>
kube-proxy-cz2rf                 1/1     Running   1          18h   10.0.0.12    node02   <none>           <none>
kube-proxy-kdp7d                 1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
kube-scheduler-master            1/1     Running   0          18h   10.0.0.10    master   <none>           <none>
#-n指定名称空间

正常来说,也不会直接访问coredns的Pod的IP来直接访问,也是通过coredns的service进行访问

[[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   18h

可以通过这个service ip:10.96.0.10进行解析上面的nginx服务。
提前准备dig命令 yum install -y bind-utils,这里需要使用完整的服务名称,否则可能由于dns搜索域问题导致失败

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

; <<>> DiG 9.9.4-RedHat-9.9.4-73.el7_6 <<>> -t A nginx.default.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58335
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

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

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

;; Query time: 1 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: 四 3月 28 11:02:11 CST 2019
;; MSG SIZE  rcvd: 96

尝试通过集群内pod客户端进行访问:

[[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
options ndots:5
/ # wget -O - -q http://nginx:80
<!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>

此时name为nginx的service就为name为nginx-deploy的deployment提供了固定访问断电,无论deployment内Pod的增加删除,都不影响访问,使用命令来查看services的详细信息:

[[email protected] ~]# kubectl describe svc nginx
Name:              nginx
Namespace:         default
Labels:            run=nginx-deploy
Annotations:       <none>
Selector:          run=nginx-deploy
Type:              ClusterIP
IP:                10.98.2.12
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.4:80
Session Affinity:  None
Events:            <none>

#Endpoints: Pod的IP地址

无论Pod怎么增删,只要标Labels存在run=nginx-deploy的 就会被svc锁访问。

[[email protected] ~]# kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE   LABELS
client                          1/1     Running   0          28m   run=client
nginx-deploy-84cbfc56b6-tcssz   1/1     Running   0          51m   pod-template-hash=84cbfc56b6,run=nginx-deploy

run=nginx-deploy就是这个应用的标签,所以当pod的改变,并不会影响service的访问。

kubectl scale 动态伸缩应用副本

语法

Usage:
  kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)

举例

#创建新的deployment
[[email protected] ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/myapp created
[[email protected] ~]# kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp          2/2     2            2           48s
nginx-deploy   1/1     1            1           3h58m
#查看pod
[[email protected] ~]# kubectl get pods -o wide
NAME                            READY   STATUS      RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
client                          0/1     Completed   0          3h2m    10.244.2.5   node02   <none>           <none>
myapp-9b4987d5-98k45            1/1     Running     0          64s     10.244.1.4   node01   <none>           <none>
myapp-9b4987d5-ncpb7            1/1     Running     0          64s     10.244.2.6   node02   <none>           <none>
nginx-deploy-84cbfc56b6-tcssz   1/1     Running     0          3h26m   10.244.2.4   node02   <none>           <none>

此时使用客户端访问

/ # wget -O - -q 10.244.2.6/hostname.html
myapp-9b4987d5-ncpb7
/ # wget -O - -q 10.244.1.4/hostname.html
myapp-9b4987d5-98k45

为myapp创建service

[[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   22h
myapp        ClusterIP   10.104.138.182   <none>        80/TCP    3s
nginx        ClusterIP   10.98.2.12       <none>        80/TCP    3h21m

所生成的myapp的地址为10.104.138.182,端口暴漏为80,协议默认为TCP
使用客户端访问myapp服务

Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-98k45
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-98k45
/ # wget -O - -q myapp/hostname.html
myapp-9b4987d5-ncpb7
#hostname.html显示的是pod的名字,也是pod的hostname

扩展副本数量

[[email protected] ~]# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled
[[email protected] ~]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
client-f5cdb799f-pklmc          1/1     Running   0          5m46s   10.244.2.7   node02   <none>           <none>
myapp-9b4987d5-98k45            1/1     Running   0          8m44s   10.244.1.4   node01   <none>           <none>
myapp-9b4987d5-ck9bt            1/1     Running   0          9s      10.244.2.8   node02   <none>           <none>
myapp-9b4987d5-gfqjp            1/1     Running   0          9s      10.244.2.9   node02   <none>           <none>
myapp-9b4987d5-ncpb7            1/1     Running   0          8m44s   10.244.2.6   node02   <none>           <none>
myapp-9b4987d5-wrp7r            1/1     Running   0          9s      10.244.1.5   node01   <none>           <none>
nginx-deploy-84cbfc56b6-tcssz   1/1     Running   0          3h33m   10.244.2.4   node02   <none>           <none>

此时使用客户端访问myapp服务

/ # for i in `seq 10000`;do wget -O - -q myapp/hostname.html;sleep 1;done
myapp-9b4987d5-wrp7r
myapp-9b4987d5-ncpb7
myapp-9b4987d5-wrp7r
myapp-9b4987d5-98k45
myapp-9b4987d5-wrp7r
myapp-9b4987d5-98k45
myapp-9b4987d5-gfqjp
myapp-9b4987d5-wrp7r
myapp-9b4987d5-gfqjp
myapp-9b4987d5-gfqjp
myapp-9b4987d5-ncpb7

再次修改副本数量为3

[[email protected] ~]# kubectl get pods -o wide                   
NAME                            READY   STATUS        RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
client-f5cdb799f-pklmc          1/1     Running       0          8m32s   10.244.2.7   node02   <none>           <none>
myapp-9b4987d5-98k45            1/1     Running       0          11m     10.244.1.4   node01   <none>           <none>
myapp-9b4987d5-ck9bt            0/1     Terminating   0          2m55s   10.244.2.8   node02   <none>           <none>
myapp-9b4987d5-gfqjp            0/1     Terminating   0          2m55s   10.244.2.9   node02   <none>           <none>
myapp-9b4987d5-ncpb7            1/1     Running       0          11m     10.244.2.6   node02   <none>           <none>
myapp-9b4987d5-wrp7r            1/1     Running       0          2m55s   10.244.1.5   node01   <none>           <none>
nginx-deploy-84cbfc56b6-tcssz   1/1     Running       0          3h36m   10.244.2.4   node02   <none>           <none>

#客户端查看
myapp-9b4987d5-98k45
myapp-9b4987d5-98k45
myapp-9b4987d5-wrp7r
myapp-9b4987d5-wrp7r
myapp-9b4987d5-wrp7r
myapp-9b4987d5-wrp7r
myapp-9b4987d5-ncpb7
myapp-9b4987d5-ncpb7
myapp-9b4987d5-98k45

此时服务就只会访问剩余的3个副本间

kubectl set image 应用版本升级

语法
Usage:
kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
[options]
举例

#升级myapp的版本为v2
[[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: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
deployment "myapp" successfully rolled out

#此过程客户端过程
/ # for i in `seq 10000`;do wget -O - -q myapp;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: 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: v2 | <a href="hostname.html">Pod Name</a>
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

也可以使用kubectl describe 来查看Pod的详细信息

[[email protected] ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
client-f5cdb799f-pklmc          1/1     Running   0          21m
myapp-65899575cd-q9wt9          1/1     Running   0          4m56s
myapp-65899575cd-xhwmr          1/1     Running   0          4m58s
myapp-65899575cd-xnqd4          1/1     Running   0          4m58s
nginx-deploy-84cbfc56b6-tcssz   1/1     Running   0          3h49m
[[email protected] ~]# kubectl describe pods myapp-65899575cd-xhwmr
Name:               myapp-65899575cd-xhwmr
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node01/10.0.0.11
Start Time:         Thu, 28 Mar 2019 14:33:50 +0800
Labels:             pod-template-hash=65899575cd
                    run=myapp
Annotations:        <none>
Status:             Running
IP:                 10.244.1.10
Controlled By:      ReplicaSet/myapp-65899575cd
Containers:
  myapp:
    Container ID:   docker://5f9c63ce9f84fb6f863b44dd2eb9626f3eff2a599b48eb7000c84f5b70f6d2c9
    Image:          ikubernetes/myapp:v2
... ...

kubectl rollout 回滚

一种方法是直接修改image版本,另外一种是使用kubectl rollout

[[email protected] ~]# kubectl rollout -h
Manage the rollout of a resource.
  
Valid resource types include: 

  * deployments  
  * daemonsets  
  * statefulsets

Examples:
  # Rollback to the previous deployment
  kubectl rollout undo deployment/abc
  
  # Check the rollout status of a daemonset
  kubectl rollout status daemonset/foo

Available Commands:
  history     显示 rollout 历史
  pause       标记提供的 resource 为中止状态
  resume      继续一个停止的 resource
  status      显示 rollout 的状态
  undo        撤销上一次的 rollout

Usage:
  kubectl rollout SUBCOMMAND [options]

举例

[[email protected] ~]# kubectl rollout undo deployment myapp
deployment.extensions/myapp rolled back
[[email protected] ~]# kubectl get pods
NAME                            READY   STATUS              RESTARTS   AGE
client-f5cdb799f-pklmc          1/1     Running             0          24m
myapp-65899575cd-q9wt9          0/1     Terminating         0          8m15s
myapp-65899575cd-xhwmr          1/1     Terminating         0          8m17s
myapp-65899575cd-xnqd4          1/1     Running             0          8m17s
myapp-9b4987d5-47sjj            0/1     ContainerCreating   0          0s
myapp-9b4987d5-684q9            1/1     Running             0          2s
myapp-9b4987d5-djdr9            1/1     Running             0          3s
nginx-deploy-84cbfc56b6-tcssz   1/1     Running             0          3h52m

#客户端查看
/ # for i in `seq 10000`;do wget -O - -q myapp;sleep 1;done
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: 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: 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>
#已经全部回退到上个版本

集群外部访问Service

[[email protected] ~]# kubectl edit svc myapp
# 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-03-28T06:20:00Z"
  labels:
    run: myapp
  name: myapp
  namespace: default
  resourceVersion: "36470"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: 8425bae4-5121-11e9-80a7-000c295ec349
spec:
  clusterIP: 10.104.138.182
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: myapp
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}                  

查看服务

[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        22h
myapp        NodePort    10.104.138.182   <none>        80:30298/TCP   38m
nginx        ClusterIP   10.98.2.12       <none>        80/TCP         4h

myapp服务80端口映射到节点的30298,且整个集群的30298节点都可以访问
技术图片

另一节点访问

技术图片

参考资料

https://www.cnblogs.com/linuxk
马永亮. Kubernetes进阶实战 (云计算与虚拟化技术丛书)

以上是关于Kubernetes之快速部署应用的主要内容,如果未能解决你的问题,请参考以下文章

k8s之Helm(快速下载yaml文件模板)

replicatedhq-ship 基于Kustomize 项目的快速kubernetes 应用部署工具

kubernetes学习01—kubernetes介绍

第154天学习打卡(Kubernetes 使用Helm快速部署应用, 如何自己创建Chart)

一分钟教你快速部署Kubernetes应用

Kubernetes(k8s)之Helm的web UI界面管理应用kubeapps