理论+实操:kubectl命令管理


kubectl 管理

kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给APIserver进行创建、查看、管理的操作

一:查看kubectl信息

[root@master1 dashboard]# kubectl --help
kubectl controls the Kubernetes cluster manager. 
#kubectl控制Kubernetes集群管理器。

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 设置一个指定的特征
  #比如set 可以修改版本号
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:
  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:
  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         修改 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]
Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@master1 bin]# kubectl run --help
创建并运行一个可能被复制的特定映像。
Creates a deployment or job to manage the created container(s).
#创建部署或作业来管理创建的容器。
例子:
  # 启动nginx的单个实例。
  kubectl run nginx --image=nginx
  # 启动hazelcast的单个实例,并让容器公开端口5701。
  kubectl run hazelcast --image=hazelcast --port=5701
  # 启动hazelcast的单个实例并设置环境变量“DNS_DOMAIN=cluster”和“POD_NAMESPACE=default”在容器中。
  kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
  # 启动一个hazelcast实例,并在容器中设置标签“app=hazelcast”和“env=prod”。
  kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"
  #启动nginx的一个复制实例。
  kubectl run nginx --image=nginx --replicas=5
  
  # 干燥的运行。打印相应的API对象而不创建它们。
  kubectl run nginx --image=nginx --dry-run
  
  # 启动nginx的单个实例,但是使用从json解析的部分值集合来重载部署规范。
  kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
  
  # 启动一组busybox并将其放在前台,如果它退出,不要重新启动它。
  kubectl run -i -t busybox --image=busybox --restart=Never
  
  # 使用默认命令启动nginx容器,但是使用自定义参数(arg1 ..对于那个命令。
  kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
  
  # 使用不同的命令和自定义参数启动nginx容器。
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
  
  # 启动cron作业来计算π- 2000每5分钟和打印出来的地方。
  kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
  
  # 启动cron作业来计算π- 2000每5分钟和打印出来的地方。
  kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

选项:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --attach=false: If true, wait for the Pod to start running, and then attach to the Pod as if 'kubectl attach ...'
were called.  Default false, unless '-i/--stdin' is set, in which case the default is true. With '--restart=Never' the
exit code of the container process is returned.
      --cascade=true: If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a
ReplicationController).  Default true.
      --command=false: If true and extra arguments are present, use them as the 'command' field in the container, rather
than the 'args' field which is the default.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
      --env=[]: Environment variables to set in the container
      --expose=false: If true, a public, external service is created for the container(s) which are run
  -f, --filename=[]: to use to replace the resource.
      --force=false: Only used when grace-period=0. If true, immediately remove resources from API and bypass graceful
deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires
confirmation.
      --generator='': 使用 API generator 的名字, 在
http://kubernetes.io/docs/user-guide/kubectl-conventions/#generators 查看列表.
      --grace-period=-1: Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.
Set to 1 for immediate shutdown. Can only be set to 0 when --force is true (force deletion).
      --hostport=-1: The host port mapping for the container port. To demonstrate a single-machine container.
      --image='': 指定容器要运行的镜像.
      --image-pull-policy='': 容器的镜像拉取策略. 如果为空, 这个值将不会 被 client 指定且使用
server 端的默认值
  -l, --labels='': Comma separated labels to apply to the pod(s). Will override previous values.
      --leave-stdin-open=false: If the pod is started in interactive mode or with stdin, leave stdin open after the
first attach completes. By default, stdin will be closed after the first attach completes.
      --limits='': The resource requirement limits for this container.  For example, 'cpu=200m,memory=512Mi'.  Note that
server side components may assign limits depending on the server configuration, such as limit ranges.
  -o, --output='': Output format. One of:
json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.
      --overrides='': An inline JSON override for the generated object. If this is non-empty, it is used to override the
generated object. Requires that the object supply a valid apiVersion field.
      --pod-running-timeout=1m0s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one
pod is running
      --port='': The port that this container exposes.  If --expose is true, this is also the port used by the service
