【4】k8s_NameSpace&&Pod

本文介绍了Kubernetes中命名空间的作用,用于资源隔离,并展示了如何通过命令行直接操作Pod,包括创建、查看和描述Pod的详细信息。此外,还提到了通过yaml文件配置Pod以及在Pod中运行多个容器的方法。

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

目录

一、NameSpace

二、Pod

【1】命令式对象管理: 直接使用命令去操作kubernetes资源

【2】命令式对象配置: 通过命令配置和配置文件去操作作kubernetes资源

1、写一个ymal文件

2、然后运行yaml文件(提前创建好了命名空间string)

3、用bashboard创建pod

4、一个pod里面运行多个容器


一、NameSpace

  • 命名空间,用来对集群资源进行隔离划分。默认只隔离资源,不隔离网络
[root@master ~]# kubectl create ns dev
namespace/dev created
[root@master ~]#  kubectl get ns
NAME                   STATUS   AGE
default                Active   23h
dev                    Active   6s
kube-node-lease        Active   23h
kube-public            Active   23h
kube-system            Active   23h
kubernetes-dashboard   Active   77m

二、Pod

  • 运行中的一组容器,Pod是kuberneres中应用的最小单位

【1】命令式对象管理: 直接使用命令去操作kubernetes资源

[root@master ~]# kubectl get pod -A
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-system            calico-kube-controllers-56c7cdffc6-trxkj     1/1     Running   1          23h
kube-system            calico-node-8zsdw                            1/1     Running   1          22h
kube-system            calico-node-khp6k                            1/1     Running   1          22h
kube-system            calico-node-vkqhc                            1/1     Running   1          23h
kube-system            coredns-7f89b7bc75-65jnr                     1/1     Running   1          23h
kube-system            coredns-7f89b7bc75-lrtqq                     1/1     Running   1          23h
kube-system            etcd-master                                  1/1     Running   1          23h
kube-system            kube-apiserver-master                        1/1     Running   1          23h
kube-system            kube-controller-manager-master               1/1     Running   1          23h
kube-system            kube-proxy-9k8ps                             1/1     Running   1          23h
kube-system            kube-proxy-nvzmj                             1/1     Running   1          22h
kube-system            kube-proxy-zph7s                             1/1     Running   1          22h
kube-system            kube-scheduler-master                        1/1     Running   1          23h
kubernetes-dashboard   dashboard-metrics-scraper-79c5968bdc-l4fjp   1/1     Running   0          70m
kubernetes-dashboard   kubernetes-dashboard-658485d5c7-dj8qn        1/1     Running   0          70m
[root@master ~]# kubectl run mynginx --image=nginx
pod/mynginx created
[root@master ~]# kubectl get pod
NAME      READY   STATUS              RESTARTS   AGE
mynginx   0/1     ContainerCreating   0          10s
[root@master ~]# kubectl describe pod mynginx
Name:         mynginx
Namespace:    default
Priority:     0
Node:         node02/172.31.0.2
Start Time:   Fri, 06 Jan 2023 23:27:42 +0800
Labels:       run=mynginx
Annotations:  cni.projectcalico.org/containerID: 2eec547318e2d978dfbbf589915cd684fbd2f8450609583cfe20e43c3d5e39df
              cni.projectcalico.org/podIP: 192.168.140.71/32
              cni.projectcalico.org/podIPs: 192.168.140.71/32
Status:       Running
IP:           192.168.140.71
IPs:
  IP:  192.168.140.71
