k8s中的微服务

目录

一、什么是微服务

二、微服务的类型

三、IPVS模式

1、ipvs模式配置方式

(1)在所有节点中安装ipvsadm

(2)修改master节点的代理配置

(3)重启pod

四、微服务类型详解

1、clusterip

示例:

2、clusterIP中的特殊模式headless

3、nodeport

4、loadbalancer

5、metalLB

6、externalname

五、Ingress-nginx

1、ingress-nginx功能

2、部署ingress

(1)下载/上传部署文件

(2)测试ingress

3、ingress的高级用法

(1)实验环境

(2)基于路径的访问

(3)基于域名的访问

(4)建立tls加密

(5)建立auth认证

(6)rewrite重定向

六、Canary金丝雀发布

1、概念

2、Canary发布

(1)基于header(http包头)灰度

(2)基于权重的灰度发布


一、什么是微服务

用控制器来完成集群的工作负载,那么应用如何暴漏出去?需要通过微服务暴漏出去后才能被访问

  • Service是一组提供相同服务的Pod对外开放的接口。

  • 借助Service,应用可以实现服务发现和负载均衡。

  • service默认只支持4层负载均衡能力,没有7层功能。(可以通过Ingress实现)

二、微服务的类型

微服务类型作用描述
ClusterIP默认值,k8s系统给service自动分配的虚拟IP,只能在集群内部访问
NodePort将Service通过指定的Node上的端口暴露给外部,访问任意一个NodeIP:nodePort都将路由到ClusterIP
LoadBalancer在NodePort的基础上,借助cloud provider创建一个外部的负载均衡器,并将请求转发到 NodeIP:NodePort,此模式只能在云服务器上使用
ExternalName将服务通过 DNS CNAME 记录方式转发到指定的域名(通过 spec.externlName 设定)

示例 

