k8s(10)声明式对象配置--yaml文件

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: k8s(10)声明式对象配置--yaml文件

声明式对象配置--yaml文件

程度

  • 有模板
  • 能看懂
  • 能修改
  • 能排错即可

yaml规范

  • 缩进代表上下级关系
  • 缩进时不允许使用Tab键,只允许使用空格,通常缩进2个空格
  • : 键值对,后面必须有空格
  • -列表,后面必须有空格
  • [ ]数组
  • #注释
  • | 多行文本块
  • ---表示文档的开始,多用于分割多个资源对象

使用yaml定义一个Pod

Pod配置模版

vim my-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.22
    ports:
    - containerPort: 80

如果develop命名空间被删除了,记得换回来

[root@k8s home]# kubectl apply -f my-pod.yaml
Error from server (NotFound): error when creating "my-pod.yaml": namespaces "develop" not found
[root@k8s home]# kubectl config set-context $(kubectl config current-context) --namespace=default
Context "default" modified.

创建pod

[root@k8s home]# kubectl apply -f my-pod.yaml
pod/my-nginx created

删除pod

[root@k8s home]# kubectl delete -f my-pod.yaml 
pod "my-nginx" deleted

标签

标签(Labels) 是附加到对象(比如 Pod)上的键值对,用于补充对象的描述信息。

标签使用户能够以松散的方式管理对象映射,而无需客户端存储这些映射。

由于一个集群中可能管理成千上万个容器,我们可以使用标签高效的进行选择和操作容器集合。


  • 键的格式:
  • 前缀(可选)/名称(必须)
  • 有效名称和值:
  • 必须为 63 个字符或更少(可以为空)
  • 如果不为空,必须以字母数字字符([a-z0-9A-Z])开头和结尾
  • 包含破折号-、下划线_、点.和字母或数字

label配置模版

在yaml配置模板前提上加了一个labels属性

apiVersion: v1
kind: Pod
metadata:
  name: label-demo
  labels: #定义Pod标签
    environment: test
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.22
    ports:
    - containerPort: 80
[root@k8s home]# vim label-pod.yaml
[root@k8s home]# kubectl apply -f label-pod.yaml 
pod/label-demo created
[root@k8s home]# kubectl get pod
NAME                            READY   STATUS    RESTARTS      AGE
nginx-deploy-855866bb46-5rqh4   1/1     Running   1 (38m ago)   6h23m
nginx-deploy-855866bb46-8fgzg   1/1     Running   1 (38m ago)   6h23m
nginx-deploy-855866bb46-87scw   1/1     Running   2 (35m ago)   6h23m
label-demo                      1/1     Running   0             4s
[root@k8s home]# kubectl get pod --show-labels
NAME                            READY   STATUS    RESTARTS      AGE     LABELS
nginx-deploy-855866bb46-5rqh4   1/1     Running   1 (38m ago)   6h24m   app=nginx-deploy,pod-template-hash=855866bb46
nginx-deploy-855866bb46-8fgzg   1/1     Running   1 (38m ago)   6h24m   app=nginx-deploy,pod-template-hash=855866bb46
nginx-deploy-855866bb46-87scw   1/1     Running   2 (36m ago)   6h24m   app=nginx-deploy,pod-template-hash=855866bb46
label-demo                      1/1     Running   0             24s     app=nginx,environment=test
[root@k8s home]# kubectl get pod -l app=nginx
NAME         READY   STATUS    RESTARTS   AGE
label-demo   1/1     Running   0          41s
[root@k8s home]# kubectl get pod -l app=nginx
NAME         READY   STATUS    RESTARTS   AGE
label-demo   1/1     Running   0          50s
[root@k8s home]# kubectl get pod -l app=nginx-deploy
NAME                            READY   STATUS    RESTARTS      AGE
nginx-deploy-855866bb46-5rqh4   1/1     Running   1 (39m ago)   6h24m
nginx-deploy-855866bb46-8fgzg   1/1     Running   1 (38m ago)   6h24m
nginx-deploy-855866bb46-87scw   1/1     Running   2 (36m ago)   6h24m
[root@k8s home]# kubectl get pod -l app=nginx,
Error from server (BadRequest): Unable to find "/v1, Resource=pods" that match label selector "app=nginx,", field selector "": found '', expected: identifier after ','
[root@k8s home]# kubectl get pod -l app=nginx
NAME         READY   STATUS    RESTARTS   AGE
label-demo   1/1     Running   0          70s
[root@k8s home]# kubectl get pod -l app=nginx,environment=text
No resources found in default namespace.
[root@k8s home]# kubectl get pod -l app=nginx,environment=test
NAME         READY   STATUS    RESTARTS   AGE
label-demo   1/1     Running   0          79s