Containers:
  mynginx:
    Container ID:   docker://8f0e73fc980847280fe363558dd6dbae3fd1f803a3823a982208e32d87827f8b
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 06 Jan 2023 23:28:52 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-dcw8j (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-dcw8j:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-dcw8j
    Optional:    false
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  86s   default-scheduler  Successfully assigned default/mynginx to node02
  Normal  Pulling    85s   kubelet            Pulling image "nginx"
  Normal  Pulled     20s   kubelet            Successfully pulled image "nginx" in 1m5.956007089s
  Normal  Created    16s   kubelet            Created container mynginx
  Normal  Started    16s   kubelet            Started container mynginx

进入pod

[root@master ~]# kubectl exec -it mynginx -- /bin/bash
root@mynginx:/# ls
bin   dev           docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc             lib   media  opt  root  sbin  sys  usr

查看log 加上f表示实时查看

[root@master ~]# 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
2023/01/09 09:25:47 [notice] 1#1: using the "epoll" event method
2023/01/09 09:25:47 [notice] 1#1: nginx/1.21.5
2023/01/09 09:25:47 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/01/09 09:25:47 [notice] 1#1: OS: Linux 3.10.0-1127.el7.x86_64
2023/01/09 09:25:47 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/01/09 09:25:47 [notice] 1#1: start worker processes
2023/01/09 09:25:47 [notice] 1#1: start worker process 32
2023/01/09 09:25:47 [notice] 1#1: start worker process 33
192.168.219.64 - - [09/Jan/2023:09:33:45 +0000] "GET / HTTP/1.1" 200 

【2】命令式对象配置: 通过命令配置和配置文件去操作作kubernetes资源

1、写一个ymal文件

apiVersion: v1          # API版本号,注意:具有多个,不同的对象可能会使用不同API
kind: Pod               # 对象类型,pod
metadata:               # 元数据
  name: mynginx         # POD名称

spec:                   # specification of the resource content(资源内容的规范)
  containers:           # 容器列表
    - name: mynginx     # 容器名称
      image: nginx      # 容器镜像
                                    

2、然后运行yaml文件(提前创建好了命名空间string)

[root@master ~]# vi pod.yaml 
[root@master ~]# kubectl apply -f pod.yaml 
pod/mynginx created
[root@master ~]# kubectl get pod -n string
NAME      READY   STATUS             RESTARTS   AGE
mynginx   1/1     Running            0          27s

[root@master ~]# kubectl describe pod mynginx -n string
Name:         mynginx
Namespace:    string
Priority:     0
Node:         node02/172.31.0.2
Start Time:   Mon, 09 Jan 2023 17:10:22 +0800
Labels:       <none>
Annotations:  cni.projectcalico.org/containerID: 5a1eefb7be0f61fc7cceca2c8bfe67e72fe104db89a13b9f58921804278a2904
              cni.projectcalico.org/podIP: 192.168.140.74/32
              cni.projectcalico.org/podIPs: 192.168.140.74/32
Status:       Running
IP:           192.168.140.74
IPs:
  IP:  192.168.140.74
Containers:
  mynginx:
    Container ID:   docker://613b31a4f4736788af136f9187bf78d64ac0f6e0b3960a34087ed481ebed7847
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 09 Jan 2023 17:10:23 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-tv5f7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-tv5f7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-tv5f7
    Optional:    false
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  52s   default-scheduler  Successfully assigned string/mynginx to node02
  Normal  Pulling    51s   kubelet            Pulling image "nginx"
  Normal  Pulled     51s   kubelet            Successfully pulled image "nginx" in 429.471972ms
  Normal  Created    51s   kubelet            Created container mynginx
  Normal  Started    51s   kubelet            Started container mynginx
[root@master ~]# kubectl delete -f pod2.yaml 
pod "mynginx" deleted

3、用bashboard创建pod

4、一个pod里面运行多个容器

先创建一个yaml

apiVersion: v1          # API版本号,注意:具有多个,不同的对象可能会使用不同API
kind: Pod               # 对象类型,pod
metadata:               # 元数据
  name: mynginx         # POD名称
  namespace: string     # 所属的命名空间
spec:                   # specification of the resource content(资源内容的规范)
  containers:           # 容器列表
    - name: mynginx     # 容器名称
      image: nginx      # 容器镜像
 
    - name: mytomcat
      image: tomcat                                                

运行容器

[root@master ~]# kubectl apply -f pod.yaml 
pod/mynginx created
[root@master ~]# kubectl get pod -n string
NAME      READY   STATUS              RESTARTS   AGE
mynginx   0/2     ContainerCreating   0          23s
[root@master ~]# kubectl get pod -n string -o wide
NAME      READY   STATUS              RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
mynginx   0/2     ContainerCreating   0          33s   <none>   node01   <none>           <none>
[root@master ~]# kubectl get pod mynginx -n string -o wide
NAME      READY   STATUS    RESTARTS   AGE     IP                NODE     NOMINATED NODE   READINESS GATES
mynginx   2/2     Running   0          7m40s   192.168.196.139   node01   <none>           <none>

进入容器查看

[root@master ~]# kubectl exec -it mynginx -n string -- /bin/bash
Defaulting container name to mynginx.
Use 'kubectl describe pod/mynginx -n string' to see all of the containers in this pod.


root@mynginx:/# curl 127.0.0.1
<!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>
root@mynginx:/# curl 127.0.0.1:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>root@mynginx:/# 

!!注意在一个pod里不能有多个应用都用同一个端口

<think>嗯,用户问的是如何在Kubernetesk8s)中查看namespace。我需要先回忆一下相关的kubectl命令。首先,namespace是用来隔离集群资源的,所以查看现有的namespace应该是基础操作。 用户可能刚接触k8s,需要简单的命令。最直接的就是kubectl get namespaces,或者简写为kubectl get ns。这个命令会列出所有的namespace及其状态和时间。我得确认这个命令是否正确,会不会有其他参数需要说明? 然后,用户可能需要更详细的信息,比如特定的namespace。这时候可以用kubectl describe namespace <name>,这样可以看到具体的资源配额和限制。有没有例子呢?比如kubectl describe ns default,这样用户能更清楚如何使用。 另外,有时候用户可能想查看某个namespace下的资源,比如pods。这时候需要加上-n参数指定namespace,比如kubectl get pods -n <namespace>。或者设置当前上下文中的默认namespace,避免每次都要指定,用kubectl config set-context --current --namespace=<namespace>。这部分可能需要分步骤说明,确保用户明白如何操作。 还要考虑用户是否有权限问题,比如列出namespace时如果没有权限可能会报错,但可能不需要深入,除非用户特别问到。不过基本的命令还是先提供,权限问题可以提一下可能需要的权限。 有没有其他相关命令?比如用kubectl get ns -o wide或者自定义输出格式,比如JSON或YAML,用户如果有进一步需求可能会用到,但基础回答里可能不需要太深入,但可以简要提到这些选项。 还要注意格式,按照用户的要求,行内数学表达式用$...$,但这里可能用不到数学公式,主要是代码和命令。不过代码部分应该用反引号包裹,比如`kubectl get ns`。用户要求结构清晰,分步骤说明,所以可能需要分点列出,每个命令的用途和示例。 需要检查是否有常见的错误,比如拼写错误,比如namespace是否正确,或者ns作为缩写。同时,确认命令的正确性,比如kubectl get namespaces确实有效,并且是推荐的方式。 可能还需要提到查看特定namespace的详细信息,比如资源配额,用describe命令。以及如何查看所有namespace中的资源,比如加上--all-namespaces参数,比如kubectl get pods --all-namespaces。 总结一下,步骤应该是:1. 列出所有namespace;2. 查看特定namespace的详细信息;3. 查看某个namespace下的资源;4. 设置默认namespace;5. 其他相关命令和技巧。这样结构清晰,用户能一步步跟着操作。 还要注意用中文,避免术语错误,比如“命名空间”是正确的翻译,但有时候直接用namespace可能更常见,需要统一。可能中英文混合使用,但用户可能更习惯namespace这个术语,所以保持使用namespace,同时括号注明中文翻译。 最后,是否需要提醒用户确保kubectl配置正确,以及集群连接正常?可能作为注意事项提及,但用户的问题主要是查看namespace,可能不需要深入,除非常见问题。 现在组织这些信息,按照用户的结构要求,分步骤说明,每个步骤给出命令和示例,以及简要说明用途。这样用户能快速理解并应用。</think>在Kubernetesk8s)中查看Namespace(命名空间)是管理集群资源的常见操作。以下是逐步说明: --- ### 1. **列出所有Namespace** 使用 `kubectl get namespaces` 或简写 `kubectl get ns`: ```bash $ kubectl get ns ``` 输出示例: ``` NAME STATUS AGE default Active 10d kube-system Active 10d kube-public Active 10d kube-node-lease Active 10d ``` - **用途**:查看集群中所有Namespace的名称、状态和创建时间。 --- ### 2. **查看特定Namespace的详细信息** 使用 `kubectl describe namespace <namespace名称>`: ```bash $ kubectl describe ns default ``` 输出示例: ``` Name: default Labels: <none> Annotations: <none> Status: Active No resource quota. No LimitRange resource. ``` - **用途**:查看Namespace的详细信息,包括资源配额(Resource Quota)和限制范围(LimitRange)。 --- ### 3. **查看某个Namespace下的资源** 通过 `-n` 或 `--namespace` 参数指定Namespace: ```bash $ kubectl get pods -n kube-system ``` - **用途**:查看指定Namespace中的资源(如Pod、Service、Deployment等)。 --- ### 4. **设置当前上下文的默认Namespace** 避免每次操作都指定Namespace: ```bash $ kubectl config set-context --current --namespace=<目标Namespace> ``` 示例: ```bash $ kubectl config set-context --current --namespace=default ``` - **用途**:后续命令默认在指定的Namespace中执行。 --- ### 5. **其他常用命令** - **查看所有Namespace中的资源**: ```bash $ kubectl get pods --all-namespaces ``` - **以YAML/JSON格式输出**: ```bash $ kubectl get ns -o yaml # YAML格式 $ kubectl get ns -o json # JSON格式 ``` --- ### 注意事项 - 需要确保已正确配置 `kubectl` 并连接到集群。 - 如果无权限查看Namespace,需联系管理员授权(如RBAC权限)。 通过以上步骤,可以高效查看和管理Kubernetes中的Namespace
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值