在kubernetes中部署应用流程

IT
IT
IT
335
文章
1
评论
2020年9月15日19:38:37 评论 1,061 5978字阅读19分55秒

流程图

在kubernetes中部署应用流程

使用Deployment部署Java应用

tomcat-java-demo-master

  • 上传源代码到服务器
[root@k8s-master1 tomcat-java-demo-master]# pwd
/root/tomcat-java-demo-master
[root@k8s-master1 tomcat-java-demo-master]# ll
total 32
drwx------ 2 root root 4096 Sep 15 19:41 db
-rw------- 1 root root 148 Aug 4 2019 Dockerfile
-rw------- 1 root root 11357 Aug 4 2019 LICENSE
-rw------- 1 root root 1930 Aug 4 2019 pom.xml
-rw------- 1 root root 270 Aug 4 2019 README.md
drwx------ 3 root root 4096 Sep 15 19:41 src

镜像分类:

  • 基础镜像,例如centos,ubuntu
  • 环境镜像,例如jdk,python,go
  • 项目镜像,是将项目打包到里面

制作镜像

  • 由于要编译打包,先安装基础环境jdk和maven
[root@k8s-master1 ~]# yum install java-1.8.0-openjdk maven -y
  • 编译构建(构建新的包,跳过单元测试)
[root@k8s-master1 tomcat-java-demo-master]# pwd
/root/tomcat-java-demo-master
[root@k8s-master1 tomcat-java-demo-master]# mvn clean package -D maven.skip.test=true
  • 编译完成,会生成一个target目录,里面就是我们打包完成打war包
[root@k8s-master1 tomcat-java-demo-master]# ll
total 36
drwx------ 2 root root 4096 Sep 15 19:41 db
-rw------- 1 root root 148 Aug 4 2019 Dockerfile
-rw------- 1 root root 11357 Aug 4 2019 LICENSE
-rw------- 1 root root 1930 Aug 4 2019 pom.xml
-rw------- 1 root root 270 Aug 4 2019 README.md
drwx------ 3 root root 4096 Sep 15 19:41 src
drwxr-xr-x 7 root root 4096 Sep 18 20:11 target
  • 编写Dockerfile
[root@k8s-master1 tomcat-java-demo-master]# vim Dockerfile 
FROM tomcat:8 ###使用官方tomcat8的镜像
LABEL maintainer www.centoscn.vip ###打个标签
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war  #####将刚才编译打包后的war包,拷贝到镜像的这个目录下
  • 构建镜像
[root@k8s-master1 tomcat-java-demo-master]# docker build -t java-demo:v1 .
  • 查看构建完成的镜像
[root@k8s-master1 tomcat-java-demo-master]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java-demo v1 38871248ae51 23 seconds ago 548MB
  • 上传镜像到仓库(比如阿里云的镜像仓库,hub等等)
[root@k8s-master1 tomcat-java-demo-master]# 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: centoscn
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
  • 打tag号上传到Hub上
[root@k8s-master1 ~]# docker tag java-demo:v1 centoscn/java-demo:v1
  • 推送镜像到远程仓库
[root@k8s-master1 ~]# docker push centoscn/java-demo:v1
The push refers to repository [docker.io/centoscn/java-demo]

在kubernetes中部署应用流程

Deployment控制器功能

  • 部署无状态应用
    管理pod和replicaset
    具有上线部署,副本设定,滚动升级,回滚等功能。
    提供声明式更新,例如只更新一个新的lmage
    应用场景:Web服务,微服务

控制器管理pod

  • 由于之前使用web做过测试,直接删除掉
[root@k8s-master1 ~]# kubectl delete deploy web
deployment.apps "web" deleted
[root@k8s-master1 ~]# kubectl delete svc web
service "web" deleted
  • 创建
