微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

k8s 部署一个应用到容器

这里以Nginx为例,主要是提供一种思路。

    需要用到基础镜像的时候,优先使用官方的,如果官方的不符合要求,则自己做一个,(不用使用centos镜像,推荐使用Alpine、busyBox,都是精简过的,达到同样的功能,占用空间少了几百兆)

 

 

1.将配置文件储存为configmap

apiVersion: v1
data:
  Nginx.conf: "user Nginx;                     \nworker_processes auto;             \npid        /var/run/Nginx.pid;\nworker_rlimit_nofile    65536;     \nevents {      \n      use epoll;                       \n      worker_connections 65535;    \n}\nhttp {\ncharset UTF-8; \nsendfile on;                \ntcp_nopush on;  \ntcp_nodelay on;\ntypes_hash_max_size 2048;\nkeepalive_timeout  65;\nclient_header_timeout 60s;\nclient_body_timeout 60s;\nsend_timeout 300s;\nserver_tokens off;               \nclient_max_body_size 8m;      \ninclude /etc/Nginx/mime.types;\ndefault_type application/octet-stream;\nlog_format  main   - []       ;                     \naccess_log /var/log/Nginx/Nginx-access.log  main;\nerror_log /var/log/Nginx/Nginx-error.log;\n\n        limit_conn_zone $binary_remote_addr zone=perip:10m;\n        limit_conn_zone $server_name zone=perserver:10m;\n        limit_conn perip 200;\nlimit_conn perserver 200;\nlimit_rate 3000k;\n        add_header X-Cache $upstream_cache_status;\n        add_header X-Content-Type-Options nosniff;\ngzip on;\ngzip_disable \"msie6\";\n        gzip_vary on;\n        gzip_proxied any;\n        gzip_comp_level 6;\n        gzip_buffers 16 8k;\n        gzip_http_version 1.1;\n        gzip_types image/png text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-PHP image/jpeg image/gif;   \ninclude /etc/Nginx/conf.d/*.conf;     \n}\n\n\n"
kind: ConfigMap
Metadata:
  name: Nginx-conf
  namespace: default

 

 

2.创建一个deployment。

注:mountPath的方式挂载会覆盖整个Nginx目录,挂载配置文件数据卷,通过subpath的方式指定挂载单个文件

apiVersion: apps/v1
kind: Deployment
Metadata:
  annotations: {}
  labels:
    app: Nginx-test
  name: Nginx-test
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: Nginx-test
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    Metadata:
      creationTimestamp: null
      labels:
        app: Nginx-test
    spec:
      affinity: {}
      containers:
      - env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: Nginx:latest
        imagePullPolicy: Always
        lifecycle: {}
        name: Nginx-test
        ports:
        - containerPort: 80
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 10m
            memory: 10Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
        - mountPath: /etc/Nginx/Nginx.conf
          name: Nginx-config
          readOnly: true
          subPath: Nginx.conf
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone
      - configMap:
          defaultMode: 420
          name: Nginx-conf
        name: Nginx-config

就是做了一件事,将之前保存到configmap的Nginx配置文件挂载到镜像里面覆盖了原有的配置文件

 

 

注解:

 

 

配置文件挂载方式:
      volumes:
        - mountPath: /etc/Nginx/Nginx.conf
          name: Nginx-config
          readOnly: true
          subPath: Nginx.conf  #一定要指定文件这样他只会覆盖这个文件,要不然覆盖整个目录Nginx就没办法启动了

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