K8s之traefik(ingess)发布服务实战
成都创新互联公司服务项目包括前郭网站建设、前郭网站制作、前郭网页制作以及前郭网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,前郭网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到前郭省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!上篇描述了基于k8s集群安装部署traefik作为ingress服务,简单演示了一下发布服务,本篇将细节讲述如果结合traefik发布业务服务。
安装部署参考:
https://blog.51cto.com/michaelkang/2429929
版本介绍
traefik:v1.7
k8s:v1.15.1
快速部署traefik
如果你的k8s集群已经部署完成,想快速部署traefik,执行命令如下:
kubectl create -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
kubectl create -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml
执行成功,在浏览器数据一个node节点的IP地址:8080 端口即可看到traefik的管理页面。
实战开始
编写一个业务yaml文件
如果对yaml不太熟悉,看这边:https://blog.51cto.com/michaelkang/2429745
---
#配置deployment
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
#设置dm名称
name: dm-pttest
#添加标签 pttest
labels:
app: pttest
spec:
# 通过replicas声明pod个数是2
replicas: 2
# 通过标签选择被控制的pod
selector:
matchLabels:
app: pttest
# 在template中定义pod
template:
metadata:
# 给pod打上标签app=pttest
labels:
app: pttest
spec:
containers:
# 声明容器名称,注意不是pod名称,pod名称应该定义在metadata中
- name: myapp
image: ikubernetes/myapp:v1
ports:
- containerPort: 80
---
#服务配置
apiVersion: v1
# 声明一个Service资源对象
kind: Service
metadata:
#pod 名称
name: svcpttest
labels:
app: pttest
annotations:
#检查后端服务返回错误率,大于%50,踢出集群
traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5"
spec:
ports:
- name: http
port: 80
selector:
app: pttest
---
#配置ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myapp-traefik-ingress
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: pttest.pt.com
http:
paths:
- path: /
#指定后端服务
backend:
serviceName: svcpttest
servicePort: 80
验证服务
#查看服务
[root@kubm-02 traefik]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svcpttest ClusterIP 10.245.148.114 80/TCP 2m12s app=pttest <==lable
#查看container
[root@kubm-02 traefik]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dm-pttest-6f6cd797f5-cdjk5 1/1 Running 0 63s 10.244.3.115 kubnode-01
dm-pttest-6f6cd797f5-tqh64 1/1 Running 0 63s 10.244.4.135 kubnode-02
#ingress 信息
[root@kubm-02 traefik]# kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
myapp-traefik-ingress pttest.pt.com 80 23s
#deployment
[root@kubm-02 traefik]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
dm-pttest 2/2 2 2 12m
验证访问:
请求pttest.pt.com, 服务svcpttest会把请求负载给后端的pod。
#请求两次分别返回两个
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-cdjk5
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tqh64
扩容节点
kubectl edit deployment dm-pttest
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2019-08-16T10:13:49Z"
generation: 1
labels:
app: pttest
name: dm-pttest
namespace: default
resourceVersion: "3871589"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/dm-pttest
uid: 153a60fb-66df-4884-aa9a-49ac47cebd2c
spec:
progressDeadlineSeconds: 2147483647
replicas: 5 《=====pod数量,有默认2个,调整到5个
保存退出,k8s集群会自动执行;
验证
kubectl get pods
[root@kubm-02 traefik]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dm-pttest-6f6cd797f5-cdjk5 1/1 Running 0 17m
dm-pttest-6f6cd797f5-qd4cl 1/1 Running 0 99s
dm-pttest-6f6cd797f5-r25dc 1/1 Running 0 99s
dm-pttest-6f6cd797f5-tbfmh 1/1 Running 0 99s
dm-pttest-6f6cd797f5-tqh64 1/1 Running 0 17m
#请求
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-r25dc
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tbfmh
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tqh64
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-cdjk5
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-qd4cl
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-r25dc
[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tbfmh
使用命令调整pod数量
调整pod数量为1;
kubectl scale deployment dm-pttest --replicas=1
调整pod数量为2;
kubectl scale deployment dm-pttest --replicas=2
参考文档
https://kubernetes.io/zh/docs/concepts/services-networking/connect-applications-service/
https://blog.csdn.net/u010606397/article/details/90752262
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:K8s之traefik(ingess)发布服务-实战-创新互联
URL标题:http://scpingwu.com/article/dcidgp.html