Kubernetes學習筆記(二):Pod、標籤、註解

pod與容器

一個pod是一組緊密相關的容器,它們老是一塊兒運行在同一個節點上,以及同一個LInux命名空間中。
每一個pod擁有本身的ip,包含若干個容器。pod分佈在不一樣的節點上。html

爲何須要pod

爲何須要pod,而不是直接使用容器:
由於容器被設計爲只運行一個進程,因爲不可以將多個進程彙集在一個單獨的容器中,就須要另外一種結構將容器綁定在一塊兒,並將它們做爲一個單元管理,這就是pod的根本原理。node

pod中容器的隔離共享

在同一個pod中,多個容器有些資源是共享的,有些是隔離的。
同一個pod中的全部容器都運行在相同的network、UTS、IPC空間下。因此它們共享網絡接口、主機名,能夠經過IPC互相通訊。
同一個pod中的全部容器的文件系統是隔離的。nginx

什麼時候在pod中使用多個容器

  • 它們是不是一個總體?
  • 它們是否須要在一塊兒運行?
  • 它們是否要一塊兒擴縮容?

運行pod

pod和其餘Kubernetes資源一般都是經過向Kubernetes REST API提供JSON或者YAML文件來建立的。
咱們使用nginx鏡像建立一個pod。git

nginx.yaml

apiVersion: v1	# API版本
kind: Pod	# 資源類型
metadata:
  name: nginx	# pod的名稱
spec: 
  containers: 
    - image: nginx	# 建立容器所用的鏡像地址
      name: nginx	# 容器名稱
      ports:
        - containerPort: 80	# 應用監聽的端口
          protocol: TCP

這裏只給出了一個簡單的描述文件,大部分字段沒有給出。docker

kubectl explain

kubectl explain至關於一個文檔,能夠查看每一個API對象支持哪些字段。json

-> [root@kube0.vm] [~] k explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
.................

kubectl create

使用kubectl create建立podapi

-> [root@kube0.vm] [~] k create -f nginx.yaml
pod/nginx created

kubectl get

使用kubectl get查看,指定-o wide查看更多字段網絡

-> [root@kube0.vm] [~] k get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          11s

-> [root@kube0.vm] [~] k get -o wide pods 
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          22s   10.244.2.6   kube2.vm   <none>           <none>

完整定義

使用kubectl get -o yaml或者kubectl get -o json查看pod的完整定義。這個定義包含如下三大部分:app

  • metadata:包括名稱、命名空間、標籤和關於該pod的其餘信息
  • spec:包含pod內容的實際說明,如容器、卷等
  • status:包含運行中pod的當前信息,如pod IP、宿主機IP、每一個容器的描述和狀態
-> [root@kube0.vm] [~] k get -o yaml pod/nginx
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-05-20T00:32:43Z"
  name: nginx
  namespace: default
  resourceVersion: "109529"
  selfLink: /api/v1/namespaces/default/pods/nginx
  uid: a2b83142-9f17-4cfe-a9ac-04f57de82053
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: nginx
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-vlqvz
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: kube2.vm
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-vlqvz
    secret:
      defaultMode: 420
      secretName: default-token-vlqvz
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-05-20T02:46:50Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2020-05-20T02:46:57Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2020-05-20T02:46:57Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2020-05-20T00:32:43Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://b63c67379def88a5253e8da543655552185f14e6eb962926d65ec74c5a7ab6f7
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
    lastState: {}
    name: nginx
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2020-05-20T02:46:57Z"
  hostIP: 192.168.199.212
  phase: Running
  podIP: 10.244.2.6
  podIPs:
  - ip: 10.244.2.6
  qosClass: BestEffort
  startTime: "2020-05-20T02:46:50Z"

kubectl logs

kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
使用kubectl logs能夠查看pod中的日誌。若是pod中有多個容器,可使用-c <container>指定容器。好比:curl

-> [root@kube0.vm] [~] k logs nginx -c nginx -f

kubectl port-forward

kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
pod已經運行了,那如何向集羣中的pod發出請求呢。咱們經過kubectl port-forward命令,將本地端口映射到指定pod的端口上。

  • 窗口1:運行kubectl port-forward
-> [root@kube0.vm] [~] k port-forward nginx 8000:80
Forwarding from 127.0.0.1:8000 -> 80
Forwarding from [::1]:8000 -> 80
Handling connection for 8000 # curl請求發出後出現
  • 窗口2:運行kubectl logs
