K8S配置vue

1、编写Dockerfile

1)、Dockerfile 文件

FROM nginx:1.22.1
COPY dist /etc/nginx/vueTemplate
EXPOSE 30098
CMD ["nginx", "-g", "daemon off;"]

2)、制作镜像

docker build -t vue-template:latest .
docker tag vue-template:latest 172.171.2.148:5000/vue-template:latest
docker push 172.171.2.148:5000/vue-template:latest

2、配置configmap

1)、nginx.conf文件


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       30098;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /etc/nginx/vueTemplate;
	    try_files $uri $uri/ @router;
            index  index.html index.htm;
        }

	location @router {
      	    rewrite ^.*$ /index.html last;
   	}

	location /api/{

	    proxy_pass http://gateway-demo.dev:8081/api/;
	}

        #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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

2)、应用nginx.conf 文件 到 configmap

kubectl create configmap nginx-vue-template --from-file=./nginx.conf -n dev

3、配置yaml文件


apiVersion: v1
kind: Service
metadata:
  name: vue-template
  namespace: dev
  labels:
    app: vue-template
spec:
  type: NodePort
  ports:
    - port: 5174
      targetPort: 30098 #service对外开放端口
      nodePort: 30098
  selector:
    app: vue-template
---
apiVersion: apps/v1
kind: Deployment #对象类型
metadata:
  name: vue-template #名称
  namespace: dev
  labels:
    app: vue-template #标注
spec:
  replicas: 1 #运行容器的副本数,修改这里可以快速修改分布式节点数量
  selector:
    matchLabels:
      app: vue-template
  template:
    metadata:
      labels:
        app: vue-template
    spec:
      containers: #docker容器的配置
        - name: vue-template
          image: 172.171.2.148:5000/vue-template:latest  # pull镜像的地址 ip:prot/dir/images:tag
          imagePullPolicy: Always   #pull镜像时机,
          ports:
            - containerPort: 5174  #容器对外开放端口,需与springboot配置文件一致
          volumeMounts:
          - name: vueconf
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
            readOnly: true
      volumes:
      - name: vueconf
        configMap:
          name: nginx-vue-template
          items:
            - key: nginx.conf
              path: nginx.conf
posted @ 2023-03-16 11:24  eqwal  阅读(192)  评论(0编辑  收藏  举报