that is created.
      --quiet=false: If true, suppress prompt messages.
      --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
  -r, --replicas=1: Number of replicas to create for this container. Default is 1.
      --requests='': 资源为 container 请求 requests . 例如, 'cpu=100m,memory=256Mi'.
注意服务端组件也许会赋予 requests, 这决定于服务器端配置, 比如 limit ranges.
      --restart='Always': 这个 Pod 的 restart policy.  Legal values [Always, OnFailure, Never]. 如果设置为
'Always' 一个 deployment 被创建, 如果设置为 ’OnFailure' 一个 job 被创建, 如果设置为 'Never',
一个普通的 pod 被创建. 对于后面两个 --replicas 必须为 1.  默认 'Always', 为 CronJobs 设置为
`Never`.
      --rm=false: If true, delete resources created in this command for attached containers.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
      --schedule='': A schedule in the Cron format the job should be run with.
      --service-generator='service/v2': 使用 gnerator 的名称创建一个 service.  只有在 --expose 为 true
的时候使用
      --service-overrides='': An inline JSON override for the generated service object. If this is non-empty, it is used
to override the generated object. Requires that the object supply a valid apiVersion field.  Only used if --expose is
true.
      --serviceaccount='': Service account to set in the pod spec
  -i, --stdin=false: Keep stdin open on the container(s) in the pod, even if nothing is attached.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
      --timeout=0s: The length of time to wait before giving up on a delete, zero means determine a timeout from the
size of the object
  -t, --tty=false: Allocated a TTY for each container in the pod.
      --wait=false: If true, wait for resources to be gone before returning. This waits for finalizers.

2.1 创建一个nginx,副本集为3

控制器deployment自动创建

[root@master1 dashboard]# kubectl run nginx-gsy --image=nginx --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-gsy created

查看pods

pod一个个创建,再次查看都在运行状态

[root@master1 dashboard]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
nginx-dbddb74b8-sx4m6        1/1     Running             0          5d17h
nginx-gsy-668fbbd8f5-7v8kv   1/1     Running             0          44s
nginx-gsy-668fbbd8f5-brwjf   1/1     Running             0          44s
nginx-gsy-668fbbd8f5-zkrh6   0/1     ContainerCreating   0          44s
[root@master1 dashboard]# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-sx4m6        1/1     Running   0          5d17h
nginx-gsy-668fbbd8f5-7v8kv   1/1     Running   0          62s
nginx-gsy-668fbbd8f5-brwjf   1/1     Running   0          62s
nginx-gsy-668fbbd8f5-zkrh6   1/1     Running   0          62s

–replicas=3代表创建三个资源

kubectl get pods -w 动态监控pod信息

查看pod的网络状态,发现都创建在144节点

[root@master1 dashboard]# kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE
nginx-dbddb74b8-sx4m6        1/1     Running   0          5d17h   172.17.42.3   192.168.247.144   <none>
nginx-gsy-668fbbd8f5-7v8kv   1/1     Running   0          2m56s   172.17.42.4   192.168.247.144   <none>
nginx-gsy-668fbbd8f5-brwjf   1/1     Running   0          2m56s   172.17.42.6   192.168.247.144   <none>
nginx-gsy-668fbbd8f5-zkrh6   1/1     Running   0          2m56s   172.17.42.7   192.168.247.144   <none>

查看节点状态

[root@master1 dashboard]# kubectl get nodes
NAME              STATUS     ROLES    AGE   VERSION
192.168.247.143   NotReady   <none>   8d    v1.12.3
192.168.247.144   Ready      <none>   8d    v1.12.3

2.2解决node节点掉线问题

此时重启143的kubelet