-> [root@kube0.vm] [~] k logs nginx -f
127.0.0.1 - - [20/May/2020:03:19:50 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" # curl請求發出後出現
  • 窗口3:curl發出請求
-> [root@kube0.vm] [~] curl http://localhost:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
........

標籤

通俗的講,標籤就是用於將Pod和其餘Kubernetes資源進行分類,每一個資源均可以有多個標籤
標籤是能夠附加到資源上的鍵值對,用以選擇具備該標籤的資源(經過標籤選擇器完成)。

nginx-labels.yaml

建立一個帶有標籤的描述文件

-> [root@kube0.vm] [~] cat nginx-labels.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-labels
  labels: # 添加了兩個標籤
    env: prod
    app: order
spec:
  containers:
    - image: nginx
      name: nginx
      ports:
        - containerPort: 80
          protocol: TCP

查看標籤

使用--show-labels查看標籤,po是pod的簡寫

-> [root@kube0.vm] [~] k create -f nginx-labels.yaml
pod/nginx-labels created

-> [root@kube0.vm] [~] k get po --show-labels
NAME           READY   STATUS              RESTARTS   AGE   LABELS
nginx-labels   0/1     ContainerCreating   0          3s    app=order,env=prod

使用-L查看指定標籤,而且單獨成列。

-> [root@kube0.vm] [~] k get po -L app,env
NAME           READY   STATUS    RESTARTS   AGE     APP     ENV
nginx-labels   1/1     Running   0          4m13s   order   prod

kubectl label

多個標籤以空格分隔

添加

-> [root@kube0.vm] [~] k label po nginx-labels test=123
pod/nginx-labels labeled

-> [root@kube0.vm] [~] k get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
nginx-labels   1/1     Running   0          6m23s   app=order,env=prod,test=123

修改

修改已存在的標籤,須要指定--overwrite選項

-> [root@kube0.vm] [~] k label po nginx-labels test=456 --overwrite
pod/nginx-labels labeled

-> [root@kube0.vm] [~] k get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
nginx-labels   1/1     Running   0          7m24s   app=order,env=prod,test=456

刪除

-> [root@kube0.vm] [~] k label po nginx-labels test-
pod/nginx-labels labeled

-> [root@kube0.vm] [~] k get pod --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
nginx-labels   1/1     Running   0          8m8s   app=order,env=prod

標籤選擇器

標籤選擇器可使咱們獲取具備特定標籤的pod子集。規則經過-l選項指定,多個用逗號分隔。

選擇規則

  • key:選擇存在標籤key的
  • !key:選擇不存在標籤key的
  • key=value:key存在而且值等於value
  • key!= value:選擇"key=value"的補集。也就存在key而且不等value的,或者不存在key的。
  • key in (value1,value2):key存在且值爲value1或者value2
  • key notin (value1,value2):key存在且值不爲value1和value2

實操

準備幾個不一樣label的pod:

-> [root@kube0.vm] [~] k get po --show-labels
NAME             READY   STATUS    RESTARTS   AGE     LABELS
nginx-labels     1/1     Running   0          40m     app=order,env=prod
nginx-labels-1   1/1     Running   0          9m42s   env=debug
nginx-labels-2   1/1     Running   0          8m8s    app=user,env=dev

app

-> [root@kube0.vm] [~] k get po --show-labels -l app
NAME             READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels     1/1     Running   0          50m   app=order,env=prod
nginx-labels-2   1/1     Running   0          18m   app=user,env=dev

!app

-> [root@kube0.vm] [~] k get po --show-labels -l '!app'
NAME             READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels-1   1/1     Running   0          20m   env=debug

app=order

-> [root@kube0.vm] [~] k get po --show-labels -l app=order
NAME           READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels   1/1     Running   0          77m   app=order,env=prod

app!=order

-> [root@kube0.vm] [~] k get po --show-labels -l app!=order
NAME             READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels-1   1/1     Running   0          46m   env=debug
nginx-labels-2   1/1     Running   0          45m   app=user,env=dev

env in (dev,prod)

-> [root@kube0.vm] [~] k get po --show-labels -l 'env in (dev,prod)'
NAME             READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels     1/1     Running   0          78m   app=order,env=prod
nginx-labels-2   1/1     Running   0          46m   app=user,env=dev

env notin (prod)

-> [root@kube0.vm] [~] k get po --show-labels -l 'env notin (prod)'
NAME             READY   STATUS    RESTARTS   AGE   LABELS
nginx-labels-1   1/1     Running   0          48m   env=debug
nginx-labels-2   1/1     Running   0          46m   app=user,env=dev

使用標籤選擇器約束pod調度

其核心思想是,爲工做節點(node)打上標籤,好比地區、機房、CPU密集、IO密集等。而後在建立pod的描述文件時指定對應標籤,調度器就會將pod調度到符合標籤選擇器規則的工做節點上。

如今假設一個場景:須要將新建的pod調度到IO密集的工做節點上。

給node打標籤

-> [root@kube0.vm] [~] k label node kube1.vm io=true
node/kube1.vm labeled

-> [root@kube0.vm] [~] k get node -L io
NAME       STATUS   ROLES    AGE   VERSION   IO
kube0.vm   Ready    master   19h   v1.17.3
kube1.vm   Ready    <none>   19h   v1.17.3   true
kube2.vm   Ready    <none>   19h   v1.17.3

nginx-io.yaml

帶有選擇器的yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-io
spec:
  nodeSelector: # 選擇器
    io: "true"
  containers:
    - image: nginx
      name: nginx

建立pod,查看結果確實是運行在kube1.vm上。

-> [root@kube0.vm] [~] k create -f nginx-io.yaml
pod/nginx-io created

-> [root@kube0.vm] [~] k get pod -o wide
NAME             READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
nginx-io         1/1     Running   0          42s    10.244.1.7    kube1.vm   <none>           <none>

註解

註解也是鍵值對,與標籤相似,但註解並不用於標識和選擇對象。
註解主要爲Pod和其餘API對象添加說明,以便每一個使用集羣的人能夠快速查找與對象有關的信息。

kubectl annotate

使用kubectl annotate爲pod添加註解,一樣修改已存在的註解須要指定--overwrite

-> [root@kube0.vm] [~] k annotate po nginx-io my.com/someannotation="123"
pod/nginx-io annotated

查看

-> [root@kube0.vm] [~] k describe po nginx-io
..........
Annotations:  my.com/someannotation: 123
..........

命名空間

簡單講,命名空間爲Pod等資源提供了做用域,在不一樣命名空間內的資源名稱能夠相同。
咱們以前一導致用的是默認命名空間:default,以前的 nginx-xxx 這些pod都位於這個命名空間。

查看命名空間

-> [root@kube0.vm] [~] k get ns #ns是namespace縮寫
NAME              STATUS   AGE
default           Active   21h
kube-node-lease   Active   21h
kube-public       Active   21h
kube-system       Active   21h

kubectl create namespace

使用kubectl create建立一個namespace,固然也能夠寫一個描述文件建立(麻煩了點)。

-> [root@kube0.vm] [~] k create ns test
namespace/test created

-> [root@kube0.vm] [~] k get ns -o wide
NAME              STATUS   AGE
........
test              Active   94s

爲資源指定命名空間

能夠在描述文件的metadata下添加一個字段namespace: test,或者在命令行的時候指定kubectl create -f xxx.yaml -n test

命名空間的誤解

首先要明確的是:命名空間並不提供對正在運行的對象的任何隔離
舉個例子:兩個pod並不會由於在不一樣的命名空間而沒法進行網絡通訊,是否能夠通訊不取決於命名空間,而取決於網絡解決方案。

kubectl delete

按名字刪除pod

k delete po nginx-io

使用標籤選擇器刪除

k delete po -l app

刪除全部pod

k delete po --all

刪除命名空間

k delete ns test

刪除命名空間(幾乎)全部資源

第一個 all 表示當前命名空間全部資源類型,第二個 --all 表示全部資源實例。

k delete all --all

小結

  • 如何決定多個容器是否應該放在一個pod
  • 經過提交描述文件建立API對象
  • 使用標籤組織API對象
  • 經過標籤選擇器調度pod
  • 命名空間使不一樣團隊很方便的使用同一個集羣
  • 一些命令:create、get、delete、label、annotate、port-forward、describe、logs、explain
相關文章
相關標籤/搜索