模块四 Kubernetes应用程序生命周期管理

1、在k8s中部署应用流程

  • 制作镜像(一般通过Dockerfile)
    1、hub.docker.com(标准镜像)
    2、满足个性化定制化需求(标准化、调优参数等)
  • 控制器管理Pod
  • 暴露应用服务
  • 对外发布应用
  • 管理日志/集群监控

2、使用Deployment部署Java应用

[root@k8s-m ~]#  docker run -d --name=web -p 80:80 e a=test nginx
[root@k8s-m ~]# kubectl create --help
[root@k8s-m ~]# kubectl create deployment --help
[root@k8s-m ~]# docker pull yejf/java-demo
[root@k8s-m ~]# kubectl create deployment web --image=yejf/java-demo:latest

[root@k8s-m ~]# kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
nginx-f89759699-bxftk         1/1     Running   0          17h
web-7757ff5c-pt6jd            1/1     Running   0          83s
[root@k8s-m ~]# kubectl get deploy,pods
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx        1/1     1            1           2d16h
deployment.apps/web          1/1     1            1           2m53s

NAME                              READY   STATUS    RESTARTS   AGE
pod/nginx-f89759699-bxftk         1/1     Running   0          17h
pod/web-7757ff5c-pt6jd            1/1     Running   0          2m53s
[root@k8s-m ~]# kubectl get pod -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATES
nginx-f89759699-bxftk         1/1     Running   0          17h     10.244.215.77    k8s-n1              
web-7757ff5c-pt6jd            1/1     Running   0          5m55s   10.244.111.205   k8s-n2              
[root@k8s-m ~]#
[root@k8s-m ~]# kubectl expose deployment web --port=80 --target-port=8080 --name=web --type=NodePort
[root@k8s-m ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP        2d17h
nginx        NodePort    10.100.4.40              80:30026/TCP   2d16h
web          NodePort    10.104.68.193            80:32583/TCP   47s
[root@k8s-m ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP        2d17h
nginx        NodePort    10.100.4.40              80:30026/TCP   2d16h
web          NodePort    10.104.68.193            80:32583/TCP   51s

http://192.168.X.63:32853

3、使用YAML部署Java应用

# vim java-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: java-demo
  template:
    metadata:
      labels:
        app: java-demo
    spec:
      containers:
      - name: nginx
        image: yejf/java-demo
        ports:
[root@k8s-m ~]# kubectl apply -f java-demo.yaml
deployment.apps/java-demo created
[root@k8s-m ~]# kubectl get deploy
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
java-demo    1/1     1            1           41s
nginx        1/1     1            1           2d17h
web          1/1     1            1           27m
[root@k8s-m ~]# kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
java-demo-d9787d978-7dcp6     1/1     Running   0          50s
nginx-f89759699-bxftk         1/1     Running   0          18h
web-7757ff5c-pt6jd            1/1     Running   0          28m

2、应用部署/升级、回滚和弹性伸缩

你可能感兴趣的:(容器,devops,运维)