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文件.  

k8s常用资源管理_第1张图片

 

[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:

Containers:

  nginx:

    Container ID:

    Image: nginx:1.13

    Image ID:

    Port: 80/TCP

    State: Waiting

      Reason: ContainerCreating

    Ready: False

    Restart Count: 0

    Volume Mounts:

    Environment Variables:

Conditions:

  Type Status

  Initialized True

  Ready False

  PodScheduled True

No volumes.

QoS Class: BestEffort

Tolerations:

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



Welcome to nginx!



Welcome to nginx!


If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to
nginx.org.

Commercial support is available at
nginx.com.

Thank you for using nginx.



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                k8s-node2
test2     2/2       Running             0          5m        172.16.48.3   k8s-master
test3     0/2       ContainerCreating   0          4m                k8s-node2

你可能感兴趣的:(kubernetes,容器,云原生)