004.kubernets對於pod的簡單管理


一 pod簡介

1.1 介紹

  • Pod是K8s集群中所有業務類型的基礎
  • Pod是在K8s集群中運行部署應用或服務的最小單元,它是可以支持多容器的。
  • Pod的設計理念是支持多個容器在一個Pod中共享網絡地址和文件系統

pod和容器的區別就是,一個pod可以有多個容器,當一個pod只有一個容器的時候,訪問pod就是訪問容器,對於一個kubernets來說,一個pods至少有兩個容器,一個是不可見的,稱為pause容器,另一個就是業務容器

pod是一個邏輯概念,pod中的一個容器異常,整個pod重新創建

  • Kubernetes為每個Pod都分配了唯一的IP地址,稱之為PodIP,一個Pod里的多個容器共享PodIP地址。要求底層網絡支持集群內任意兩個Pod之間的直接通信,通常采用虛擬二層網絡技術來實現(Flannel)。
  • POD可以與其它主機上的POD直接通訊。
  • 如果有POD意外停止,K8S會根據資源設定重啟或創建POD,直到符合預期設定值
  • pause容器劫持業務容器的所有流量,IP是配置在pause容器的,在創建pod的時候,自動創建,用來接管容器網絡

1.2 pod的一個應用場景

  • pod含有兩個容器,File Puller先於web server容器啟動
  • 拉取代碼放到volume中,然后自毀
  • web server容器啟動,讀物volume的代碼,用於用戶訪問

二 POD簡單操作

2.1 創建一個關於nginx的pods

[root@docker-server1 namespace]# cd ../

[root@docker-server1 yamls]# mkdir pods

[root@docker-server1 yamls]# cd pods

[root@docker-server1 pods]# vi nginx-pods.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    annotations: 
      test: this is a test app
spec:                 #資源描述信息
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

這是一個最簡單的pods,只是運行一個nginx的業務,沒有任何其他的東西

[root@docker-server1 pods]# kubectl apply -f nginx-pods.yaml

由於沒有指定ns,所以pods運行在defaults中,查看

[root@docker-server1 pods]# kubectl get pods

NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          85s

1/1:后面的1表示這個pods運行了幾個容器,前面的1表示幾個容器處於redy狀態

查看容器

[root@docker-server1 pods]# kubectl get pods -o wide

NAME    READY   STATUS    RESTARTS   AGE     IP           NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          6m41s   10.244.2.6   192.168.132.133   <none>           <none>

訪問

[root@docker-server1 pods]# curl http://10.244.2.6

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

2.2 配置映射端口

[root@docker-server1 pods]# vim nginx-pods.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  hostNetwork: true
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

[root@docker-server1 pods]# kubectl delete -f nginx-pods.yaml

[root@docker-server1 pods]# kubectl create  -f nginx-pods.yaml 

[root@docker-server1 pods]# kubectl get pods -o wide

NAME    READY   STATUS              RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx   0/1     ContainerCreating   0          4s    192.168.132.133   192.168.132.133   <none>           <none>

[root@docker-server1 pods]# kubectl get pods -o wide

NAME    READY   STATUS    RESTARTS   AGE   IP                NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          95s   192.168.132.133   192.168.132.133   <none>           <none>

2.3 pod常用配置

name: string
image: string
imagePullPolicy: [Always |Never | IfNotPresent]    #拉取鏡像策略,默認是第三種,先看本地,本地沒有,才拉取
restartPolicy: [Always | Never | OnFailure]
command: [string]
args: [string]
ports:
containerPort: int
hostPort: int
protocol: string
env:
name: string
value: string


hostNetwork: bool
resources
volumes
livenessProbe
ReadnessProbe

2.4 配置其他策略

運行多個容器,並使用拉取鏡像策略

[root@docker-server1 pods]# vim nginx-pods.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
  annotations:
    test: this is a test app
spec:
  imagePullPolicy: Always
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
      hostPost: 8080
    env:
    - name: test
      value: aaa
    - name: test1
      value: bbb
  - name: busybox
    image: busybox
    command:
      - sh
      - -c
      - sleep 3600

2.4 刪除再創建pod

[root@docker-server1 pods]# kubectl delete pod nginx

