第4章:应用程序生命周期管理

在 Kubernetes部署应用程序流程

1 制作镜像
2 控制器管理Pod
3 暴露应用
4 对外发布应用
5 日志/监控

镜像分类:
1、基础镜像,例如centos、ubuntu
2、环境镜像,例如jdk、python、go
3、项目镜像,基于环境镜像,将项目打包里面


为什么自己构建镜像?
1、特殊环境配置
2、容易故障排查
3、标准化
4、更安全一些

1、准备编译环境
yum install java-1.8.0-openjdk maven -y
2、编译构建
mvn clean package -D maven.skip.test=true

修改源Maven编译的时候快一些:
vi /etc/maven/settings.xml
  <mirrors>
    <mirror>     
      <id>central</id>     
      <mirrorOf>central</mirrorOf>     
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>     
    </mirror>
  </mirrors>

构建镜像


docker build -t java-demo:v1 .
docker tag java-demo:v1 lizhenliang/java-demo:v1       # 打标签
docker push lizhenliang/java-demo:v1            # 发布容容器


- 推送到公共镜像仓库 - 搭建一个harbor私有镜像仓库 - 导入导出: docker save java-demo:v1 > java-demo.tar docker load < java-demo.tar

 

[root@k8s-m1 tomcat-java-demo-master]# ls
db  Dockerfile  LICENSE  pom.xml  README.md  src  target
[root@k8s-m1 tomcat-java-demo-master]# cat Dockerfile
FROM tomcat:8
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
Dockerfile

demo地址:
github.com/lizhenliang/tomcat-java-demo

使用 Deployment部署Java应用

0、制作镜像
准备项目代码->编译构建->将产岀的war包打包到镜像中->推送到镜像仓库
1、使用 Deployment控制器部署镜像 kubectl create deployment web --image=lizhenliang/java-demo
2、使用 Service发布Pod kubectl expose deployment web --port=80 --type=NodePort --target-port=8080 --name=web kubectl get service

命令行部署

[root@k8s-m1 tomcat-java-demo-master]# kubectl create deployment web --image=lizhenliang/java-demo
deployment.apps/web created
[root@k8s-m1 tomcat-java-demo-master]# kubectl get  deploy,pods
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           11d
deployment.apps/web     1/1     1            1           57s

NAME                        READY   STATUS    RESTARTS   AGE
pod/nginx-f89759699-qjjjb   1/1     Running   1          5d8h
pod/web-6b9c78c94f-vvtks    1/1     Running   0          57s


[root@k8s-m1 tomcat-java-demo-master]# kubectl expose deployment web --port=80 --type=NodePort --target-port=8080 --name=web
service/web exposed

[root@k8s-m1 tomcat-java-demo-master]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        11d
nginx        NodePort    10.96.231.76     <none>        80:31473/TCP   11d
web          NodePort    10.109.105.181   <none>        80:32116/TCP   8s


[root@k8s-m1 tomcat-java-demo-master]# kubectl get ep
NAME         ENDPOINTS             AGE
kubernetes   10.0.0.23:6443        11d
nginx        10.244.215.76:80      11d
web          10.244.111.210:8080   22m

[root@k8s-m1 tomcat-java-demo-master]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE    IP               NODE     NOMINATED NODE   READINESS GATES
nginx-f89759699-qjjjb   1/1     Running   1          5d9h   10.244.215.76    k8s-n1   <none>           <none>
web-6b9c78c94f-vvtks    1/1     Running   0          35m    10.244.111.210   k8s-n2   <none>           <none>

YAML部署

YAML文件创建资源对象

YAML文件格式说明

YAML是一种简洁的非标记语言。
语法格式:

  • 缩进表示层级关系
  • 不支持制表符"tab”缩进,使用空格缩进
  • 通常开头缩进2个空格
  • 字符后缩进1个空格,如冒号、逗号等
  • ‘---’ 表示YAML格式,一个文件的开始
  • #”注释

编写 yml 配置文件

[root@k8s-m1 chp4]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web2
spec:
  replicas: 1
  selector:
    matchLabels:
      project: demo
      app: java
  template:
    metadata:
      labels:
        project: demo
        app: java
    spec:
      containers:
      - name: web
        image: lizhenliang/java-demo
        ports:
        - containerPort: 8080
[root@k8s-m1 chp4]# kubectl apply -f deployment.yml

[root@k8s-m1 chp4]# cat service.yml
kind: Service
apiVersion: v1
metadata:
  name: web2
spec:
  selector:
    project: demo
    app: java
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: NodePort
[root@k8s-m1 chp4]# kubectl apply -f service.yml

[root@k8s-m1 chp4]# kubectl get svc,pod,deploy
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
service/nginx NodePort 10.96.231.76 <none> 80:31473/TCP 12d
service/web NodePort 10.109.105.181 <none> 80:32116/TCP 24h
service/web2 NodePort 10.100.117.24 <none> 80:31359/TCP 11m

NAME READY STATUS RESTARTS AGE
pod/nginx-f89759699-qjjjb 1/1 Running 2 6d9h
pod/web-6b9c78c94f-vvtks 1/1 Running 1 24h
pod/web2-564664575d-v9768 1/1 Running 0 14m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 12d
deployment.apps/web 1/1 1 1 24h
deployment.apps/web2 1/1 1 1 14m

