K8s前端项目试水

/ k8sDocker / 没有评论 / 1290浏览

部署一个前端项目

Dockerfile

FROM nginx

COPY front.conf /etc/nginx/conf.d/default.conf
COPY  --chown=nginx:nginx  ./public/*  /www/front/

EXPOSE 80

nginx子配置文件

root@paa2:/opt/slave/workspace/slave_node# cat front.conf
server {
        listen      80;
        server_name localhost;
            #root   /tmp/build/;
           root /www/front;
            index index.html;
#auth_basic "Please input password"; #这里是验证时的提示信息
#  auth_basic_user_file /usr/local/src/nginx/passwd;
                 location / {
                 access_log /var/log/nginx_access.log ;
        }
}

push到镜像仓库

这里的仓库需要是公开的仓库

# docker tag 0cacac4e22ed hub.lijinghua.cn/test/k8s:front
# docker push hub.lijinghua.cn/test/k8s:front

开干

创建一个service

[root@k8s-master test]# kubectl create -f nginx-rc.yaml
replicationcontroller/nginx-controller created
[root@k8s-master test]# kubectl create -f nginx-service.yaml
service/nginx-service-clusterip created

获取集群ip

[root@k8s-master test]# kubectl get services
NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes                ClusterIP   10.96.0.1       <none>        443/TCP    3d5h
nginx-service-clusterip   ClusterIP   10.103.50.116   <none>        8001/TCP   10s

检测

[root@k8s-master test]# curl 10.103.50.116:8001
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="shortcut icon" href="../src/assets/icon-hires.png">
  <title>喵喵喵系统</title>
  <link rel="stylesheet" href="index.css" />
</head>

▽
<body>

  <div id="root"></div>
  <script src="index.js"></script>

</body>
</html>

配置文件

[root@k8s-master test]# cat nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: hub.lijinghua.cn/test/k8s:front
          ports:
            - containerPort: 80
[root@k8s-master test]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-clusterip
spec:
  ports:
    - port: 8001
      targetPort: 80
      protocol: TCP
  selector:
    name: nginx
  type: ClusterIP