11、kubectl配置文件管理资源

11、kubectl配置文件管理资源

一、YAML是一种简洁非标记语言

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

二、YAML配置模板介绍:

11、kubectl配置文件管理资源

三、YAML配置创建NGINX实战

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---           表示YAML格式,一个文件的开始                            
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx

四、执行方式

[root@k8s-master2 deployment]# kubectl create -f nginx-deployment.yaml 
[root@k8s-master2 deployment]# kubectl get pods,svc,deployment -w
NAME                                 READY   STATUS    RESTARTS   AGE
nginx-deployment-574b87c764-2k62c    1/1     Running   0          3h27m
nginx-deployment-574b87c764-t4wb8    1/1     Running   0          3h27m
nginx-deployment-574b87c764-x48c4    1/1     Running   0          3h27m
tomcat-deployment-5f5584ddf5-5zrq6   1/1     Running   0          81m
tomcat-deployment-5f5584ddf5-lz9lz   1/1     Running   0          81m
tomcat-deployment-5f5584ddf5-tj77j   1/1     Running   0          81m

说明:-w 参数实时跟着状态

 

五、将运行POD节点镜像容器生成YAML配置模板

1、用run命令生成

[root@k8s-master2 deployment]# kubectl run nginx-web --image=nginx:latest --port=80 --replicas=3 --dry-run -o yaml >nginx-deloy.yaml
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.
[root@k8s-master2 deployment]# vim nginx-deloy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx-web
  name: nginx-web
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx-web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx-web
    spec:
      containers:
      - image: nginx:latest
        name: nginx-web
        ports:
        - containerPort: 80
        resources: {}
status: {}

2、用get命令导出

[root@k8s-master2 deployment]# kubectl get deployment/tomcat-deployment --export -o yaml >tomcat-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  name: tomcat-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - image: tomcat:8.0.27
        imagePullPolicy: IfNotPresent
        name: tomcat
        ports:
        - containerPort: 8080

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
百度已收录
Linux

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: