1. k8s 1.24.x集群环境中   master节点需安装 docker环境

2. 在/opt/docker 目录下上传 springboot  JAR包 。 编写Dockerfile文件

把springboot包 上传到/opt/docker  下

 

 本人项目 端口 8081

Dockerfile暴露8081端口

vi Dockerfile

FROM java:8
MAINTAINER zq  
VOLUME /tmp    
ADD common-0.0.1-SNAPSHOT.jar demo.jar   
RUN bash -c 'touch /demo.jar'
ENTRYPOINT ["java","-jar","/demo.jar"]
EXPOSE 8081   

上传完成与编写Dockerfile完成效果图:

3.在当前目录下 打包镜像

docker build -t demo:1.0 .

4.自己运行测试:

docker run -d -p 8081:8081 demo:1.0

看是否能运行成功。

5.登录自己的dockerHub

[root@master docker]# docker login

登录dockerhub  ,打tag镜像,推送镜像到dockerhub

docker tag 镜像id 用户名/镜像名称:版本号
docker push 用户名/镜像名称:版本号


docker tag 0696d2e82f48 heidaodageshiwo/demo:1.0
docker push heidaodageshiwo/demo:1.0
 

6.登录自己docekerhub查看是否推送成功镜像:

7.查看当前k8s镜像仓库中并没有  刚才上传的镜像

8.进入dockerhub查看上传镜像:

拉取镜像:docker pull heidaodageshiwo/demo:1.0

k8s 1.24版本使用   crictl pull heidaodageshiwo/demo:1.0   拉取镜像(

把docker换成 crictl ) 

看到镜像已经在  当前命名空间下:

9.本机器命令:


[root@master docker]# vi Dockerfile
[root@master docker]# docker build -t demo:1.0 .
Sending build context to Docker daemon  26.78MB
Step 1/7 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/7 : MAINTAINER zq
 ---> Running in 53a67a6f03b9
Removing intermediate container 53a67a6f03b9
 ---> 3b18bb3da9e8
Step 3/7 : VOLUME /tmp
 ---> Running in 342c0bc5f02e
Removing intermediate container 342c0bc5f02e
 ---> a501a838d0a1
Step 4/7 : ADD common-0.0.1-SNAPSHOT.jar demo.jar
 ---> 718be419cca3
Step 5/7 : RUN bash -c 'touch /demo.jar'
 ---> Running in ed41bdb91fa4
Removing intermediate container ed41bdb91fa4
 ---> 4c87a3541bfb
Step 6/7 : ENTRYPOINT ["java","-jar","/demo.jar"]
 ---> Running in 1712e351f016
Removing intermediate container 1712e351f016
 ---> 022cf13c85c9
Step 7/7 : EXPOSE 8081
 ---> Running in 331249034e13
Removing intermediate container 331249034e13
 ---> 0696d2e82f48
