k8s服务编排-YAML

2021-11-14  本文已影响0人  小李飞刀_lql

YAML语法格式

001 缩进表示层级关系 
002 不支持制表符“tab”缩进,使用空格缩进
003 通常开头缩进 2 个空格
004 字符后缩进 1 个空格,如冒号、逗号、-等
005 “---” 表示YAML格式,一个文件的开始
006 “#”注释

资源对象Deployment

相关定义

------------------------------------控制器定义------------------------------------------
apiVersion:API版本
kind:资源类型
metadata:资源元数据
spec:资源规格
replicas:副本数量
selector:标签选择器
-------------------------------------被控制对象----------------------------------------
template:Pod模板
metadata:Pod元数据
spec:Pod规格
containers:容器配置

创建Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx2
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web2
  template:
    metadata:
      labels:
        app: web2
    spec:
      containers:
      - name: web
        image: nginx:1.17
      
-------------------------------------------------------------------------
001 等同于:kubectl create deployment web --image=lizhenliang/java-demo -n default 
002 控制器根据标签label去关联资源
003 标签必须唯一,否则可能为别的pod做嫁妆

资源对象Service

相关定义

ports:端口
selector:标签选择器
type:Service类型

创建Service

apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web2
  type: NodePort

---------------------------------------------------------------------------
001:
kubectl expose deployment web --port=80 --target-port=8080 --type=NodePort -n default
002 app的名称要与deployment指定的标签是一样的

资源部署

[root@k8smaster yaml]# kubectl apply -f deployment.yaml 

[root@k8smaster yaml]# kubectl get pod,deployment
NAME                                 READY   STATUS    RESTARTS   AGE

pod/nginx2-5f67b479c5-5pgz2          1/1     Running   0          3m49s
pod/nginx2-5f67b479c5-gw5sm          1/1     Running   0          3m49s
pod/nginx2-5f67b479c5-ls5wp          1/1     Running   0          3m49s


NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx2           3/3     3            3           3m49s


[root@k8smaster yaml]# kubectl apply -f  service.yaml 

[root@k8smaster yaml]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
web          NodePort    10.100.231.77    <none>        80:31588/TCP   39h

#访问
http://192.168.153.21:31588/

资源卸载

[root@k8smaster yaml]# kubectl delete -f service.yaml 
service "web" deleted

[root@k8smaster yaml]# kubectl delete -f deployment.yaml 
deployment.apps "nginx2" deleted

导出资源的yaml

[root@k8smaster yaml]# kubectl get deployment web -o json
[root@k8smaster yaml]# kubectl get deployment web -o json > json

[root@k8smaster yaml]# kubectl get deployment web -o yaml
[root@k8smaster yaml]# kubectl get deployment web -o yaml > yaml
上一篇下一篇

猜你喜欢

热点阅读