#生成控制器文件并建立控制器
[root@k8s-master ~]# kubectl create deployment ws --image reg.zx.org/library/myapp:v1 --replicas 2 --dry-run=client -o yaml > ws.yml
[root@k8s-master ~]# kubectl apply -f ws.yml 
deployment.apps/ws created#生成微服务yaml追加到已有yaml中
[root@k8s-master ~]# kubectl expose deployment ws --port 80 --target-port 80 --dry-run=client -o yaml >> ws.yml
[root@k8s-master ~]# vim ws.yml         #不同资源间用---隔开
[root@k8s-master ~]# cat ws.yml 
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: wsname: ws
spec:replicas: 2selector:matchLabels:app: wsstrategy: {}template:metadata:creationTimestamp: nulllabels:app: wsspec:containers:- image: reg.zx.org/library/myapp:v1name: myappresources: {}---apiVersion: v1
kind: Service
metadata:creationTimestamp: nulllabels:app: wsname: ws
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:app: ws[root@k8s-master ~]# kubectl apply -f ws.yml 
deployment.apps/ws configured
service/ws created
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
deployment   ClusterIP   10.100.147.145   <none>        80/TCP    13h
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   38h
readiness    ClusterIP   10.101.55.183    <none>        80/TCP    15h
ws           ClusterIP   10.98.35.35      <none>        80/TCP    9s[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE   SELECTOR
deployment   ClusterIP   10.100.147.145   <none>        80/TCP    13h   app=myapp
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   38h   <none>
readiness    ClusterIP   10.101.55.183    <none>        80/TCP    15h   run=readiness
ws           ClusterIP   10.98.35.35      <none>        80/TCP    67s   app=ws[root@k8s-master ~]# iptables -t nat -nL
……
Chain KUBE-SERVICES (2 references)
target     prot opt source               destination         
KUBE-SVC-NPX46M4PTMTKRN6Y  tcp  --  0.0.0.0/0            10.96.0.1            /* default/kubernetes:https cluster IP */ tcp dpt:443
KUBE-SVC-6GEDBFKJV7TTAFN7  tcp  --  0.0.0.0/0            10.98.35.35          /* default/ws cluster IP */ tcp dpt:80
KUBE-NODEPORTS  all  --  0.0.0.0/0            0.0.0.0/0            /* kubernetes service nodeports; NOTE: this must be the last rule in this chain */ ADDRTYPE match dst-type LOCAL
……
[root@k8s-master ~]# vim ws.yml         # 添加:“type: NodePort”
[root@k8s-master ~]# cat ws.yml 
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: wsname: ws
spec:replicas: 2selector:matchLabels:app: wsstrategy: {}template:metadata:creationTimestamp: nulllabels:app: wsspec:containers:- image: reg.zx.org/library/myapp:v1name: myappresources: {}---apiVersion: v1
kind: Service
metadata:creationTimestamp: nulllabels:app: wsname: ws
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:app: wstype: NodePort[root@k8s-master ~]# kubectl apply -f ws.yml 
deployment.apps/ws configured
service/ws configured[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
deployment   ClusterIP   10.100.147.145   <none>        80/TCP         13h
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        38h
readiness    ClusterIP   10.101.55.183    <none>        80/TCP         15h
ws           NodePort    10.98.35.35      <none>        80:31381/TCP   4m51s

三、IPVS模式

  • Service 是由 kube-proxy 组件,加上 iptables 来共同实现的

  • kube-proxy 通过 iptables 处理 Service 的过程,需要在宿主机上设置相当多的 iptables 规则,如果宿主机有大量的Pod,不断刷新iptables规则,会消耗大量的CPU资源

  • IPVS模式的service,可以使K8s集群支持更多量级的Pod

1、ipvs模式配置方式

切换ipvs模式后,kube-proxy会在宿主机上添加一个虚拟网卡:kube-ipvs0,并分配所有service IP

[root@k8s-master ~]# ip a | tail -n 20
8: veth411bce96@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default link/ether 92:64:68:74:60:d6 brd ff:ff:ff:ff:ff:ff link-netnsid 2inet6 fe80::9064:68ff:fe74:60d6/64 scope link valid_lft forever preferred_lft forever
9: vethd4d7ef26@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default link/ether 66:5a:fa:34:d8:02 brd ff:ff:ff:ff:ff:ff link-netnsid 3inet6 fe80::645a:faff:fe34:d802/64 scope link valid_lft forever preferred_lft forever
11: kube-ipvs0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default link/ether 7a:63:54:5e:87:51 brd ff:ff:ff:ff:ff:ffinet 10.96.186.254/32 scope global kube-ipvs0valid_lft forever preferred_lft foreverinet 10.96.0.10/32 scope global kube-ipvs0valid_lft forever preferred_lft foreverinet 10.100.147.145/32 scope global kube-ipvs0valid_lft forever preferred_lft foreverinet 10.96.0.1/32 scope global kube-ipvs0valid_lft forever preferred_lft foreverinet 10.101.55.183/32 scope global kube-ipvs0valid_lft forever preferred_lft forever

(1)在所有节点中安装ipvsadm

yum install ipvsadm -y

(2)修改master节点的代理配置

[root@k8s-master ~]# kubectl -n kube-system edit cm kube-proxymetricsBindAddress: ""mode: "ipvs"							#设置kube-proxy使用ipvs模式nftables:

(3)重启pod

在pod运行时配置文件中采用默认配置,当改变配置文件后已经运行的pod状态不会变化,所以要重启pod

[root@k8s-master ~]# kubectl -n kube-system get  pods   | awk '/kube-proxy/{system("kubectl -n kube-system delete pods "$1)}'
pod "kube-proxy-9ftsb" deleted
pod "kube-proxy-chthh" deleted
pod "kube-proxy-dzcxh" deleted[root@k8s-master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.254.200:32752 rr-> 10.244.1.65:80               Masq    1      0          0         -> 10.244.2.70:80               Masq    1      0          0         
TCP  10.96.0.1:443 rr-> 172.25.254.200:6443          Masq    1      0          0         
TCP  10.96.0.10:53 rr
TCP  10.96.0.10:9153 rr
TCP  10.96.186.254:80 rr-> 10.244.1.65:80               Masq    1      0          0         -> 10.244.2.70:80               Masq    1      0          0         
TCP  10.100.147.145:80 rr
TCP  10.101.55.183:80 rr
TCP  10.244.0.1:32752 rr-> 10.244.1.65:80               Masq    1      0          0         -> 10.244.2.70:80               Masq    1      0          0   # 回收
[root@k8s-master ~]# kubectl delete -f ws.yml
deployment.apps "ws" deleted
service "ws" deleted

四、微服务类型详解

1、clusterip

特点:clusterip模式只能在集群内访问,并对集群内的pod提供健康检测和自动发现功能

示例

[root@k8s-master ~]# kubectl run testpod --image reg.zx.org/library/myapp:v1 
pod/testpod created
[root@k8s-master ~]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
testpod   1/1     Running   0          9s
[root@k8s-master ~]# kubectl get pods -o wide --show-labels 
NAME      READY   STATUS    RESTARTS   AGE   IP            NODE               NOMINATED NODE   READINESS GATES   LABELS
testpod   1/1     Running   0          64s   10.244.1.66   k8s-node1.zx.org   <none>           <none>            run=testpod
[root@k8s-master ~]# kubectl expose pod testpod --port 80 --target-port 80 --dry-run=client -o yaml > testpod-svc.yml
[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# cat testpod-svc.yml 
apiVersion: v1
kind: Service
metadata:labels:run: testpodname: testpod
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: testpodtype: ClusterIP[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod created[root@k8s-master ~]# kubectl describe svc testpod 
Name:              testpod
Namespace:         default
Labels:            run=testpod
Annotations:       <none>
Selector:          run=testpod
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.105.248.212
IPs:               10.105.248.212
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.66:80
Session Affinity:  None
Events:            <none>
[root@k8s-master ~]# 
[root@k8s-master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr-> 172.25.254.200:6443          Masq    1      0          0         
TCP  10.96.0.10:53 rr
TCP  10.96.0.10:9153 rr
TCP  10.100.147.145:80 rr
TCP  10.101.55.183:80 rr
TCP  10.105.248.212:80 rr-> 10.244.1.66:80               Masq    1      0          0         
UDP  10.96.0.10:53 rr
[root@k8s-master ~]# kubectl run testpod1 --image reg.zx.org/library/myapp:v1 [root@k8s-master ~]# kubectl get pods -o wide --show-labels     # 两者标签不一致
NAME       READY   STATUS    RESTARTS   AGE    IP            NODE               NOMINATED NODE   READINESS GATES   LABELS
testpod    1/1     Running   0          7m8s   10.244.1.66   k8s-node1.zx.org   <none>           <none>            run=testpod
testpod1   1/1     Running   0          10s    10.244.2.72   k8s-node2.zx.org   <none>           <none>            run=testpod1
[root@k8s-master ~]# ipvsadm -Ln        # testpod1未在testpod调度里
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr-> 172.25.254.200:6443          Masq    1      0          0         
TCP  10.96.0.10:53 rr
TCP  10.96.0.10:9153 rr
TCP  10.100.147.145:80 rr
TCP  10.101.55.183:80 rr
TCP  10.105.248.212:80 rr-> 10.244.1.66:80               Masq    1      0          0         
UDP  10.96.0.10:53 rr
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl label pod testpod1 run=testpod --overwrite     # 修改标签为tetpod
pod/testpod1 labeled
[root@k8s-master ~]# kubectl get pods -o wide --show-labels     # 标签一致
NAME       READY   STATUS    RESTARTS   AGE     IP            NODE               NOMINATED NODE   READINESS GATES   LABELS
testpod    1/1     Running   0          7m43s   10.244.1.66   k8s-node1.zx.org   <none>           <none>            run=testpod
testpod1   1/1     Running   0          45s     10.244.2.72   k8s-node2.zx.org   <none>           <none>            run=testpod
[root@k8s-master ~]# ipvsadm -Ln        
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr-> 172.25.254.200:6443          Masq    1      0          0         
TCP  10.96.0.10:53 rr
TCP  10.96.0.10:9153 rr
TCP  10.100.147.145:80 rr
TCP  10.101.55.183:80 rr
TCP  10.105.248.212:80 rr            # 添加-> 10.244.1.66:80               Masq    1      0          0         -> 10.244.2.72:80               Masq    1      0          0         
UDP  10.96.0.10:53 rr

[root@k8s-master ~]# kubectl -n default get svc 
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP   30m
testpod      ClusterIP   10.102.121.5   <none>        80/TCP    25s[root@k8s-master ~]#  dig testpod.default.svc.cluster.local. @10.96.0.10; <<>> DiG 9.16.23-RH <<>> testpod.default.svc.cluster.local. @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37665
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 56102b725cf1025e (echoed)
;; QUESTION SECTION:
;testpod.default.svc.cluster.local. IN	A;; ANSWER SECTION:
testpod.default.svc.cluster.local. 30 IN A	10.102.121.5;; Query time: 136 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Mon Sep 16 11:15:09 CST 2024
;; MSG SIZE  rcvd: 123# 回收
[root@k8s-master ~]# kubectl delete -f testpod-svc.yml 
service "testpod" deleted

2、clusterIP中的特殊模式headless

headless(无头服务)

对于无头 Services 并不会分配 Cluster IP,kube-proxy不会处理它们, 而且平台也不会为它们进行负载均衡和路由,集群访问通过dns解析直接指向到业务pod上的IP,所有的调度有dns单独完成

[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# cat testpod-svc.yml 
apiVersion: v1
kind: Service
metadata:labels:run: testpodname: testpod
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: testpodtype: ClusterIPclusterIP: None[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod created
[root@k8s-master ~]# kubectl get service testpod 
NAME      TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
testpod   ClusterIP   None         <none>        80/TCP    10s[root@k8s-master ~]# kubectl describe svc testpod 
Name:              testpod
Namespace:         default
Labels:            run=testpod
Annotations:       <none>
Selector:          run=testpod
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                None
IPs:               None
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.66:80,10.244.2.72:80    ##直接解析到pod上
Session Affinity:  None
Events:            <none>#开启一个busyboxplus的pod测试
[root@k8s-master ~]# kubectl run  test --image reg.zx.org/library/busyboxplus:latest -it
If you don't see a command prompt, try pressing enter.
/ # nslookup testpod

3、nodeport

通过ipvs暴漏端口从而使外部主机通过master节点的对外ip:<port>来访问pod业务

其访问过程为:

[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod created
[root@k8s-master ~]# kubectl get service testpod 
NAME      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
testpod   NodePort   10.102.166.250   <none>        80:32171/TCP   12s
[root@k8s-master ~]# for i in {1..5}
> do
> curl 172.25.254.200:32171/hostname.html
> done

nodeport默认端口是30000-32767,超出会报错

[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
The Service "testpod" is invalid: spec.ports[0].nodePort: Invalid value: 33333: provided port is not in the valid range. The range of valid ports is 30000-32767

如果需要使用这个范围以外的端口就需要特殊设定

[root@k8s-master ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml

添加“--service-node-port-range=“ 参数,端口范围可以自定义

修改后api-server会自动重启,等apiserver正常启动后才能操作集群

集群重启自动完成在修改完参数后全程不需要人为干预

[root@k8s-master ~]# kubectl get nodes
The connection to the server 172.25.254.200:6443 was refused - did you specify the right host or port?
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get nodes
The connection to the server 172.25.254.200:6443 was refused - did you specify the right host or port?
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get nodes
NAME                STATUS   ROLES           AGE   VERSION
k8s-master.zx.org   Ready    control-plane   40h   v1.30.0
k8s-node1.zx.org    Ready    <none>          40h   v1.30.0
k8s-node2.zx.org    Ready    <none>          40h   v1.30.0
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod configured
[root@k8s-master ~]# kubectl get service testpod 
NAME      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
testpod   NodePort   10.102.166.250   <none>        80:33333/TCP   10m

4、loadbalancer

云平台会为我们分配vip并实现访问,如果是裸金属主机那么需要metallb来实现ip的分配

LoadBalancer模式适用云平台,裸金属环境需要安装metallb提供支持

[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# cat testpod-svc.yml 
apiVersion: v1
kind: Service
metadata:labels:run: testpodname: testpod
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: testpodtype: LoadBalancer
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod configured
# 默认无法分配外部访问IP
[root@k8s-master ~]# kubectl get svc
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
deployment   ClusterIP      10.100.147.145   <none>        80/TCP         34h
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        2d11h
readiness    ClusterIP      10.101.55.183    <none>        80/TCP         35h
testpod      LoadBalancer   10.102.166.250   <pending>     80:33333/TCP   19h

5、metalLB

功能——为LoadBalancer分配vip

# 设置ipvs模式
[root@k8s-master ~]# kubectl edit cm -n kube-system kube-proxy
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:strictARP: true[root@k8s-master ~]# kubectl -n kube-system get  pods   | awk '/kube-proxy/{system("kubectl -n kube-system delete pods "$1)}'
pod "kube-proxy-27gds" deleted
pod "kube-proxy-f7tzt" deleted
pod "kube-proxy-kkt58" deleted
[root@k8s-master ~]# [root@k8s-master ~]# mkdir metalLB
[root@k8s-master ~]# cd metalLB/
[root@k8s-master metalLB]# ls
metallb-native.yaml  metalLB.tag.gz
[root@k8s-master metalLB]# vim metallb-native.yaml 
[root@k8s-master metalLB]# docker load -i metalLB.tag.gz 
……
Loaded image: quay.io/metallb/controller:v0.14.8
……
Loaded image: quay.io/metallb/speaker:v0.14.8[root@k8s-master metalLB]# docker tag quay.io/metallb/controller:v0.14.8 reg.zx.org/metallb/quay.io/metallb/controller:v0.14.8
[root@k8s-master metalLB]# docker push reg.zx.org/metallb/quay.io/metallb/controller:v0.14.8[root@k8s-master metalLB]# docker tag quay.io/metallb/speaker:v0.14.8 reg.zx.org/metallb/quay.io/metallb/speaker:v0.14.8
[root@k8s-master metalLB]# docker push reg.zx.org/metallb/quay.io/metallb/speaker:v0.14.8[root@k8s-master metalLB]# vim metallb-native.yaml     # 修改文件中镜像地址,与harbor仓库路径保持一致(两处)
# 部署服务
[root@k8s-master metalLB]# kubectl apply -f metallb-native.yaml 
[root@k8s-master metalLB]# kubectl get namespaces 
NAME              STATUS   AGE
default           Active   2d11h
kube-flannel      Active   2d11h
kube-node-lease   Active   2d11h
kube-public       Active   2d11h
kube-system       Active   2d11h
metallb-system    Active   44s[root@k8s-master metalLB]# kubectl -n metallb-system get all
NAME                              READY   STATUS    RESTARTS   AGE
pod/controller-566fd4c654-mkkrc   1/1     Running   0          39s
pod/speaker-bbjt2                 1/1     Running   0          37s
pod/speaker-j9nvx                 1/1     Running   0          37s
pod/speaker-ljtgz                 1/1     Running   0          37s# 配置分配地址段
[root@k8s-master metalLB]# vim configmap.yml
[root@k8s-master metalLB]# cat configmap.yml 
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:name: first-pool                #地址池名称namespace: metallb-system
spec:addresses:- 172.25.254.50-172.25.254.99    #修改为自己本地地址段---                                #两个不同的kind中间必须加分割
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:name: examplenamespace: metallb-system
spec:ipAddressPools:- first-pool                    #使用地址池 [root@k8s-master metalLB]# kubectl apply -f configmap.yml

# 接上 ,解决loadbalancer无外部IP的问题
[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod created
[root@k8s-master ~]# kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>          443/TCP        16m
testpod      LoadBalancer   10.97.210.224   172.25.254.50   80:32231/TCP   6s
[root@k8s-master ~]# cat testpod-svc.yml 
apiVersion: v1
kind: Service
metadata:labels:run: testpodname: testpod
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: testpodtype: LoadBalancer

6、externalname

  • 开启services后,不会被分配IP,而是用dns解析CNAME固定域名来解决ip变化问题

  • 一般应用于外部业务和pod沟通或外部业务迁移到pod内时

  • 在应用向集群迁移过程中,externalname在过度阶段就可以起作用了。

  • 集群外的资源迁移到集群时,在迁移的过程中ip可能会变化,但是域名+dns解析能完美解决此问题

[root@k8s-master ~]# vim testpod-svc.yml 
[root@k8s-master ~]# kubectl apply -f testpod-svc.yml 
service/testpod configured
[root@k8s-master ~]# kubectl get service testpod 
NAME      TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)   AGE
testpod   ExternalName   <none>       www.baidu.com   80/TCP    7m2s
[root@k8s-master ~]# cat testpod-svc.yml 
apiVersion: v1
kind: Service
metadata:labels:run: testpodname: testpod
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:run: testpodtype: ExternalNameexternalName: www.baidu.com

五、Ingress-nginx

1、ingress-nginx功能

  • 一种全局的、为了代理不同后端 Service 而设置的负载均衡服务,支持7层

  • Ingress由两部分组成:Ingress controllerIngress服务

  • Ingress Controller 会根据你定义的 Ingress 对象,提供对应的代理能力。

  • 业界常用的各种反向代理项目,比如 Nginx、HAProxy、Envoy、Traefik 等,都已经为Kubernetes 专门维护了对应的 Ingress Controller。

2、部署ingress

(1)下载/上传部署文件

[root@k8s-master ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.2/deploy/static/provider/baremetal/deploy.yaml
[root@k8s-master ~]# docker load -i ingress-nginx-1.11.2.tar.gz 
Loaded image: reg.harbor.org/ingress-nginx/controller:v1.11.2
Loaded image: reg.harbor.org/ingress-nginx/kube-webhook-certgen:v1.4.3[root@k8s-master ~]# docker tag reg.harbor.org/ingress-nginx/controller:v1.11.2 reg.zx.org/ingress-nginx/controller:v1.11.2[root@k8s-master ~]# docker tag reg.harbor.org/ingress-nginx/kube-webhook-certgen:v1.4.3 reg.zx.org/ingress-nginx/kube-webhook-certgen:v1.4.3[root@k8s-master ~]# docker push reg.zx.org/ingress-nginx/controller:v1.11.2
[root@k8s-master ~]# docker push reg.zx.org/ingress-nginx/kube-webhook-certgen:v1.4.3[root@k8s-master ~]# vim deploy.yaml    # 修改镜像位置(三处)image: reg.zx.org/ingress-nginx/controller:v1.11.2image: reg.zx.org/ingress-nginx/kube-webhook-certgen:v1.4.3image: reg.zx.org/ingress-nginx/kube-webhook-certgen:v1.4.3
[root@k8s-master ~]# kubectl apply -f deploy.yaml 
[root@k8s-master ~]# kubectl get namespaces 

 

(2)测试ingress

[root@k8s-master ~]# kubectl create deployment myapp1 --image reg.zx.org/library/myapp:v1 --dry-run=client -o yaml > myapp-v1.yml
[root@k8s-master ~]# vim myapp-v1.yml 
[root@k8s-master ~]# cp myapp-v1.yml myapp-v2.yml
[root@k8s-master ~]# vim myapp-v2.yml
[root@k8s-master ~]# kubectl apply -f myapp-v1.yml 
deployment.apps/myapp1 created
[root@k8s-master ~]# kubectl apply -f myapp-v2.yml 
deployment.apps/myapp2 created
[root@k8s-master ~]# kubectl expose deployment myapp1 --port 80 --target-port 80 --dry-run=client -o yaml >> myapp-v1.yml 
[root@k8s-master ~]# kubectl expose deployment myapp2 --port 80 --target-port 80 --dry-run=client -o yaml >> myapp-v2.yml 
[root@k8s-master ~]# vim myapp-v1.yml 
[root@k8s-master ~]# vim myapp-v2.yml 
[root@k8s-master ~]# kubectl apply -f myapp-v1.yml 
deployment.apps/myapp1 configured
service/myapp1 created
[root@k8s-master ~]# kubectl apply -f myapp-v2.yml 
deployment.apps/myapp2 configured
service/myapp2 created
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   41m
myapp1       ClusterIP   10.99.71.198     <none>        80/TCP    14s
myapp2       ClusterIP   10.101.156.212   <none>        80/TCP    8s
[root@k8s-master ~]# curl 10.99.71.198
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.101.156.212
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>[root@k8s-master ~]# cat myapp-v1.yml 
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: myapp1name: myapp1
spec:replicas: 1selector:matchLabels:app: myapp1template:metadata:creationTimestamp: nulllabels:app: myapp1spec:containers:- image: reg.zx.org/library/myapp:v1name: myapp
---
apiVersion: v1
kind: Service
metadata:creationTimestamp: nulllabels:app: myapp1name: myapp1
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:app: myapp1[root@k8s-master ~]# cat myapp-v2.yml 
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: myapp2name: myapp2
spec:replicas: 1selector:matchLabels:app: myapp2template:metadata:creationTimestamp: nulllabels:app: myapp2spec:containers:- image: reg.zx.org/library/myapp:v2name: myapp
---
apiVersion: v1
kind: Service
metadata:creationTimestamp: nulllabels:app: myapp2name: myapp2
spec:ports:- port: 80protocol: TCPtargetPort: 80selector:app: myapp2
[root@k8s-master ~]# kubectl -n ingress-nginx get svc
NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.109.170.26    <none>        80:31012/TCP,443:30324/TCP   4m18s
ingress-nginx-controller-admission   ClusterIP   10.103.195.107   <none>        443/TCP                      4m17s[root@k8s-master ~]# kubectl create ingress myapp1 --class nginx --rule="/=myapp1:80" --dry-run=client -o yaml > ingress1.yml
[root@k8s-master ~]# vim ingress1.yml 
[root@k8s-master ~]# kubectl -n ingress-nginx get all 
[root@k8s-master ~]# kubectl -n ingress-nginx get all
NAME                                            READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-79c2p        0/1     Completed   0          7m57s
pod/ingress-nginx-admission-patch-mghg6         0/1     Completed   1          7m57s
pod/ingress-nginx-controller-6db9bc976b-ml8lg   1/1     Running     0          7m57sNAME                                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx-controller             NodePort    10.109.170.26    <none>        80:31012/TCP,443:30324/TCP   8m
service/ingress-nginx-controller-admission   ClusterIP   10.103.195.107   <none>        443/TCP                      7m59s#修改微服务为loadbalancer——“type: LoadBalancer”
[root@k8s-master ~]# kubectl -n ingress-nginx edit svc ingress-nginx-controller
service/ingress-nginx-controller edited
[root@k8s-master ~]# kubectl -n ingress-nginx get all
NAME                                            READY   STATUS      RESTARTS   AGE
pod/ingress-nginx-admission-create-79c2p        0/1     Completed   0          10m
pod/ingress-nginx-admission-patch-mghg6         0/1     Completed   1          10m
pod/ingress-nginx-controller-6db9bc976b-ml8lg   1/1     Running     0          10mNAME                                         TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)                      AGE
service/ingress-nginx-controller             LoadBalancer   10.109.170.26    172.25.254.50   80:31012/TCP,443:30324/TCP   10m
service/ingress-nginx-controller-admission   ClusterIP      10.103.195.107   <none>          443/TCP                      10m#建立ingress控制器
[root@k8s-master ~]# kubectl apply -f ingress1.yml 
ingress.networking.k8s.io/myapp1 created
[root@k8s-master ~]# kubectl get ingress
NAME     CLASS   HOSTS   ADDRESS   PORTS   AGE
myapp1   nginx   *                 80      13s
[root@k8s-master ~]# curl 172.25.254.50        在ingress-nginx-controller中看到的对外IP就是ingress最终对外开放的ip[root@k8s-master ~]# cat ingress1.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:creationTimestamp: nullname: myapp1
spec:ingressClassName: nginxrules:- http:paths:- backend:service:name: myapp1port:number: 80path: /pathType: Prefix
#Exact(精确匹配),ImplementationSpecific(特定实现),Prefix(前缀匹配),Regular expression(正则表达式匹配)

3、ingress的高级用法

(1)实验环境

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE        NAME                                        READY   STATUS      RESTARTS      AGE
default          myapp1-57778d7fdb-cgx7n                     1/1     Running     0             43m
default          myapp2-58657f48db-krv4l                     1/1     Running     0             43m
default          test                                        1/1     Running     1 (53m ago)   56m
default          web                                         1/1     Running     0             78m
ingress-nginx    ingress-nginx-admission-create-79c2p        0/1     Completed   0             20m
ingress-nginx    ingress-nginx-admission-patch-mghg6         0/1     Completed   1             20m
ingress-nginx    ingress-nginx-controller-6db9bc976b-ml8lg   1/1     Running     0             20m
kube-flannel     kube-flannel-ds-f9b4w                       1/1     Running     0             80m
kube-flannel     kube-flannel-ds-gpg9t                       1/1     Running     0             81m
kube-flannel     kube-flannel-ds-n7nfd                       1/1     Running     0             81m
kube-system      coredns-558b94794c-4ctss                    1/1     Running     0             82m
kube-system      coredns-558b94794c-v8zrd                    1/1     Running     0             82m
kube-system      etcd-k8s-master.zx.org                      1/1     Running     0             82m
kube-system      kube-apiserver-k8s-master.zx.org            1/1     Running     0             82m
kube-system      kube-controller-manager-k8s-master.zx.org   1/1     Running     0             82m
kube-system      kube-proxy-8ktfv                            1/1     Running     0             75m
kube-system      kube-proxy-pkbzb                            1/1     Running     0             75m
kube-system      kube-proxy-qz6ck                            1/1     Running     0             75m
kube-system      kube-scheduler-k8s-master.zx.org            1/1     Running     0             82m
metallb-system   controller-566fd4c654-trtv9                 1/1     Running     0             72m
metallb-system   speaker-f5hwh                               1/1     Running     0             72m
metallb-system   speaker-f8xg8                               1/1     Running     0             72m
metallb-system   speaker-fr8fg                               1/1     Running     0             72m
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get ingress
NAME     CLASS   HOSTS   ADDRESS         PORTS   AGE
myapp1   nginx   *       172.25.254.20   80      9m36s
[root@k8s-master ~]# kubectl delete ingress myapp1 
ingress.networking.k8s.io "myapp1" deleted
[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   83m
myapp1       ClusterIP   10.99.71.198     <none>        80/TCP    41m
myapp2       ClusterIP   10.101.156.212   <none>        80/TCP    41m
[root@k8s-master ~]# 
[root@k8s-master ~]# curl 10.99.71.198
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.101.156.212
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

(2)基于路径的访问

[root@k8s-master ~]# cp ingress1.yml ingress2.yml
[root@k8s-master ~]# vim ingress2.yml 
[root@k8s-master ~]# cat ingress2.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:annotations:nginx.ingress.kubernetes.io/rewrite-target: /        #访问路径后加任何内容都被定向到/creationTimestamp: nullname: myapp
spec:ingressClassName: nginxrules:- http:paths:- backend:service:name: myapp1port:number: 80path: /v1pathType: Prefix- backend:service:name: myapp2port:number: 80path: /v2pathType: Prefixhost: www.zx.org[root@k8s-master ~]# kubectl apply -f ingress2.yml 
ingress.networking.k8s.io/myapp created[root@k8s-master ~]# kubectl describe ingress myapp
Name:             myapp
Labels:           <none>
Namespace:        default
Address:          
Ingress Class:    nginx
Default backend:  <default>
Rules:Host        Path  Backends----        ----  --------www.zx.org  /v1   myapp1:80 (10.244.1.84:80)/v2   myapp2:80 (10.244.1.85:80)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /
Events:Type    Reason  Age   From                      Message----    ------  ----  ----                      -------Normal  Sync    46s   nginx-ingress-controller  Scheduled for sync[root@k8s-master ~]# kubectl get ingress myapp 
NAME    CLASS   HOSTS   ADDRESS         PORTS   AGE
myapp   nginx   *       172.25.254.20   80      88s
[root@k8s-master ~]# echo 172.25.254.50 www.zx.org >> /etc/hosts
[root@k8s-master ~]# curl www.zx.org/v1
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl www.zx.org/v2
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

(3)基于域名的访问

[root@k8s-master ~]# cp ingress2.yml ingress3.yml
[root@k8s-master ~]# vim ingress3.yml 
[root@k8s-master ~]# vim /etc/hosts
[root@k8s-master ~]# kubectl apply -f ingress3.yml 
ingress.networking.k8s.io/myapp created
[root@k8s-master ~]# kubectl describe ingress myapp 
Name:             myapp
Labels:           <none>
Namespace:        default
Address:          172.25.254.20
Ingress Class:    nginx
Default backend:  <default>
Rules:Host           Path  Backends----           ----  --------myapp1.zx.org  /   myapp1:80 (10.244.1.84:80)myapp2.zx.org  /   myapp2:80 (10.244.1.85:80)
Annotations:     <none>
Events:Type    Reason  Age                From                      Message----    ------  ----               ----                      -------Normal  Sync    28s (x2 over 50s)  nginx-ingress-controller  Scheduled for sync[root@k8s-master ~]# curl myapp1.zx.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl myapp2.zx.org
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>[root@k8s-master ~]# cat ingress3.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: myapp
spec:ingressClassName: nginxrules:- http:paths:- backend:service:name: myapp1port:number: 80path: /pathType: Prefixhost: myapp1.zx.org- http:paths:- backend:service:name: myapp2port:number: 80path: /pathType: Prefixhost: myapp2.zx.org[root@k8s-master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.200	k8s-master.zx.org
172.25.254.10 k8s-node1.zx.org
172.25.254.20 k8s-node2.zx.org
172.25.254.100 reg.zx.org
172.25.254.50 www.zx.org myapp1.zx.org myapp2.zx.org

(4)建立tls加密

#建立证书
[root@k8s-master ~]# openssl req -newkey rsa:2048 -nodes -keyout tls.key -x509 -days 365 -subj "/CN=nginxsvc/O=nginxsvc" -out tls.crt#建立加密资源类型secret
[root@k8s-master ~]# kubectl create secret tls  web-tls-secret --key tls.key --cert tls.crt[root@k8s-master ~]# kubectl get secrets
NAME             TYPE                DATA   AGE
web-tls-secret   kubernetes.io/tls   2      23s
# secret通常在kubernetes中存放敏感数据,它并不是一种加密方式
#建立ingress4基于tls认证的yml文件
[root@k8s-master ~]# cp ingress3.yml ingress4.yml
[root@k8s-master ~]# vim ingress4.yml 
[root@k8s-master ~]# cat ingress4.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: myapp
spec:tls:- hosts:- myapp-tls.zx.orgsecretName: web-tls-secretingressClassName: nginxrules:- http:paths:- backend:service:name: myapp1port:number: 80path: /pathType: Prefix
[root@k8s-master ~]# kubectl apply -f ingress4.yml 
ingress.networking.k8s.io/myapp configured[root@k8s-master ~]# kubectl describe ingress myapp 
Name:             myapp
Labels:           <none>
Namespace:        default
Address:          172.25.254.20
Ingress Class:    nginx
Default backend:  <default>
TLS:web-tls-secret terminates myapp-tls.zx.org
Rules:Host        Path  Backends----        ----  --------*           /   myapp1:80 (10.244.1.84:80)
Annotations:  <none>
Events:Type    Reason  Age                From                      Message----    ------  ----               ----                      -------Normal  Sync    34s (x3 over 15m)  nginx-ingress-controller  Scheduled for sync[root@k8s-master ~]# curl -k https://myapp-tls.zx.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
在浏览器访问,需在电脑host文件中添加解析——	172.25.254.50	myapp-tls.zx.org

(5)建立auth认证

#建立认证文件
[root@k8s-master ~]# dnf install httpd-tools -y
[root@k8s-master ~]# htpasswd -cm auth zx
New password: 
Re-type new password: 
Adding password for user zx#建立认证类型资源
[root@k8s-master ~]# kubectl create secret generic auth-web --from-file auth
[root@k8s-master ~]# kubectl describe secrets auth-web
Name:         auth-web
Namespace:    default
Labels:       <none>
Annotations:  <none>Type:  OpaqueData
====
auth:  41 bytes[root@k8s-master ~]# kubectl get secrets auth-web -o yaml
apiVersion: v1
data:auth: eng6JGFwcjEkSWJjV3pIZzUkZWtrSDFSeWIzWEYvTjBvRjdrRi4uLwo=
kind: Secret
metadata:creationTimestamp: "2024-09-16T04:50:39Z"name: auth-webnamespace: defaultresourceVersion: "13126"uid: 36088669-225e-4fa5-a1a2-b2d5c3823779
type: Opaque
[root@k8s-master ~]# cp ingress4.yml ingress5.yml
[root@k8s-master ~]# vim ingress5.yml 
[root@k8s-master ~]# cat ingress5.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: myappannotations:nginx.ingress.kubernetes.io/auth-type: basicnginx.ingress.kubernetes.io/auth-secret: auth-webnginx.ingress.kubernetes.io/auth-realm: "Please input username and password"
spec:tls:- hosts:- myapp-tls.zx.orgsecretName: web-tls-secretingressClassName: nginxrules:- host: myapp-tls.zx.orghttp:paths:- backend:service:name: myapp1port:number: 80path: /pathType: Prefix[root@k8s-master ~]# kubectl apply -f ingress5.yml 
ingress.networking.k8s.io/myapp configured
[root@k8s-master ~]# kubectl describe ingress myapp # 测试
[root@k8s-master ~]# curl -k https://myapp-tls.zx.org
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@k8s-master ~]# 
[root@k8s-master ~]# curl -k https://myapp-tls.zx.org -uzx
Enter host password for user 'zx':
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# 

(6)rewrite重定向

#指定默认访问的文件到hostname.html上
[root@k8s-master ~]# cp ingress5.yml ingress6.yml
[root@k8s-master ~]# vim ingress6.yml 
[root@k8s-master ~]# kubectl apply -f ingress6.yml 
ingress.networking.k8s.io/myapp configured
[root@k8s-master ~]#  kubectl describe ingress myapp 
Name:             myapp
Labels:           <none>
Namespace:        default
Address:          172.25.254.20
Ingress Class:    nginx
Default backend:  <default>
TLS:web-tls-secret terminates myapp-tls.zx.org
Rules:Host              Path  Backends----              ----  --------myapp-tls.zx.org  /   myapp1:80 (10.244.1.84:80)
Annotations:        nginx.ingress.kubernetes.io/app-root: /hostname.htmlnginx.ingress.kubernetes.io/auth-realm: Please input username and passwordnginx.ingress.kubernetes.io/auth-secret: auth-webnginx.ingress.kubernetes.io/auth-type: basic
Events:Type    Reason  Age                From                      Message----    ------  ----               ----                      -------Normal  Sync    17s (x5 over 31m)  nginx-ingress-controller  Scheduled for sync
[root@k8s-master ~]# 
[root@k8s-master ~]# curl -Lk https://myapp-tls.zx.org -uzx
Enter host password for user 'zx':
myapp1-57778d7fdb-cgx7n
[root@k8s-master ~]# curl -Lk https://myapp-tls.zx.org/zx/hostname.html -uzx
Enter host password for user 'zx':
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

#解决重定向路径问题
[root@k8s-master ~]# vim ingress6.yml 
[root@k8s-master ~]# kubectl apply -f ingress6.yml 
ingress.networking.k8s.io/myapp configured
[root@k8s-master ~]# curl -Lk https://myapp-tls.zx.org/zx/hostname.html -uzx
Enter host password for user 'zx':
myapp1-57778d7fdb-cgx7n
[root@k8s-master ~]# cat ingress6.yml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: myappannotations:nginx.ingress.kubernetes.io/rewrite-target: /$2nginx.ingress.kubernetes.io/use-regex: "true"nginx.ingress.kubernetes.io/auth-type: basicnginx.ingress.kubernetes.io/auth-secret: auth-webnginx.ingress.kubernetes.io/auth-realm: "Please input username and password"
spec:tls:- hosts:- myapp-tls.zx.orgsecretName: web-tls-secretingressClassName: nginxrules:- host: myapp-tls.zx.orghttp:paths:- backend:service:name: myapp1port:number: 80path: /pathType: Prefix- backend:service:name: myapp1port:number: 80path: /zx(/|$)(.*)pathType: ImplementationSpecific

六、Canary金丝雀发布

1、概念

金丝雀发布(Canary Release)也称为灰度发布,是一种软件发布策略。

主要目的是在将新版本的软件全面推广到生产环境之前,先在一小部分用户或服务器上进行测试和验证,以降低因新版本引入重大问题而对整个系统造成的影响。

是一种Pod的发布方式。金丝雀发布采取先添加、再删除的方式,保证Pod的总量不低于期望值。并且在更新部分Pod后,暂停更新,当确认新Pod版本运行正常后再进行其他版本的Pod的更新。

2、Canary发布

(1)基于header(http包头)灰度

  • 通过Annotaion扩展

  • 创建灰度ingress,配置灰度头部key以及value

  • 灰度流量验证完毕后,切换正式ingress到新版本

  • 之前我们在做升级时可以通过控制器做滚动更新,默认25%利用header可以使升级更为平滑,通过key 和vule 测试新的业务体系是否有问题。

#建立版本1的ingress
[root@k8s-master app]# vim ingress7.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:annotations:name: myapp-v1-ingress
spec:ingressClassName: nginxrules:- host: myapp-tls.zx.orghttp:paths:- backend:service:name: myapp-v1port:number: 80path: /pathType: Prefix[root@k8s-master app]# kubectl describe ingress myapp-v1-ingress
Name:             myapp-v1-ingress
Labels:           <none>
Namespace:        default
Address:          172.25.254.10
Ingress Class:    nginx
Default backend:  <default>
Rules:Host                 Path  Backends----                 ----  --------myapp-tls.zx.org/   myapp-v1:80 (10.244.2.31:80)
Annotations:           <none>
Events:Type    Reason  Age                From                      Message----    ------  ----               ----                      -------Normal  Sync    44s (x2 over 73s)  nginx-ingress-controller  Scheduled for sync#建立基于header的ingress
[root@k8s-master app]# vim ingress8.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:annotations:nginx.ingress.kubernetes.io/canary: "true"nginx.ingress.kubernetes.io/canary-by-header: versionnginx.ingress.kubernetes.io/canary-by-header-value: 2name: myapp-v2-ingress
spec:ingressClassName: nginxrules:- host: myapp-tls.zx.orghttp:paths:- backend:service:name: myapp-v2port:number: 80path: /pathType: Prefix
[root@k8s-master app]# kubectl apply -f ingress8.yml
ingress.networking.k8s.io/myapp-v2-ingress created
[root@k8s-master app]# kubectl describe ingress myapp-v2-ingress
Name:             myapp-v2-ingress
Labels:           <none>
Namespace:        default
Address:
Ingress Class:    nginx
Default backend:  <default>
Rules:Host                 Path  Backends----                 ----  --------myapp-tls.zx.org/   myapp-v2:80 (10.244.2.32:80)
Annotations:           nginx.ingress.kubernetes.io/canary: truenginx.ingress.kubernetes.io/canary-by-header: versionnginx.ingress.kubernetes.io/canary-by-header-value: 2
Events:Type    Reason  Age   From                      Message----    ------  ----  ----                      -------Normal  Sync    21s   nginx-ingress-controller  Scheduled for sync
#测试:
[root@reg ~]# curl  myapp-tls.zx.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@reg ~]# curl -H "version: 2" myapp-tls.zx.org
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

(2)基于权重的灰度发布

  • 通过Annotaion拓展

  • 创建灰度ingress,配置灰度权重以及总权重

  • 灰度流量验证完毕后,切换正式ingress到新版本

#基于权重的灰度发布
[root@k8s-master app]# vim ingress8.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:annotations:nginx.ingress.kubernetes.io/canary: "true"nginx.ingress.kubernetes.io/canary-weight: "10"		#更改权重值nginx.ingress.kubernetes.io/canary-weight-total: "100"name: myapp-v2-ingress
spec:ingressClassName: nginxrules:- host: myapp-ls.zx.orghttp:paths:- backend:service:name: myapp-v2port:number: 80path: /pathType: Prefix[root@k8s-master app]# kubectl apply -f ingress8.yml
ingress.networking.k8s.io/myapp-v2-ingress created#测试:
[root@reg ~]# vim check_ingress.sh
#!/bin/bash
v1=0
v2=0for (( i=0; i<100; i++))
doresponse=`curl -s myapp.timinglee.org |grep -c v1`v1=`expr $v1 + $response`v2=`expr $v2 + 1 - $response`done
echo "v1:$v1, v2:$v2"[root@reg ~]# sh check_ingress.sh
v1:90, v2:10#更改完毕权重后继续测试可观察变化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/146325.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Flink提交任务

第3章 Flink部署 3.1 集群角色 3.2 Flink集群搭建 3.2.1 集群启动 0&#xff09;集群规划 表3-1 集群角色分配 具体安装部署步骤如下&#xff1a; 1&#xff09;下载并解压安装包 &#xff08;1&#xff09;下载安装包flink-1.17.0-bin-scala_2.12.tgz&#xff0c;将该jar包…

有什么兼容macOS 15 Sequoia系统的加密软件?

前言&#xff1a;近日&#xff0c;苹果更新了 macOS 15 Sequoia正式版&#xff0c;已经有用户在电脑上安装使用了。在这个信息化时代&#xff0c;系统一直在更新&#xff0c;运用一些工具时需要考虑兼容性。 刚有个客户来问迅软&#xff1a;你们迅软DSE客户端支持新发布的macO…

Linux 磁盘清理重新格式化挂载脚本及问题解决

Linux 磁盘清理重新格式化挂载脚本&#xff1a;diskformat.sh #!/bin/bash for i in {1…8} do umount /data0$i done PIDARRAY() for i in a b c d e f g h do parted -s /dev/sd i m k l a b e l g p t p a r t e d − s / d e v / s d i mklabel gpt parted -s /dev/sd im…

【高阶数据结构】二叉搜索树的插入、删除和查找(精美图解+完整代码)

&#x1f921;博客主页&#xff1a;醉竺 &#x1f970;本文专栏&#xff1a;《高阶数据结构》 &#x1f63b;欢迎关注&#xff1a;感谢大家的点赞评论关注&#xff0c;祝您学有所成&#xff01; ✨✨&#x1f49c;&#x1f49b;想要学习更多《高阶数据结构》点击专栏链接查看&a…

【计算机网络篇】计算机网络概述

本文主要介绍计算机网络第一章节的内容&#xff0c;文中的内容是我认为的重点内容&#xff0c;并非所有。参考的教材是谢希仁老师编著的《计算机网络》第8版。跟学视频课为河南科技大学郑瑞娟老师所讲计网。 文章目录 &#x1f3af;一.计算机网络的组成 ✨主要内容 1.边缘部…

seL4 Capabilities(翻自官网)(一)

官网教程链接: Capability 初始化Capabilities tutorials // 先使用repo拉取一下tutorials&#xff0c;然后执行repo sync&#xff0c;所有的教程都在里面&#xff0c;学习某个的时候只需要改变的是 --tut 后面的参数 ./init --tut capabilities # building the tutorial exe…

国内可以使用的ChatGPT服务【9月持续更新】

首先基础知识还是要介绍得~ 一、模型知识&#xff1a; GPT-4o&#xff1a;最新的版本模型&#xff0c;支持视觉等多模态&#xff0c;OpenAI 文档中已经更新了 GPT-4o 的介绍&#xff1a;128k 上下文&#xff0c;训练截止 2023 年 10 月&#xff08;作为对比&#xff0c;GPT-4…

演示:基于WPF的DrawingVisual开发的Chart图表和表格绘制

一、目的&#xff1a;基于WPF的DrawingVisual开发的Chart图表和表格绘制 二、预览 钻井井轨迹表格数据演示示例&#xff08;应用Table布局&#xff0c;模拟井轨迹深度的绘制&#xff09; 饼图表格数据演示示例&#xff08;应用Table布局&#xff0c;模拟多个饼状图组合显示&am…

git使用“保姆级”教程2——初始化及工作机制解释

1、设置用户签名 解释&#xff1a; 签名的作用就是用来&#xff1a;标识用户&#xff0c;以区分不同的开发人员简单来说&#xff1a;用来标识"你是谁"&#xff0c;在提交代码时&#xff0c;会显示提交代码的是谁&#xff0c;把设置的信息一起提交上去 设置&#xff…

weblogic CVE-2019-2725 靶场攻略

漏洞描述 wls9-async等组件为WebLogic Server提供异步通讯服务&#xff0c;默认应⽤于WebLogic部分版本。由于该 WAR包在反序列化处理输⼊信息时存在缺陷&#xff0c;攻击者通过发送精⼼构造的恶意 HTTP 请求&#xff0c;即可获得⽬标服务器的权限&#xff0c;在未授权的情况…

4.使用 VSCode 过程中的英语积累 - View 菜单(每一次重点积累 5 个单词)

前言 学习可以不局限于传统的书籍和课堂&#xff0c;各种生活的元素也都可以做为我们的学习对象&#xff0c;本文将利用 VSCode 页面上的各种英文元素来做英语的积累&#xff0c;如此做有 3 大利 这些软件在我们工作中是时时刻刻接触的&#xff0c;借此做英语积累再合适不过&a…

Codeforces Round 973 (Div. 2) F1. Game in Tree (Easy Version)(思维题 博弈)

题目 思路来源 乱搞ac 题解 两个人的策略是一样的&#xff0c;把1到u的路径标记&#xff0c; 如果能走旁边的链&#xff08;也就是当前点&#xff0c;刨去标记链以外的子树中最长的链&#xff09;&#xff0c; 使得对面走剩余的连通块无法比你大&#xff0c;就走旁边的链&…

【计算机网络】网络层协议解析

网络层的两种服务IPv4分类编址划分子网无分类地址 IPv4地址应用IP数据报的发送和转发过程主机发送IP数据报路由器转发IP数据报 IPv4数据报首部格式ICMP网际控制报文协议虚拟专用网VPN与网络地址转换NAT 网络层主要任务是实现网络互连&#xff0c;进而实现数据包在各网络之间的传…

充电桩项目:前端实现

上次基于VueElement plus实现了充电桩项目后台管理系统的基本架子。 后端管理 员工管理 这次&#xff0c;又把用户端的基本架子搭建完毕&#xff1a;VueVant 首页 个人中心 充值 选择充值方式 优惠券中心 已过期优惠券 用户登录 用户注册 慢慢项目就有点样子了&#xff0c;代码…

远程桌面连接工具Microsoft Remote Desktop Beta for Mac

Microsoft Remote Desktop Beta for Mac 是一款功能强大的远程桌面连接工具&#xff0c;具有以下功能特点&#xff1a; 软件下载地址 跨平台连接&#xff1a; 允许 Mac 用户轻松连接到运行 Windows 操作系统的计算机&#xff0c;打破了操作系统的界限&#xff0c;无论这些 Wi…

Shiro-550—漏洞分析(CVE-2016-4437)

文章目录 漏洞原理源码分析加密过程解密过程 漏洞复现 漏洞原理 Shiro-550(CVE-2016-4437)反序列化漏洞 在调试cookie加密过程的时候发现开发者将AES用来加密的密钥硬编码了&#xff0c;并且所以导致我们拿到密钥后可以精心构造恶意payload替换cookie&#xff0c;然后让后台最…

基于无人机影像的可见光单木分割数据集-json格式

基于无人机影像的可见光单木分割数据集&#xff0c;共1700张影像&#xff0c;数据集大小3.6GB&#xff0c;分割标注采用标准json格式。 该数据集是一个专门用于基于无人机可见光影像进行单木分割的数据集&#xff0c;旨在帮助研究人员和开发者训练和评估基于深度学习的图像分割…

EndNoteX9快捷插入引用文献的教程以及出现的一些问题的解决方法(一)

使用EndNote向Word文档中插入引用文献时报错如图1所示&#xff1a; 解决方法为&#xff1a; 采用管理员身份运行Word与EndNote软件 当电脑中安装好Word与EndNote两款软件之后如何在Word中快速插入引用文献 &#xff08;一&#xff09;直接打开Word如图2&#xff0c;3&#x…

VisionPro - 基础 - 00 模板匹配技术和在VP中的使用 - PMAlign - PatMax - (3)

前言&#xff1a; 针对PatMax 的高级应用和原理&#xff0c;在这一节继续进行说明&#xff1a;这一节主要考虑的是PatMax模板匹配的原理&#xff1a; How PatMax Finds Patterns in an Image PatMax 模板匹配原理 1 Run-time Space When you search for a PatMax pattern in …

生信初学者教程(五):R语言基础

文章目录 数据类型整型逻辑型字符型日期型数值型复杂数数据结构向量矩阵数组列表因子数据框ts特殊值缺失值 (NA)无穷大 (Inf)非数字 (NaN)安装R包学习材料R语言是一种用于统计计算和图形展示的编程语言和软件环境,广泛应用于数据分析、统计建模和数据可视化。1991年:R语言的最…