Master 负责管理集群 负责协调集群中的全部活动,例如调度应用程序,维护应用程序的状态,扩展和更新应用程序。 Worker节点是VM(虚拟机)或物理计算机,充当k8s集群中的工做计算机。 每一个Worker节点都有一个Kubelet,它管理一个Worker节点并与负责与Master节点通讯。该Worker节点还应具备用于处理容器操做的工具,例如Docker。 执行命令 kubectl get namespaces 能够查看名称空间 执行 kubectl 命令时,可使用 --namespace 参数指定名称空间,例如: kubectl run nginx --image=nginx --namespace=<您的名称空间> kubectl get pods --namespace=<您的名称空间> 能够经过 set-context 命令改变当前 kubectl 上下文 的名称空间,后续全部命令都默认在此名称空间下执行。 kubectl config set-context --current --namespace=<您的名称空间> # 验证结果 kubectl config view --minify | grep namespace: 执行一下命令可查看哪些 Kubernetes 对象在名称空间里,哪些不在: # 在名称空间里 kubectl api-resources --namespaced=true # 不在名称空间里 kubectl api-resources --namespaced=false kubectl 是 k8s 的客户端工具,可使用命令行管理集群 Deployment 译名为 部署。在k8s中,经过发布 Deployment,能够建立应用程序 (docker image) 的实例 (docker container),这个实例会被包含在称为 Pod 的概念中,Pod 是 k8s 中最小单元的可管理单元。 在 k8s 集群中发布 Deployment 后,Deployment 将指示 k8s 如何建立和更新应用程序的实例,master 节点将应用程序实例调度到集群中的具体的节点上。 建立应用程序实例后,Kubernetes Deployment Controller 会持续监控这些实例。若是运行实例的 worker 节点关机或被删除,则 Kubernetes Deployment Controller 将在群集中资源最优的另外一个 worker 节点上从新建立一个新的实例。这提供了一种自我修复机制来解决机器故障或维护问题。 Deployment 处于 master 节点上,经过发布 Deployment,master 节点会选择合适的 worker 节点建立 Container,Container 会被包含在 Pod 里。 建立 Deployment 后,k8s建立了一个 Pod(容器组) 来放置应用程序实例(container 容器) Kubernetes Pods(容器组) Pod 容器组 是一个k8s中一个抽象的概念,用于存放一组 container(可包含一个或多个 container 容器),以及这些 container (容器)的一些共享资源 这些资源包括: 共享存储,称为卷(Volumes) 网络,每一个 Pod(容器组)在集群中有个惟一的 IP,pod(容器组)中的 container(容器)共享该IP地址 container(容器)的基本信息,例如容器的镜像版本,对外暴露的端口等 Pod(容器组)是 k8s 集群上的最基本的单元。当咱们在 k8s 上建立 Deployment 时,会在集群上建立包含容器的 Pod (而不是直接建立容器)。每一个Pod都与运行它的 worker 点(Node)绑定,并保持在那里直到终止或被删除。若是节点(Node)发生故障,则会在群集中的其余可用节点(Node)上运行相同的 Pod(从一样的镜像建立 Container,使用一样的配置,IP 地址不一样,Pod 名字不一样)。 Pod 是一组容器(可包含一个或多个应用程序容器),以及共享存储(卷 Volumes)、IP 地址和有关如何运行容器的信息。 若是多个容器紧密耦合而且须要共享磁盘等资源,则他们应该被部署在同一个Pod(容器组)中。 Kubernetes Nodes(节点) 执行如下命令可查看全部节点的列表: kubectl get nodes -o wide 执行如下命令可查看节点状态以及节点的其余详细信息: kubectl describe node <your-node-name> Pod(容器组)老是在 Node(节点) 上运行。Node(节点)是 kubernetes 集群中的计算机,能够是虚拟机或物理机。每一个 Node(节点)都由 master 管理。一个 Node(节点)能够有多个Pod(容器组),kubernetes master 会根据每一个 Node(节点)上可用资源的状况,自动调度 Pod(容器组)到最佳的 Node(节点)上。 每一个 Kubernetes Node(节点)至少运行: Kubelet,负责 master 节点和 worker 节点之间通讯的进程;管理 Pod(容器组)和 Pod(容器组)内运行的 Container(容器)。 容器运行环境(如Docker)负责下载镜像、建立和运行容器等。 Kubernetes 中的 Service(服务) 提供了这样的一个抽象层,它选择具有某些特征的 Pod(容器组)并为它们定义一个访问方式。Service(服务)使 Pod(容器组)之间的相互依赖解耦(本来从一个 Pod 中访问另一个 Pod,须要知道对方的 IP 地址)。一个 Service(服务)选定哪些 Pod(容器组) 一般由 LabelSelector(标签选择器) 来决定。 在建立Service的时候,经过设置配置文件中的 spec.type 字段的值,能够以不一样方式向外部暴露应用程序: ClusterIP(默认) 在群集中的内部IP上公布服务,这种方式的 Service(服务)只在集群内部能够访问到 NodePort 使用 NAT 在集群中每一个的同一端口上公布服务。这种方式下,能够经过访问集群中任意节点+端口号的方式访问服务 <NodeIP>:<NodePort>。此时 ClusterIP 的访问方式仍然可用。 LoadBalancer 在云环境中(须要云供应商能够支持)建立一个集群外部的负载均衡器,并为使用该负载均衡器的 IP 地址做为服务的访问地址。此时 ClusterIP 和 NodePort 的访问方式仍然可用。 Rolling Update滚动更新 经过使用新版本的 Pod 逐步替代旧版本的 Pod 来实现 Deployment 的更新,从而实现零停机。新的 Pod 将在具备可用资源的 Node(节点)上进行调度。 Kubernetes 更新多副本的 Deployment 的版本时,会逐步的建立新版本的 Pod,逐步的中止旧版本的 Pod,以便使应用一直处于可用状态。这个过程当中,Service 可以监视 Pod 的状态,将流量始终转发到可用的 Pod 上。 默认状况下,Rolling Update 滚动更新 过程当中,Kubernetes 逐个使用新版本 Pod 替换旧版本 Pod(最大不可用 Pod 数为 一、最大新建 Pod 数也为 1)。这两个参数能够配置为数字或百分比。在Kubernetes 中,更新是版本化的,任何部署更新均可以恢复为之前的(稳定)版本。 滚动更新容许如下操做: 将应用程序从准上线环境升级到生产环境(经过更新容器镜像) 回滚到之前的版本 持续集成和持续交付应用程序,无需停机 查看名称空间:kubectl get namespace 建立名称空间:kubectl create namespace test_hkd 查看详细信息:kubectl describe namespace test_hkd 使用 kubectl api-versions 便可查看当前集群支持的版本 应用 YAML 文件: kubectl apply -f nginx-deployment.yaml kubectl get - 显示资源列表 # kubectl get 资源类型 #获取类型为Deployment的资源列表 kubectl get deployments #获取类型为Pod的资源列表 kubectl get pods #获取类型为Node的资源列表 kubectl get nodes kubectl describe - 显示有关资源的详细信息 # kubectl describe 资源类型 资源名称 #查看名称为nginx-XXXXXX的Pod的信息 kubectl describe pod nginx-XXXXXX #查看名称为nginx的Deployment的信息 kubectl describe deployment nginx kubectl logs - 查看pod中的容器的打印日志(和命令docker logs 相似) # kubectl logs Pod名称 #查看名称为nginx-pod-XXXXXXX的Pod内的容器打印的日志 #本案例中的 nginx-pod 没有输出日志,因此您看到的结果是空的 kubectl logs -f nginx-pod-XXXXXXX kubectl exec - 在pod中的容器环境内执行命令(和命令docker exec 相似) # kubectl exec Pod名称 操做命令 # 在名称为nginx-pod-xxxxxx的Pod中运行bash kubectl exec -it nginx-pod-xxxxxx /bin/bash #### 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 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 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 Attach to a running container 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: diff Diff live version against would-be applied version 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 a specific condition on one or many resources. convert Convert config files between different API versions kustomize Build a kustomization target from a directory or a remote url. 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: 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 Provides utilities for interacting with plugins. 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). ####