[root@node01 ~]# systemctl restart kubelet
[root@node01 ~]# systemctl status kubelet
● kubelet.service - Kubernetes Kubelet
   Loaded: loaded (/usr/lib/systemd/system/kubelet.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-05-08 13:02:00 CST; 20s ago

然后查看master中的请求证书,重新颁发

[root@master1 dashboard]# kubectl get csr
NAME                                                   AGE    REQUESTOR           CONDITION
node-csr-Xy9S6WNkga8w0P36-gJKl_mnm__GAZ8LZ7Gwg1GTXco   127m   kubelet-bootstrap   Pending
[root@master1 dashboard]# kubectl certificate approve node-csr-Xy9S6WNkga8w0P36-gJKl_mnm__GAZ8LZ7Gwg1GTXco
certificatesigningrequest.certificates.k8s.io/node-csr-Xy9S6WNkga8w0P36-gJKl_mnm__GAZ8LZ7Gwg1GTXco approved
[root@master1 dashboard]# kubectl get csr
NAME                                                   AGE    REQUESTOR           CONDITION
node-csr-Xy9S6WNkga8w0P36-gJKl_mnm__GAZ8LZ7Gwg1GTXco   128m   kubelet-bootstrap   Approved,Issued
[root@master1 dashboard]# kubectl get nodes
NAME              STATUS   ROLES    AGE   VERSION
192.168.247.143   Ready    <none>   8d    v1.12.3
192.168.247.144   Ready    <none>   8d    v1.12.3

2.3 删掉之前创建的pod资源,重新执行

删除资源,删的是控制器

[root@master1 dashboard]# kubectl delete deployment/nginx-gsy
deployment.extensions "nginx-gsy" deleted
[root@master1 dashboard]# kubectl get all
NAME                             READY   STATUS        RESTARTS   AGE
pod/nginx-dbddb74b8-sx4m6        1/1     Running       0          5d17h
pod/nginx-gsy-668fbbd8f5-7v8kv   0/1     Terminating   0          14m
pod/nginx-gsy-668fbbd8f5-brwjf   0/1     Terminating   0          14m
pod/nginx-gsy-668fbbd8f5-zkrh6   0/1     Terminating   0          14m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   8d

NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1         1         1            1           5d17h

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dbddb74b8   1         1         1       5d17h
[root@master1 dashboard]# kubectl get pods -w
NAME                    READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-sx4m6   1/1     Running   0          5d17h

重新创建

[root@master1 dashboard]# kubectl run nginx-gsy --image=nginx --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-gsy created
[root@master1 dashboard]# kubectl get pods -w
NAME                         READY   STATUS              RESTARTS   AGE
nginx-dbddb74b8-sx4m6        1/1     Running             0          5d17h
nginx-gsy-668fbbd8f5-ch66w   0/1     ContainerCreating   0          3s
nginx-gsy-668fbbd8f5-mmcd4   0/1     ContainerCreating   0          3s
nginx-gsy-668fbbd8f5-q72rw   0/1     ContainerCreating   0          3s
nginx-gsy-668fbbd8f5-mmcd4   1/1   Running   0     5s
nginx-gsy-668fbbd8f5-q72rw   1/1   Running   0     15s
nginx-gsy-668fbbd8f5-ch66w   1/1   Running   0     16s
^C[root@master1 dashboard]# 

此时再查看网络状态

加上原有的一个pod资源,四个pod被分摊到各个节点

^C[root@master1 dashboard]# kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE
nginx-dbddb74b8-sx4m6        1/1     Running   0          5d17h   172.17.42.3   192.168.247.144   <none>
nginx-gsy-668fbbd8f5-ch66w   1/1     Running   0          61s     172.17.45.3   192.168.247.143   <none>
nginx-gsy-668fbbd8f5-mmcd4   1/1     Running   0          61s     172.17.42.4   192.168.247.144   <none>
nginx-gsy-668fbbd8f5-q72rw   1/1     Running   0          61s     172.17.45.4   192.168.247.143   <none>

查看其他,service,deployment,replicaset

[root@master1 dashboard]# kubectl get all
NAME                             READY   STATUS    RESTARTS   AGE
pod/nginx-dbddb74b8-sx4m6        1/1     Running   0          5d17h
pod/nginx-gsy-668fbbd8f5-ch66w   1/1     Running   0          117s
pod/nginx-gsy-668fbbd8f5-mmcd4   1/1     Running   0          117s
pod/nginx-gsy-668fbbd8f5-q72rw   1/1     Running   0          117s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   8d

NAME                        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx       1         1         1            1           5d17h
deployment.apps/nginx-gsy   3         3         3            3           117s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dbddb74b8        1         1         1       5d17h
replicaset.apps/nginx-gsy-668fbbd8f5   3         3         3       117s

2.4 测试,删掉刚才创建的其中一个pod

[root@master1 dashboard]# kubectl delete nginx-gsy-668fbbd8f5-mmcd4
error: resource(s) were provided, but no name, label selector, or --all flag specified
错误:提供了资源,但没有指定名称、标签选择器或——all标志

在创建资源时,pod创建的同时,控制器deployment和副本集replicaset都会同时创建出来

三:expose发布nginx,创建service服务

service服务提供负载均衡的功能

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]

