1、获取一个springboot 应用jar包

2、将jar包打成docker镜像

Dockerfile如下:

FROM java:8-alpine
ADD docker-hello-world-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar", "/app.jar"]

打镜像:

docker build -t hellword:0.0.4 .

3、准备docker 镜像私服
参考https://blog.csdn.net/u011943534/article/details/81331231

4、配置私服地址

编辑/etc/docker/daemon.json,添加nexus仓库地址

{ 
	"exec-opts": ["native.cgroupdriver=systemd"],
	"insecure-registries":["192.168.100.89:80", "172.16.10.190:8008", "quay.io", "k8s.gcr.io", "gcr.io"] 
}

重启docker

systemctl restart docker

5、将镜像推送到远程私服

docker tag helloworld:0.0.4 172.16.10.190:8008/helloworld:0.0.4

登陆nexus

docker login --username=admin 172.16.10.190:8008
输入密码登陆
docker push 172.16.10.190:8008/helloworld:0.0.4

6、配置secret拉取私有镜像仓库内容

kubectl create secret docker-registry myregistrykey --docker-server=172.16.10.190:8008 --docker-username=admin --docker-password=admin123 --docker-email=admin@example.org
获取创建的secret
kubectl get secrets

NAME                                     TYPE                                  DATA   AGE
cluster-admin-dashboard-sa-token-hld8l   kubernetes.io/service-account-token   3      3d19h
default-token-7c9dp                      kubernetes.io/service-account-token   3      4d
myregistrykey                            kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey2                           kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey3                           kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey4                           kubernetes.io/dockerconfigjson        1      2d22h
myregistrykey5                           kubernetes.io/dockerconfigjson        1      2d22h
myregistrykey6                           kubernetes.io/dockerconfigjson        1 

7、创建deployment.yaml
imagePullSecrets处配置上一步获得的secret

---
 
apiVersion: extensions/v1beta1
 
kind: Deployment
 
metadata:
 
  name: kube-node
 
spec:
 
  replicas: 2
 
  template:
 
    metadata:
 
      labels:
 
        app: web
 
    spec:
 
      containers:
 
        - name: kube-node-demo-instance
 
          image: kube-node-demo1
 
          ports:
 
            - containerPort: 8081
      imagePullSecrets:
       - name: myregistrykey

8、部署

kubectl create -f deployment.yaml

9、查看部署状态
如果状态为running部署成功

[root@k8s-node1 k8s]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
kube-node-87dcfc9cf-9kj8j   1/1     Running   0          25m
kube-node-87dcfc9cf-l2ppz   1/1     Running   0          25m
  

10、测试应用

[root@k8s-node1 k8s]# kubectl get pods -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
kube-node-87dcfc9cf-9kj8j   1/1     Running   0          26m   10.244.1.23   k8s-node3   <none>           <none>
kube-node-87dcfc9cf-l2ppz   1/1     Running   0          26m   10.244.2.24   k8s-node2   <none>           <none>


[root@k8s-node1 k8s]# curl 10.244.2.24:8080/index
hello,world

11、如果要删除应用,可以删除deployment

kubectl delete deployment/kube-node

12、重启deployment

kubectl rollout restart deployment your_deployment_name

13、滚动升级

---
 
apiVersion: extensions/v1beta1
 
kind: Deployment
 
metadata:
 
  name: kube-node
 
spec:
 
  replicas: 2
 
  template:
 
    metadata:
 
      labels:
 
        app: web
 
    spec:
 
      containers:
 
        - name: kube-node-demo-instance
 
          image: 172.16.10.190:8008/helloworld:0.0.5
 
          ports:
 
            - containerPort: 8080
      imagePullSecrets:

        - name: myregistrykey6

  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1

minReadySeconds:
Kubernetes在等待设置的时间后才进行升级
如果没有设置该值,Kubernetes会假设该容器启动起来后就提供服务了
如果没有设置该值,在某些极端情况下可能会造成服务不正常运行
maxSurge:
升级过程中最多可以比原先设置多出的POD数量
例如:maxSurage=1,replicas=5,则表示Kubernetes会先启动1一个新的Pod后才删掉一个旧的POD,整个升级过程中最多会有5+1个POD。
maxUnavaible:
升级过程中最多有多少个POD处于无法提供服务的状态
当maxSurge不为0时,该值也不能为0
例如:maxUnavaible=1,则表示Kubernetes整个升级过程中最多会有1个POD处于无法服务的状态。

升级

[root@k8s-node1 k8s]# kubectl apply -f deployment.yaml --record=true                                                                                                                     
deployment.extensions/kube-node configured    

查看升级状态

root@k8s-node1 k8s]# kubectl rollout status deployment/kube-node
Waiting for deployment "kube-node" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "kube-node" rollout to finish: 1 old replicas are pending termination...
deployment "kube-node" successfully rolled out

暂停升级

 kubectl rollout pause deployment <deployment>

继续升级

kubectl rollout resume deployment <deployment>

查看RS状态

[root@k8s-node1 k8s]# kubectl get rs
NAME                   DESIRED   CURRENT   READY   AGE
kube-node-6545cf5596   0         0         0       30h
kube-node-6b9f6d4b59   2         2         2       51m
kube-node-6cd4f7bd56   0         0         0       11m

14、版本回退

查看历史版本:

[root@k8s-node1 k8s]# kubectl rollout history deployment kube-node
deployment.extensions/kube-node 
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         kubectl apply --filename=deployment.yaml --record=true

在升级的时候最好携带参数–record=true,方便查看历史版本信息。如使用 kubectl apply -f deployment.yaml --record=true

在deployment.yaml中最好控制记录历史版本数量,因为每个历史版本都记录在etcd中,多了会造成etcd的负担。
添加一个revisionHistoryLimit: 10参数

spec:
  revisionHistoryLimit: 10
  replicas: 2

查看某个历史版本的信息:

[root@k8s-node1 k8s]# kubectl rollout history deployment kube-node --revision=4
deployment.extensions/kube-node with revision #4
Pod Template:
  Labels:       app=web
        pod-template-hash=6b9f6d4b59
  Annotations:  kubectl.kubernetes.io/restartedAt: 2019-08-29T13:42:33+08:00
        kubernetes.io/change-cause: kubectl apply --filename=deployment.yaml --record=true
  Containers:
   kube-node-demo-instance:
    Image:      172.16.10.190:8008/helloworld:0.0.5
    Port:       8080/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

假如现在要直接回退到当前版本的前一个版本:

# kubectl rollout undo deployment kube-node
deployment "kube-node" rolled back

用revision回退到指定的版本:

# kubectl rollout undo deployment kube-node --to-revision=3
deployment "kube-node" rolled back
Logo

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

更多推荐