[kubernetes]

SourceURL:file:///home/student/Desktop/k8s.doc

资源清单文件

  • Yaml格式(一)

---

kind: Pod

apiVersion: v1

metadata:

  name: myweb

spec:

  containers:

  - name: nginx

    image: myos:nginx

status: {}

  • Yaml格式(二)

---

kind: Pod

apiVersion: v1

metadata: { name: myweb }

spec: { containers: [{name: nginx, image: myos:nginx }] }

status: {}

模板与帮助信息

# 获取资源对象模板

[root@master ~]# kubectl create namespace work --dry-run=client -o yaml

apiVersion: v1

kind: Namespace

metadata:

  creationTimestamp: null

  name: work

spec: {}

status: {}

# 查询帮助信息

[root@master ~]# kubectl explain Pod.metadata

KIND:       Pod

VERSION:    v1

FIELD: metadata <ObjectMeta> ... ...

  namespace <string>

    Namespace defines the space within which each name must be unique. An empty

    namespace is equivalent to the "default" namespace, but "default" is the

    canonical representation. Not all objects are required to be scoped to a

    namespace - the value of this field for those objects will be empty.

[root@master ~]# kubectl explain Pod.metadata.namespace

KIND:       Pod

VERSION:    v1

FIELD: namespace <string>

DESCRIPTION:

    Namespace defines the space within which each name must be unique. An empty

    namespace is equivalent to the "default" namespace, but "default" is the

    canonical representation. Not all objects are required to be scoped to a

    namespace - the value of this field for those objects will be empty.

    

    Must be a DNS_LABEL. Cannot be updated. More info:

    https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces

配置名称空间

[root@master ~]# vim myweb.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: myweb

  namespace: default

spec:

  containers:

  - name: nginx

    image: myos:nginx

status: {}

管理资源对象

# 创建多个资源清单文件

[root@master ~]# mkdir app

[root@master ~]# sed "s,myweb,app1," myweb.yaml >app/app1.yaml

[root@master ~]# sed "s,myweb,app2," myweb.yaml >app/app2.yaml

[root@master ~]# sed "s,myweb,app3," myweb.yaml >app/app3.yaml

[root@master ~]# tree app/

app/

├── app1.yaml

├── app2.yaml

└── app3.yaml

# 创建应用

[root@master ~]# kubectl apply -f app/app1.yaml -f app/app2.yaml

pod/app1 created

pod/app2 created

# 执行目录下所有资源清单文件

[root@master ~]# kubectl apply -f app/

pod/app1 unchanged

pod/app2 unchanged

pod/app3 created

# 删除目录下所有的资源对象

[root@master ~]# kubectl delete -f app/

pod "app1" deleted

pod "app2" deleted

pod "app3" deleted

# 合并管理资源清单文件

[root@master ~]# cat app/* >app.yaml

[root@master ~]# kubectl apply -f app.yaml

pod/app1 created

pod/app2 created

pod/app3 created

多容器 Pod

[root@master ~]# vim mynginx.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mynginx

  namespace: default

spec:

  containers:

  - name: nginx

    image: myos:nginx

  - name: php

    image: myos:php-fpm

[root@master ~]# kubectl apply -f mynginx.yaml

pod/mynginx created

[root@master ~]# kubectl get pods

NAME      READY   STATUS    RESTARTS   AGE

mynginx   2/2     Running   0          3s

管理多容器 Pod
  • 受到多容器影响的命令: ["logs", "exec", "cp"]

# 查看日志

[root@master ~]# kubectl logs mynginx -c nginx

[root@master ~]#

[root@master ~]# kubectl logs mynginx -c php

[06-Mar-2024 12:56:18] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root

[06-Mar-2024 12:56:18] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

# 执行命令

[root@master ~]# kubectl exec -it mynginx -c nginx -- pstree -p

nginx(1)-+-nginx(7)

         `-nginx(8)

[root@master ~]# kubectl exec -it mynginx -c php -- pstree -p

php-fpm(1)

# 拷贝文件

[root@master ~]# kubectl cp mynginx:/etc/php-fpm.conf /root/php.conf -c nginx

tar: Removing leading `/' from member names