Successfully built 0696d2e82f48
Successfully tagged demo:1.0
[root@master docker]# docker run -d -p 8081:8081 demo:1.0
02f5ea6548252d8ac6d25baed43fa27036f6e8f30446f5882f965391ba909028
[root@master docker]# docker ps
CONTAINER ID   IMAGE      COMMAND                 CREATED         STATUS         PORTS                                       NAMES
02f5ea654825   demo:1.0   "java -jar /demo.jar"   4 seconds ago   Up 2 seconds   0.0.0.0:8081->8081/tcp, :::8081->8081/tcp   serene_hopper
[root@master docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
demo         1.0       0696d2e82f48   58 seconds ago   697MB
java         8         d23bdf5b1b1b   5 years ago      643MB
[root@master docker]# docker rm -f 02f5ea654825
02f5ea654825
[root@master docker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@master docker]# docker iamges
docker: 'iamges' is not a docker command.
See 'docker --help'
[root@master docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
demo         1.0       0696d2e82f48   About a minute ago   697MB
java         8         d23bdf5b1b1b   5 years ago          643MB
[root@master docker]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: heidaodageshiwo
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@master docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
demo         1.0       0696d2e82f48   2 minutes ago   697MB
java         8         d23bdf5b1b1b   5 years ago     643MB
[root@master docker]# docker tag 0696d2e82f48 heidaodageshiwo/demo:1.0
[root@master docker]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
heidaodageshiwo/demo   1.0       0696d2e82f48   4 minutes ago   697MB
demo                   1.0       0696d2e82f48   4 minutes ago   697MB
java                   8         d23bdf5b1b1b   5 years ago     643MB
[root@master docker]# docker push heidaodageshiwo/demo:1.0
The push refers to repository [docker.io/heidaodageshiwo/demo]
527bd9dadf74: Pushed 
f5b6bd9d054c: Pushed 
35c20f26d188: Mounted from library/java 
c3fe59dd9556: Mounted from library/java 
6ed1a81ba5b6: Mounted from library/java 
a3483ce177ce: Mounted from library/java 
ce6c8756685b: Mounted from library/java 
30339f20ced0: Mounted from library/java 
0eb22bfb707d: Mounted from library/java 
a2ae92ffcd29: Mounted from library/java 
1.0: digest: sha256:006e9d9d6a34d99617d0d8e938fbd883cb42f84dd83c09ca9728fe13c4008fb9 size: 2424
[root@master docker]# crictl iamges
No help topic for 'iamges'
[root@master docker]# crictl images
IMAGE                                                             TAG                 IMAGE ID            SIZE
docker.io/calico/cni                                              v3.23.2             a87d3f6f1b8fd       111MB
docker.io/calico/node                                             v3.23.2             a3447b26d32c7       77.8MB
docker.io/library/nginx                                           latest              670dcc86b69df       56.7MB
registry.aliyuncs.com/google_containers/coredns                   v1.8.6              a4ca41631cc7a       13.6MB
registry.aliyuncs.com/google_containers/etcd                      3.5.3-0             aebe758cef4cd       102MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.24.0             529072250ccc6       33.8MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.24.0             88784fb4ac2f6       31MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.24.0             77b49675beae1       39.5MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.24.0             e3ed7dee73e93       15.5MB
registry.aliyuncs.com/google_containers/pause                     3.7                 221177c6082a8       311kB
[root@master docker]# crictl pull heidaodageshiwo/demo:1.0
Image is up to date for sha256:0696d2e82f48a36fb155691fe8c3a8bc35c52ce5c3236620e2e7fba13fa16aeb
[root@master docker]# crictl images
IMAGE                                                             TAG                 IMAGE ID            SIZE
docker.io/calico/cni                                              v3.23.2             a87d3f6f1b8fd       111MB
docker.io/calico/node                                             v3.23.2             a3447b26d32c7       77.8MB
docker.io/heidaodageshiwo/demo                                    1.0                 0696d2e82f48a       292MB
docker.io/library/nginx                                           latest              670dcc86b69df       56.7MB
registry.aliyuncs.com/google_containers/coredns                   v1.8.6              a4ca41631cc7a       13.6MB
registry.aliyuncs.com/google_containers/etcd                      3.5.3-0             aebe758cef4cd       102MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.24.0             529072250ccc6       33.8MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.24.0             88784fb4ac2f6       31MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.24.0             77b49675beae1       39.5MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.24.0             e3ed7dee73e93       15.5MB
registry.aliyuncs.com/google_containers/pause                     3.7                 221177c6082a8       311kB
[root@master docker]# crictl pull java:8
Image is up to date for sha256:d23bdf5b1b1b1afce5f1d0fd33e7ed8afbc084b594b9ccf742a5b27080d8a4a8
[root@master docker]# crictl images
IMAGE                                                             TAG                 IMAGE ID            SIZE
docker.io/calico/cni                                              v3.23.2             a87d3f6f1b8fd       111MB
docker.io/calico/node                                             v3.23.2             a3447b26d32c7       77.8MB
docker.io/heidaodageshiwo/demo                                    1.0                 0696d2e82f48a       292MB
docker.io/library/java                                            8                   d23bdf5b1b1b1       243MB
docker.io/library/nginx                                           latest              670dcc86b69df       56.7MB
registry.aliyuncs.com/google_containers/coredns                   v1.8.6              a4ca41631cc7a       13.6MB
registry.aliyuncs.com/google_containers/etcd                      3.5.3-0             aebe758cef4cd       102MB
registry.aliyuncs.com/google_containers/kube-apiserver            v1.24.0             529072250ccc6       33.8MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.24.0             88784fb4ac2f6       31MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.24.0             77b49675beae1       39.5MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.24.0             e3ed7dee73e93       15.5MB
registry.aliyuncs.com/google_containers/pause                     3.7                 221177c6082a8       311kB
[root@master docker]# 

10.空运行测试生成我们的yaml文件
1)命令:
     kubectl create deployment deployment名称 --image=我们项目的镜像名称 --dry-run -o yaml > 要生成的yaml文件
     注意:--dry-run表示空运行测试,不会真正的运行
               -o 表示测试输出
2)执行命令生成我们的yaml模板
      命令:kubectl create deployment springboot-k8s --image=docker.io/heidaodageshiwo/demo:1.0 --dry-run -o yaml > deploy.yaml

