資源:對象node
service pod deploymentnginx
workload:pod,ReplicaSet,Deployment,statefulset,DaemonSet,Job,Crontabjob,...shell
服務發現和負載均衡:service ,Ingress,....json
配置與存儲:Volumn存儲卷 ,CSI存儲接口vim
ConfigMap,Secret,api
DownwardAPIapp
集羣級資源:namespace,Node,Role,ClusterRole,RoleBinding,ClusterRoleBinding,負載均衡
元數據型資源frontend
HPA,PodTemplate,LimitRangeide
kubectl get pod myapp-84cd4b7f95-h47cj -o yaml
yml文件查看pod詳細的配置清單
建立資源的方法:
apiserver僅接收JSON格式的資源定義;
yaml格式提供配置清單,apiserver可自動轉爲json格式,然後在提交
kubectl api-versions
大部分資源的配置清單:
apiVerison:group/version,
kind:資源類別
metadata:元數據
name
namespace
labels
annotations 資源註解
每一個資源的引用PATH
/api/GROUP/VERSION/namespaces/NAMESPACE/TYPE/NAME
spec:規格 指望的狀態 disired state
spec.containers
- name <string>
image <string>
imagePullPolicy <string>
Always,Never,IfNotPresent
老是去倉庫下載 不下載 不存在就去下載,存在就直接使用
修改鏡像中默認的運用:
kubectl explain pods.spec.containers
command ,args
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
標籤labels
kubectl get pods --show-labels
key=value(小於63個字符)
key:字母,數字,」-_」
value:能夠爲空
kubectl get pods -l app --show-labels 找出含有app標籤的pod
kubectl get pods -L app,run --show-labels -o wide 匹配字段查詢
打標籤
kubectl label –help
kubectl label pods pod-demo release=canary
類型 資源名 標籤名=canary
kubectl get pods -l app --show-labels 查看是否打上標籤
覆蓋修改標籤
kubectl label pods pod-demo release=stable –overwrite
kubectl get pods -l app,release --show-labels 既有app也有release標籤
標籤選擇器
等值:=,==,!=
kubectl get pods -l release=stable --show-labels
kubectl label pods nginx-7bb7cd8db5-7dn9r release=canary
kubectl get pods -l release
kubectl get pods -l release=canary
kubectl get pods -l release,app
kubectl get pods -l release=stable,app=myapp
kubectl get pods -l release!=canary --show-labels
集合關係:key in (values1,value2…), key not in (values1,value2…),key,!key
kubectl get pods -l "release in (canary,beta,alpha)" --show-labels
kubectl get pods -l "release notin (canary,beta,alpha)" --show-labels
許多資源支持內嵌字段定義其使用標籤選擇器:
matchLabels:直接給定鍵值,至關於=
matchExpressions:基於給定的表達式來定義使用標籤選擇器,{key:」KEY」,operator:」OPERATOR」,values:[VAL1,VAL2…]}大寫字母表示替換
operator操做符:In ,NotIn values字段必須爲非空列表
Exists,NotExists values字段必須爲空列表
各類對象都能打標籤,不只限於pod,好比node
kubectl get nodes --show-labels
kubectl label nodes node02 disktype=ssd 對node02打標籤
kubectl explain pods.spec
nodeSelector <map[string]string> 節點標籤選擇器
kubectl label nodes node01 disktype=hd
vim pod-demo.yaml
containers:
…
nodeSelector:
disktype: hd
kubectl delete -f pod-demo.yaml
kubectl create -f pod-demo.yaml 再次建立會運行在node01上
kubectl describe pods pod-demo
nodeName <string> 指定運行在哪一個節點上
annotations:資源註解
與label不一樣的地方在於,它不能用於挑選資源對象,僅用於爲對象提供」元數據」,
vim pod-demo.yaml
metadata:
…
annotations:
work in node02: "disk is ssd"
kubectl delete -f pod-demo.yaml
kubectl create -f pod-demo.yaml
kubectl describe pods pod-demo
Annotations: work_in_node02: disk is ssd
status:當前狀態,current state,本字段由kubernetes集羣維護;用戶不能定義
kubectl explain pods 查詢pods怎麼定義
kubectl explain pods.metadata 查詢pods的metadata怎麼定義
kubectl explain pods.spec
kubectl explain pods.spec.containers
kubectl explain pods.spec.containers.livenessProbe
例如:自定義一個pod資源
vim pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
註釋:
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
至關於 spec:{containers:['name:myapp'],image:"ikubernetes/myapp:v1"}
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
至關於 command:["/bin/sh","-c","sleep 3600"]
kubectl create -f pod-demo.yaml 基於yaml清單文件建立pod資源
從文件加載建立資源
kubectl get pods 再查看一下建立的pod
kubectl describe pods pod-demo 查看建立的資源的描述信息
查看容器日誌
kubectl logs pod-demo myapp
pod名 pod裏面的容器名
進入容器
kubectl exec -it pod-demo -c myapp -- /bin/sh
輸入和終端 pod名 -c指定容器名 --執行的命令
kubectl delete -f pod-demo.yaml 刪除基於資源清單yaml文件定義的資源