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 數為 1、最大新建 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).
####