kubernetes发布tomcat服务,通过deployment,service布署(转)
1.制作tomcat镜像
此处直接拉取
查看已有可镜像
先设置docker阿里源,即添加 "registry-mirrors": ["https://mj9kvemk.mirror.aliyuncs.com"]
[root@k8s-master docker]# pwd
/etc/docker
[root@k8s-master docker]# cat daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"registry-mirrors": ["https://mj9kvemk.mirror.aliyuncs.com"]
}
[root@k8s-master docker]#
service docker restart
[root@k8s-master docker]# docker pull tomcat:8
8: Pulling from library/tomcat
e79bb959ec00: Pull complete
d4b7902036fe: Pull complete
1b2a72d4e030: Pull complete
de423484a946: Pull complete
ceaac3b844f7: Pull complete
88f01b722a52: Pull complete
c23be56a9ac1: Pull complete
d852ffd6d31f: Pull complete
11775a3d792d: Pull complete
acd9db02854a: Pull complete
3032f09d91b5: Pull complete
Digest: sha256:84e91645e3176f55e1f2ad63edf657216ed4ef3dde82e381f1b8ceac1145a21c
Status: Downloaded newer image for tomcat:8
[root@k8s-master docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 8 f1332ae3f570 3 days ago 463MB
k8s.gcr.io/kube-proxy v1.14.0 5cd54e388aba 7 days ago 82.1MB
k8s.gcr.io/kube-scheduler v1.14.0 00638a24688b 7 days ago 81.6MB
k8s.gcr.io/kube-controller-manager v1.14.0 b95b1efa0436 7 days ago 158MB
k8s.gcr.io/kube-apiserver v1.14.0 ecf910f40d6e 7 days ago 210MB
quay.io/coreos/flannel v0.11.0-amd64 ff281650a721 2 months ago 52.6MB
k8s.gcr.io/coredns 1.3.1 eb516548c180 2 months ago 40.3MB
k8s.gcr.io/etcd 3.3.10 2c4adeb21b4f 4 months ago 258MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 15 months ago 742kB
[root@k8s-master docker]#
2.利用docker启动镜像,验证可用性
[root@k8s-master docker]# docker run -d -p 58080:8080 --name mytomcat_1 tomcat:8
c8a00a7aab85ee3b342fb7cd1515a464bde1195397432173b6126f4242244ef9
[root@k8s-master docker]# docker ps -a|grep tomcat
c8a00a7aab85 tomcat:8 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:58080->8080/tcp mytomcat_1
[root@k8s-master docker]#
chrome验证 http://192.168.111.130:58080/
3.发布k8s集群
创建一个deployment
(可参考https://blog.csdn.net/wucong60/article/details/81699196)
kubernetes yaml语法,横杠表示数组,缩进表示层级,缩进不能有tab,参考Kubernetes之YAML文件
[root@k8s-master ~]# pwd
/root
[root@k8s-master ~]# cat deployment-hello.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello
spec:
replicas: 4
template:
metadata:
labels:
run: hello
spec:
containers:
- name: hello
image: tomcat:8 #确保node节点上有该镜像且可正常运行,注意是node节点机器上,不是master机器
imagePullPolicy: IfNotPresent ##Always,IfNotPresent,Never
ports:
- name: http
containerPort: 8080
kubectl 创建pod
[root@k8s-master ~]# kubectl create -f deployment-hello.yaml
deployment.extensions/hello created
deployments详情
root@k8s-master ~]# kubectl get deployments #查看deploy是否成功
NAME READY UP-TO-DATE AVAILABLE AGE
hello 0/4 4 0 55s
[root@k8s-master ~]# kubectl get rs ##因为deployment是三层架构,看rs是否成功,我们看自动创建4个rs.名称后面的字符串是 模板的哈希值。是不会发生变化的,最后pod的是随机值
NAME DESIRED CURRENT READY AGE
hello-68df45bc79 4 4 0 8m29s
[root@k8s-master ~]# kubectl get pods #查看最后一层pod
NAME READY STATUS RESTARTS AGE
hello-7d46c7db4c-42bwl 0/1 ContainerCreating 0 67s
hello-7d46c7db4c-rg9fq 0/1 ContainerCreating 0 67s
hello-7d46c7db4c-tclps 0/1 ContainerCreating 0 67s
hello-7d46c7db4c-whvsw 0/1 ContainerCreating 0 67s [root@k8s-master ~]# kubectl describe deployment hello
Name: hello
Namespace: default
CreationTimestamp: Wed, 03 Apr 2019 10:18:53 +0800
Labels: run=hello
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=hello
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=hello
Containers:
hello:
Image: tomcat:8
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: hello-57b49c67cf (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m30s deployment-controller Scaled up replica set hello-57b49c67cf to 4
[root@k8s-master ~]# ##过两分钟状态就会变为running
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello 4/4 4 4 6m52s
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-57b49c67cf-6hh59 1/1 Running 0 4m56s
hello-57b49c67cf-6ml78 1/1 Running 0 4m56s
hello-57b49c67cf-8xqgw 1/1 Running 0 4m56s
hello-57b49c67cf-pd826 1/1 Running 0 4m56s
[root@k8s-master ~]# kubectl describe pod hello-57b49c67cf-6hh59
Name: hello-57b49c67cf-6hh59
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: k8s-node1/192.168.111.131
Start Time: Wed, 03 Apr 2019 10:18:53 +0800
Labels: pod-template-hash=57b49c67cf
run=hello
Annotations: <none>
Status: Running
IP: 10.244.1.22
Controlled By: ReplicaSet/hello-57b49c67cf
Containers:
hello:
Container ID: docker://8cd27dd2bb4dffd50f8faf8938870af9fa9489f853498f37277a360e3059c476
Image: mytomcat:v8
Image ID: docker-pullable://tomcat@sha256:3e3d18321127bb9114f4226f95802d3899aeec4c36df84d0359e5da300e9bc72
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 03 Apr 2019 10:18:57 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-92rjn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-92rjn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-92rjn
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 5m37s default-scheduler Successfully assigned default/hello-57b49c67cf-6hh59 to k8s-node1
Normal Pulled 5m34s kubelet, k8s-node1 Container image "mytomcat:v8" already present on machine
Normal Created 5m34s kubelet, k8s-node1 Created container hello
Normal Started 5m33s kubelet, k8s-node1 Started container hello
如果kubectl get pod状态为ErrImagePull && ImagePullBackOff ErrImageNeverPull,请确保node上有相应的镜像,若无,则在node机器执行第1,2步
pod发布位置
root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-7d46c7db4c-42bwl 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none>
hello-7d46c7db4c-rg9fq 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none>
hello-7d46c7db4c-tclps 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none>
hello-7d46c7db4c-whvsw 0/1 ContainerCreating 0 5m53s <none> k8s-node1 <none> <none>
正常情况下
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-665548d-5fcbf 1/1 Running 0 11m
hello-665548d-h69xx 1/1 Running 0 11m
hello-665548d-jl6m6 1/1 Running 0 11m
hello-665548d-zd7kw 1/1 Running 0 11m
创建一个service
[root@k8s-master ~]# cat service-hello.yaml
apiVersion: v1
kind: Service
metadata:
name: service-hello
labels:
name: service-hello
spec:
type: NodePort #这里代表是NodePort类型的,另外还有ingress,LoadBalancer
ports:
- port: 80 #这里的端口和clusterIP(kubectl describe service service-hello中的IP的port)对应,即在集群中所有机器上curl 10.98.166.242:80可访问发布的应用服务。
targetPort: 8080 #端口一定要和container暴露出来的端口对应,nodejs暴露出来的端口是8081,所以这里也应是8081
protocol: TCP
nodePort: 31111 # 所有的节点都会开放此端口30000--32767,此端口供外部调用。
selector:
run: hello #这里选择器一定要选择容器的标签,之前写name:kube-node是错的。
[root@k8s-master ~]# pwd
/root
[root@k8s-master ~]#
kubectl create -f 创建对像,删除用kubectl delete -f
[root@k8s-master ~]# kubectl create -f service-hello.yaml
service/service-hello created
[root@k8s-master ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h24m
service-hello NodePort 10.98.166.242 <none> 80:31111/TCP 42s
[root@k8s-master ~]#
root@k8s-master ~]# kubectl get services -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h25m <none>
service-hello NodePort 10.98.166.242 <none> 80:31111/TCP 104s run=hello
[root@k8s-master ~]# kubectl describe service service-hello
Name: service-hello
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=hello
Type: NodePort
IP: 10.98.166.242
Port: <unset> 80/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31111/TCP
Endpoints: 10.244.1.22:8080,10.244.1.23:8080,10.244.1.24:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master ~]#
service创建后,自动生成endpoints,每个pod ip都在里面,简单认为:动态存储pod名字与pod ip对应关系的list,并提供将请求转发到实际pod上的能力 kubernets之endpoints
[root@k8s-master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.111.130:6443 20h
service-hello 10.244.1.22:8080,10.244.1.23:8080,10.244.1.24:8080 + 1 more... 15h
[root@k8s-master ~]# kubectl describe endpoint service-hello
error: the server doesn't have a resource type "endpoint"
[root@k8s-master ~]# kubectl describe endpoints service-hello
Name: service-hello
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2019-04-03T02:18:57Z
Subsets:
Addresses: 10.244.1.22,10.244.1.23,10.244.1.24,10.244.1.25
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
<unset> 8080 TCP Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedToUpdateEndpoint 48m (x2 over 69m) endpoint-controller Failed to update endpoint default/service-hello: Operation cannot be fulfilled on endpoints "service-hello": the object has been modified; please apply your changes to the latest version and try again
[root@k8s-master ~]#
4.验证发布
http://192.168.111.130:31111/ 或者http://192.168.111.131:31111/
kubernetes发布tomcat服务,通过deployment,service布署(转)的更多相关文章
- kubernetes发布tomcat服务,通过deployment,service布署
1.制作tomcat镜像 参考docker tomcat镜像制作 此处直接拉取 查看已有可镜像 先设置docker阿里源,即添加 "registry-mirrors": [&quo ...
- 使用GeoServer+QGIS发布WMTS服务 | Publishing WMTS Service Using GeoServer+QGIS
Web GIS系列: 1.搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 2.使用GeoServer+QGIS发布WMTS服务 3.使 ...
- 第一篇【Zabbix服务端的完整布署】
1.环境准备 服务器版本: [root@filestore-v2 ~]# cat /etc/redhat-release CentOS Linux release (Core) 内核版本: [root ...
- WCF开发实战系列四:使用Windows服务发布WCF服务
WCF开发实战系列四:使用Windows服务发布WCF服务 (原创:灰灰虫的家http://hi.baidu.com/grayworm) 上一篇文章中我们通过编写的控制台程序或WinForm程序来为本 ...
- Openstack+Kubernetes+Docker微服务实践之路--服务发布
结合上文,我们的服务已经可以正常运行了,但它的访问方式只能通过服务器IP加上端口来访问,如何通过域名的方式来访问到我们服务,本来想使用Kubernetes的Ingress来做,折腾一天感觉比较麻烦,I ...
- 详解k8s零停机滚动发布微服务 - kubernetes
1.前言 在当下微服务架构盛行的时代,用户希望应用程序时时刻刻都是可用,为了满足不断变化的新业务,需要不断升级更新应用程序,有时可能需要频繁的发布版本.实现"零停机"." ...
- 一文看懂 Kubernetes 服务发现: Service
Service 简介 K8s 中提供微服务的实体是 Pod,Pod 在创建时 docker engine 会为 pod 分配 ip,"外部"流量通过访问该 ip 获取微服务.但 ...
- 无service.bat的tomcat服务怎么设置自启动
在正式环境中,经常需要设置tomcat自启动,这样在重启系统服务器后就不需要再手动去开启tomcat服务器了.通过设置tomcat下的service.bat可以实现自启动的目的,但有时候会发现自己的t ...
- Windows下使用service.bat安装tomcat服务, 启动停止tomcat服务
在项目开发过程中,以前只是在Eclipse中配置.启动.停止tomcat服务器 如果只想在机器中使用tomcat服务器,而不想安装MyEclipse,可以使用service.bat 将tomcat安装 ...
随机推荐
- python并发编程之线程(二):死锁和递归锁&信号量&定时器&线程queue&事件evevt
一 死锁现象与递归锁 进程也有死锁与递归锁,在进程那里忘记说了,放到这里一切说了额 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将 ...
- 【转】char data[0]用法总结
@2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...
- 车钥匙开关上找不到+24V的问题 - 岱峰 - DGY90
背景: 本人外行,用万用表,在车身电路上查找电瓶正极. 机种:吊管机:机型:岱峰-DGY90 过程: 经过测试,车钥匙开关各连接点电压: 标记B - OFF时电压0,ON时电压+25V 标记BR - ...
- mybatis详解(三)
一,动态sql,where,trim,set和foreach parameterType的属性可以不用写 xml文件sql的书写 <select id="queryByParams&q ...
- TCP/IP协议簇 端口 三次握手 四次挥手 11种状态集
第一章:概念介绍 1.1 VLAN 1.1.1 什么是VLAN VLAN (Virturl LAN) ,翻译成中文是:“虚拟局域网”.VLAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计 ...
- IE的F12开发人员工具不显示 转载自:http://blog.csdn.net/longyulu/article/details/8749705
IE的F12开发人员工具不显示问题: 按下F12之后,开发人员工具在桌面上看不到,但是任务栏里有显示.将鼠标放在任务栏的开发人员工具上,出现一片透明的区域,选中之后却出不来.将鼠标移动到开发人员工具的 ...
- InheritableThreadLocal——父线程传递本地变量到子线程的解决方式及分析
转自https://blog.csdn.net/hewenbo111/article/details/80487252 上一个博客提到ThreadLocal变量的基本使用方式,可以看出ThreadLo ...
- codeblock的一个小问题
也许不叫问题吧,也可能是编译器的特性. 我的codeblock选择的编译器: 编写.cpp文件时,floatl类型输入输出都是 %f,double类型输入只能是 %lf,输出只能是 %f. 但是在co ...
- codeforces1209E2 状压dp,枚举子集
题意 给一个n行m列的矩阵,每一列可以循环移位,问经过任意次移位后每一行的最大值总和最大为多少. 分析 每一行的最大值之和最大,可以理解为每一行任意选择一个数使它们的和最大. 设\(dp[i][S]\ ...
- springboot 生产环境与开发环境配置
通过修改yml文件里的active属性,prod(生产环境) 与 dev (开发环境)