[root@k8s-master1 ~]# kubectl create deployment web --image=centoscn/java-demo:v1
deployment.apps/web created
[root@k8s-master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 2 5d14h
web-b9b864798-l7nrp 1/1 Running 0 10s
  • 发布出去,暴露端口
[root@k8s-master1 ~]# kubectl expose deployment web --port=80 --type=NodePort --target-port=8080 --name=web
service/web exposed
  • 查看service
[root@k8s-master1 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 5d15h
web NodePort 10.0.0.197 <none> 80:30230/TCP 11s
  • 使用curl请求测试
[root@k8s-master1 ~]# curl 10.0.0.197 
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>把美女带回家应用案例</title>
<meta name="description" content="把美女带回家应用案例">
<meta name="keywords" content="index">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="../js/layui/css/layui.css" media="all">
<link rel="stylesheet" href="../css/reset.css">
<link rel="stylesheet" href="../css/supersized.css">
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="../css/typeit.css">
<link rel="stylesheet" href="../css/date.css">
</head>
<body>
<!-- <img src="../images/logo.png"> -->
<div class="page-container">
<h1 class="title"></h1>
<div style="margin-top: 15%;">
<button class="layui-btn layui-btn-lg layui-btn-primary layui-btn-radius" onclick="adduser()">添加美女</button>
<button class="layui-btn layui-btn-lg layui-btn-primary layui-btn-radius" onclick="queryuserList()">今晚翻盘哪个</button>
</div>
  • 浏览器测试

在kubernetes中部署应用流程

  • 查看pod详细信息
[root@k8s-master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dns-test 1/1 Running 2 5d14h 10.244.0.11 k8s-node1 <none> <none>
web-b9b864798-bzwgk 1/1 Running 0 3m30s 10.244.2.5 k8s-master1 <none> <none>
  • 查看ep
[root@k8s-master1 ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 10.10.1.37:6443,10.10.1.38:6443 5d15h
web 10.244.2.5:8080 3m55s
  • 扩容副本
[root@k8s-master1 ~]# kubectl scale deployment web --replicas=3
deployment.apps/web scaled
  • 查看节点详细信息
[root@k8s-master1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dns-test 1/1 Running 2 5d15h 10.244.0.11 k8s-node1 <none> <none>
web-b9b864798-bzwgk 1/1 Running 0 9m12s 10.244.2.5 k8s-master1 <none> <none>
web-b9b864798-d5qgt 0/1 ContainerCreating 0 2m13s <none> k8s-master2 <none> <none>
web-b9b864798-hsgd8 0/1 ContainerCreating 0 2m13s <none> k8s-node2 <none> <none>
  • 查看为啥还没部署成功,发现是在下载镜像
[root@k8s-master1 ~]# kubectl describe pod web-b9b864798-d5qgt
Name: web-b9b864798-d5qgt
Namespace: default
Priority: 0
Node: k8s-master2/10.10.1.38
Start Time: Sat, 19 Sep 2020 14:57:29 +0800
Labels: app=web
pod-template-hash=b9b864798
Annotations: <none>
Status: Pending
IP: 
IPs: <none>
Controlled By: ReplicaSet/web-b9b864798
Containers:
java-demo:
Container ID: 
Image: centoscn/java-demo:v1
Image ID: 
Port: <none>
Host Port: <none>
State: Waiting
Reason: ContainerCreating
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-6lgmm (ro)
Conditions:
Type Status
Initialized True 
Ready False 
ContainersReady False 
PodScheduled True 
Volumes:
default-token-6lgmm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-6lgmm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/web-b9b864798-d5qgt to k8s-master2
Normal Pulling 2m16s kubelet, k8s-master2 Pulling image "centoscn/java-demo:v1"
继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2020年9月15日19:38:37
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Pod Kubernetes

Pod

Pod基本概念 Pod存在的意义 Pod实现机制与设计模式 Pod镜像拉取策略
资源编排【YMAL】 Kubernetes

资源编排【YMAL】

YAML 基础 它的基本语法规则如下: 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格。 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 # 表示注释,从这个字符一直...
部署CoreDNS Kubernetes

部署CoreDNS

CoreDNS用于集群内部Service名称解析 下载配置文件 coredns.yaml 创建dns # kubectl apply -f coredns.yaml 查看pod # kubectl g...
部署Dashboard Kubernetes

部署Dashboard

下载配置文件 # wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recomme...