假设项目已经开发完成,部署流程如下:
一、制作镜像:
1、创建nginx配置文件default.conf
server {
listen 80;
server_name localhost; # 修改为docker服务宿主机的ip
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
root /usr/share/nginx/html:这个目录和下面创建的Dockerfile中目录要保持一致
2、创建Dockerfile
FROM nginx:1.20.2
MAINTAINER test
RUN rm /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/
COPY dist/ /usr/share/nginx/html
- FROM nginx -- 打包容器的底层来刚才先拉取的nginx
- MAINTAINER beizhu:备注
- RUN rm /usr/local/nginx/conf/nginx.conf.default:删除目录下的nginx.conf.default 文件
- ADD nginx.conf.default /usr/local/nginx/conf/:将default.conf复制到/usr/local/nginx/conf/下,用本地的default.conf配置来替换nginx镜像里的默认配置
- COPY dist/ /usr/local/nginx/html/:将项目根目录下dist文件夹(构建之后才会生成)下的所有文件复制到镜像/usr/local/nginx/html/目录下上一步root地址
3、准备静态模板(项目)
4、打包docker镜像image并推送
docker build -f Dockerfile -t [dockerhub用户名]/web-pro:1.0 .
docker push [dockerhub用户名]/web-pro:1.0
如果是用的阿里云镜像,请参照阿里云镜像推送方式。
二、K8S部署:
1、编写 K8S yaml 文件
vi deploy-web.yaml
apiVersion: v1
kind: Namespace
metadata:
name: shop-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: manager-web
namespace: shop-web
spec:
replicas: 1
selector:
matchLabels:
app: manager-web
template:
metadata:
labels:
app: manager-web
spec:
containers:
- name: manager-web
image: registry.cn-hangzhou.aliyuncs.com/samve/k8s:2.0
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: manager-ui-service
namespace: shop-web
spec:
selector:
app: manager-web
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
nodePort: 30001
type: NodePort
2、部署服务
kubectl apply -f deploy-web.yaml
三、使用helm部署:
1、准备环境 k8s集群
[root@k8s-master-136 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master-136 Ready control-plane,master 296d v1.21.0
k8s-node-135 Ready <none> 296d v1.21.0
k8s-node-137 Ready <none> 296d v1.21.0
2、创建一个模板的chart包,删除原来的内容,自定义成我们自己需要的内容,后面我们自定义部署的yaml文件
[root@k8s-master-136 ~]# helm create nginx-chart
Creating nginx-chart
[root@k8s-master-136 nginx-chart]# cd ./nginx-chart/templates/
[root@k8s-master-136 templates]# rm -rf ./*
自定义部署的模板yaml文件:
vim templates/nginx-deploy-service.yaml
nginx-deploy-service.yaml
apiVersion: v1
kind: Namespace
metadata:
name: {{.Values.namespace}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.Values.deployment_name}}
namespace: {{.Values.namespace}}
spec:
replicas: {{.Values.replicas}}
selector:
matchLabels:
app: {{.Values.pod_label}}
template:
metadata:
labels:
app: {{.Values.pod_label}}
spec:
containers:
- name: {{.Values.pod_label}}
image: registry.cn-hangzhou.aliyuncs.com/samve/k8s:2.0
ports:
- name: http
containerPort: {{.Values.containerport}}
---
apiVersion: v1
kind: Service
metadata:
name: {{.Values.service_name}}
namespace: {{.Values.namespace}}
spec:
selector:
app: {{.Values.pod_label}}
ports:
- name: http
protocol: TCP
port: {{.Values.port}}
targetPort: {{.Values.targetport}}
nodePort: {{.Values.nodeport}}
type: NodePort
[root@k8s-master-136 nginx-chart]# vim values.yaml
values.yaml
deployment_name: manager-web
service_name: manager-web-service
namespace: shop-web
pod_label: manager-web
replicas: 2
port: 80
targetport: 80
containerport: 80
nodeport: 30002
3、通过chart包安装一个release实例
[root@k8s-master-136 ~]# helm install nginx-service ./nginx-chart
NAME: nginx-service
LAST DEPLOYED: Sun Nov 19 21:50:28 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master-136 nginx-chart]# vim values.yaml
[root@k8s-master-136 nginx-chart]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
nginx-service default 1 2023-11-19 21:50:28.744590789 +0800 CST deployed nginx-chart-0.1.0 1.16.0