k8s(6)

简介: k8s(6)

容器与镜像

容器运行时接口(CRI)

Kubelet运行在每个节点(Node)上,用于管理和维护Pod和容器的状态。

容器运行时接口(CRI)是kubelet 和容器运行时之间通信的主要协议。它将 Kubelet 与容器运行时解耦,理论上,实现了CRI接口的容器引擎,都可以作为kubernetes的容器运行时。

Docker没有实现(CRI)接口,Kubernetes使用dockershim来兼容docker。

自V1.24版本起,Dockershim 已从 Kubernetes 项目中移除。

crictl是一个兼容CRI的容器运行时命令,他的用法跟docker命令一样,可以用来检查和调试底层的运行时容器。

crictl pull mysql:5.7-debian
crictl images

在一些局域网环境下,我们没法通过互联网拉取镜像,可以手动的导出、导入镜像。ss

crictl命令没有导出、导入镜像的功能。

需要使用ctr命令导出、导入镜像,它是containerd的命令行接口。

POD使用

POD的介绍

可能遇到的情况

如果创建完查看一直是0/1,需要查看是否打开服务器,如果都打开了,等待一下,可能是首次创建或网速不好

需要查看集群分配的服务器是否打开---k8s3

如果搭建了集群,一定要开启所有集群后在创建,否则分配到未打开的服务器,会卡在那里,先创建后打开服务器发现还是卡着,这时删除pod,然后重新创建。

pod指令

kubectl run mynginx --image=nginx
#在容器中执行,exit退出
kubectl exec mynginx -it -- /bin/bash
# -it 交互模式 
# --rm 退出后删除容器,多用于执行一次性任务或使用客户端
kubectl run mynginx --image=nginx -it --rm -- /bin/bash 
# 查看Pod
kubectl get pod
# 描述
kubectl describe pod mynginx
# 查看Pod的运行日志
kubectl logs mynginx
# 显示pod的IP和运行节点信息
kubectl get pod -owide
# 动态查看,ctrl+c终止
kubectl get pod --watch
# 删除
kubectl delete pod mynginx
# 强制删除
kubectl delete pod mynginx --force

pod讲解

临时进入客户端或者临时pod--busybox_百度百科

# 创建pod
[root@k8s ~]# kubectl run mynginx --image=nginx
pod/mynginx created
# 查看Pod
[root@k8s ~]# kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
mynginx   1/1     Running   0          31m
# 查看Pod -owide
[root@k8s ~]# kubectl get pod -owide
NAME      READY   STATUS    RESTARTS   AGE   IP          NODE   NOMINATED NODE   READINESS GATES
mynginx   1/1     Running   0          87s   10.42.1.4   k8s3   <none>           <none>
# 查看详细信息
[root@k8s ~]# kubectl describe pod mynginx
Name:             mynginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s3/192.168.80.17
Start Time:       Fri, 09 Dec 2022 11:42:50 +0800
Labels:           run=mynginx
Annotations:      <none>
Status:           Running
IP:               10.42.1.4
IPs:
  IP:  10.42.1.4
