k8s入门实战(Pod-Label-Deployment)

Pod

Pod 是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元。

k8s架构图:
在这里插入图片描述

k8s集群启动后,集群中各个组件也是以pod方式运行

[root@master ~]# kubectl get pod -n kube-system
NAME                             READY   STATUS    RESTARTS   AGE
coredns-6955765f44-7jflq         1/1     Running   1          17h
coredns-6955765f44-9h9s9         1/1     Running   1          17h
etcd-master                      1/1     Running   1          17h
kube-apiserver-master            1/1     Running   1          17h
kube-controller-manager-master   1/1     Running   1          17h
kube-proxy-gz5r2                 1/1     Running   1          17h
kube-proxy-hk8d5                 1/1     Running   1          17h
kube-proxy-vln9d                 1/1     Running   1          17h
kube-scheduler-master            1/1     Running   1          17h

创建并运行

k8s没有提供单独命令运行,是通过pod控制器运行

 #首先查看名称空间
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   17h
dev               Active   80m   #把pod运行在这个ns里
kube-flannel      Active   17h
kube-node-lease   Active   17h
kube-public       Active   17h
kube-system       Active   17h 

#命令格式 kubectl run (pod控制器名称)[参数]
#--image 指定pod镜像 --port 指定端口 --namespace 指定名称空间