Use "kubectl options" for a list of global command-line options (applies to all commands).
[root@master1 bin]# kubectl expose --help
Expose a resource as a new Kubernetes service. 

Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that
resource as the selector for a new service on the specified port. A deployment or replica set will be exposed as a
service only if its selector is convertible to a selector that service supports, i.e. when the selector contains only
the matchLabels component. Note that if no port is specified via --port and the exposed resource has multiple ports, all
will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the
resource it exposes. 

Possible resources include (case insensitive): 

pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)

Examples:
  # Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000.
  kubectl expose rc nginx --port=80 --target-port=8000
  
  # Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml",
which serves on port 80 and connects to the containers on port 8000.
  kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
  
  # Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
  kubectl expose pod valid-pod --port=444 --name=frontend
  
  # Create a second service based on the above service, exposing the container port 8443 as port 443 with the name
"nginx-https"
  kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
  
  # Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
  kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
  
  # Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on
port 8000.
  kubectl expose rs nginx --port=80 --target-port=8000
  
  # Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000.
  kubectl expose deployment nginx --port=80 --target-port=8000

Options:
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --cluster-ip='': ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create
a headless service.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
      --external-ip='': Additional external IP address (not managed by Kubernetes) to accept for the service. If this IP
is routed to a node, the service can be accessed by this IP in addition to its generated service IP.
  -f, --filename=[]: Filename, directory, or URL to files identifying the resource to expose a service
      --generator='service/v2': 使用 generator 的名称. 这里有 2 个 generators: 'service/v1' 和 'service/v2'.
为一个不同地方是服务端口在 v1 的情况下叫 'default', 如果在 v2 中没有指定名称.
默认的名称是 'service/v2'.
  -l, --labels='': Labels to apply to the service created by this call.
      --load-balancer-ip='': IP to assign to the LoadBalancer. If empty, an ephemeral IP will be created and used
(cloud-provider specific).
      --name='': 名称为最新创建的对象.
  -o, --output='': Output format. One of:
json|yaml|name|templatefile|template|go-template|go-template-file|jsonpath|jsonpath-file.
      --overrides='': An inline JSON override for the generated object. If this is non-empty, it is used to override the
generated object. Requires that the object supply a valid apiVersion field.
      --port='': 服务的端口应该被指定. 如果没有指定, 从被创建的资源中复制
      --protocol='': 创建 service 的时候伴随着一个网络协议被创建. 默认是 'TCP'.
      --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
      --selector='': A label selector to use for this service. Only equality-based selector requirements are supported.
If empty (the default) infer the selector from the replication controller or replica set.)
      --session-affinity='': If non-empty, set the session affinity for the service to this; legal values: 'None',
'ClientIP'
      --target-port='': Name or number for the port on the container that the service should direct traffic to.
Optional.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
      --type='': Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'.

3.1 expose发布

[root@master1 dashboard]# kubectl expose deployment nginx-gsy --port=80 --target-port=80 --name=nginx-gsy-service --type=nodeport
The Service "nginx-gsy-service" is invalid: spec.type: Unsupported value: "nodeport": supported values: "ClusterIP", "ExternalName", "LoadBalancer", "NodePort"
[root@master1 dashboard]# kubectl expose deployment nginx-gsy --port=80 --target-port=80 --name=nginx-gsy-service --type=NodePort
service/nginx-gsy-service exposed
[root@master1 dashboard]# kubectl get service
NAME                TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes          ClusterIP   10.0.0.1     <none>        443/TCP        8d
nginx-gsy-service   NodePort    10.0.0.204   <none>        80:49003/TCP   38s

