Kubernetes

11、kubectl配置文件管理资源

Linux · 3月19日 · 2020年

、YAML是一种简洁非标记语

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

YAML配置模板介绍:

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

 

0 条回应