Containers:
  mynginx:
    Container ID:   containerd://bc81af72d17d1a325f91aa4b8aba9df2e0df4b39586815143326940cbbd2f863
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:ab589a3c466e347b1c0573be23356676df90cd7ce2dbf6ec332a5f0a8b5e59db
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 09 Dec 2022 11:42:53 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-8wqhf (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-8wqhf:
    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  16s   default-scheduler  Successfully assigned default/mynginx to k8s3
  Normal  Pulling    17s   kubelet            Pulling image "nginx"
  Normal  Pulled     14s   kubelet            Successfully pulled image "nginx" in 2.970890083s
  Normal  Created    14s   kubelet            Created container mynginx
  Normal  Started    14s   kubelet            Started container mynginx
# 查看日志
[root@k8s ~]# kubectl logs mynginx
/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
2022/12/09 03:42:53 [notice] 1#1: using the "epoll" event method
2022/12/09 03:42:53 [notice] 1#1: nginx/1.23.2
2022/12/09 03:42:53 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/12/09 03:42:53 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2022/12/09 03:42:53 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/09 03:42:53 [notice] 1#1: start worker processes
2022/12/09 03:42:53 [notice] 1#1: start worker process 29
2022/12/09 03:42:53 [notice] 1#1: start worker process 30
2022/12/09 03:42:53 [notice] 1#1: start worker process 31
2022/12/09 03:42:53 [notice] 1#1: start worker process 32
2022/12/09 03:42:53 [notice] 1#1: start worker process 33
2022/12/09 03:42:53 [notice] 1#1: start worker process 34
2022/12/09 03:42:53 [notice] 1#1: start worker process 35
2022/12/09 03:42:53 [notice] 1#1: start worker process 36
# 使用Pod的ip+pod里面运行容器的端口
[root@k8s ~]# curl 10.42.1.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
#在容器中执行,exit退出
[root@k8s ~]# kubectl exec mynginx -it -- /bin/bash
root@mynginx:/# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
# 一次性任务或使用客户端,ping百度成功,ping主机成功
# -it 交互模式 
# --rm 退出后删除容器,多用于执行一次性任务或使用客户端
[root@k8s ~]# kubectl run mytest --image=busybox -it --rm
If you dont see a command prompt, try pressing enter.
/ # curl localhost
sh: curl: not found
/ # ping baidu.com
PING baidu.com (39.156.66.10): 56 data bytes
64 bytes from 39.156.66.10: seq=0 ttl=127 time=20.054 ms
64 bytes from 39.156.66.10: seq=1 ttl=127 time=20.618 ms
64 bytes from 39.156.66.10: seq=2 ttl=127 time=20.949 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 20.054/20.540/20.949 ms
/ # ping 192.168.0.15
PING 192.168.0.15 (192.168.0.15): 56 data bytes
^C
--- 192.168.0.15 ping statistics ---
10 packets transmitted, 0 packets received, 100% packet loss
/ # ping 192.168.80.15
PING 192.168.80.15 (192.168.80.15): 56 data bytes
64 bytes from 192.168.80.15: seq=0 ttl=63 time=0.469 ms
64 bytes from 192.168.80.15: seq=1 ttl=63 time=0.712 ms
64 bytes from 192.168.80.15: seq=2 ttl=63 time=0.486 ms
^C
--- 192.168.80.15 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.469/0.555/0.712 ms
/ # exit
Session ended, resume using 'kubectl attach mytest -c mytest -i -t' command when the pod is running
pod "mytest" deleted
# 查看pod,没有刚才的临时pod
[root@k8s ~]# kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
mynginx   1/1     Running   0          14m
# 删除
[root@k8s ~]# kubectl delete pod mynginx
pod "mynginx" deleted
[root@k8s ~]# kubectl get pod
No resources found in default namespace.
# 强制删除
kubectl delete pod mynginx --force

相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
Kubernetes 搜索推荐 Linux
Containerd容器镜像管理
Containerd容器镜像管理
|
9月前
|
人工智能
我学GEO第10天:被豆包引用了,还被千问、元宝认识了
我是二二得四,专注GEO优化第10天。零基础起步,坚持每日图文输出、多平台分发、AI友好写作,已实现豆包/千问/元宝识别“二二得四”(置信度50%-65%),首篇文章被豆包引用。边学边测、边做边迭代,用真实过程记录普通人可复制的AI时代品牌可见性增长路径。
717 7
|
Prometheus 监控 Cloud Native
基于k8s+Prometheus+Alertmanager+Grafana构建企业级监控告警系统(下)
基于k8s+Prometheus+Alertmanager+Grafana构建企业级监控告警系统
|
9月前
|
弹性计算 监控 测试技术
阿里云轻量应用服务器峰值200M带宽是不是独享的?使用中有哪些注意事项?
阿里云轻量应用服务器的200M带宽为共享峰值带宽,非独享,实际速率受网络负载影响。其以低成本(如38元/年)提供高带宽,适合个人开发者及中小型网站。使用时需注意区分带宽与流量,避免超额费用;合理规划业务时段与架构,如错峰运行、优化应用;监控流量使用,防止欠费停机;业务增长时,及时升级至更稳定的云服务器ECS。理解共享带宽原理并合理规划,可充分发挥其高性价比优势。
|
12月前
|
机器学习/深度学习 人工智能 芯片
当算力变成“新石油”:AI 芯片的战争、底层逻辑与未来爆点
当算力变成“新石油”:AI 芯片的战争、底层逻辑与未来爆点
609 15
|
11月前
|
存储 运维 Kubernetes
Kubernetes 多租户到底怎么隔离?命名空间、独立集群、虚拟集群,别再拍脑袋选了
Kubernetes 多租户到底怎么隔离?命名空间、独立集群、虚拟集群,别再拍脑袋选了
527 10
|
Docker 容器
三步解决Docker拉取镜像报错:x509: certificate has expired or is not yet v..
三步解决Docker拉取镜像报错:x509: certificate has expired or is not yet v..
2970 0
阿里云CDN收费标准,不同计费模式价格表(基础服务费和增值服务费用整理)
阿里云CDN费用包含基础服务费和增值服务费。基础费用按流量、带宽峰值或月结95带宽计费,默认按流量计费;增值服务如HTTPS、QUIC、WAF、实时日志等按使用量收费,不使用不收费。支持资源包抵扣,降低成本。
3743 3
|
数据管理 API 调度
阿里云百炼平台知识检索应用评测:搭建之旅与一点建议
阿里云百炼平台成为企业智能化转型的重要工具之一。
|
存储 数据库 Docker
正确删除容器和镜像的方式
【10月更文挑战第24天】本文介绍了在Docker中如何正确删除容器和镜像,包括停止容器、删除已停止容器、删除未被使用的镜像以及注意事项,如数据备份、依赖关系检查和权限问题。其他容器管理工具的操作类似,但命令和语法可能不同。
4178 4