CKA备考实验 | pod的基本操作

书籍来源:《CKA/CKAD应试指南:从Docker到Kubernetes完全攻略》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:CKA备考实验 | 汇总_热爱编程的通信人的博客-CSDN博客


基本上所有的操作都是以命令行操作的,大家一定要把操作在脑子里以图形化方式想象出来,这样更容易去理解。我们把pod想象成一个黑盒子,里面运行了一个进程。这个黑盒子又没有显示器,那么如何在pod里执行命令呢?如图5-5所示。

本节练习如何在容器中执行命令,往容器里拷贝文件,查看容器的日志。

在容器中执行命令的语法如下。

kubectl exec pod 名字 -- 命令

步骤1:查看pod1里/usr/share/nginx/html里的内容。

##########实操验证##########
[root@vms10 pod]# kubectl exec pod1 -- ls /usr/share/nginx/html
50x.html
index.html
[root@vms10 pod]#

注意:这里pod名后面要有--,这个是固定用法。也可以让容器和物理机之间互拷文件,物理机拷贝文件到容器的用法如下。

kubectl cp /path1/file1 pod:/path2/:把物理机里文件的/path1/file1拷贝到pod的/path2里。

也可以把容器里的东西拷贝到物理机,这里要注意拷贝的是目录还是文件。

Kubectl cp pod:/path2/ /path1/:把容器里目录/path2/里的东西拷贝到物理机的/path1里。

如果从容器里拷贝的是文件而不是目录的话,则需要在物理机里指定文件名。

kubectl cp pod:/path2/file2 /path1/file2:把容器里的文件/path2/file2拷贝到物理机的目录/path1里。

步骤2:把物理机的文件/etc/hosts拷贝到pod1里。

##########实操验证##########
[root@vms10 pod]# kubectl cp /etc/hosts pod1:/usr/share/nginx/html
[root@vms10 pod]# kubectl exec pod1 -- ls /usr/share/nginx/html
50x.html
hosts
index.html
[root@vms10 pod]#

步骤3:把pod里的东西拷贝到物理机。

##########实操验证##########
[root@vms10 pod]# kubectl cp pod1:/usr/share/nginx/html/ /opt
tar: Removing leading `/' from member names
[root@vms10 pod]# ls /opt/
50x.html  cni  containerd  hosts  index.html  rh
[root@vms10 pod]#

步骤4:进入pod里并获取bash。

##########实操验证##########
[root@vms10 pod]# kubectl exec -it pod1 -- bash
root@pod1:/# 
root@pod1:/# exit
exit
[root@vms10 pod]#

步骤5:如果pod里有多个容器的话,默认是进入第一个容器里,如图5-6所示。

##########实操验证##########
[root@vms10 pod]# kubectl exec -it pod2 -- bash
Defaulted container "c1" out of: c1, c2
root@pod2:/# exit
exit
[root@vms10 pod]#

步骤6:如果想进入第二个容器里的话,用-c指定容器名。

##########实操验证##########
[root@vms10 pod]# kubectl exec -it pod2 -c c2 -- bash
root@pod2:/# exit
exit
[root@vms10 pod]#

注意:从前面的yaml文件可知,名字为pod2的pod里有两个容器,分别是c1和c2。

步骤7:pod的具体属性可以通过describe查看。

##########实操验证##########
[root@vms10 pod]# kubectl describe pod pod2
Name:         pod2
Namespace:    default
Priority:     0
Node:         vms12.rhce.cc/192.168.1.112
Start Time:   Tue, 09 May 2023 09:44:59 +0800
Labels:       run=pod2
Annotations:  cni.projectcalico.org/containerID: b79cdc26f6ed94073e660b90bb2624d632fdf976e3e5c6b9d748747c59fc3299
              cni.projectcalico.org/podIP: 10.244.14.4/32
              cni.projectcalico.org/podIPs: 10.244.14.4/32
Status:       Running
IP:           10.244.14.4
IPs:
  IP:  10.244.14.4
Containers:
  c1:
    Container ID:  docker://f3544f71f587de202998fd1101b6a9c6197a6a03c4f2d810602bcf7d92de0b10
    Image:         nginx
    Image ID:      docker-pullable://nginx@sha256:480868e8c8c797794257e2abd88d0f9a8809b2fe956cbfbc05dcc0bca1f7cd43
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      echo aa ; sleep 1000
    State:          Running
      Started:      Tue, 09 May 2023 09:45:00 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-sfvpf (ro)
  c2:
    Container ID:   docker://a4446f9aaa28302f10e95f760c5c18ef46f1f4bb4dce56ef0652e42c7e7a331e
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:480868e8c8c797794257e2abd88d0f9a8809b2fe956cbfbc05dcc0bca1f7cd43
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 09 May 2023 09:45:00 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-sfvpf (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-sfvpf:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  7m28s  default-scheduler  Successfully assigned default/pod2 to vms12.rhce.cc
  Normal  Pulled     7m28s  kubelet            Container image "nginx" already present on machine
  Normal  Created    7m28s  kubelet            Created container c1
  Normal  Started    7m28s  kubelet            Started container c1
  Normal  Pulled     7m28s  kubelet            Container image "nginx" already present on machine
  Normal  Created    7m28s  kubelet            Created container c2
  Normal  Started    7m28s  kubelet            Started container c2
[root@vms10 pod]#

步骤8:查看pod里的输出。

##########实操验证##########
[root@vms10 pod]# kubectl logs pod1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/05/09 01:32:45 [notice] 1#1: using the "epoll" event method
2023/05/09 01:32:45 [notice] 1#1: nginx/1.23.4
2023/05/09 01:32:45 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/05/09 01:32:45 [notice] 1#1: OS: Linux 3.10.0-693.el7.x86_64
2023/05/09 01:32:45 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2023/05/09 01:32:45 [notice] 1#1: start worker processes
2023/05/09 01:32:45 [notice] 1#1: start worker process 29
2023/05/09 01:32:45 [notice] 1#1: start worker process 30
[root@vms10 pod]#

步骤9:如果一个pod里有多个容器,需要使用-c指定查看哪个容器的输出。

##########实操验证##########
[root@vms10 pod]# kubectl logs pod2
error: a container name must be specified for pod pod2, choose one of: [c1 c2]
[root@vms10 pod]# kubectl logs pod2 -c c1
aa
[root@vms10 pod]#

步骤10:删除这两个pod。

##########实操验证##########
[root@vms10 pod]# kubectl delete pod pod1
pod "pod1" deleted
[root@vms10 pod]# 
[root@vms10 pod]# kubectl delete -f pod2.yaml 
pod "pod2" deleted
[root@vms10 pod]#

注意:

(1)可以用kubectl delete pod名字或者kubectl delete -f pod.yaml删除pod。

(2)为了删除的速度更快一些,可以加上--force选项。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值