3.2 在查看资源时,可以将资源名称简写

简写格式如下

[root@master1 bin]# kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
events                            ev           events.k8s.io                  true         Event
daemonsets                        ds           extensions                     true         DaemonSet
deployments                       deploy       extensions                     true         Deployment
ingresses                         ing          extensions                     true         Ingress
networkpolicies                   netpol       extensions                     true         NetworkPolicy
podsecuritypolicies               psp          extensions                     false        PodSecurityPolicy
replicasets                       rs           extensions                     true         ReplicaSet
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

3.3 查看服务关联后端的节点

[root@master1 dashboard]# kubectl get endpoints
NAME                ENDPOINTS                                      AGE
kubernetes          192.168.247.148:6443,192.168.247.149:6443      8d
nginx-gsy-service   172.17.42.4:80,172.17.45.3:80,172.17.45.4:80   3m41s

3.4 查看网络状态详细信息

[root@master1 dashboard]# kubectl get all -o wide
NAME                             READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE
pod/nginx-dbddb74b8-sx4m6        1/1     Running   0          5d17h   172.17.42.3   192.168.247.144   <none>
pod/nginx-gsy-668fbbd8f5-ch66w   1/1     Running   0          12m     172.17.45.3   192.168.247.143   <none>
pod/nginx-gsy-668fbbd8f5-mmcd4   1/1     Running   0          12m     172.17.42.4   192.168.247.144   <none>
pod/nginx-gsy-668fbbd8f5-q72rw   1/1     Running   0          12m     172.17.45.4   192.168.247.143   <none>

NAME                        TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/kubernetes          ClusterIP   10.0.0.1     <none>        443/TCP        8d    <none>
service/nginx-gsy-service   NodePort    10.0.0.204   <none>        80:49003/TCP   64s   run=nginx-gsy

NAME                        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
deployment.apps/nginx       1         1         1            1           5d17h   nginx        nginx    run=nginx
deployment.apps/nginx-gsy   3         3         3            3           12m     nginx-gsy    nginx    run=nginx-gsy

NAME                                   DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
replicaset.apps/nginx-dbddb74b8        1         1         1       5d17h   nginx        nginx    pod-template-hash=dbddb74b8,run=nginx
replicaset.apps/nginx-gsy-668fbbd8f5   3         3         3       12m     nginx-gsy    nginx    pod-template-hash=668fbbd8f5,run=nginx-gsy

3.5 service将node节点的49003负载均衡,可以到node节点上查看

k8s例kube-proxy支持三种模式,在1.8之前我们使用的时iptables以及userspace两种模式,在kubernetes 1.8之后引入了ipvs模式

