CKAD 1. Core Concepts (13%)考题答案

本文详细介绍了如何在Kubernetes中创建、检查、修改和删除Pod。通过实例演示了Pod的生命周期管理,包括解决Pod创建过程中的错误,更改Pod的镜像,以及如何从Pod内部执行命令。此外,还展示了如何获取Pod的IP地址,使用临时Pod进行网络测试,查看Pod日志,最终清理资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Creating a Pod and Inspecting it

  1. Create the namespace ckad-prep.
  2. In the namespace ckad-prep create a new Pod named mypod with the image nginx:2.3.5. Expose the port 80.
  3. Identify the issue with creating the container. Write down the root cause of issue in a file named pod-error.txt.
  4. Change the image of the Pod to nginx:1.15.12.
  5. List the Pod and ensure that the container is running.
  6. Log into the container and run the ls command. Write down the output. Log out of the container.
  7. Retrieve the IP address of the Pod mypod.
  8. Run a temporary Pod using the image busybox, shell into it and run a wget command against the nginx Pod using port 80.
  9. Render the logs of Pod mypod.
  10. Delete the Pod and the namespace.

First, create the namespace.

$ kubectl create namespace ckad-prep

Next, create the Pod in the new namespace.

$ kubectl run mypod --image=nginx:2.3.5 --restart=Never --port=80 --namespace=ckad-prep
pod/mypod created

You will see that the image cannot be pulled as it doesn’t exist with this tag.

$ kubectl get pod -n ckad-prep
NAME    READY   STATUS             RESTARTS   AGE
mypod   0/1     ImagePullBackOff   0          1m

The list of events can give you a deeper insight.

$ kubectl describe pod -n ckad-prep
...
Events:
  Type     Reason                 Age                 From                         Message
  ----     ------                 ----                ----                         -------
  Normal   Scheduled              3m3s                default-scheduler            Successfully assigned mypod to docker-for-desktop
  Normal   SuccessfulMountVolume  3m2s                kubelet, docker-for-desktop  MountVolume.SetUp succeeded for volume "default-token-jbcl6"
  Normal   Pulling                84s (x4 over 3m2s)  kubelet, docker-for-desktop  pulling image "nginx:2.3.5"
  Warning  Failed                 83s (x4 over 3m1s)  kubelet, docker-for-desktop  Failed to pull image "nginx:2.3.5": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:2.3.5 not found
  Warning  Failed                 83s (x4 over 3m1s)  kubelet, docker-for-desktop  Error: ErrImagePull
  Normal   BackOff                69s (x6 over 3m)    kubelet, docker-for-desktop  Back-off pulling image "nginx:2.3.5"
  Warning  Failed                 69s (x6 over 3m)    kubelet, docker-for-desktop  Error: ImagePullBackOff

Go ahead and edit the existing Pod. Alternatively, you could also just use the kubectl set image pod mypod mypod=nginx --namespace=ckad-prep command.

$ kubectl edit pod mypod --namespace=ckad-prep

After setting an image that does exist, the Pod should render the status Running.

$ kubectl get pod -n ckad-prep
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          14m

You can shell into the container and run the ls command.

$ kubectl exec mypod -it --namespace=ckad-prep  -- /bin/sh
/ # ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
/ # exit

Retrieve the IP address of the Pod with the -o wide command line option.

$ kubectl get pods -o wide -n ckad-prep
NAME    READY   STATUS    RESTARTS   AGE   IP               NODE
mypod   1/1     Running   0          12m   192.168.60.149   docker-for-desktop

Remember to use the --rm to create a temporary Pod.

$ kubectl run busybox --image=busybox --rm -it --restart=Never -n ckad-prep -- /bin/sh

If you don’t see a command prompt, try pressing enter.

/ # wget -O- 192.168.60.149:80
Connecting to 192.168.60.149:80 (192.168.60.149:80)
<!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>
-                    100% |**********************************************************************|   612  0:00:00 ETA
/ # exit

The logs of the Pod should show a single line indicating our request.

$ kubectl logs mypod -n ckad-prep
192.168.60.162 - - [17/May/2019:13:35:59 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"

Delete the Pod and namespace after you are done.

$ kubectl delete pod mypod --namespace=ckad-prep
pod "mypod" deleted
$ kubectl delete namespace ckad-prep
namespace "ckad-prep" deleted
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值