k8s-部署容器化应用

一、k8s部署容器化应用的步骤

1、制作镜像(自己Dockerfile文件制作,或者从仓库pull)

2、通过控制器管理pod(把镜像启动得到一个容器,容器在pod里)

3、暴露应用,以便外界可以访问

二、k8s部署nginx示例

1、制作镜像(有现成的,不需要制作)

2、kubectl  create  deployment  nginx  --image=nginx

3、kubectl  expose  deployment  nginx  --port=80  --type=NodePort

4、访问应用

查看一下:

kubectl  get  node(s)

kubectl  get  deploy(ment)

kubectl  get   pod(s)

kubectl  get  service(s)   暴露控制器端口后才会看到service

 

#删除nginx控制器(删除控制器后,对应的pod也被删除 )

kubectl   delete  deploy  nginx

#删除nginx的service

kubectl   delete  service  nginx

#删除nginx的pod

kubectl   delete   pod    nginx-6799fc88d8-mcjsg

 

#查看命名空间

kubectl  get  namespace

#查看pod详情

kubectl  describe  pods  pod名称

#查看pod实时日志

kubectl  logs  -f  pod名称

 三、k8s集群中部署SpringBoot应用

1、项目打包(jar、war)--->可以采用一些工具git、jenkins,maven

2、制作Dockerfile文件,生成镜像

3、kubectl  create  deployment  your-springboot  --image=你的镜像

4、你的springboot就部署好了,是以docker容器方式运行在pod里面

Master控制node-->service-->deployment(控制器)-->pod-->docker

自定义JDK镜像:

FROM centos:latest
MAINTAINER  BAI
ADD  jdk-8u251-linux-x64.tar.gz /usr/local/java
ENV JAVA_HOME /usr/local/java/jdk1.8.0_251
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
CMD java -version

构建java镜像:docker  build  -t  jdk1.8.0_251  .

构建项目镜像:

FROM  jdk1.8.0_251
MAINTAINER  BAI
ADD  springboot-k8s-1.0.0.jar  /opt
RUN  chmod  +x  /opt/springboot-k8s-1.0.0.jar
CMD  java  -jar  /opt/springboot-k8s-1.0.0.jar

构建项目镜像:docker  build  -t  springboot-k8s-1.0.0-jar  .

空运行测试:

Kubectl  create  deployment  springboot-k8s  --image=springboot-k8s-1.0.0-jar  --dry-run  -o  yaml
Kubectl  create  deployment  springboot-k8s  --image=springboot-k8s-1.0.0-jar  --dry-run  -o  yaml  >  deploy.yaml

  yaml文件内容:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: springboot-k8s
  name: springboot-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-k8s
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: springboot-k8s
    spec:
      containers:
      - image: springboot-k8s-1.0.0-jar
        name: springboot-k8s-1.0.0-jar
        #配置从本地拉取镜像,不从镜像仓库拉取
imagePullPolicy:Never resources: {} status: {}

Yml文件部署:

kubectl  apply  -f  deploy.yaml
kubectl expose deploy springboot-k8s --port=80 --type=NodePort
kubectl get service
kubectl get deploy kubectl get pods

  

 

posted @ 2021-06-20 23:52  漫步sch  阅读(2442)  评论(0编辑  收藏  举报