pod(三):pod的管理

服务器版本 docker软件版本 CPU架构 CentOS Linux release 7.4.1708 (Core) Docker version 20.10.12 x86_64

pod的常见管理。

3.1 环境介绍

Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点

服务器 操作系统版本 CPU架构 进程 功能描述 k8scloude1/192.168.110.130 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master节点 k8scloude2/192.168.110.129 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点 k8scloude3/192.168.110.128 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点

3.2 管理pod

使用Nginx镜像创建一个pod

[root@k8scloude1 pod]# vim nginx.yaml

#kind: Pod表示资源类型为Pod   labels指定pod标签   metadata下面的name指定pod名字   containers下面全是容器的定义
#image指定镜像名字  imagePullPolicy指定镜像下载策略   containers下面的name指定容器名
#resources指定容器资源(CPU,内存等)   env指定容器里的环境变量   dnsPolicy指定DNS策略
#restartPolicy容器重启策略    ports指定容器端口
[root@k8scloude1 pod]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
    ports:
    - name: http
      containerPort: 80
      protocol: TCP
    env:
    - name: xx
      value: "12"
    - name: yy
      value: "21"
    - name: zz
      value: hello
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

[root@k8scloude1 pod]# ls
nginx.yaml  pod1.yaml  pod2.yaml

[root@k8scloude1 pod]# kubectl apply -f nginx.yaml
pod/nginx created

[root@k8scloude1 pod]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          5s

不进入容器,执行ls / 命令

#不进入容器,执行命令:kubectl exec podname -- 命令
[root@k8scloude1 pod]# kubectl exec nginx -- ls /
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

进入容器执行命令

#进入容器:kubectl exec -it podname -- bash
[root@k8scloude1 pod]# kubectl exec -it nginx -- bash
root@nginx:/# which nginx
/usr/sbin/nginx
root@nginx:/# exit
exit

从物理机复制文件到pod里

[root@k8scloude1 pod]# kubectl cp /etc/hosts nginx:/tmp

[root@k8scloude1 pod]# kubectl exec nginx -- ls /tmp
hosts

从pod里复制文件到物理机

