标签:外部 tab more clust 类型 随机 node seq probe
k8s基本概念-如何使用Services2018/1/5
### 获取所有的 service 列表:
[root@tvm-00 ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d
[root@tvm-00 ~]# kubectl expose deployments/test-deployment-app-whoami --type="NodePort" --port 80
service "test-deployment-app-whoami" exposed
[root@tvm-00 ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d
test-deployment-app-whoami NodePort 10.108.8.154 <none> 80:31816/TCP 9s
[root@tvm-00 ~]# kubectl get services -l app=whoami
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
test-deployment-app-whoami NodePort 10.108.8.154 <none> 80:31816/TCP 23s
- 查看细节
```bash
### 查看 service 的细节:
[root@tvm-00 ~]# kubectl describe services/test-deployment-app-whoami
Name: test-deployment-app-whoami
Namespace: default
Labels: app=whoami
Annotations: <none>
Selector: app=whoami
Type: NodePort
IP: 10.108.8.154
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 31816/TCP
Endpoints: 172.30.11.74:80,172.30.11.75:80,172.30.11.77:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
### 此处也可以通过 label 来筛选
[root@tvm-00 ~]# kubectl describe services -l app=whoami
Name: test-deployment-app-whoami
Namespace: default
Labels: app=whoami
Annotations: <none>
Selector: app=whoami
Type: NodePort
IP: 10.108.8.154
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 31816/TCP
Endpoints: 172.30.11.74:80,172.30.11.75:80,172.30.11.77:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@tvm-00 ~]# export NODE_PORT=$(kubectl get services/test-deployment-app-whoami -o go-template=‘{{(index .spec.ports 0).nodePort}}‘)
[root@tvm-00 ~]# echo NODE_PORT=$NODE_PORT
NODE_PORT=31816
[root@tvm-00 ~]# kubectl get pods -l app=whoami
NAME READY STATUS RESTARTS AGE
test-deployment-app-whoami-6cf9cd6bf4-2dd5m 1/1 Running 0 15h
test-deployment-app-whoami-6cf9cd6bf4-bb7v7 1/1 Running 0 15h
test-deployment-app-whoami-6cf9cd6bf4-c7cht 1/1 Running 0 15h
test-deployment-app-whoami-6cf9cd6bf4-jhtqz 1/1 Running 0 15h
test-deployment-app-whoami-6cf9cd6bf4-px24h 1/1 Running 0 15h
[root@tvm-00 ~]# for i in $(seq 1 10); do curl -s tvm-00:$NODE_PORT|grep Hostname; done
Hostname: test-deployment-app-whoami-6cf9cd6bf4-jhtqz
Hostname: test-deployment-app-whoami-6cf9cd6bf4-bb7v7
Hostname: test-deployment-app-whoami-6cf9cd6bf4-px24h
Hostname: test-deployment-app-whoami-6cf9cd6bf4-jhtqz
Hostname: test-deployment-app-whoami-6cf9cd6bf4-2dd5m
Hostname: test-deployment-app-whoami-6cf9cd6bf4-c7cht
Hostname: test-deployment-app-whoami-6cf9cd6bf4-c7cht
Hostname: test-deployment-app-whoami-6cf9cd6bf4-bb7v7
Hostname: test-deployment-app-whoami-6cf9cd6bf4-jhtqz
Hostname: test-deployment-app-whoami-6cf9cd6bf4-px24h
- 删除 service
```bash
[root@tvm-00 ~]# kubectl delete services -l app=whoami
service "test-deployment-app-whoami" deleted
[root@tvm-00 ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d
[root@tvm-00 ~]# cat ~/k8s_install/test/whoami/app.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: app-whoami
labels:
app: whoami
spec:
replicas: 3
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: opera443399/whoami:0.9
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: svc-whoami
labels:
app: whoami
spec:
selector:
app: whoami
ports:
- 执行
```bash
[root@tvm-00 ~]# kubectl apply -f whoami/app.yaml --record
deployment "app-whoami" created
service "svc-whoami" created
[root@tvm-00 ~]# kubectl get all -l app=whoami
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/app-whoami 3 3 3 3 25s
NAME DESIRED CURRENT READY AGE
rs/app-whoami-6cf9cd6bf4 3 3 3 25s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/app-whoami 3 3 3 3 25s
NAME DESIRED CURRENT READY AGE
rs/app-whoami-6cf9cd6bf4 3 3 3 25s
NAME READY STATUS RESTARTS AGE
po/app-whoami-6cf9cd6bf4-2pxlh 1/1 Running 0 25s
po/app-whoami-6cf9cd6bf4-82ng2 1/1 Running 0 25s
po/app-whoami-6cf9cd6bf4-msbmk 1/1 Running 0 25s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/svc-whoami NodePort 10.96.100.22 <none> 80:30080/TCP 25s
- 测试
```bash
[root@tvm-00 ~]# curl -s 10.96.100.22:80 |grep Hostname
Hostname: app-whoami-6cf9cd6bf4-msbmk
[root@tvm-00 ~]# curl -s tvm-00:30080 |grep Hostname
Hostname: app-whoami-6cf9cd6bf4-2pxlh
[root@tvm-00 ~]# curl -s tvm-01:30080 |grep Hostname
Hostname: app-whoami-6cf9cd6bf4-2pxlh
[root@tvm-00 ~]# curl -s tvm-02:30080 |grep Hostname
Hostname: app-whoami-6cf9cd6bf4-msbmk
标签:外部 tab more clust 类型 随机 node seq probe
原文地址:http://blog.51cto.com/nosmoking/2062903