2021-10-21

要把一个h5的文件夹做成个镜像然后部署
  1. 首先整个空文件夹,把h5文件放进去,写个dockerfile

    mkdir test-h5
    cd test-h5/
    vim Dockerfile
      FROM nginx:1.18.0-alpine
      RUN mkdir -p /mnt/nginx/html
      COPY ./h5 /mnt/nginx/html/h5              #./h5是要copy的文件夹  /mnt/nginx/html/h5复制文件夹到文件夹
      ENV TZ=Asia/Shanghai
      CMD ["nginx","-g","daemon off;"]          #nginx前台运行,CMD后面有空格!!!
      
      #具体还有啥命令自己百度
     在dockerfile所在的文件夹下,生成镜像
      docker build -t test-h5:v1 .
    
  2. 开始写deployment.yaml,service.yaml

    (参考:https://blog.csdn.net/dy1316434466/article/details/105440172)

    vim test-h5-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: test-h5
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: test-h5
          group: default-app
      template:
        metadata:
          labels:
            app: test-h5
            group: default-app
        spec:
          containers:
          - name: web   
            image: test-h5:v1  #镜像
            imagePullPolicy: Always 
            resources:
              limits:
                cpu: 300m
                memory: 100Mi
              requests:
                cpu: 100m
                memory: 50Mi
            workingDir: /mnt/nginx/html
            ports:
            - name: http-port
              containerPort: 80
            livenessProbe:
              tcpSocket:
                port: http-port
              initialDelaySeconds: 5
              periodSeconds: 30
              failureThreshold: 5
            readinessProbe:
              tcpSocket:
                port: http-port
              initialDelaySeconds: 2
              periodSeconds: 5
              successThreshold: 2
              failureThreshold: 5
            volumeMounts:
            - name: test-h5
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
              readOnly: false
            - name: test-h5
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: test-h5.conf
              readOnly: false
            - name: log-path
              mountPath: /var/log/nginx
              readOnly: false
          hostname: test-h5
          imagePullSecrets:
          - name: [secret-name]   #自己的secret-name
          restartPolicy: Always
          volumes:
          - name: test-h5
            configMap:
              name: test-h5
              items:
              - key: nginx.conf
                path: nginx.conf
              - key: test-h5.conf
                path: test-h5.conf
          - name: log-path
            hostPath:
              path: /home/logs/test-h5
              type: DirectoryOrCreate
    
    
    vim test-h5-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: test-h5
      namespace: default
    spec:
      type: NodePort
      selector:
        app: test-h5
      ports:
      - port: 80
        nodePort: 30099
        name: http-port
        targetPort: http-port
    
    
  3. 部署

     kubectl create -f ./test-h5-deployment.yaml -f ./test-h5-service.yaml
    
  4. 这个时候通过kubectl get pod 发现是在等待容器的创建

    docker images 发现镜像是存在的,那么就考虑deployment.yaml了。镜像是FROM nginx:1.18.0-alpine,需要替换nginx.conf

    vim test-h5-configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: test-h5
      namespace: default
    data:
      nginx.conf: |    
        user  root;
        worker_processes  1;
        
        error_log  /var/log/nginx/error.log warn;
        pid        /var/run/nginx.pid;    
        
        events {
            worker_connections  5000;
        }
        
        http {
            include       /etc/nginx/mime.types;
            default_type  application/octet-stream;
            
            log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                              '$status $body_bytes_sent "$http_referer" '
                              '"$http_user_agent" "$http_x_forwarded_for"';
            
            access_log  /var/log/nginx/access.log  main;
            
            sendfile        on;
            #tcp_nopush     on;
            
            keepalive_timeout  65;
            
            gzip  on;
            
            include /etc/nginx/conf.d/*.conf;
            
         }  
      test-h5.conf: |
        server {
            listen     80;
            server_name  _;
    
            location / {
                try_files $uri $uri/ /;
                root        /mnt/nginx/html/h5;
                index index.html;
                add_header Last-Modified $date_gmt;
                add_header Cache-Control 'no-store, no-cache,must-revalidate, proxy-revalidate, max-age=0';
                if_modified_since off;
                expires off;
                etag off;                
            }
            location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js)$ {
                root        /mnt/nginx/html/h5;
                access_log  off;
                expires 1d; 
            }
            error_page  404              /404.html;
                    location = /404-light.html {
            }
        }    
    
    
  5. apply一下

    kubectl apply -f ./test-h5-configmap.yaml
    
  6. 这个时候发现镜像拉取失败,但是docker images中有,也不知道为啥,那就把它推远程吧
    (似乎找到原因了:imagePullSecrets: restartPolicy: Always。Always:是拉取远程仓库的镜像,如果远程仓库不存在,pod会报错;IfNotPresent:优先使用本地镜像,本地不存在回去远程仓库拉;Never:只使用本地镜像)

    docker images     #查看生成的镜像所对应的ImageId
    docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/***/test-h5:v1
    docker push registry.cn-hangzhou.aliyuncs.com/***/test-h5:v1
    #(要记得把deployment中的镜像名称改成这个)
    

关于namespace的问题,如果在deployment中配置了namespace那么在service和configmap都需要,这个决定这个服务跑在哪个命名空间下。

关于h5文件更新问题:这个镜像是根据文件夹来的,文件夹每改一下,镜像就要更改一下。docker build -t test-h5:v1 .这条命令生成test-h5镜像能够覆盖原有的,再进行推送,推送的镜像会覆盖原有的镜像。然后把部署的服务redeploy一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值