一、静态网页项目

1.前端项目源码下载

链接:https://pan.baidu.com/s/15jCVawpyJxa0xhCJ9SwTCQ
提取码:m4an

2.编写nginx.conf和Dockerfile,放在项目根目录下

(1). 创建nginx.conf 替换原镜像中的配置文件,并将对应的端口设置到9000(端口可自己设定)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;  #有字体文件,加载css样式不出错
    sendfile         on;
    tcp_nodelay       on;
    keepalive_timeout  30;
    server {
        listen 9000;
        server_name localhost;
        root /usr/share/nginx/dist;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        location  ~* \.(eot|ttf|ttc|otf|eot|woff|woff2|svg)$i{       
            try_files $uri $uri/ =404;
            index index.html index.htm;        #设置首页
            add_header Access-Control-Allow-Origin *;
        }
    }
}

(2). 创建构建镜像的文件, Dockerfile

FROM nginx
RUN mkdir /usr/share/nginx/dist
RUN rm -rf /etc/nginx/nginx.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./ /usr/share/nginx/dist
WORKDIR /usr/share/nginx/dist
EXPOSE 9000

3.编写yaml文件,运行命令,部署服务

Yaml内容如下:

apiVersion: v1
kind: ReplicationController
metadata:
 name: xiaomweb
spec:
 replicas: 1
 selector:
   app: xiaomweb
 template:
   metadata:
     labels:
       app: xiaomweb
   spec:
     containers:
     - name: xiaomweb
       image: xiaomweb:v1.0

---
apiVersion: v1
kind: Service
metadata:
 name: xiaomweb
spec:
 type: NodePort
 ports:
 - port: 9000
   nodePort: 30122 
 selector:
   app: xiaomweb

在这里插入图片描述

4.通过Dockerfile构建镜像

首先k8s上查看pod在那个节点上,运行该节点终端
切换到文件路径,运行命令:

docker image build –t xiaomweb:v1.0 .   

在这里插入图片描述
Ps:如果有私有仓库最好,把镜像上传到仓库,配置docker源,就不用每次本地生成了。

5.通过端口访问服务

已经通过k8s将容器的端口9000,映射到集群每一台主机端口30122

在这里插入图片描述
在浏览器输入集群任何一台主机名加端口号30122,可以访问了
在这里插入图片描述


二.javaweb项目

1.项目源码下载

链接:https://pan.baidu.com/s/15mS48L9yyaHdyW2eoF7n_Q
提取码:k7gk

2.k8s部署mysql

mysql.Yaml:

---
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-rc
  labels:
    name: mysql-rc
spec:
  replicas: 1
  selector:
    name: mysql-pod
  template:
    metadata:
      labels: 
        name: mysql-pod
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  labels: 
    name: mysql-svc
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 30306
  selector:
name: mysql-pod
kubectl apply –f mysql.Yaml
kubectl get pod 
kubectl get svc

创建表

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `pwd` varchar(255) NOT NULL,
  `sex` varchar(255) NOT NULL,
  `home` varchar(255) NOT NULL,
  `info` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('3', '123', '123', '123', '123', '123');
INSERT INTO `user` VALUES ('4', '123123', '123123', '男', '北京', '123123');

在这里插入图片描述

修改jdbc
在这里插入图片描述

3.打包javaweb镜像

将项目打包成war,与dockerfile放一起

Dockerfile:

# 版本信息
FROM tomcat

#打包项目并拷贝到tomcat webapps目录
WORKDIR /usr/local/tomcat/webapps
ADD ./javaweb.war  /usr/local/tomcat/webapps

#开启内部服务端口
EXPOSE 8080

#启动tomcat服务器
CMD ["/usr/local/tomcat/bin/catalina.sh","run"] && tail -f /usr/local/tomcat//logs/catalina.out
docker build –t xiaoweb:v1.0 .  

4.k8s部署javaweb

xiaoweb.Yaml:

---
apiVersion: v1
kind: ReplicationController
metadata:
  name: xiaoweb
  labels:
    name: xiaoweb
spec:
  replicas: 1
  selector:
    name: xiaoweb
  template:
    metadata:
      labels: 
        name: xiaoweb
    spec:
      containers:
      - name: xiaoweb
        image: xiaoweb:v1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
      
---
apiVersion: v1
kind: Service
metadata:
  name: xiaoweb
  labels: 
    name: xiaoweb
spec:
  type: NodePort
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
    name: http
    nodePort: 30030
  selector:
name: xiaoweb-pod
kubectl apply -f xiaoweb.yaml
kubectl get pod

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LPyqo0Rq-1574758557512)(https://note.youdao.com/yws/api/personal/file/WEBdb430fa4a60203dd3e228b7c1d32c715?method=getImage&version=17&cstk=Bm99i71g)]

kubectl get svc

在这里插入图片描述

5.浏览器访问测试

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