欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kubernetes - 从Docker 镜像到K8s Pod操作示例

程序员文章站 2022-07-14 09:24:51
...

上一篇有写到怎样将一个普通的springboot的jar包,制作成docker镜像,并push到镜像仓库,

Docker - 创建并运行一个docker(springboot)容器    启动这个docker容器,并对外提供服务等等;

这里我们介绍下,怎样将你做好的容器纳入K8S的统筹管理之下;let`s go

1、首先你要有一个k8s环境,例如   kubectl version

Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.2", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:08:19Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

2、将我们做好的docker镜像push到镜像仓库(例如xxx-xxx:5000)

docker push xxx-xxx:5000/docker.io/http-server:v1.0

The push refers to a repository [xxx-xxx:5000/docker.io/http-server]
54c2f122xxx: Pushed 
2dd3536cxxx: Pushed 
28982f07xxx: Pushed 
5a3181efxxx: Pushed 
9323bf93xxx: Layer already exists 
35c20f26xxx: Layer already exists 
c3fe59ddxxx: Layer already exists 
6ed1a81bxxx: Layer already exists 
a3483ce1xxx: Layer already exists 
ce6c8756xxx: Layer already exists 
30339f20xxx: Layer already exists 
0eb22bfbxxx: Layer already exists 
a2ae92ffxxx: Layer already exists 
v1.0: digest: sha256:b812c4c7bf5cdb3eba4df544da4efc69d71eef size: 3041

3、我们在镜像仓库已经有我们需要的镜像的情况下,编写  springboot-httpserver-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: http-server
  labels:
    name: http-server
spec:
  replicas: 2
  selector:
    name: http-server
  template:
    metadata:
     labels:
       name: http-server
    spec:
     containers:
     - name: http-server
       image: xxx-xxx:5000/docker.io/http-server:v1.0  #此处的xxx是你镜像仓库的名字
       imagePullPolicy: IfNotPresent
       volumeMounts:
       - name: config-volume
         mountPath: /home/daopinz/test/
       ports:
       - containerPort: 12222
     volumes:
     - name: config-volume
       emptyDir: {}
    #   configMap:
    #     name: httpserver-config-1

4、我们需要将服务提供出去供调用,就需要把服务端口暴露出去编写springboot-httpserver-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: http-server
  labels:
    name: http-server
spec:
  type: NodePort 
  ports:
  - port: 12222
    nodePort: 32222
  selector:
    name: http-server

5、创建pod(以RC方式): kubectl create -f springboot-httpserver-rc.yaml

6、创建service(以SVC方式) : kubectl create -f springboot-httpserver-svc.yaml

7、此时

            kubectl get pods 即可看到已经启动的Pod

            kubectl get rc 即可看到已经启动的RC

            kubectl get svc 即可看到已经启动的SVC

8、删除pod,可直接执行kubectl delete pod  podname  但是会被rc的配置主动拉起来

所以我们可以执行kubectl delete -f springboot-httpserver-rc.yaml或者kubectl delete rc http-server,可彻底删除对应pod

9、删除service,可以直接执行kubectl delete -f springboot-httpserver-svc.yaml或者kubectl delete svc http-server ,可彻底删除对应svc