Example - How to use label and selector
create deployment, service with labels and selector
deployment(nginx-label01~04)에 label(app,environment,release)을 작성한다.
- deployment-nginx-label01 ~04.yaml 작성
- 위 표에 맞게 라벨을 작성한다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-label01 # deployment별로 label01~04로 설정
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels: # labels 항목, 3개의 labels
app: nginx
environment: develop
release: beta
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
service(develop, stable)에 selector(environment: develop, release: stable) 를 작성한다.
- labe-service01~02.yaml 작성
- 위 표에 맞게 selector를 작성
apiVersion: v1
kind: Service
metadata:
name: label-develop-service
spec:
type: ClusterIP
selector:
environment: develop
ports:
- protocol: TCP
port: 80
targetPort: 80
apiVersion: v1
kind: Service
metadata:
name: label-stable-service
spec:
type: ClusterIP
selector:
release: stable
ports:
- protocol: TCP
port: 80
targetPort: 80
deployment, service apply
[dewble@instance-1 labels]$ kubectl apply -f .
deployment.apps/nginx-label01 created
deployment.apps/nginx-label02 created
deployment.apps/nginx-label03 created
deployment.apps/nginx-label04 created
service/label-develop-service created
service/label-stable-service created
작성한 label과 selector에 맞게 서비스가 연결되었는지 확인해본다.
What is the result that I expect
develop-service → deployment01,03
stable-service→ deployment03,04
배포한 deploy의 pod가 어떤 IP를 가지고 만들어 졌는지 확인
[dewble@instance-1 labels]$ kubectl get pods -o wide | grep label
nginx-label01-58f576c6f4-gnp7h 1/1 Running 0 2m6s 10.233.118.39 instance-5 <none> <none>
nginx-label02-657ccdb46-pjvjg 1/1 Running 0 2m6s 10.233.125.120 instance-4 <none> <none>
nginx-label03-686d484df7-vhj2z 1/1 Running 0 2m6s 10.233.125.138 instance-4 <none> <none>
nginx-label04-6ff7b6c8bf-xmhpw 1/1 Running 0 2m6s 10.233.125.113 instance-4 <none> <none>
pod | IP |
nginx-label01 | 10.233.118.39 |
nginx-label02 | 10.233.125.120 |
nginx-label03 | 10.233.125.138 |
nginx-label04 | 10.233.125.113 |
서비스가 어떤 셀렉터를 가지고 만들어졌는지 확인
[dewble@instance-1 labels]$ kubectl get svc -o wide | grep label
label-develop-service ClusterIP 10.233.9.113 <none> 80/TCP 2m37s environment=develop
label-stable-service ClusterIP 10.233.41.156 <none> 80/TCP 2m37s release=stable
service | label |
label-develop-service | environment=develop |
label-stable-service | release=stable |
서비스가 설정한 파드를 선택했는지 확인
[dewble@instance-1 labels]$ kubectl describe svc
Name: label-develop-service
Namespace: kube-system
Labels: <none>
Annotations: <none>
Selector: environment=develop
Type: ClusterIP
IP: 10.233.9.113
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.233.118.39:80,10.233.125.138:80
Session Affinity: None
Events: <none>
Name: label-stable-service
Namespace: kube-system
Labels: <none>
Annotations: <none>
Selector: release=stable
Type: ClusterIP
IP: 10.233.41.156
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.233.125.113:80,10.233.125.138:80
Session Affinity: None
Events: <none>
service | Endpoints |
nginx-label01 | 10.233.118.39 |
nginx-label02 | 10.233.125.120 |
nginx-label03 | 10.233.125.138 |
nginx-label04 | 10.233.125.113 |
service | label | pod IP |
label-develop-service | environment=develop | nginx-label01 10.233.118.39 nginx-label03 10.233.125.138 |
label-stable-service | release=stable | nginx-label03 10.233.125.138 nginx-label04 10.233.125.113 |
- 예상했던 대로 service의 Endpoints에 각 label에 맞는 pod의 IP가 할당된 것을 알 수 있다.
Example - How to use selector in command
[dewble@instance-1 labels]$ kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-label01-58f576c6f4-gnp7h 1/1 Running 0 6m17s
nginx-label02-657ccdb46-pjvjg 1/1 Running 0 6m17s
nginx-label03-686d484df7-vhj2z 1/1 Running 0 6m17s
nginx-label04-6ff7b6c8bf-xmhpw 1/1 Running 0 6m17s
[dewble@instance-1 labels]$ kubectl get pods -l environment=develop,release=stable
NAME READY STATUS RESTARTS AGE
nginx-label03-686d484df7-vhj2z 1/1 Running 0 7m30s
[dewble@instance-1 labels]$ kubectl get pods -l "app=nginx,environment notin (develop)"
NAME READY STATUS RESTARTS AGE
nginx-label02-657ccdb46-pjvjg 1/1 Running 0 8m5s
nginx-label04-6ff7b6c8bf-xmhpw 1/1 Running 0 8m5s
[dewble@instance-1 labels]$ kubectl get pods -l release!=stable
NAME READY STATUS RESTARTS AGE
nginx-label01-58f576c6f4-gnp7h 1/1 Running 0 10m
nginx-label02-657ccdb46-pjvjg 1/1 Running 0 10m
[dewble@instance-1 labels]$ kubectl get pods -l release!=stable
NAME READY STATUS RESTARTS AGE
nginx-label01-58f576c6f4-gnp7h 1/1 Running 0 10m
nginx-label02-657ccdb46-pjvjg 1/1 Running 0 10m
'Cloud-native > Kubernetes' 카테고리의 다른 글
[Kubernetes]How to create Kubernetes Secret from Json/Yaml/Literal (0) | 2022.07.08 |
---|---|
[Kubernetes]What is Annotation in Kubernetes? (0) | 2022.06.06 |
[Kubernetes]How to use Label and Selector - Concept (0) | 2022.06.05 |
[Kubernetes]What is kube-proxy (userspace, iptables, IPVS) (0) | 2022.06.05 |
[Kubernetes]What is Pause Container(Infra Container) (0) | 2022.06.03 |