[root@k8scloude1 pod]# kubectl cp nginx:/etc/hosts nginx_hosts
tar: Removing leading `/' from member names

[root@k8scloude1 pod]# ls
nginx_hosts  nginx.yaml  pod1.yaml  pod2.yaml

[root@k8scloude1 pod]# cat nginx_hosts
Kubernetes-managed hosts file.

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.244.251.202  nginx

[root@k8scloude1 pod]# rm -rf nginx_hosts

查看pod的日志

[root@k8scloude1 pod]# kubectl logs nginx
/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/01/12 13:41:43 [notice] 1#1: using the "epoll" event method
2022/01/12 13:41:43 [notice] 1#1: nginx/1.21.5
2022/01/12 13:41:43 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/01/12 13:41:43 [notice] 1#1: OS: Linux 3.10.0-693.el7.x86_64
2022/01/12 13:41:43 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/01/12 13:41:43 [notice] 1#1: start worker processes
2022/01/12 13:41:43 [notice] 1#1: start worker process 31
2022/01/12 13:41:43 [notice] 1#1: start worker process 32

当一个pod里有两个容器,怎么查看?kubectl exec -it podname -c 容器名 — 命令

首先创建一个包含2个容器的pod

[root@k8scloude1 pod]# vim pod2.yaml

[root@k8scloude1 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod1
  name: pod1
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: n1
    resources: {}
  - image: nginx
    imagePullPolicy: IfNotPresent
    command: ["sh","-c","sleep 10"]
    name: n2
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

[root@k8scloude1 pod]# kubectl apply -f pod2.yaml
pod/pod1 created

[root@k8scloude1 pod]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
pod1    2/2     Running   0          6s

查看pod1的描述信息

[root@k8scloude1 pod]# kubectl describe pod pod1
Name:         pod1
Namespace:    pod
Priority:     0
Node:         k8scloude2/192.168.110.129
Start Time:   Wed, 12 Jan 2022 21:53:05 +0800
Labels:       run=pod1
Annotations:  cni.projectcalico.org/containerID: d103a6cb8e6535c5cfa8cf52153a80c11b75c0b7a744c7ad1028f3f4e88a627e
              cni.projectcalico.org/podIP: 10.244.112.141/32
              cni.projectcalico.org/podIPs: 10.244.112.141/32
Status:       Running
IP:           10.244.112.141
IPs:
  IP:  10.244.112.141
Containers:
  n1:
    Container ID:   docker://e54540c02e54109af7437fd00f18bcca3969e75eafb336dadb9ddb21022520ed
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:
    Host Port:
    State:          Running
      Started:      Wed, 12 Jan 2022 21:53:06 +0800
    Ready:          True
    Restart Count:  0
    Environment:
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-42h2q (ro)
  n2:
    Container ID:  docker://2d09c058d4c11f51c1c22c58012c79f5b8ec8327fcdd43e17f4533cb01f098d0
    Image:         nginx
    Image ID:      docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:
    Host Port:
    Command:
      sh
      -c
      sleep 10
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 12 Jan 2022 21:53:39 +0800
      Finished:     Wed, 12 Jan 2022 21:53:49 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 12 Jan 2022 21:53:17 +0800
      Finished:     Wed, 12 Jan 2022 21:53:27 +0800
    Ready:          False
    Restart Count:  2
    Environment:
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-42h2q (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-42h2q:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:
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  46s                default-scheduler  Successfully assigned pod/pod1 to k8scloude2
  Normal   Pulled     46s                kubelet            Container image "nginx" already present on machine
  Normal   Created    46s                kubelet            Created container n1
  Normal   Started    46s                kubelet            Started container n1
  Normal   Pulled     13s (x3 over 46s)  kubelet            Container image "nginx" already present on machine
  Normal   Created    13s (x3 over 46s)  kubelet            Created container n2
  Normal   Started    13s (x3 over 46s)  kubelet            Started container n2
  Warning  BackOff    3s (x2 over 24s)   kubelet            Back-off restarting failed container

查看pod里的n1容器的/tmp目录

#当一个pod里有两个容器,怎么查看:kubectl exec -it podname -c 容器名 -- 命令
[root@k8scloude1 pod]# kubectl exec -it pod1 -c n1 -- ls /tmp

查看pod里的n2容器的/tmp目录

[root@k8scloude1 pod]# kubectl exec -it pod1 -c n2 -- ls /tmp

进入pod1里的n1容器

[root@k8scloude1 pod]# kubectl exec -it pod1 -c n1 -- bash
root@pod1:/# which nginx
/usr/sbin/nginx
root@pod1:/# exit
exit

查看pod1里的n1容器日志

[root@k8scloude1 pod]# kubectl logs pod1 -c n1
/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/01/12 13:53:06 [notice] 1#1: using the "epoll" event method
2022/01/12 13:53:06 [notice] 1#1: nginx/1.21.5
2022/01/12 13:53:06 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/01/12 13:53:06 [notice] 1#1: OS: Linux 3.10.0-693.el7.x86_64
2022/01/12 13:53:06 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/01/12 13:53:06 [notice] 1#1: start worker processes
2022/01/12 13:53:06 [notice] 1#1: start worker process 32
2022/01/12 13:53:06 [notice] 1#1: start worker process 33

编辑pod: kubectl edit pod podname

容器里运行命令的一种写法是使用command

[root@k8scloude1 pod]# vim pod2.yaml

[root@k8scloude1 pod]# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod1
  name: pod1
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: n1
    resources: {}
  - image: nginx
    imagePullPolicy: IfNotPresent
    command: ["sh","-c","sleep 10"]
    name: n2
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

容器里运行命令的另一种写法是使用args

[root@k8scloude1 pod]# kubectl run podtest --image=nginx --dry-run=client -o yaml -- sh -c sleep 100
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: podtest
  name: podtest
spec:
  containers:
  - args:
    - sh
    - -c
    - sleep
    - "100"
    image: nginx
    name: podtest
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Original: https://www.cnblogs.com/renshengdezheli/p/16707021.html
Author: 人生的哲理
Title: pod(三):pod的管理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582019/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Java实现哈希表

    2.1、哈希冲突 冲突位置,把数据构建为链表结构。 装载因子=哈希表中的元素个数 / (散列表)哈希表的长度 装载因子越大,说明链表越长,性能就越低,那么哈希表就需要扩容,把数据迁…

    Linux 2023年6月14日
    078
  • 微信公众号开发之获取微信用户的openID

    (注:openID同一用户同一应用唯一,UnionID同一用户不同应用唯一。不同应用指微信开放平台下的不同用户。) 1、 申请测试号(获得appID、appsecret) 2、 填…

    Linux 2023年6月13日
    076
  • Linux常用命令

    基本操作 创建目录: mkdir xxx 删除目录: rm -rf xxx 切换目录: cd xxx,返回: cd – 创建文件: touch xxx.txt 复制文件: cp x…

    Linux 2023年6月8日
    0108
  • 解决JSP文件在浏览器访问中文乱码问题

    指定编码类型为支持中文的编码1.添加 第二句是设置输出到浏览器,浏览器选择的编码方式加上这两句已后页面访问将不再是乱码了. Original: https://www.cnblog…

    Linux 2023年6月7日
    086
  • Color 16 Base Code 颜色代码大全

    颜色预览表,请参考以下图片。 十六进制颜色编码字符串如下所示(前置的英语单词都是颜色) ‘aliceblue’: ‘#F0F8FF’…

    Linux 2023年6月7日
    0113
  • NTP服务器实现

    时间服务器是一种计算机网络仪器,它从参考时钟获取实际时间,再利用计算机网络把时间信息传递给用户。虽然还有一些比较少用或过时的协议仍然在使用,但现时最重要及广泛使用,作为时间信息发送…

    Linux 2023年6月7日
    080
  • Failed to configure a DataSource ‘url’ attribute问题解决

    才写了一行代码又报错了.. *************************** APPLICATION FAILED TO START ********************…

    Linux 2023年6月13日
    077
  • Java高级

    抽象类和抽象方法 1.定义 随着继承层次中一个个新子类的定义,类变得越来越具体,而父类则更一般,更通用。 类的设计应该保证父类和子类都能够共享特征。 有时候将一个父类设计的非常抽象…

    Linux 2023年6月13日
    0105
  • ArrayList中的遍历删除

    例如我们有下列数据,要求遍历列表并删除所有偶数。 List myList = new ArrayList<>(Arrays.toList(new Integer[]{2…

    Linux 2023年6月13日
    079
  • supervisord 进程管理利器

    Supervisor概述 ​ supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。superv…

    Linux 2023年6月14日
    0101
  • ceph存储集群搭建以及介绍

    转载至https://blog.csdn.net/weixin_44989941/article/details/123370410 Original: https://www.c…

    Linux 2023年6月14日
    086
  • django.template.exceptions.TemplateDoesNotExist: django_filters/rest_framework/form.html

    django.template.exceptions.TemplateDoesNotExist: django_filters/rest_framework/form.htmlER…

    Linux 2023年6月14日
    0151
  • Ubuntu更换镜像源

    当修改 sources.list文件时,我们需要将下面任意一个镜像源的代码 复制粘贴到该文件中。 阿里源 阿里镜像源 deb http://mirrors.aliyun.com/u…

    Linux 2023年6月14日
    081
  • JavaScript快速入门-05-基本语句

    5 基本语句 5.1 if 语句 if 语句常用语法如下所示: if (condition) { statement1; } else { statement2; } 或 if (…

    Linux 2023年6月7日
    0129
  • NO.1 通讯录管理系统+源代码(C++)

    功能描述:显示简单的菜单,供用户选择操作 实现步骤:直接cout输出 功能描述:根据用户不同的操作代码选择,进入不同的功能,我们使用switch分支结构进行搭建 实现步骤:用whi…

    Linux 2023年6月7日
    0149
  • BKT的胡测题解:第一套第一题parts

    博客园 :当前访问的博文已被密码保护 请输入阅读密码: Original: https://www.cnblogs.com/Grharris/p/11530239.htmlAuth…

    Linux 2023年6月6日
    083
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球