[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml 

error: error validating "nginx-pods.yaml": error validating data: ValidationError(Pod.spec): unknown field "imagePullPolicy" in io.k8s.api.core.v1.PodSpec; if you choose to ignore these errors, turn validation off with --validate=false

imagePullPolicy這個不能指定所有容器

[root@docker-server1 pods]# vim nginx-pods.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
  annotations:
    test: this is a test app
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: Always
    ports:
    - containerPort: 80
      hostPort: 8080
    env:
    - name: test
      value: aaa
    - name: test1
      value: bbb
  - name: busybox
    image: busybox
    command:
      - sh
      - -c
      - sleep 3600

[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml

[root@docker-server1 pods]# kubectl get pods

NAME    READY   STATUS    RESTARTS   AGE
nginx   2/2     Running   0          28s

[root@docker-server1 pods]# kubectl get pods -o wide

NAME    READY   STATUS    RESTARTS   AGE   IP           NODE              NOMINATED NODE   READINESS GATES
nginx   2/2     Running   0          36s   10.244.2.7   192.168.132.133   <none>           <none>

做了端口映射,兩種方式訪問

[root@docker-server1 pods]# curl http://192.168.132.133:8080

[root@docker-server1 pods]# curl http://10.244.2.7

2.5 查看pods的詳細信息

[root@docker-server1 pods]# kubectl describe po nginx

Name:         nginx
Namespace:    default
Priority:     0
Node:         192.168.132.133/192.168.132.133
Start Time:   Thu, 09 Jan 2020 18:17:48 -0500
Labels:       app=nginx
Annotations:  test: this is a test app
Status:       Running
IP:           10.244.2.7
IPs:
  IP:  10.244.2.7
Containers:
  nginx:
    Container ID:   docker://676a2d9bebda40d86138190093d1a6d6cf6f16e5ff0e89fc22df53a74bdf8048
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:8aa7f6a9585d908a63e5e418dc5d14ae7467d2e36e1ab4f0d8f9d059a3d071ce
    Port:           80/TCP
    Host Port:      8080/TCP
    State:          Running
      Started:      Thu, 09 Jan 2020 18:17:55 -0500
    Ready:          True
    Restart Count:  0
    Environment:
      test:   aaa
      test1:  bbb
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-bwbrn (ro)
  busybox:
    Container ID:  docker://e8cc006f3ab292701d9876d84881af90f4c97ea22f32bf0cabf2b93d82b8c82b
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      sleep 3600
    State:          Running
      Started:      Thu, 09 Jan 2020 18:18:00 -0500
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-bwbrn (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-bwbrn:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-bwbrn
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From                      Message
  ----    ------     ----   ----                      -------
  Normal  Pulling    4m43s  kubelet, 192.168.132.133  Pulling image "nginx"
  Normal  Scheduled  4m41s  default-scheduler         Successfully assigned default/nginx to 192.168.132.133
  Normal  Pulled     4m38s  kubelet, 192.168.132.133  Successfully pulled image "nginx"
  Normal  Created    4m37s  kubelet, 192.168.132.133  Created container nginx
  Normal  Started    4m37s  kubelet, 192.168.132.133  Started container nginx
  Normal  Pulling    4m37s  kubelet, 192.168.132.133  Pulling image "busybox"
  Normal  Pulled     4m32s  kubelet, 192.168.132.133  Successfully pulled image "busybox"
  Normal  Created    4m32s  kubelet, 192.168.132.133  Created container busybox
  Normal  Started    4m32s  kubelet, 192.168.132.133  Started container busybox

三 yaml文件找回

如果不小心刪除了yaml文件,可以通過描述信息找回

3.1  刪除yaml文件

[root@docker-server1 pods]# rm  -rf nginx-pods.yaml 

[root@docker-server1 pods]# kubectl get pods nginx -o yaml

apiVersion: v1
kind: Pod
metadata:
  annotations:
    test: this is a test app
  creationTimestamp: "2020-01-09T23:17:51Z"
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "43864"
  selfLink: /api/v1/namespaces/default/pods/nginx
  uid: 41510342-de97-4b37-ab95-0a01dd73aac7
spec:
  containers:
  - env:
    - name: test
      value: aaa
    - name: test1
      value: bbb
    image: nginx
    imagePullPolicy: Always
    name: nginx
    ports:
    - containerPort: 80
      hostPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-bwbrn
      readOnly: true
  - command:
    - sh
    - -c
    - sleep 3600
    image: busybox
    imagePullPolicy: Always
    name: busybox
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-bwbrn
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: 192.168.132.133
  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-bwbrn
    secret:
      defaultMode: 420
      secretName: default-token-bwbrn
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-01-09T23:17:48Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2020-01-09T23:18:01Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2020-01-09T23:18:01Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2020-01-09T23:17:51Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://e8cc006f3ab292701d9876d84881af90f4c97ea22f32bf0cabf2b93d82b8c82b
    image: busybox:latest
    imageID: docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
    lastState: {}
    name: busybox
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2020-01-09T23:18:00Z"
  - containerID: docker://676a2d9bebda40d86138190093d1a6d6cf6f16e5ff0e89fc22df53a74bdf8048
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:8aa7f6a9585d908a63e5e418dc5d14ae7467d2e36e1ab4f0d8f9d059a3d071ce
    lastState: {}
    name: nginx
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2020-01-09T23:17:55Z"
  hostIP: 192.168.132.133
  phase: Running
  podIP: 10.244.2.7
  podIPs:
  - ip: 10.244.2.7
  qosClass: BestEffort
  startTime: "2020-01-09T23:17:48Z"

3.2 使用命令恢復

[root@docker-server1 pods]# kubectl get pods nginx -o yaml > nginx-pods.yaml

刪除不必要的信息

[root@docker-server1 pods]# vim nginx-pods.yaml

apiVersion: v1
kind: Pod
metadata:
  annotations:
    test: this is a test app
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  containers:
  - env:
    - name: test
      value: aaa
    - name: test1
      value: bbb
    image: nginx
    imagePullPolicy: Always
    name: nginx
    ports:
    - containerPort: 80
      hostPort: 8080
      protocol: TCP
  - command:
    - sh
    - -c
    - sleep 3600
    image: busybox
    imagePullPolicy: Always
    name: busybox
  restartPolicy: Always

[root@docker-server1 pods]# kubectl delete pod nginx

[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml 

3.3 根據恢復的yaml文件驗證

[root@docker-server1 pods]# kubectl get pods

NAME    READY   STATUS    RESTARTS   AGE
nginx   2/2     Running   0          14s

[root@docker-server1 pods]# kubectl get pods -o wide

NAME    READY   STATUS    RESTARTS   AGE   IP           NODE              NOMINATED NODE   READINESS GATES
nginx   2/2     Running   0          27s   10.244.2.8   192.168.132.133   <none>           <none>

[root@docker-server1 pods]# curl http://192.168.132.133:8080

[root@docker-server1 pods]# curl http://10.244.2.8

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 pod的簡單操作學習到這里


博主聲明:本文的內容來源主要來自譽天教育晏威老師,由本人實驗完成操作驗證,需要的博友請聯系譽天教育(http://www.yutianedu.com/),獲得官方同意或者晏老師(https://www.cnblogs.com/breezey/)本人同意即可轉載,謝謝!


免責聲明!

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



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