Kubernetes(K8S)(三)——Kubernetes的资源清单

文章目录

  • 1.Kubernetes的资源清单的介绍
  • 2. 自主式Pod资源清单
    • 2.1 创建自主式Pod资源清单
    • 2.2 交互式进入pod内部
    • 2.4 pod的镜像拉取策略
    • 2.5 容器的端口映射
    • 2.6 定义pod使用主机网络模式
    • 2.7 定义pod的重启策略
    • 2.8 限制pod资源
    • 2.9 定义Node的label过滤标签(节点选择器)
    • 2.10 定义pod的标签
  • 3.控制器部署pod清单

1.Kubernetes的资源清单的介绍

官网参考:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deployment-v1-apps
命令参考:
kubectl explain pod
kubectl get pod -o yaml |less

Kubernetes(K8S)(三)——Kubernetes的资源清单_第1张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第2张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第3张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第4张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第5张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第6张图片


2. 自主式Pod资源清单

2.1 创建自主式Pod资源清单

Kubernetes(K8S)(三)——Kubernetes的资源清单_第7张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第8张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第9张图片Kubernetes(K8S)(三)——Kubernetes的资源清单_第10张图片

[kubeadm@server1 ~]$ mkdir mainfest
[kubeadm@server1 ~]$ cd mainfest/
[kubeadm@server1 mainfest]$ ls
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myapp
      image: myapp:v1
[kubeadm@server1 mainfest]$ kubectl create -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
myapp   1/1     Running   0          9s
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          4m16s   10.244.1.13   server2   <none>           <none>
[kubeadm@server1 mainfest]$ curl 10.244.1.13
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

Kubernetes(K8S)(三)——Kubernetes的资源清单_第11张图片
在这里插入图片描述