[root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created 

#查询pod
 #这个pod刚刚部署到node1节点 启动了71s
[root@master ~]# kubectl get pod -n dev -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
nginx-64777cd554-p5nvj   1/1     Running   0          71s   10.244.1.5   node1   <none>           <none>
nginxpod                 1/1     Running   1          85m   10.244.2.8   node2   <none>           <none>

通过刚刚创建的pod IP和端口访问页面

[root@master ~]# curl 10.244.1.5:80
<!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

#查看pod
[root@master ~]# kubectl get pod -n dev
NAME                     READY   STATUS    RESTARTS   AGE
nginx-64777cd554-p5nvj   1/1     Running   0          13m
nginxpod                 1/1     Running   1          98m

#查看pod控制器
[root@master ~]# kubectl get deployment -n dev
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           15m

 #如果删除了pod控制器 pod就自动删除  如果仅仅删除了pod pod控制器 
#还会给你创建一个新的

[root@master ~]# kubectl delete  deployment nginx -n dev
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pod -n dev
NAME       READY   STATUS    RESTARTS   AGE
nginxpod   1/1     Running   1          102m

配置操作

创建pod-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.1
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
#运行pod
[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx created 


#查询
[root@master ~]# kubectl get pod -n dev
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          51s 


#删除pod
[root@master ~]# kubectl delete -f pod-nginx.yaml
pod "nginx" deleted
[root@master ~]# kubectl get pod -n dev
No resources found in dev namespace.

Label

标签(Labels) 是附加到 Kubernetes 对象(比如 Pod)上的键值对。 标签旨在用于指定对用户有意义且相关的对象的标识属性,但不直接对核心系统有语义含义。 标签可以用于组织和选择对象的子集。标签可以在创建时附加到对象,随后可以随时添加和修改。 每个对象都可以定义一组键/值标签。每个键对于给定对象必须是唯一的。

简单来说就是一种标识选择机制

  • 一个Label会以key/value键值对形式附加到各种资源对象上

  • 一个资源可以打上多个标签,一个标签可以给多个资源对象定义

  • Label通常资源对象定义时确定,也可以在资源运行时定义

"metadata": {
  "labels": {
    "key1" : "value1",
    "key2" : "value2"
  }
}

标签定义完后还有注意标签选择

Label Selector

标签不支持唯一性。通常,我们希望许多对象携带相同的标签。

通过标签选择算符,客户端/用户可以识别一组对象。标签选择算符是 Kubernetes 中的核心分组原语。

API 目前支持两种类型的选择算符:基于等值的基于集合的。 标签选择算符可以由逗号分隔的多个需求组成。 在多个需求的情况下,必须满足所有要求,因此逗号分隔符充当逻辑&&)运算符。

空标签选择算符或者未指定的选择算符的语义取决于上下文, 支持使用选择算符的 API 类别应该将算符的合法性和含义用文档记录下来。

基于等值基于不等值的需求允许按标签键和值进行过滤。 匹配对象必须满足所有指定的标签约束,尽管它们也可能具有其他标签。 可接受的运算符有 ===!= 三种。 前两个表示相等(并且是同义词),而后者表示不相等。例如:

environment = production
tier != frontend

前者选择所有资源,其键名等于 environment,值等于 production。 后者选择所有资源,其键名等于 tier,值不同于 frontend,所有资源都没有带有 tier 键的标签。 可以使用逗号运算符来过滤 production 环境中的非 frontend 层资源:environment=production,tier!=frontend

基于等值的标签要求的一种使用场景是 Pod 要指定节点选择标准。 例如,下面的示例 Pod 选择带有标签 “accelerator=nvidia-tesla-p100”。

apiVersion: v1
kind: Pod
metadata:
  name: cuda-test
spec:
  containers:
    - name: cuda-test
      image: "registry.k8s.io/cuda-vector-add:v0.1"
      resources:
        limits:
          nvidia.com/gpu: 1
  nodeSelector:
    accelerator: nvidia-tesla-p100

基于集合的标签需求允许你通过一组值来过滤键。 支持三种操作符:innotinexists(只可以用在键标识符上)。例如:

environment in (production, qa)
tier notin (frontend, backend)
partition
!partition
  • 第一个示例选择了所有键等于 environment 并且值等于 production 或者 qa 的资源。
  • 第二个示例选择了所有键等于 tier 并且值不等于 frontend 或者 backend 的资源,以及所有没有 tier 键标签的资源。
  • 第三个示例选择了所有包含了有 partition 标签的资源;没有校验它的值。
  • 第四个示例选择了所有没有 partition 标签的资源;没有校验它的值。

命令方式

#根据上文中的ymal文件,创建一个pod  
[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx created
[root@master ~]# kubectl get pod -n dev
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          21s
[root@master ~]# kubectl get pod -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE   LABELS
nginx   1/1     Running   0          49s   <none>


#给pod打标签

[root@master ~]# kubectl label pod nginx -n dev version=1.0
pod/nginx labeled
[root@master ~]# kubectl get pod -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE    LABELS
nginx   1/1     Running   0          2m7s   version=1.0


[root@master ~]# kubectl get pod -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE     LABELS
nginx   1/1     Running   0          2m56s   tier=back,version=1.0

#更新标签
[root@master ~]# kubectl label pod nginx -n dev version=2.0 --overwrite
pod/nginx labeled
[root@master ~]# kubectl get pod -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE     LABELS
nginx   1/1     Running   0          4m36s   tier=back,version=2.0 


#更改yaml文件,再新建一个pod
[root@master ~]# vim pod-nginx.yaml
[root@master ~]# cat pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.1
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP


[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx1 created 

#打标签

[root@master ~]# kubectl create -f pod-nginx.yaml
pod/nginx1 created


[root@master ~]# kubectl get pods -n dev --show-labels
NAME     READY   STATUS    RESTARTS   AGE     LABELS
nginx    1/1     Running   0          7m55s   tier=back,version=2.0
nginx1   1/1     Running   0          105s    version=1.0

#筛选
[root@master ~]# kubectl get pods -l "version=2.0" -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE     LABELS
nginx   1/1     Running   0          9m55s   tier=back,version=2.0
[root@master ~]# kubectl get pods -l "version!=2.0" -n dev --show-labels
NAME     READY   STATUS    RESTARTS   AGE     LABELS
nginx1   1/1     Running   0          3m59s   version=1.0 

#删除标签

[root@master ~]# kubectl label pod nginx -n dev tier-
pod/nginx labeled
[root@master ~]# kubectl get pods -l "version=2.0" -n dev --show-labels
NAME    READY   STATUS    RESTARTS   AGE   LABELS
nginx   1/1     Running   0          13m   version=2.0

配置文件添加label

更新yaml文件内容

[root@master ~]# vim pod-nginx.yaml
[root@master ~]# cat pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
  labels:
    version: "3.0"
    env: "test"
spec:
  containers:
  - image: nginx:1.17.1
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
[root@master ~]# kubectl apply -f pod-nginx.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
pod/nginx configured
[root@master ~]# kubectl get pods  -n dev --show-labels
NAME     READY   STATUS    RESTARTS   AGE   LABELS
nginx    1/1     Running   0          58m   env=test,version=3.0
nginx1   1/1     Running   0          52m   version=1.0

Deployment

Pod控制器,用于Pod管理,保障Pod资源始终保存可预期状态

命令操作

# 命令格式: kubectl run deployment名称  [参数] 
# --image  指定pod的镜像
# --port   指定端口
# --replicas  指定创建pod数量
# --namespace  指定na
mespace

[root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --replicas=3 --namespace=dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created 


[root@master ~]# kubectl get deployment,pods -n dev
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           6s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-64777cd554-2txpz   1/1     Running   0          6s
pod/nginx-64777cd554-5j686   1/1     Running   0          6s
pod/nginx-64777cd554-z5psb   1/1     Running   0          6s  

#查看控制器信息

[root@master ~]# kubectl describe deploy nginx -n dev
Name:                   nginx
Namespace:              dev
CreationTimestamp:      Tue, 23 May 2023 02:59:31 -0400
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:1.17.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-64777cd554 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  5m10s  deployment-controller  Scaled up replica set nginx-64777cd554 to 3
#删除
[root@master ~]# kubectl delete  deploy nginx -n dev
deployment.apps "nginx" deleted

配置操作

创建一个yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx:1.17.1
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP          
[root@master ~]# vim deploy-nginx.yaml
[root@master ~]# cat deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx:1.17.1
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
[root@master ~]# kubectl create -f deploy-nginx.yaml
deployment.apps/nginx created
[root@master ~]# kubectl get deployment,pods -n dev
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           24s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-64777cd554-69c7c   1/1     Running   0          24s
pod/nginx-64777cd554-g4cpm   1/1     Running   0          24s
pod/nginx-64777cd554-zjzsx   1/1     Running   0          24s
  
#删除

[root@master ~]# kubectl delete -f deploy-nginx.yaml
deployment.apps "nginx" deleted
[root@master ~]# kubectl get deployment,pods -n dev #删除需要时间 正在删除中
NAME                         READY   STATUS        RESTARTS   AGE
pod/nginx-64777cd554-g4cpm   0/1     Terminating   0          78s
pod/nginx-64777cd554-zjzsx   0/1     Terminating   0          78s
[root@master ~]# kubectl get deployment,pods -n dev
NAME                         READY   STATUS        RESTARTS   AGE
pod/nginx-64777cd554-g4cpm   0/1     Terminating   0          86s
[root@master ~]# kubectl get deployment,pods -n dev
No resources found in dev namespace.


Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