k8s常用资源管理

目录

Pod(容器组):Pod是Kubernetes中最小的部署单元,可以包含一个或多个容器。Pod提供了一种逻辑上的封装,使得容器可以一起共享网络和存储资源

1、创建一个pod

2、pod管理

pod操作


Pod(容器组):Pod是Kubernetes中最小的部署单元,可以包含一个或多个容器。Pod提供了一种逻辑上的封装,使得容器可以一起共享网络和存储资源

1、创建一个pod

1)创建yuml文件

[root@k8s-master ~]# mkdir k8s

[root@k8s-master ~]# vim ./k8s/nginx.yaml

piVersion: v1

kind: Pod

metadata:

name: nginx

labels:

app: web

spec:

containers:

  • name: nginx

image: nginx:1.13

ports:

  • containerPort: 80

2)创建容器

方法一. yum安装

[root@k8s-master ~]#yum install *rhsm*

方法二

执行命令:

[root@k8s-master ~]#wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
[root@k8s-master ~]# yum -y install *rhsm*

[root@k8s-master ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm

[root@k8s-master ~]# rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem

前两个命令会生成/etc/rhsm/ca/redhat-uep.pem文件.

[root@k8s-master ~]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ...

latest: Pulling from registry.access.redhat.com/rhel7/pod-infrastructure

26e5ed6899db: Downloading 34.95 MB/74.87 MB

66dbe984a319: Download complete

9138e7863e08: Download complete

创建pod

[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml

pod "nginx" created

3)查看所有pod创建运行状态

[root@k8s-master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx 0/1 ContainerCreating 0 17s

4)查看指定pod资源

[root@k8s-master ~]# kubectl get pod nginx

NAME READY STATUS RESTARTS AGE

nginx 0/1 ContainerCreating 0 41s

5)查看pod运行的详细信息

[root@k8s-master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Node: k8s-node2/192.168.50.51

Start Time: Thu, 10 Aug 2023 20:33:09 +0800

Labels: app=web

Status: Pending

IP:

Controllers: <none>

Containers:

nginx:

Container ID:

Image: nginx:1.13

Image ID:

Port: 80/TCP

State: Waiting

Reason: ContainerCreating

Ready: False

Restart Count: 0

Volume Mounts: <none>

Environment Variables: <none>

Conditions:

Type Status

Initialized True

Ready False

PodScheduled True

No volumes.

QoS Class: BestEffort

Tolerations: <none>

Events:

FirstSeen LastSeen Count From SubObjectPath Type ReasonMessage


1m 1m 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to k8s-node2

25s 25s 1 {kubelet k8s-node2} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""

1m 4s 3 {kubelet k8s-node2} Warning FailedSync Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request. details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

显示调度节点 验证运行的pod

[root@k8s-master ~]# kubectl get pod nginx -o wide

NAME READY STATUS RESTARTS AGE IP NODE

nginx 0/1 ContainerCreating 0 2m <none> k8s-node2

2、pod管理

1)删除pod

[root@k8s-master ~]# kubectl delete pod nginx

pod "nginx" deleted

2)查看删除pod无法找到

[root@k8s-master ~]# kubectl get pod nginx -o wide

Error from server (NotFound): pods "nginx" not found

3)创建pod

[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml

pod "nginx" created

4)发现最先创建的pod运行在k8s-master节点上,下载镜像速度太慢没法运行

[root@k8s-master ~]# kubectl get pod nginx -o wide

NAME READY STATUS RESTARTS AGE IP NODE

nginx 0/1 ContainerCreating 0 38s <none> k8s-master

5)访问pod节点中的服务

[root@k8s-master ~]# kubectl get pod nginx -o wide

NAME READY STATUS RESTARTS AGE IP NODE

nginx 1/1 Running 0 21m 172.16.48.2 k8s-master

[root@k8s-master ~]# curl -l http://172.16.48.2

<!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>

pod操作

1)在一个pod中修改配置文件运行多个业务容器

[root@k8s-master ~]# vim ./k8s/nginx.yaml

apiVersion: v1

kind: Pod

metadata:

name: test3

labels:

app: web

spec:

containers:

  • name: nginx02

image: nginx:1.13

ports:

  • containerPort: 80

  • name: busybox

image: docker.io/busybox:latest

command: ["sleep","3600"]

ports:

  • containerPort: 80

~

2)创建资源

[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml

3)查看资源使用情况 Pod会 分配了各自的 IP 过程会有点慢正常现象

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

NAME READY STATUS RESTARTS AGE IP NODE

nginx 1/1 Running 0 25m 172.16.48.2 k8s-master

test1 0/2 ContainerCreating 0 9m <none> k8s-node2

test2 2/2 Running 0 5m 172.16.48.3 k8s-master

test3 0/2 ContainerCreating 0 4m <none> k8s-node2

相关推荐
小扳3 小时前
微服务篇-深入了解 MinIO 文件服务器(你还在使用阿里云 0SS 对象存储图片服务?教你使用 MinIO 文件服务器:实现从部署到具体使用)
java·服务器·分布式·微服务·云原生·架构
aherhuo13 小时前
kubevirt网络
linux·云原生·容器·kubernetes
陌北v113 小时前
Docker Compose 配置指南
运维·docker·容器·docker-compose
catoop14 小时前
K8s 无头服务(Headless Service)
云原生·容器·kubernetes
阿里嘎多学长14 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
小峰编程14 小时前
独一无二,万字详谈——Linux之文件管理
linux·运维·服务器·云原生·云计算·ai原生
小马爱打代码15 小时前
云原生服务网格Istio实战
云原生
liuxuzxx15 小时前
1.24.1-Istio安装
kubernetes·istio·service mesh
G_whang15 小时前
windos 安装docker
运维·docker·容器
道一云黑板报15 小时前
Flink集群批作业实践:七析BI批作业执行
大数据·分布式·数据分析·flink·kubernetes