声明式对象配置--yaml文件
程度
- 有模板
- 能看懂
- 能修改
- 能排错即可
yaml规范
- 缩进代表上下级关系
- 缩进时不允许使用Tab键,只允许使用空格,通常缩进2个空格
:
键值对,后面必须有空格-
列表,后面必须有空格[ ]
数组#
注释|
多行文本块---
表示文档的开始,多用于分割多个资源对象
使用yaml定义一个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
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.