k8s 部署一個應用到容器
阿新 • • 發佈:2021-12-24
這裡以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就沒辦法啟動了