tar: /etc/php-fpm.conf: Cannot stat: No such file or directory

tar: Exiting with failure status due to previous errors

[root@master ~]# kubectl cp mynginx:/etc/php-fpm.conf /root/php.conf -c php

tar: Removing leading `/' from member names

案例 3 排错

[root@master ~]# vim web2.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: web2

  namespace: default

spec:

  containers:

  - name: nginx

    image: myos:nginx

  - name: apache

    image: myos:httpd

status: {}

[root@master ~]# kubectl apply -f web2.yaml

pod/web2 created

[root@master ~]# kubectl get pods web2

NAME   READY   STATUS    RESTARTS     AGE

web2   1/2     Error     1 (4s ago)   8s

自定义任务

[root@master ~]# vim mycmd.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mycmd

spec:

  containers:

  - name: linux

    image: myos:httpd

    command: [sleep]       # 配置自定义命令

    args:                  # 设置命令参数

    - "30"

[root@master ~]# kubectl apply -f mycmd.yaml

pod/mycmd created

[root@master ~]# kubectl get pods -w

NAME    READY   STATUS      RESTARTS     AGE

mycmd   1/1     Running     0            4s

mycmd   0/1     Completed   0            31s

mycmd   1/1     Running     1 (2s ago)   32s

嵌入式脚本

[root@master ~]# vim mycmd.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mycmd

spec:

  containers:

  - name: linux

    image: myos:8.5

    command: [sh]

    args:

    - -c

    - |

      ID=${RANDOM}

      for i in {1..9};do

          echo "${ID} : hello world."

          sleep 5

      done

[root@master ~]# kubectl replace --force -f mycmd.yaml

pod "mycmd" deleted

pod/mycmd replaced

[root@master ~]# kubectl logs mycmd

hello world.

hello world.

hello world.

容器保护策略

[root@master ~]# vim mycmd.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mycmd

spec:

  restartPolicy: OnFailure      # 配置重启策略

  containers:

  - name: linux

    image: myos:8.5

    command: [sh]

    args:

    - -c

    - |

      ID=${RANDOM}

      for i in {1..9};do

          echo "${ID} : hello world."

          sleep 5

      done

      exit $((ID%2))

[root@master ~]# kubectl replace --force -f mycmd.yaml

pod "mycmd" deleted

pod/mycmd replaced

[root@master ~]# kubectl get pods -w

NAME    READY   STATUS      RESTARTS     AGE

mycmd   1/1     Running     0            4s

mycmd   0/1     Completed   0            31s

宽限期策略

[root@master ~]# vim mycmd.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mycmd

spec:

  terminationGracePeriodSeconds: 0  # 设置宽限期

  restartPolicy: OnFailure

  containers:

  - name: linux

    image: myos:8.5

    command: [sh]

    args:

    - -c

    - |

      ID=${RANDOM}

      for i in {1..9};do

          echo "${ID} : hello world."

          sleep 5

      done

      exit $((ID%2))

[root@master ~]# kubectl delete pods mycmd

pod "mycmd" deleted

[root@master ~]# kubectl apply -f mycmd.yaml

pod/mycmd created

[root@master ~]# kubectl delete pods mycmd

pod "mycmd" deleted

最大生命周期

[root@master ~]# vim mycmd.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: mycmd

spec:

  terminationGracePeriodSeconds: 0

  activeDeadlineSeconds: 60      # 可以执行的最大时长

  restartPolicy: OnFailure

  containers:

  - name: linux

    image: myos:8.5

    command: [sh]

    args:

    - -c

    - |

      ID=${RANDOM}

      for i in {1..9};do

          echo "${ID} : hello world."

          sleep 5

      done

      exit $((ID%2))

[root@master ~]# kubectl replace --force -f mycmd.yaml

pod "mycmd" deleted

pod/mycmd replaced

[root@master ~]# kubectl get pods -w

mycmd   1/1     Running             0          1s

mycmd   1/1     Running             1          30s

mycmd   0/1     Error               1          62s

案例 4 答案

---

kind: Pod

apiVersion: v1

metadata:

  name: mymem

spec:

  restartPolicy: OnFailure

  containers:

  - name: linux

    image: myos:8.5

    command: [sh]

    args:

    - -c

    - |

      while sleep 5;do

          use=$(free -m |awk '$1=="Mem:"{print $3}')

          if  (( ${use} < 1000 ));then

              echo -e "\x1b[32mINFO:\x1b[39m running normally"

          else

              echo -e "\x1b[31mWARN:\x1b[39m high memory usage"

          fi

      done

Pod调度策略

基于名称调度

[root@master ~]# vim myhttp.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: myhttp

spec:

  nodeName: node-0001     # 基于节点名称进行调度

  containers:

  - name: apache

    image: myos:httpd

[root@master ~]# kubectl apply -f myhttp.yaml  

pod/myhttp created

[root@master ~]# kubectl get pods -o wide

NAME      READY   STATUS    RESTARTS   AGE   IP           NODE

myhttp    1/1     Running   0          3s    10.244.1.6   node-0001

标签管理

# 查看标签

[root@master ~]# kubectl get pods --show-labels

NAME      READY   STATUS    RESTARTS   AGE     LABELS

myhttp    1/1     Running   0          2m34s   <none>

# 添加标签

[root@master ~]# kubectl label pod myhttp app=apache

pod/myhttp labeled

[root@master ~]# kubectl get pods --show-labels

NAME      READY   STATUS    RESTARTS   AGE   LABELS

myhttp    1/1     Running   0          14m   app=apache

# 删除标签

[root@master ~]# kubectl label pod myhttp app-

pod/myhttp unlabeled

[root@master ~]# kubectl get pods --show-labels

NAME      READY   STATUS    RESTARTS   AGE   LABELS

myhttp    1/1     Running   0          14m   <none>

# 资源清单文件配置标签

[root@master ~]# vim myhttp.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: myhttp

  labels:

    app: apache

spec:

  containers:

  - name: apache

    image: myos:httpd

[root@master ~]# kubectl replace --force -f myhttp.yaml

pod "myhttp" deleted

pod/myhttp replaced

[root@master ~]# kubectl get pods --show-labels

NAME     READY   STATUS    RESTARTS   AGE   LABELS

myhttp   1/1     Running   0          7s    app=apache

# 使用标签过滤资源对象

[root@master ~]# kubectl get pods -l app=apache

NAME     READY   STATUS    RESTARTS   AGE

myhttp   1/1     Running   0          6m44s

[root@master ~]# kubectl get nodes -l kubernetes.io/hostname=master

NAME     STATUS   ROLES           AGE    VERSION

master   Ready    control-plane   5d6h   v1.29.2

[root@master ~]# kubectl get namespaces -l kubernetes.io/metadata.name=default

NAME      STATUS   AGE

default   Active   5d6h

基于标签调度

# 查询 node 节点上的标签

[root@master ~]# kubectl get nodes --show-labels

NAME        STATUS   ROLES            VERSION   LABELS

master      Ready    control-plane    v1.29.2   kubernetes.io/hostname=master

node-0001   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0001

node-0002   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0002

node-0003   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0003

# 使用 node 上的标签调度 Pod

[root@master ~]# vim myhttp.yaml

---

kind: Pod

apiVersion: v1

metadata:

  name: myhttp

  labels:

    app: apache

spec:

  nodeSelector:

    kubernetes.io/hostname: node-0002

  containers:

  - name: apache

    image: myos:httpd

[root@master ~]# kubectl replace --force -f myhttp.yaml

pod "myhttp" deleted

pod/myhttp replaced

[root@master ~]# kubectl get pods -o wide

NAME      READY   STATUS    RESTARTS   AGE   IP            NODE

myhttp    1/1     Running   0          1s    10.244.2.11   node-0002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值