004.kubernets pod for simple management

About a pod

1.1 Introduction

  • Pod is the basis K8s cluster all business types
  • Pod is the smallest unit in K8s running in the cluster deploy applications or services, it can support multiple containers.
  • Pod's design philosophy is to support multiple containers shared network addresses and file system in a Pod

Difference pod and containers is that a pod can have multiple containers, when a pod is only a container, access pod is access to the container, for a kubernets, one pods have at least two containers, one is invisible, vessel called the pause, the other is the container business

pod is a logical concept, a container pod anomalies, re-create the entire pod

  • Pod Kubernetes each assigned a unique IP address, called PodIP, a plurality of containers in a Pod PodIP shared address. Requirements of the underlying network supports direct communication between any two cluster Pod, usually virtual network technology to implement Layer (Flannel).
  • POD POD can communicate directly with the other hosts.
  • If there POD stops unexpectedly, K8S will restart based on the resource settings or create POD, until the set value in line with expectations
  • pause vessel hijacked container business all traffic, IP is configured in a pause of the container, when creating the pod automatically created to take over the container network

1.2 pod of a scenario

  • pod containing two containers, File Puller first start in web server container
  • Pulling code into the volume, and then self-destruct
  • web server container starts, the volume readings code for user access

Two POD simple operation

2.1 Creating a pods of about nginx

[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

This is one of the most simple of pods, but nginx is running a business, there is no other thing

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

Since there is no designated ns, so the pods running in defaults, view

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

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

1/1: 1 indicates that the back of the pods run several containers in front of the container 1 represents several state is redy

View container

[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>

access

[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 Configuring port mapping

[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 common configuration

name: String 
Image: String 
imagePullPolicy: [Always | Never | IfNotPresent] # pulling mirror policy, the default is the third, look at the local, local is not only pulling 
restartPolicy: [Always | Never | OnFailure] 
the Command: [ String ] 
args: [ String ] 
the ports: 
containerPort: int 
hostPort: int 
Protocol: String 
the env : 
name: String 
value: String 


hostNetwork: BOOL 
Resources 
Volumes 
livenessProbe 
ReadnessProbe

2.4 on other policy

Running a plurality of containers, and using pulling mirror policy

[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 Delete and then create 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 this can not specify all containers

[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>

Port mapping, two ways to access

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

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

2.5 view details of 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

Three yaml file back

If you accidentally delete a file yaml, you can retrieve descriptive information

3.1 yaml delete files

[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 Use command to recover

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

Remove unnecessary information

[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 In accordance with the validation file recovery 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>

 The pod is simple to learn here


Bloggers Disclaimer: This article from the content source Yutian Yan Wei teacher education, which I am finished verification experiment is required, please contact the Friends of Bo Yutian education (http://www.yutianedu.com/), to obtain official approval or Yan teacher ( https://www.cnblogs.com/breezey/ ) I agree to reprint, thank you!

Guess you like

Origin www.cnblogs.com/zyxnhr/p/12182534.html