[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml ##自主式pod清单的修改需要先删除在创建
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod.yml
[kubeadm@server1 mainfest]$ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myapp
      image: myapp:v2
[kubeadm@server1 mainfest]$ kubectl create -f pod.yml
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          3s    10.244.2.14   server3   <none>           <none>
[kubeadm@server1 mainfest]$ curl 10.244.2.14
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

Kubernetes(K8S)(三)——Kubernetes的资源清单_第12张图片


2.2 交互式进入pod内部

Kubernetes(K8S)(三)——Kubernetes的资源清单_第13张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第14张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第15张图片


[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml 
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
    - name: demo
      image: busyboxplus
      tty: true
      stdin: true
      stdinOnce: true
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
myapp   2/2     Running   0          5s
[kubeadm@server1 mainfest]$ kubectl describe pod myapp 
Name:         myapp
Namespace:    default
Priority:     0
Node:         server2/172.25.1.2
Start Time:   Mon, 22 Jun 2020 20:43:54 +0800
Labels:       <none>
Annotations:  Status:  Running
IP:           10.244.1.22
IPs:
  IP:  10.244.1.22
Containers:
  myappv1:
    Container ID:   docker://c3a8b858d190b68214e4bed175c27b96c202b983006ffbb0e5f1b8c3dfdc2517
    Image:          myapp:v1
    Image ID:       docker-pullable://myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 22 Jun 2020 20:43:55 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
  demo:
    Container ID:   docker://7596aa40bd7d6623791038c06bacecd8169ccdff65d76609f92831f2697ce434
    Image:          busyboxplus
    Image ID:       docker-pullable://busyboxplus@sha256:ef538eae80f40015736f1ee308d74b4f38f74e978c65522ce64abdf8c8c5e0d6
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 22 Jun 2020 20:43:55 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-5qqxc:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5qqxc
    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  Scheduled  10s   default-scheduler  Successfully assigned default/myapp to server2
  Normal  Pulled     10s   kubelet, server2   Container image "myapp:v1" already present on machine
  Normal  Created    10s   kubelet, server2   Created container myappv1
  Normal  Started    10s   kubelet, server2   Started container myappv1
  Normal  Pulling    10s   kubelet, server2   Pulling image "busyboxplus"
  Normal  Pulled     10s   kubelet, server2   Successfully pulled image "busyboxplus"
  Normal  Created    10s   kubelet, server2   Created container demo
  Normal  Started    10s   kubelet, server2   Started container demo

Kubernetes(K8S)(三)——Kubernetes的资源清单_第16张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第17张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第18张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第19张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第20张图片


2.4 pod的镜像拉取策略

kubectl explain pod.spec.containers
Kubernetes(K8S)(三)——Kubernetes的资源清单_第21张图片

[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
    - name: demo
      image: busyboxplus
      tty: true
      stdin: true
      stdinOnce: true
      imagePullPolicy: IfNotPresent

Kubernetes(K8S)(三)——Kubernetes的资源清单_第22张图片


2.5 容器的端口映射

Kubernetes(K8S)(三)——Kubernetes的资源清单_第23张图片
在这里插入图片描述

[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
      ports:
      - name: http
        containerPort: 80
        hostPort: 80
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          10s   10.244.1.23   server2   <none>           <none>
[kubeadm@server1 mainfest]$ curl 10.244.1.23
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

Kubernetes(K8S)(三)——Kubernetes的资源清单_第24张图片
在这里插入图片描述


2.6 定义pod使用主机网络模式

定义容器使用主机网络模式会直接使用宿主机的网络,但是只能有一个不能有副本
Kubernetes(K8S)(三)——Kubernetes的资源清单_第25张图片

[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
      ports:
      - name: http
        containerPort: 80
        hostPort: 80
  hostNetwork: true
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          4s    172.25.1.2   server2   <none>           <none>
[kubeadm@server1 mainfest]$ curl 172.25.1.2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

Kubernetes(K8S)(三)——Kubernetes的资源清单_第26张图片


2.7 定义pod的重启策略

Kubernetes(K8S)(三)——Kubernetes的资源清单_第27张图片

[kubeadm@server1 mainfest]$ kubectl run demo --image=busyboxplus -it --restart=Never
If you don't see a command prompt, try pressing enter.
[ root@demo:/ ]$ ls
bin/     etc/     lib/     linuxrc  mnt/     proc/    run      sys/     usr/
dev/     home/    lib64    media/   opt/     root/    sbin/    tmp/     var/
[ root@demo:/ ]$ [kubeadm@server1 mainfest]$ logout
[root@server1 ~]# su - kubeadm 
Last login: Mon Jun 22 18:36:13 CST 2020 on pts/0
[kubeadm@server1 ~]$ kubectl get pod 
NAME   READY   STATUS      RESTARTS   AGE
demo   0/1     Completed   0          18s

Kubernetes(K8S)(三)——Kubernetes的资源清单_第28张图片


[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
  restartPolicy: Never
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl exec -it myapp -- sh
/ # nginx -s stop
2020/06/22 14:00:56 [notice] 12#12: signal process started
/ # command terminated with exit code 137
[kubeadm@server1 mainfest]$ kubectl get pod
NAME    READY   STATUS      RESTARTS   AGE
myapp   0/1     Completed   0          36s

Kubernetes(K8S)(三)——Kubernetes的资源清单_第29张图片


2.8 限制pod资源

[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
      resources:
        requests:
          memory: 100Mi
          cpu: 0.1
        limits:
          memory: 200Mi
          cpu: 0.2
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl describe pod myapp 
Name:         myapp
Namespace:    default
Priority:     0
Node:         server2/172.25.1.2
Start Time:   Mon, 22 Jun 2020 22:32:49 +0800
Labels:       <none>
Annotations:  Status:  Running
IP:           10.244.1.25
IPs:
  IP:  10.244.1.25
Containers:
  myappv1:
    Container ID:   docker://7e81871169467479581a5294c0f5094de5f01495d7e9746ad141ff30726d4776
    Image:          myapp:v1
    Image ID:       docker-pullable://myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 22 Jun 2020 22:32:50 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  200Mi
    Requests:
      cpu:        100m
      memory:     100Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5qqxc (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-5qqxc:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-5qqxc
    Optional:    false
QoS Class:       Burstable
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  Scheduled  21s   default-scheduler  Successfully assigned default/myapp to server2
  Normal  Pulled     21s   kubelet, server2   Container image "myapp:v1" already present on machine
  Normal  Created    21s   kubelet, server2   Created container myappv1
  Normal  Started    21s   kubelet, server2   Started container myappv1

Kubernetes(K8S)(三)——Kubernetes的资源清单_第30张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第31张图片


2.9 定义Node的label过滤标签(节点选择器)

Kubernetes(K8S)(三)——Kubernetes的资源清单_第32张图片

[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml 
pod "myapp" deleted
[kubeadm@server1 mainfest]$ kubectl get node --show-labels ##查看节点的标签
NAME      STATUS   ROLES    AGE     VERSION   LABELS
server1   Ready    master   3d18h   v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
server2   Ready    <none>   3d18h   v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server2,kubernetes.io/os=linux
server3   Ready    <none>   3d3h    v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server3,kubernetes.io/os=linux

[kubeadm@server1 mainfest]$ vim pod.yml 
[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
      resources:
        requests:
          memory: 100Mi
          cpu: 0.1
        limits:
          memory: 200Mi
          cpu: 0.2
  nodeSelector: 
    kubernetes.io/hostname: server3
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE      NOMINATED NODE   READINESS GATES
myapp   1/1     Running   0          9s    10.244.2.28   server3   <none>           <none>

在这里插入图片描述
Kubernetes(K8S)(三)——Kubernetes的资源清单_第33张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第34张图片


2.10 定义pod的标签

$ kubectl get pod --show-labels //查看标签
$ kubectl get pod -l app //过滤包含app的标签
$ kubectl label pod demo version=v1 //打标签
$ kubectl label pod demo app=nginx --overwrite //更改标签

[kubeadm@server1 mainfest]$ cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  containers:
    - name: myappv1
      image: myapp:v1
      resources:
        requests:
          memory: 100Mi
          cpu: 0.1
        limits:
          memory: 200Mi
          cpu: 0.2
  nodeSelector: 
    kubernetes.io/hostname: server3
[kubeadm@server1 mainfest]$ kubectl apply -f pod.yml 
pod/myapp created
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels 
NAME    READY   STATUS    RESTARTS   AGE   LABELS
myapp   1/1     Running   0          6s    app=myapp

[kubeadm@server1 mainfest]$ kubectl label pod myapp app=nginx --overwrite
pod/myapp labeled
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels 
NAME    READY   STATUS    RESTARTS   AGE   LABELS
myapp   1/1     Running   0          74s   app=nginx

Kubernetes(K8S)(三)——Kubernetes的资源清单_第35张图片
在这里插入图片描述


3.控制器部署pod清单

Kubernetes(K8S)(三)——Kubernetes的资源清单_第36张图片

[kubeadm@server1 mainfest]$ kubectl delete -f pod.yml
pod "myapp" deleted
[kubeadm@server1 mainfest]$ vim pod2.yml
[kubeadm@server1 mainfest]$ cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        # Apply this label to pods and default
        # the Deployment label selector to this value
        app: nginx
    spec:
      containers:
      - name: nginx
        # Run this image
        image: myapp:v1

[kubeadm@server1 mainfest]$ kubectl create -f pod2.yml
deployment.apps/deployment-example created
[kubeadm@server1 mainfest]$ kubectl get pod
NAME                                  READY   STATUS    RESTARTS   AGE
deployment-example-7d5c95894c-4snlm   1/1     Running   0          29s
deployment-example-7d5c95894c-88t4h   1/1     Running   0          29s
deployment-example-7d5c95894c-tlxfs   1/1     Running   0          29s
[kubeadm@server1 mainfest]$ kubectl get pod --show-labels
NAME                                  READY   STATUS    RESTARTS   AGE   LABELS
deployment-example-7d5c95894c-4snlm   1/1     Running   0          60s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-88t4h   1/1     Running   0          60s   app=nginx,pod-template-hash=7d5c95894c
deployment-example-7d5c95894c-tlxfs   1/1     Running   0          60s   app=nginx,pod-template-hash=7d5c95894c
[kubeadm@server1 mainfest]$ vim pod2.yml

Kubernetes(K8S)(三)——Kubernetes的资源清单_第37张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第38张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第39张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第40张图片
Kubernetes(K8S)(三)——Kubernetes的资源清单_第41张图片
在这里插入图片描述
Kubernetes(K8S)(三)——Kubernetes的资源清单_第42张图片

你可能感兴趣的:(Kubernetes(K8S)(三)——Kubernetes的资源清单)