服务编排:资源字段太多,记不住怎么办

用 create命令生成
kubectl create deployment nginx --image=nginx:1.16 -o yaml --dry-run=client > my-deploy.yaml

-o 选项:json|yaml|name|wide --dry-run 尝试不实际创建 用get命令导出 kubectl get deployment web -o yaml >web.yaml Pod容器的字段拼写忘记了 kubectl explain pods.spec.containers kubectl explain deployment

Pod与 Deployment的关系

Deployment是最为常用的 controllers(也称为 workload),其他控制器还有 Daemon Set、 StatefulSet等。

controllers特点

  • controllers:在集群上管理和运行容器的对象
  • 通过labe- selector相关联
  • Pod通过控制器实现应用的运维,如伸缩,滚动升级等

Deployment 控制器介绍

Deployment的功能:

  • 管理Pod和 ReplicaSe
  • 具有上线部署、副本设定、滚动升级、回滚等功能
  • 提供声明式更新,例如只更新一个新的 Image

应用场景:网站、API、微服务

应用升级、弹性伸缩、回滚、删除

应用升级

kubectl set image deployment/web nginx=nginx:1.16
# 查看升级状态
kubectl rollout status deployment/web

[root@k8s-m1 chp4]# kubectl set image deployment web java-demo=tomcat:8
deployment.apps/web image updated
[root@k8s-m1 chp4]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
nginx-f89759699-qjjjb   1/1     Running             2          6d9h
web-69fd684457-t9qb9    0/1     ContainerCreating   0          23s
web-6b9c78c94f-vvtks    1/1     Running             1          25h
web2-564664575d-v9768   1/1     Running             0          23m
[root@k8s-m1 chp4]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
nginx-f89759699-qjjjb   1/1     Running             2          6d9h
web-69fd684457-t9qb9    0/1     ContainerCreating   0          30s
web-6b9c78c94f-vvtks    1/1     Running             1          25h
web2-564664575d-v9768   1/1     Running             0          23m
[root@k8s-m1 chp4]# kubectl get pod
NAME                    READY   STATUS              RESTARTS   AGE
nginx-f89759699-qjjjb   1/1     Running             2          6d9h
web-69fd684457-t9qb9    0/1     ContainerCreating   0          41s
web-6b9c78c94f-vvtks    1/1     Running             1          25h
web2-564664575d-v9768   1/1     Running             0          23m
[root@k8s-m1 chp4]# kubectl get pod
NAME                    READY   STATUS        RESTARTS   AGE
nginx-f89759699-qjjjb   1/1     Running       2          6d9h
web-69fd684457-t9qb9    1/1     Running       0          70s
web-6b9c78c94f-vvtks    0/1     Terminating   1          25h
web2-564664575d-v9768   1/1     Running       0          24m
[root@k8s-m1 chp4]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
nginx-f89759699-qjjjb   1/1     Running   2          6d9h
web-69fd684457-t9qb9    1/1     Running   0          81s
web2-564664575d-v9768   1/1     Running   0          24m
[root@k8s-m1 chp4]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        12d
nginx        NodePort    10.96.231.76     <none>        80:31473/TCP   12d
web          NodePort    10.109.105.181   <none>        80:32116/TCP   25h
web2         NodePort    10.100.117.24    <none>        80:31359/TCP   22m

[root@k8s-m1 chp4]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none>

[root@k8s-m1 chp4]# kubectl rollout undo deployment web
deployment.apps/web rolled back

 

[root@k8s-m1 chp4]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
2 <none>
3 <none>


[root@k8s-m1 chp4]# kubectl set image -h
Update existing container image(s) of resources.
Possible resources include (
case insensitive): [root@k8s-m1 chp4]# kubectl set image deployment web java-demo=tomcat:8 --record=true deployment.apps/web image updated [root@k8s-m1 chp4]# kubectl rollout history deployment web deployment.apps/web REVISION CHANGE-CAUSE 3 <none> 4 kubectl set image deployment web java-demo=tomcat:8 --record=true

弹性伸缩


#手动扩容
kubectl scale deployment web --replicas=10

#自动水平扩容
kubectl autoscale deployment web --min=3 --max=10 --cpu-percent=80
kubectl get hpa

回滚

kubectl rollout history deployment/web
kubectl rollout undo deployment/web    #回滚上一个版
kubectl rollout undo deployment/web --to-revision=2    #回滚指定版本

删除 kubectl delete deploy
/web kubectl delete svc/web

资源限制

[root@k8s-m1 chp4]# cat deployment-cpu.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web2
spec:
  replicas: 1
  selector:
    matchLabels:
      project: demo
      app: java
  template:
    metadata:
      labels:
        project: demo
        app: java
    spec:
      containers:
      - name: web
        image: lizhenliang/java-demo
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: 500m
          limits:
            cpu: 1

使用HPA前提:
1、部署metrics-server
2、部署pod要设置requests

 

posted @ 2020-08-11 10:54  前海渔文乐  阅读(304)  评论(0编辑  收藏  举报