选择器--创建service

标签选择器 可以识别一组对象。标签不支持唯一性。

标签选择器最常见的用法是为Service选择一组Pod作为后端。

需要有pod,这里用上一个知识点的label创建的pod

Service配置模版

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector: #与Pod的标签一致
    environment: test
    app: nginx
  ports:
      # 默认情况下,为了方便起见,`targetPort` 被设置为与 `port` 字段相同的值。
    - port: 80
      targetPort: 80
      # 可选字段
      # 默认情况下,为了方便起见,Kubernetes 控制平面会从某个范围内分配一个端口号(默认:30000-32767)
      nodePort: 30007
[root@k8s home]# vim my-service.yaml
[root@k8s home]# kubectl apply -f my-service.yaml
service/my-service created
[root@k8s home]# kubectl get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.43.0.1       <none>        443/TCP          45h
nginx-service   ClusterIP   10.43.65.176    <none>        8080/TCP         6h27m
nginx-outside   NodePort    10.43.127.65    <none>        8081:32109/TCP   5h47m
my-service      NodePort    10.43.154.163   <none>        80:30007/TCP     9m12s
[root@k8s home]# kubectl describe svc my-service
Name:                     my-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=nginx,environment=test
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.154.163
IPs:                      10.43.154.163
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30007/TCP
Endpoints:                10.42.1.33:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
[root@k8s home]# kubectl get pod -l app=nginx -owide
NAME         READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
label-demo   1/1     Running   0          2m22s   10.42.1.33   k8s3   <none>           <none>

如果没有对应的pod,

[root@k8s home]# kubectl get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.43.0.1       <none>        443/TCP          45h
nginx-service   ClusterIP   10.43.65.176    <none>        8080/TCP         6h18m
nginx-outside   NodePort    10.43.127.65    <none>        8081:32109/TCP   5h38m
my-service      NodePort    10.43.154.163   <none>        80:30007/TCP     4s
[root@k8s home]# kubectl describe svc my-service
Name:                     my-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=nginx,environment=test
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.154.163
IPs:                      10.43.154.163
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30007/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
[root@k8s home]# kubectl get pod -l app=nginx -owide
No resources found in default namespace.
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
14天前
|
Kubernetes Go 网络安全
Kubernetes 中使用consul-template渲染配置
Kubernetes 中使用consul-template渲染配置
25 1
Kubernetes 中使用consul-template渲染配置
|
8天前
|
存储 Kubernetes Linux
Kubernetes 的配置资源 ConfigMap(01部分)
Kubernetes 的配置资源 ConfigMap(01部分)
|
12天前
|
存储 Kubernetes 数据格式
精通Kubernetes:利用YAML轻松管理资源
精通Kubernetes:利用YAML轻松管理资源
|
12天前
|
存储 Kubernetes 网络安全
[k8s]使用nfs挂载pod的应用日志文件
[k8s]使用nfs挂载pod的应用日志文件
|
20小时前
|
Kubernetes 容器 Perl
在k8S中,如何实现Pod中容器的文件和宿主机之间相互拷贝?
在k8S中,如何实现Pod中容器的文件和宿主机之间相互拷贝?
|
1天前
|
Kubernetes 应用服务中间件 nginx
在K8S中,deploy的yaml如何编写?
在K8S中,deploy的yaml如何编写?
|
4天前
|
Dart iOS开发 C++
Dart ffi 使用问题之在pubspec.yaml文件中,对plugin_ffi_sample插件的依赖如何配置
Dart ffi 使用问题之在pubspec.yaml文件中,对plugin_ffi_sample插件的依赖如何配置
|
6天前
|
Kubernetes API 容器
Kubernetes(K8S) yaml 介绍
Kubernetes(K8S) yaml 介绍
5 0
|
6天前
|
Kubernetes 容器
Kubernetes(K8S) 配置静态资源服务
Kubernetes(K8S) 配置静态资源服务
16 0
|
6天前
|
XML 监控 数据格式
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
ROS 2 - Python、XML 和 YAML 编写 Launch 文件
17 0

推荐镜像

更多