(六)kubernetes部署应用service方式
k8s部署应用外网可访问,应用service方式部署应用
·
前几章验证的是部署以及内部访问,但是事实上很多情况都需使用外部访问,比如部署nacos,数据库等等。我是在搭建prometheus监控时候遇到无法访问,固想起还有外部访问方式,今天就验证一下外部可访问的部署方式service。
service是k8s中的一个重要概念,主要是提供负载均衡和服务自动发现
因为Pod IP会随着Pod的重建产生变化,可以使用Service提供的ip
1:编辑yaml文件
[root@master00 md-order]# vim md-order-03.yaml
apiVersion: apps/v1 # K8S对应的API版本
kind: Deployment # 对应的类型
metadata:
name: caoqing-deployment-test
#labels:
# name: cqoqing-deployment-test01
spec:
selector:
matchLabels:
app: caoqing-deployment-test
replicas: 2 # 镜像副本数量
template:
metadata:
labels: # 容器的标签 可和service关联
app: caoqing-deployment-test
spec:
containers:
- name: md-order9006 # 容器名和镜像
image: registry.cn-beijing.aliyuncs.com/fastdfcsx/md-order-test:2022-10-xx-xx-33-54
imagePullPolicy: Always
ports:
- name: md-order9006
containerPort: 9006
protocol: TCP
2:创建pod
[root@master00 md-order]# kubectl create -f md-order-03.yaml
deployment.apps/caoqing-deployment-test created
[root@master00 md-order]#
[root@master00 md-order]#
[root@master00 md-order]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
caoqing-deployment-test-689bbd9587-67f7n 1/1 Running 0 7s 10.111.112.135 master01 <none> <none>
caoqing-deployment-test-689bbd9587-cthlp 1/1 Running 0 7s 10.100.59.203 master02 <none> <none>
3:创建service
[root@master00 md-order]# kubectl expose deploy caoqing-deployment-test --name=cq-service01 --type=ClusterIP --target-port=9006
service/cq-service01 exposed
[root@master00 md-order]#
[root@master00 md-order]#
[root@master00 md-order]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cq-service01 ClusterIP 10.101.187.154 <none> 9006/TCP 13s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 33d
[root@master00 md-order]#
[root@master00 md-order]#
[root@master00 md-order]# kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
cq-service01 ClusterIP 10.101.187.154 <none> 9006/TCP 35s app=caoqing-deployment-test
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 33d <none>
4:以上方式无法外部访问,我们可以外部访问方式 创建,讲type改为nodeport
[root@master00 md-order]# kubectl expose deploy caoqing-deployment-test --name=cq-service02 --type=NodePort --target-port=9006
service/cq-service02 exposed
[root@master00 md-order]#
4:查看该service详细内容,默认暴露31183端口
[root@master00 md-order]# kubectl get pods,service
NAME READY STATUS RESTARTS AGE
pod/caoqing-deployment-test-689bbd9587-blrcn 1/1 Running 0 4m36s
pod/caoqing-deployment-test-689bbd9587-wwc77 1/1 Running 0 4m36s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cq-service01 ClusterIP 10.101.187.154 <none> 9006/TCP 2m27s
service/cq-service02 NodePort 10.107.171.238 <none> 9006:31183/TCP 26s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 33d
5:验证端口是否可以访问
[root@master00 md-order]# curl 192.168.0.250:31183/doc.html #拉取镜像开发swagger,可以直接访问,ip地址可以是集群内的任意一个ip
参考学习大神文章:链接
更多推荐
已为社区贡献3条内容
所有评论(0)