K8S中service与controller(控制器)通过标签选择器(selector)匹配pod
文章目录1.1 当前k8s的环境1.2 创建sc名称空间1.3 通过实践理解1.4 pc-erp-fronted 项目演示1.1 当前k8s的环境### 当前k8s的运算节点[root@hdss7-21 ~]#[root@hdss7-21 ~]# kubectl get nodeNAMESTATUSROLESAGEVERSIONhdss7-21.host.comReadymaster,node40
·
文章目录
1.1 当前k8s的环境
### 当前k8s的运算节点
[root@hdss7-21 ~]#
[root@hdss7-21 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
hdss7-21.host.com Ready master,node 40h v1.15.2
hdss7-22.host.com Ready master,node 40h v1.15.2
### 当前k8s的名称空间
[root@hdss7-21 ~]#
[root@hdss7-21 ~]# kubectl get ns
NAME STATUS AGE
default Active 2d3h
kube-node-lease Active 2d3h
kube-public Active 2d3h
kube-system Active 2d3h
1.2 创建sc名称空间
[root@hdss7-21 ~]# kubectl create ns sc
namespace/sc created
[root@hdss7-21 ~]#
[root@hdss7-21 ~]# kubectl get ns sc
NAME STATUS AGE
sc Active 3s
1.3 通过实践理解
截图说明
deployment文件 nginx-deployment.yaml 的内容如下所示
apiVersion: extensions/v1beta1 # 资源deployment对应的api版本,可以用kubectl explain deployment来查看;
kind: Deployment # 类型,这里指定的是deployment资源的类型,注意其类型的大小写;
metadata: # 元数据
name: chenliang # 给deployment这个资源(它的类型是Deployment)取个名字,应该以项目(以相关镜像名)名来命名
namespace: sc # 名字为chenliang的资源(deployment)放在哪个名称空间里面;
labels: # 给名为chenliang的资源(deployment)打标签,形式是key:value,其值应该,应该以项目(以相关镜像名)名来命名
app: sc-chenliang
spec: # 定义清单,这个字段必须存在
replicas: 2 # 指定Pod副本数,我上面指定控制器是deployment,不是所有控制器都有这个选项哈
selector: # 选择器
matchLabels: # 匹配标签,匹配的是pod的标签,key和values都得一样,
app: my-nginx
template: # 样板
metadata: # 元数据
labels: # 给pod打标签,形式为,key:value
app: my-nginx
spec: # 清单
containers: # 容器
- name: nginx # 名字,我是以镜像的名字来命名的
image: harbor.od.com/public/nginx:latest # 镜像的路径,其实nginx这几个字符应该换成其对应的项目各,比如:pc-erp-frontend
ports: # 对应服务监听的真实端口
- containerPort: 80
service文件:nginx-svc.yaml 的内容如下所示
apiVersion: v1 # 资源service对应的api版本,可以用kubectl explain service来查看;
kind: Service # 类型,这里指定的是service资源的类型,注意其类型的大小写;
metadata: # 元数据,这个必须存在
name: lili # 给service这个资源取个名字,这个名称可以随便写
namespace: sc # 名字为nginx-curl的service资源放在哪个名称空间里面
labels: # 给名字为nginx-curl的service资源打个标签,这个标签的名字你随便写
app: sc-lili
spec: # 清单
selector: # 标签选择器,这里指定的是pod的标签,key和value都得一样哈
app: my-nginx
ports: # 端口
- port: 80 # 给clusterIP的端口
protocol: TCP # 协议
targetPort: 80 # 这个端口是你Pod的端口(pod里面是容器,容器里面业务真正的端口)
type: ClusterIP # 类型,默认就是ClusterIP
应用相应的yaml文件
#### 应用nginx项目的deployment文件
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl create -f nginx-deployment.yaml
deployment.extensions/chenliang created
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl get deployment chenliang -n sc -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
chenliang 2/2 2 2 29s nginx harbor.od.com/public/nginx:latest app=my-nginx
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl get pod -o wide -n sc |grep chenliang
chenliang-69c79797bb-c5vgl 1/1 Running 0 62s 172.7.21.2 hdss7-21.host.com <none> <none>
chenliang-69c79797bb-vcqwq 1/1 Running 0 62s 172.7.22.2 hdss7-22.host.com <none> <none>
#### 应用nginx项目的svc文件
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl create -f nginx-svc.yaml
service/lili created
[root@hdss7-21 tools]# kubectl get svc lili -n sc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
lili ClusterIP 192.168.3.170 <none> 80/TCP 23s app=my-nginx
[root@hdss7-21 tools]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.4.7.21:6443 Masq 1 0 0
-> 10.4.7.22:6443 Masq 1 0 0
TCP 192.168.3.170:80 nq # 看这
-> 172.7.21.2:80 Masq 1 0 0
-> 172.7.22.2:80 Masq 1 0 0
[root@hdss7-21 tools]# curl -I 192.168.3.170:80
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 28 Dec 2021 09:03:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:29:18 GMT
Connection: keep-alive
ETag: "5964d2ae-264"
Accept-Ranges: bytes
1.4 deployment控制器部署pc-erp-frontend 项目
pc-erp-frontend-deployment.yaml文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: pc-erp-frontend # deployment资源我为其命名为我的项目名。
namespace: sc
labels: # deployment资源对应标签中的value我也是以项目名来定的。
app: pc-erp-frontend
spec:
replicas: 2
selector:
matchLabels: # 匹配标签,deployment控制器匹配的是pod的标签,key和values都得和pod的一样;
app: pc-erp-frontend
template:
metadata:
labels: # 给pod打标签,形式为key:value,其值是项目名,控制器和service后面要来匹配的;
app: pc-erp-frontend
spec:
containers:
- name: pc-erp-frontend # 名字,我是以镜像的名字来命名的;
image: harbor.od.com/public/pc-erp-frontend:latest # 镜像我是以项目来命名的;
ports: # 对应服务监听的真实端口
- containerPort: 80
pc-erp-frontend-svc.yaml文件
apiVersion: v1
kind: Service
metadata:
name: pc-erp-frontend # 给service资源命名,我是以项目来命名的
namespace: sc
labels: # service资源标签中的value我是以项目名来定的
app: pc-erp-frontend
spec:
selector: # service通过标签选择器去匹配pod的标签
app: pc-erp-frontend
ports:
- port: 80
protocol: TCP
targetPort: 80
type: ClusterIP
应用相关的yaml文件
#### 应用pc-erp-frontend项目的deployment文件
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl create -f pc-erp-frontend-deployment.yaml
deployment.extensions/pc-erp-frontend created
#### 应用pc-erp-frontend项目的svc文件
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl create -f pc-erp-frontend-svc.yaml
service/pc-erp-frontend created
#### 检查,就直接通过项目名来过滤出来
kubectl get all -n sc -o wide | grep pc-erp-frontend
[root@hdss7-21 tools]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.4.7.21:6443 Masq 1 0 0
-> 10.4.7.22:6443 Masq 1 0 0
TCP 192.168.3.170:80 nq
-> 172.7.21.2:80 Masq 1 0 0
-> 172.7.22.2:80 Masq 1 0 0
TCP 192.168.33.235:80 nq # 看这
-> 172.7.21.3:80 Masq 1 0 0
-> 172.7.22.3:80 Masq 1 0 0
[root@hdss7-21 tools]# curl -I 192.168.33.235:80
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 28 Dec 2021 09:17:34 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:29:18 GMT
Connection: keep-alive
ETag: "5964d2ae-264"
Accept-Ranges: bytes
1.5 daemonset控制器部署m-erp-frontend项目
m-erp-frontend-daemonset.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: m-erp-frontend
namespace: sc
labels:
app: m-erp-frontend
spec:
selector:
matchLabels:
app: m-erp-frontend
template:
metadata:
labels:
app: m-erp-frontend
spec:
containers:
- name: m-erp-frontend
image: harbor.od.com/public/m-erp-frontend:latest
ports:
- containerPort: 80
m-erp-frontend-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: m-erp-frontend
namespace: sc
labels:
app: m-erp-frontend
spec:
selector:
app: m-erp-frontend
ports:
- port: 80
protocol: TCP
targetPort: 80
type: ClusterIP
应用相关的yaml文件
#### 应用m-erp-frontend对应的daemonset文件
[root@hdss7-21 tools]# kubectl create -f m-erp-frontend-daemonset.yaml
daemonset.extensions/m-erp-frontend created
#### 应用m-erp-frontend对应的svc文件
[root@hdss7-21 tools]#
[root@hdss7-21 tools]# kubectl create -f m-erp-frontend-svc.yaml
service/m-erp-frontend created
#### 查看m-erp-frontend项目对应的信息
kubectl get all -n sc -o wide|grep m-erp-frontend
[root@hdss7-21 tools]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.4.7.21:6443 Masq 1 0 0
-> 10.4.7.22:6443 Masq 1 0 0
TCP 192.168.33.235:80 nq
-> 172.7.21.3:80 Masq 1 0 0
-> 172.7.22.3:80 Masq 1 0 0
TCP 192.168.36.175:80 nq # 看这
-> 172.7.21.2:80 Masq 1 0 0
-> 172.7.22.2:80 Masq 1 0 0
[root@hdss7-21 tools]# curl -I 192.168.36.175:80
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 28 Dec 2021 10:18:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:29:18 GMT
Connection: keep-alive
ETag: "5964d2ae-264"
Accept-Ranges: bytes
更多推荐
已为社区贡献1条内容
所有评论(0)