基于YUM文件運行POD應用
- 通過資源清單文件創建第一個Pod
- 更新資源清單文件
- 查看pod是否創建成功
- 查看pod的ip和pod調度到哪個節點上
- 假如pod里有多個容器,進入到pod里的指定容器
- 查看pod詳細信息
- 查看pod具有哪些標簽:
- 刪除pod
- 通過kubectl run創建Pod
- Pod資源清單字段解讀
- Pod資源清單編寫技巧
- 查看pod.metadata字段如何定義
- 查看pod.spec字段如何定義
- 查看pod.spec.containers字段如何定義
- 查看pod.spec.container.ports字段如何定義
通過資源清單文件創建第一個Pod
解壓鏡像:把tomcat.tar.gz上傳到k8snode1和k8snode2節點
鏈接:https://pan.baidu.com/s/1qLcoJDqYSC9dfiDcWY-s0g?pwd=44ad
提取碼:44ad
ctr -n=k8s.io images import tomcat.tar.gz
vim pod-first.yaml
apiVersion: v1
kind: Pod
metadata:name: tomcat-testnamespace: defaultlabels:app: tomcat
spec:containers:- name: tomcat-javaports:- containerPort: 8080image: tomcat/tomcat-8.5-jre8:v1imagePullPolicy: IfNotPresent
更新資源清單文件
kubectl apply -f pod-first.yaml
查看pod是否創建成功
kubectl get pods -l app=tomcat
查看pod的ip和pod調度到哪個節點上
kubectl get pods -owide
查看pod日志
kubectl logs tomcat-test
進入到剛才創建的pod,剛才創建的pod名字是tomcat-test
kubectl exec -it tomcat-test -- /bin/bash
假如pod里有多個容器,進入到pod里的指定容器
kubectl exec -it tomcat-test -c tomcat-java -- /bin/bash
查看pod詳細信息
kubectl describe pods tomcat-test
查看pod具有哪些標簽:
kubectl get pods --show-labels
刪除pod
#kubectl delete pods tomcat-test
kubectl delete -f pod-first.yaml
我們上面創建的pod是一個自主式pod,也就是通過pod創建一個應用程序,如果pod出現故障停掉,那么我們通過pod部署的應用也就會停掉,不安全, 還有一種控制器管理的pod,通過控制器創建pod,可以對pod的生命周期做管理,可以定義pod的副本數,如果有一個pod意外停掉,那么會自動起來一個pod替代之前的pod
通過kubectl run創建Pod
kubectl run tomcat --image=tomcat/tomcat-8.5-jre8:v1 --image-pull-policy='IfNotPresent' --port=8080
Pod資源清單字段解讀
vim pod-tomcat.yaml
apiVersion: v1 #api版本
kind: Pod #創建的資源
metadata: name: tomcat-test #Pod的名字namespace: default #Pod所在的名稱空間labels:app: tomcat #Pod具有的標簽
spec:containers:- name: tomcat-java #Pod里容器的名字ports:- containerPort: 8080 #容器暴露的端口image: tomcat/tomcat-8.5-jre8:v1 #容器使用的鏡像imagePullPolicy: IfNotPresent #鏡像拉取策略
Pod資源清單編寫技巧
通過kubectl explain 查看定義Pod資源包含哪些字段。
kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated by clients and scheduled onto hosts.
[Pod是可以在主機上運行的容器的集合。此資源是由客戶端創建并安排到主機上。]FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
[APIVersion定義了對象,代表了一個版本。]kind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
[Kind是字符串類型的值,代表了要創建的資源。服務器可以從客戶端提交的請求推斷出這個資源。]metadata <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
[metadata是對象,定義元數據屬性信息的]spec <Object>Specification of the desired behavior of the pod. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
[spec制定了定義Pod的規格,里面包含容器的信息]status <Object>Most recently observed status of the pod. This data may not be up to date.Populated by the system. Read-only. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
[status表示狀態,這個不可以修改,定義pod的時候也不需要定義這個字段]
查看pod.metadata字段如何定義
kubectl explain pod.metadata
KIND: Pod
VERSION: v1
RESOURCE: metadata <Object># metadata是對象<Object>,下面可以有多個字段DESCRIPTION:Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataObjectMeta is metadata that all persisted resources must have, whichincludes all objects users must create.FIELDS:annotations <map[string]string>Annotations is an unstructured key value map stored with a resource thatmay be set by external tools to store and retrieve arbitrary metadata. Theyare not queryable and should be preserved when modifying objects. Moreinfo: http://kubernetes.io/docs/user-guide/annotations
# annotations是注解,map類型表示對應的值是key-value鍵值對,<string,string>表示 key和value都是String類型的
例如:
"metadata": {"annotations": {"key1" : "value1","key2" : "value2"}
}
用Annotation來記錄的信息包括:
build信息、release信息、Docker鏡像信息等,例如時間戳、release id號、鏡像hash值、docker registry地址等;
日志庫、監控庫、分析庫等資源庫的地址信息;
程序調試工具信息,例如工具名稱、版本號等;
團隊的聯系信息,例如電話號碼、負責人名稱、網址等。
clusterName <string>The name of the cluster which the object belongs to. This is used todistinguish resources with same name and namespace in different clusters.This field is not set anywhere right now and apiserver is going to ignoreit if set in create or update request.
#對象所屬群集的名稱。這是用來區分不同集群中具有相同名稱和命名空間的資源。此字段現在未設置在任何位置,apiserver將忽略它,如果設置了就使用設置的值creationTimestamp <string>deletionGracePeriodSeconds <integer>deletionTimestamp <string>finalizers <[]string>generateName <string>generation <integer>labels <map[string]string> #創建的資源具有的標簽
Map of string keys and values that can be used to organize and categorize(scope and select) objects. May match selectors of replication controllersand services. More info: http://kubernetes.io/docs/user-guide/labels
#labels是標簽,labels是map類型,map類型表示對應的值是key-value鍵值對,<string,string>表示 key和value都是String類型的managedFields <[]Object>name <string> #創建的資源的名字namespace <string> #創建的資源所屬的名稱空間
Namespace defines the space within which each name must be unique. An empty
namespace is equivalent to the "default" namespace, but "default" is the
canonical representation. Not all objects are required to be scoped to a
namespace - the value of this field for those objects will be empty.Must be a DNS_LABEL. Cannot be updated. More info:http://kubernetes.io/docs/user-guide/namespaces
# namespaces劃分了一個空間,在同一個namesace下的資源名字是唯一的,默認的名稱空間是default。ownerReferences <[]Object>resourceVersion <string>selfLink <string>uid <string>
查看pod.spec字段如何定義
kubectl explain pod.spec
KIND: Pod
VERSION: v1
RESOURCE: spec <Object>
DESCRIPTION:Specification of the desired behavior of the pod. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusPodSpec is a description of a pod.
#Pod的spec字段是用來描述Pod的FIELDS:activeDeadlineSeconds <integer>
#表示Pod可以運行的最長時間,達到設置的值后,Pod會自動停止。
affinity <Object>#定義親和性的automountServiceAccountToken <boolean>containers <[]Object> -required-
#containers是對象列表,用來定義容器的,是必須字段。對象列表 表示下面有很多對象,對象列表下面的內容用 - 連接。dnsConfig <Object>dnsPolicy <string>enableServiceLinks <boolean>ephemeralContainers <[]Object>hostAliases <[]Object>hostIPC <boolean>hostNetwork <boolean>hostPID <boolean>hostname <string>imagePullSecrets <[]Object>initContainers <[]Object>nodeName <string>nodeSelector <map[string]string>overhead <map[string]string>preemptionPolicy <string>priority <integer>priorityClassName <string>readinessGates <[]Object>restartPolicy <string>runtimeClassName <string>schedulerName <string>securityContext <Object>serviceAccount <string>serviceAccountName <string>setHostnameAsFQDN <boolean>shareProcessNamespace <boolean>subdomain <string>terminationGracePeriodSeconds <integer>tolerations <[]Object>topologySpreadConstraints <[]Object>volumes <[]Object>
查看pod.spec.containers字段如何定義
kubectl explain pod.spec.containers
KIND: Pod
VERSION: v1RESOURCE: containers <[]Object>
DESCRIPTION:List of containers belonging to the pod. Containers cannot currently beadded or removed. There must be at least one container in a Pod. Cannot beupdated.A single application container that you want to run within a pod.
#container是定義在pod里面的,一個pod至少要有一個容器。FIELDS:args <[]string>command <[]string>env <[]Object>envFrom <[]Object>image <string>
#image是用來指定容器需要的鏡像的imagePullPolicy <string>
#鏡像拉取策略,pod是要調度到node節點的,那pod啟動需要鏡像,可以根據這個字段設置鏡像拉取策略,支持如下三種:
Always:不管本地是否存在鏡像,都要重新拉取鏡像
Never: 從不拉取鏡像
IfNotPresent:如果本地存在,使用本地的鏡像,本地不存在,從官方拉取鏡像lifecycle <Object>livenessProbe <Object>name <string> -required-
#name是必須字段,用來指定容器名字的ports <[]Object>
#port是端口,屬于對象列表readinessProbe <Object>resources <Object>securityContext <Object>startupProbe <Object>stdin <boolean>stdinOnce <boolean>terminationMessagePath <string>terminationMessagePolicy <string>tty <boolean>volumeDevices <[]Object>volumeMounts <[]Object>workingDir <string>
查看pod.spec.container.ports字段如何定義
kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1
RESOURCE: ports <[]Object>
DESCRIPTION:List of ports to expose from the container. Exposing a port here gives thesystem additional information about the network connections a containeruses, but is primarily informational. Not specifying a port here DOES NOTprevent that port from being exposed. Any port which is listening on thedefault "0.0.0.0" address inside a container will be accessible from thenetwork. Cannot be updated.ContainerPort represents a network port in a single container.
FIELDS:containerPort <integer> -required-Number of port to expose on the pod's IP address. This must be a valid portnumber, 0 < x < 65536.
#containerPort是必須字段, pod中的容器需要暴露的端口。hostIP <string>What host IP to bind the external port to.
#將容器中的服務暴露到宿主機的端口上時,可以指定綁定的宿主機 IP。hostPort <integer>Number of port to expose on the host. If specified, this must be a validport number, 0 < x < 65536. If HostNetwork is specified, this must matchContainerPort. Most containers do not need this.
#容器中的服務在宿主機上映射的端口name <string>If specified, this must be an IANA_SVC_NAME and unique within the pod. Eachnamed port in a pod must have a unique name. Name for the port that can bereferred to by services.
#端口的名字protocol <string>Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".