k8s資源清單創建pod


資源:對象

service pod deployment

 

workload:pod,ReplicaSet,Deployment,statefulset,DaemonSet,Job,Crontabjob,...

 

服務發現和負載均衡:service ,Ingress,....

 

配置與存儲:Volumn存儲卷 ,CSI存儲接口

ConfigMap,Secret,

DownwardAPI

 

集群級資源:namespace,Node,Role,ClusterRole,RoleBinding,ClusterRoleBinding,

 

元數據型資源

HPA,PodTemplate,LimitRange

 

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文件定義的資源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM