k8s 部署nginx前端,部署java后端

1.构建docker镜像,k8s拉取镜像运行


docker自己安装 

[root@master1 ~]# docker pull nginx:1.24.0
[root@master1 ~]# mkdir k8s-nginx
[root@master1 ~]# cd k8s-nginx
[root@master1 k8s-nginx]# vim nginx.conf
    server_tokens off;
    server {
        listen       8010; #web访问端口
        server_name  localhost;
            
        keepalive_timeout  65;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
                
        location / {
            root /etc/nginx/dist;   #web代码路径
            index  index.html index.htm;
        }

        #后端代码接口配置
        #location /api {
        #    proxy_pass http://127.0.0.1:8001;
        #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
[root@master1 k8s-nginx]# mkdir dist #这个dist是前端包目录,我这里只做个测试
[root@master1 k8s-nginx]# cd dist
[root@master1 dist]# vim index.html
this is a test!

写Dockerfile:

[root@master1 k8s-nginx]# vim Dockerfile
FROM nginx:1.24.0

COPY nginx.conf /etc/nginx/conf.d/web.conf

COPY dist /etc/nginx/dist

构建镜像:

[root@master1 k8s-nginx]# docker build -t nginx:v1 .

如果上传到阿里云仓库可以直接打成阿里云仓库格式tag镜像:

docker build -t registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2 -f Dockerfile 

#后面的"-f"是指定Docker文件,如果不指定需要写成"."
docker build -t registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2 .

Sending build context to Docker daemon  5.12 kB
Step 1/3 : FROM nginx:1.24.0
 ---> 6b753f58c54e
Step 2/3 : COPY nginx.conf /etc/nginx/conf.d/web.conf
 ---> Using cache
 ---> c67c98f8e802
Step 3/3 : COPY dist /etc/nginx/dist
 ---> 546db553f62a
Removing intermediate container d9a8e88cb4da
Successfully built 546db553f62a
将镜像上传到镜像仓库,我这里是上传到阿里云的镜像仓库
仓库地址:
https://cr.console.aliyun.com/cn-zhangjiakou/instance/credentials

登录镜像仓库:
docker login --username=asula registry.cn-zhangjiakou.aliyuncs.com

推送镜像:
docker tag nginx:v1 registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v1
docker push registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v1

k8s拉取私有仓库需要登录,有时候不可能为每个k8s节点登录
我们就需要为创建k8s集群的secret,设置秘钥配置imagePullSecrets

1.创建secret

kubectl create secret docker-registry secret名 --docker-server=仓库地址 --docker-username=用户名 --docker-password=密码 

例如:

kubectl create secret docker-registry secret-key --docker-server=registry.cn-zhangjiakou.aliyuncs.com --docker-username=ABCD --docker-password=QWER!@
--docker-server=registry.cn-zhangjiakou.aliyuncs.com  #阿里云仓库地址
--docker-username=ABCD  #阿里云仓库登录的用户名
--docker-password=QWER!@#$ #阿里云仓库的登录密码


删除secret的命令:

kubectl delete secret secret-key 

2.编写k8s的yaml文件:

[root@master1 k8s-nginx]# vim nginx-test.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
  namespace: default
spec:
  ports:
	#对外暴露端口30003
  - nodePort: 30003
    port: 8010
    protocol: TCP
    targetPort: 8010
  selector:
    app: nginx-web
  #NodePort对外暴露端口
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-web
  name: nginx-web
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-web
  template:
    metadata:
      labels:
        app: nginx-web
      namespace: default
    spec:
	  imagePullSecrets:
	  - name: secret-key
      containers:
      - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v1
        name: nginx
        env:
        - name: TZ
          value: Asia/Shanghai
		imagePullPolicy: Always
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 1Gi
          limits:
            cpu: 100m
            memory: 1Gi
        #pod内目录
	    #volumeMounts:
        #- name: conf
        #  mountPath: /etc/nginx/conf.d
        #- name: log
        #  mountPath: /var/log/nginx
        #- name: html
        #  mountPath: /etc/nginx/dist
      #宿主机目录需要自己创建
      #volumes:
      #- name: conf
      #  hostPath:
      #    path: /root/k8s-nginx/nginx/conf.d
      #- name: log
      #  hostPath:
      #    path: /root/k8s-nginx/nginx/log
      #- name: html
      #  hostPath:
      #    path: /root/k8s-nginx/nginx/dist
[root@master1 k8s-nginx]# kubectl apply -f nginx-test.yaml 
service/nginx-service configured
deployment.apps/nginx-web created
[root@master1 k8s-nginx]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
nginx-web-9f5fbbb7b-bjwvg   1/1     Running   0          3s


验证:http://10.10.10.10:30003/index.html

 java后端部署

和前端差不多,需要自己安装docker,然后把基础jdk镜像或者java镜像上传到镜像仓库

Dockerfile参考:

FROM registry.cn-zhangjiakou.aliyuncs.com/ymku/java:8
ADD app.jar /app
WORKDIR /app
ENTRYPOINT java -Dfile.encoding=utf-8 -Xms4096m -Xmx8192m -Dspring.profiles.active=test -jar /app/app.jar

docker build参考:

docker build -t 10.10.10.10/app/app:20240108 .

k8s的yaml参考:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: 10.10.10.10/app/app:20240108
        imagePullPolicy: Always
        #command: ["/bin/sh", "-ce", "tail -f /dev/null"]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小邋遢2.0

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值