对上面生成的deploy.yaml文件进行修改,让它使用本地的镜像,因为它默认是去中心仓库拉取镜像的

 加入:imagePullPolicy: "IfNotPresent"

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: docker.io/heidaodageshiwo/demo:1.0
        name: demo
        imagePullPolicy: "IfNotPresent"
        resources: {}
status: {}

11.运行:

[root@master docker]# kubectl apply -f deploy.yaml

12.查看:

13.看到已经运行成功:

暴露3万端口也可以写service.yml文件方式:

kubectl expose deployment springboot-k8s --port=8081 --type=NodePort

[root@master docker]# kubectl expose deployment springboot-k8s --port=8081 --type=NodePort
service/springboot-k8s exposed
[root@master docker]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          11d
springboot-k8s   NodePort    10.109.223.141   <none>        8081:30395/TCP   5s
[root@master docker]#

14.访问:

我的springboot 访问接口

界面访问:暴露的30395端口

springboot-k8s   NodePort    10.109.223.141   <none>        8081:30395/TCP   5s
 

 15.本机命令:


[root@master docker]# ll
总用量 26152
-rw-r--r-- 1 root root 26775474 7月  29 16:23 common-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 root root      168 7月  29 16:22 Dockerfile
[root@master docker]# kubectl create deployment springboot-k8s --image=docker.io/heidaodageshiwo/demo:1.0 --dry-run -o yaml > deploy.yaml
W0729 16:46:32.594741   18750 helpers.go:636] --dry-run is deprecated and can be replaced with --dry-run=client.
[root@master docker]# ls
common-0.0.1-SNAPSHOT.jar  deploy.yaml  Dockerfile
[root@master docker]# kubectl apply -f deploy.yaml
deployment.apps/springboot-k8s created
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          6s
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          10s
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          11s
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          12s
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          13s
[root@master docker]# kubectl get po
NAME                              READY   STATUS              RESTARTS   AGE
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          31s
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS              RESTARTS   AGE   IP       NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          35s   <none>   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS              RESTARTS   AGE   IP       NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          95s   <none>   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS              RESTARTS   AGE   IP       NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          98s   <none>   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS              RESTARTS   AGE   IP       NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          99s   <none>   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS              RESTARTS   AGE     IP       NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   0/1     ContainerCreating   0          2m15s   <none>   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   1/1     Running   0          2m46s   10.244.104.3   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   1/1     Running   0          2m48s   10.244.104.3   node2   <none>           <none>
[root@master docker]# kubectl get po -o wide
NAME                              READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
springboot-k8s-645899cbc4-8dqvm   1/1     Running   0          2m49s   10.244.104.3   node2   <none>           <none>
[root@master docker]# kubectl expose deployment springboot-k8s --port=8081 --type=NodePort
service/springboot-k8s exposed
[root@master docker]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          11d
springboot-k8s   NodePort    10.109.223.141   <none>        8081:30395/TCP   5s
[root@master docker]# 

16.看到 本项目被调度到node2 节点:

查看node2节点镜像:可以看到 demo 与java8镜像都已经拉取到了本空间中

17.结束

Logo

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

更多推荐