快速撑握编写Kubernets YAML的技巧
  yUuqgAt7EwFk 2023年11月02日 76 0

一、kubernetes v1.18.0+

1、关键参数:kubernetes v1.18.0+版本,kubectl --dry-run参数变更为“none”、“server”或“client”。如果是“client”策略,则结果输出yaml,不向apiserver发送。如果是“server”策略,则结果输出yaml,也向apiserver提交请求而不持久化资源(不创建资源对象)。如果是“none”,则结果输出yaml,也向apiserver提交请求持久化资源(会创建资源对象)。

2、快速获取已校验通过POD部署yaml

[root@master01 ~]# kubectl run nginx --image=nginx:1.22.0 --port=80 -n default --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
  namespace: default
spec:
  containers:
  - image: nginx:1.22.0
    name: nginx
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

3、快速获取已校验通过deployment部署yaml

[root@master01 ~]# kubectl create deployment nginx --image=nginx:1.22.0 --port=80 -r 1 -n default --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
status: {}

4、快速获取已校验通过service部署yaml

# ClusterIP类型
[root@master01 ~]# kubectl create service clusterip myweb --clusterip="None"  --tcp=8080:8080 -n default --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: myweb
  name: myweb
  namespace: default
spec:
  clusterIP: None
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: myweb
  type: ClusterIP
status:
  loadBalancer: {}
  
# NodePort类型
[root@master01 ~]# kubectl create service nodeport nginx --node-port=30080 --tcp=80:80 -n default --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  ports:
  - name: 80-80
    nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
status:
  loadBalancer: {}

5、快速获取已校验通过configmap部署yaml

# 当前目录需存放default.conf文件
[root@master01 ~]#  kubectl create configmap nginx-cm --from-file=default.conf -n default --dry-run=client -o yaml
apiVersion: v1
data:
  default.conf: |+
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location /nginx_status {
                stub_status on;
                #access_log off;
                #allow 127.0.0.1;
                #deny all;
         }

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

kind: ConfigMap
metadata:
  creationTimestamp: null
  name: nginx-cm
  namespace: default

6、获取已校验通过ingress部署yaml:带--annotation参数

[root@master01 ~]# kubectl create ingress otp-ingress --annotation="kubernetes.io/ingress.class=nginx"  --annotation="nginx.ingress.kubernetes.io/rewrite-target=/" --annotation="nginx.ingress.kubernetes.io/use-regex=true" --annotation="nginx.ingress.kubernetes.io/server-alias=otp.learn.org" --rule="otp.learn.org/*=learn-otp:8086,tls=otp-cert" -n default --dry-run=client -o yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/server-alias: otp.learn.org
    nginx.ingress.kubernetes.io/use-regex: "true"
  creationTimestamp: null
  name: otp-ingress
  namespace: default
spec:
  rules:
  - host: otp.learn.org
    http:
      paths:
      - backend:
          service:
            name: learn-otp
            port:
              number: 8086
        path: /
        pathType: Prefix
  tls:
  - hosts:
    - otp.learn.org
    secretName: otp-cert
status:
  loadBalancer: {}

7、获取已校验通过ingress部署yaml:带--class和--default-backend参数

[root@master01 ~]# kubectl create ingress otp-ingress --rule="otp.learn.org/*=learn-otp:8086,tls=my-cert" --class='nginx' --default-backend='learn-otp:8086' -n default --dry-run=client -o yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  creationTimestamp: null
  name: otp-ingress
  namespace: default
spec:
  defaultBackend:
    service:
      name: learn-otp
      port:
        number: 8086
  ingressClassName: nginx
  rules:
  - host: otp.learn.org
    http:
      paths:
      - backend:
          service:
            name: learn-otp
            port:
              number: 8086
        path: /
        pathType: Prefix
  tls:
  - hosts:
    - otp.learn.org
    secretName: my-cert
status:
  loadBalancer: {}

8、根据编写需求,可查询containers对象有哪些可用的字段

kubectl explain deployment.spec.template.spec.containers

9、根据编写需求,可查询deployment.spec对象有哪些可用的字段

kubectl explain deployment.spec

二、低于kubernetes v1.18.0

1、关键参数:kubectl --dry-run参数默认为false,则结果输出yaml,会向apiserver发送请求持久化资源(会创建资源对象)。如果配置为true,则结果输出yaml,不向apiserver发送请求。

2、kubectl --dry-run参数配置为true,获取部署yaml命令的其他参数与第一章一致。

三、更多k8s学习资料

  1、kubernetes原理精讲【基础原理+实践篇】

  2、kubernetes原理精讲【自签证书原理+实践篇】

  3、kubernetes原理精讲【监控体系原理+实践篇】



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

yUuqgAt7EwFk