[root@node01 ~]# yum install ipvsadm -y 
[root@node01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.247.143:49003 rr
  -> 172.17.42.4:80               Masq    1      0          0         
  -> 172.17.45.3:80               Masq    1      0          0         
  -> 172.17.45.4:80               Masq    1      0          0                

3.6 在master操作,查看三个pod的访问日志

备注:如果访问其他node便无法访问检查proxy组件

访问一次
在这里插入图片描述

[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-ch66w
[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-mmcd4
[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-q72rw
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/08 05:34:48 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.45.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.247.143:49003", referrer: "http://192.168.247.143:49003/"
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.247.143:49003/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

刷新访问第二次

[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-mmcd4
[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-ch66w
[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-q72rw
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/08 05:34:48 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.45.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.247.143:49003", referrer: "http://192.168.247.143:49003/"
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.247.143:49003/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
172.17.45.1 - - [08/May/2020:05:35:57 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

刷新访问第三次

[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-q72rw
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/08 05:34:48 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.45.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.247.143:49003", referrer: "http://192.168.247.143:49003/"
172.17.45.1 - - [08/May/2020:05:34:48 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.247.143:49003/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
172.17.45.1 - - [08/May/2020:05:35:57 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
172.17.45.1 - - [08/May/2020:05:36:49 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

可能是因为会话保持,所以三次都访问同一个pod

清空历史数据,重新访问

第一次依旧访问q72rw

第二次访问ch66w

[root@master1 dashboard]# kubectl logs nginx-gsy-668fbbd8f5-ch66w
172.17.45.1 - - [08/May/2020:05:40:35 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/08 05:40:35 [error] 6#6: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.45.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.247.143:49003", referrer: "http://192.168.247.143:49003/"
172.17.45.1 - - [08/May/2020:05:40:35 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.247.143:49003/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"

四: 更新nginx版本

更新nginx版本为1.14

4.1 先查看当前nginx版本

为1.17.10
在这里插入图片描述

4.2 kubectl set 查看信息

[root@master1 bin]# kubectl set --help
Configure application resources 

These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          更新一个 pod template 的镜像
  resources      在对象的 pod templates 上更新资源的 requests/limits
  selector       设置 resource 的 selector
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding

Usage:
  kubectl set SUBCOMMAND [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 set image

[root@master1 bin]# kubectl set image --help
更新资源的现有容器映像。

可能的资源包括(不区分大小写):

  pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)

Examples:
  # 将部署的nginx容器映像设置为'nginx:1.9.1',将其busybox容器映像设置为'busybox'。
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
  
  # 将所有部署'和rc的nginx容器映像更新为'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all
  
  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1
  
  # Print result (in yaml format) of updating nginx container image from local file, without hitting the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Options:
      --all=false: Select all resources, including uninitialized ones, in the namespace of the specified resource types
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --dry-run=false: If true, only print the object that would be sent, without sending it.
  -f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a server.
      --include-uninitialized=false: If true, the kubectl command applies to uninitialized objects. If explicitly set to
false, this flag overrides other flags that make the kubectl commands apply to uninitialized objects, e.g., "--all".
Objects with empty metadata.initializers are regarded as initialized.
      --local=false: If true, set image will NOT contact api-server but run locally.
  -o, --output='': Output format. One of:
json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.
      --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
  -l, --selector='': Selector (label query) to filter on, not including uninitialized ones, supports '=', '==', and
'!='.(e.g. -l key1=value1,key2=value2)
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

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

Use "kubectl options" for a list of global command-line options (applies to all commands).

4.3 获取修改模板

kubectl set image deplo/nginx busybox=busybox nginx=nginx:1.9.1

4.4 更新nginx版本

[root@master1 ~]# kubectl set image deployment/nginx-gsy nginx-gsy=nginx:1.14
deployment.extensions/nginx-gsy image updated

4.5 -w实时查看状态

[root@master1 ~]# kubectl get pods -w
NAME                         READY   STATUS              RESTARTS   AGE
nginx-6c94d899fd-xsxct       1/1     Running             0          3h6m
nginx-gsy-668fbbd8f5-ch66w   1/1     Running             0          3h53m
nginx-gsy-668fbbd8f5-mmcd4   1/1     Running             0          3h53m
nginx-gsy-668fbbd8f5-q72rw   1/1     Running             0          3h53m
nginx-gsy-67ccdc47d9-zbzdh   0/1     ContainerCreating   0          13s
nginx-gsy-67ccdc47d9-zbzdh   1/1   Running   0     21s
nginx-gsy-668fbbd8f5-ch66w   1/1   Terminating   0     3h54m
nginx-gsy-67ccdc47d9-qr9zn   0/1   Pending   0     0s
nginx-gsy-67ccdc47d9-qr9zn   0/1   ContainerCreating   0     0s
nginx-gsy-668fbbd8f5-ch66w   0/1   Terminating   0     3h54m
nginx-gsy-67ccdc47d9-qr9zn   1/1   Running   0     25s
nginx-gsy-668fbbd8f5-q72rw   1/1   Terminating   0     3h54m
nginx-gsy-67ccdc47d9-t8jr7   0/1   Pending   0     0s
nginx-gsy-67ccdc47d9-t8jr7   0/1   ContainerCreating   0     0s
nginx-gsy-668fbbd8f5-q72rw   0/1   Terminating   0     3h54m
nginx-gsy-67ccdc47d9-t8jr7   1/1   Running   0     20s
nginx-gsy-668fbbd8f5-mmcd4   1/1   Terminating   0     3h54m
[root@master1 ~]# 
[root@master1 ~]# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
nginx-6c94d899fd-xsxct       1/1     Running   0          9h
nginx-gsy-67ccdc47d9-qr9zn   1/1     Running   0          6h30m
nginx-gsy-67ccdc47d9-t8jr7   1/1     Running   0          6h30m
nginx-gsy-67ccdc47d9-zbzdh   1/1     Running   0          6h31m

观察可以发现,更新pod资源的方式是

在原有的三个pod在run的状态下,首先先创建一个pod,Pending,ContainerCreating,然后Running

接着删掉原来的一个pod,以此往复,保持副本数量不低于三个,直至所有的pod都被换成新的pod

小结:

资源容器是不可以重启的,只会被删除然后重新创建

更新过程中是一个逐步滚动更新状态

先创建再删除,始终会保持副本数量,不能低于三个

4.6 查看副本集

[root@master1 ~]# kubectl get replicaset
NAME                   DESIRED   CURRENT   READY   AGE
nginx-6c94d899fd       1         1         1       9h
nginx-dbddb74b8        0         0         0       6d4h
nginx-gsy-668fbbd8f5   0         0         0       10h		#旧的
nginx-gsy-67ccdc47d9   3         3         3       6h36m	#新的

4.7 此时再去访问nginx

版本变为1.14

在这里插入图片描述

五:回滚nginx

[root@master1 bin]# kubectl rollout --help
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]

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).

5.1 首先查看一下rollrout历史

[root@master1 ~]# kubectl rollout history deploy/nginx-gsy
deployment.extensions/nginx-gsy 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

[root@master1 ~]# 

5.2 执行回滚

[root@master1 ~]# kubectl rollout undo deploy/nginx-gsy
deployment.extensions/nginx-gsy

5.3 实时查看状态-w

[root@master1 ~]# kubectl get pods -w
NAME                         READY   STATUS              RESTARTS   AGE
nginx-6c94d899fd-xsxct       1/1     Running             0          9h
nginx-gsy-668fbbd8f5-j5psk   0/1     ContainerCreating   0          3s
nginx-gsy-668fbbd8f5-pz7kl   1/1     Running             0          10s
nginx-gsy-67ccdc47d9-qr9zn   1/1     Running             0          6h42m
nginx-gsy-67ccdc47d9-t8jr7   0/1     Terminating         0          6h42m
nginx-gsy-67ccdc47d9-zbzdh   1/1     Running             0          6h42m
nginx-gsy-67ccdc47d9-t8jr7   0/1   Terminating   0     6h42m
nginx-gsy-668fbbd8f5-j5psk   1/1   Running   0     10s
nginx-gsy-67ccdc47d9-qr9zn   1/1   Terminating   0     6h42m
nginx-gsy-668fbbd8f5-cnf72   0/1   Pending   0     0s
nginx-gsy-668fbbd8f5-cnf72   0/1   ContainerCreating   0     0s
nginx-gsy-67ccdc47d9-qr9zn   0/1   Terminating   0     6h42m
nginx-gsy-668fbbd8f5-cnf72   1/1   Running   0     13s
nginx-gsy-67ccdc47d9-zbzdh   1/1   Terminating   0     6h43m
^C[root@master1 ~]# kubect get pods
NAME                         READY   STATUS    RESTARTS   AGE
nginx-6c94d899fd-xsxct       1/1     Running   0          9h
nginx-gsy-668fbbd8f5-cnf72   1/1     Running   0          83s
nginx-gsy-668fbbd8f5-j5psk   1/1     Running   0          93s
nginx-gsy-668fbbd8f5-pz7kl   1/1     Running   0          100s

5.4 查看回滚状态

[root@master1 ~]# kubectl rollout status deploy/nginx-gsy
deployment "nginx-gsy" successfully rolled out

5.5 再次查看回滚的历史

状态虽然回去了,但是序号还是依旧递增

[root@master1 ~]# kubectl rollout history deploy/nginx-gsy
deployment.extensions/nginx-gsy 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

5.6 刷新一下网页

版本变成1.17

在这里插入图片描述

六:删除nginx资源

6.1 删除控制器deployment

[root@master1 ~]# kubectl get deploy
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx       1         1         1            1           6d4h
nginx-gsy   3         3         3            3           10h
[root@master1 ~]# kubectl delete deploy/nginx-gsy
deployment.extensions "nginx-gsy" deleted
[root@master1 ~]# kubectl get deploy
NAME    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx   1         1         1            1           6d4h
[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6c94d899fd-xsxct   1/1     Running   0          9h

6.2 创建pod资源时,还会创建副本集(不指定的话默认为1)和控制器deployment

这三个都要删除掉

而且,pod是业务节点,若是将pod对外提供服务的话,还需要进行expose发布,删除pod后也要删除对应的service,以免占用资源

6.3 删除服务service

[root@master1 ~]# kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6c94d899fd-xsxct   1/1     Running   0          9h

NAME                        TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes          ClusterIP   10.0.0.1     <none>        443/TCP        8d
service/nginx-gsy-service   NodePort    10.0.0.204   <none>        80:49003/TCP   10h

NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1         1         1            1           6d4h

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-6c94d899fd   1         1         1       9h
replicaset.apps/nginx-dbddb74b8    0         0         0       6d4h
[root@master1 ~]# kubectl delete service/nginx-gsy-service
service "nginx-gsy-service" deleted
[root@master1 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   8d

七:查看资源的具体详细信息

7.1 查看pod

[root@master1 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6c94d899fd-xsxct   1/1     Running   0          10h
[root@master1 ~]# kubectl describe pod nginx-6c94d899fd-xsxct 
Name:               nginx-6c94d899fd-xsxct
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               192.168.247.144/192.168.247.144
Start Time:         Fri, 08 May 2020 13:57:49 +0800
Labels:             pod-template-hash=6c94d899fd
                    run=nginx
Annotations:        <none>
Status:             Running
IP:                 172.17.42.6
Controlled By:      ReplicaSet/nginx-6c94d899fd
Containers:
  nginx:
    Container ID:   docker://21161e9d900529e8d75d076998600df2fc6a7426d33b3dcc8aad60e21af90cf5
    Image:          nginx:1.14
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 08 May 2020 13:58:11 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-qm9rm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-qm9rm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-qm9rm
    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:          <none>

7.2 查看deployment

[root@master1 ~]# kubectl describe deploy nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Sat, 02 May 2020 19:33:53 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 2
Selector:               run=nginx
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
  Containers:
   nginx:
    Image:        nginx:1.14
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-6c94d899fd (1/1 replicas created)
Events:          <none>

7.3 在有些pod的详细信息中还有一些日志消息

现在是默认自动调度方法,被调度器所管理

Events:
  Type    Reason     Age   From                      Message
  ----    ------     ----  ----                      -------
  Normal  Scheduled  37s   default-scheduler         Successfully assigned default/nginx-gsy1-7df78bc5fd-bn824 to 192.168.247.143
  Normal  Pulling    36s   kubelet, 192.168.247.143  pulling image "nginx:latest"
  Normal  Pulled     31s   kubelet, 192.168.247.143  Successfully pulled image "nginx:latest"
  Normal  Created    30s   kubelet, 192.168.247.143  Created container
  Normal  Started    30s   kubelet, 192.168.247.143  Started container

7.4 exec 进入pod

[root@master1 ~]# kubectl exec -it nginx-gsy1-7df78bc5fd-bn824 bash
root@nginx-gsy1-7df78bc5fd-bn824:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@nginx-gsy1-7df78bc5fd-bn824:/# ifconfig
bash: ifconfig: command not found
root@nginx-gsy1-7df78bc5fd-bn824:/# yum install net-tools -y
root@nginx-gsy1-7df78bc5fd-bn824:/# yum install net-tools -y
bash: yum: command not found
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章