2020-09-10-k8s-service

创建service

[root@master1 yamldir]# kubectl apply -f 03_create_deployment_nginx_app2.yaml 
deployment.apps/nginx-app2 created
[root@master1 yamldir]# kubectl get deployment.apps
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
nginx-app2   1/1     1            1           32s
[root@master1 yamldir]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
nginx-app2-7b78bfb8c8-wzvc9   1/1     Running   0          70s
[root@master1 yamldir]# 
[root@master1 yamldir]# kubectl expose deployment.apps nginx-app2 --type=ClusterIP --target-port=80 --port=80
service/nginx-app2 exposed
[root@master1 yamldir]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   8h
nginx-app2   ClusterIP   10.106.82.170   <none>        80/TCP    31s
[root@master1 yamldir]# curl http://10.106.82.170
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master1 yamldir]# kubectl get endpoints
NAME         ENDPOINTS              AGE
kubernetes   192.168.216.100:6443   8h
nginx-app2   172.16.235.131:80      90s
[root@master1 yamldir]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP               NODE      NOMINATED NODE   READINESS GATES
nginx-app2-7b78bfb8c8-wzvc9   1/1     Running   0          4m49s   172.16.235.131   worker1   <none>           <none>
[root@master1 yamldir]# 

按资源清单方式

[root@master1 yamldir]# cat 04_create_deployment_nginx_app3_service.yaml
---
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: nginx-app3
spec: 
  replicas: 2 
  selector: 
    matchLabels:
       apps: nginx 
  template: 
    metadata: 
      labels: 
        apps: nginx 
    spec: 
      containers: 
      - name: nginxapp3 
        image: nginx:latest 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app3-svc
spec:
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    apps: nginx
[root@master1 yamldir]# kubectl apply -f 04_create_deployment_nginx_app3_service.yaml
deployment.apps/nginx-app3 created
service/nginx-app3-svc created
[root@master1 yamldir]# kubectl get deployment.apps
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
nginx-app2   1/1     1            1           31m
nginx-app3   2/2     2            2           39s
[root@master1 yamldir]# kubectl get service
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP   8h
nginx-app2       ClusterIP   10.106.82.170    <none>        80/TCP    28m
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP    11m
[root@master1 yamldir]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP   8h
nginx-app2       ClusterIP   10.106.82.170    <none>        80/TCP    29m
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP    12m
[root@master1 yamldir]# kubectl get endpoints
NAME             ENDPOINTS                            AGE
kubernetes       192.168.216.100:6443                 8h
nginx-app2       172.16.235.131:80                    29m
nginx-app3-svc   172.16.189.69:80,172.16.235.132:80   12m
[root@master1 yamldir]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
nginx-app2-7b78bfb8c8-wzvc9   1/1     Running   0          33m
nginx-app3-86984c5bb9-f8dg2   1/1     Running   0          2m50s
nginx-app3-86984c5bb9-t8w4x   1/1     Running   0          2m50s
[root@master1 yamldir]# kubectl exec -it nginx-app3-86984c5bb9-f8dg2 sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /usr/share/nginx/html  
# ls
50x.html  index.html
# echo "xiaobu pod1" >index.html       
# cat index.html
xiaobu pod1
# exit
You have new mail in /var/spool/mail/root
[root@master1 yamldir]# kubectl exec -it nginx-app3-86984c5bb9-t8w4x sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /usr/share/nginx/html
# ls
50x.html  index.html
# echo "xiaobu pod2">index.html
# cat index.html
xiaobu pod2
# exit
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod2
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# 

资源清单创建nodeport类型service

[root@master1 yamldir]# cat 05_create_deployment_nginx_app5_service.yaml
---
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: nginx-app5
spec: 
  replicas: 2 
  selector: 
    matchLabels:
       apps: nginx-app5 
  template: 
    metadata: 
      labels: 
        apps: nginx-app5 
    spec: 
      containers: 
      - name: nginxapp5 
        image: nginx:latest 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app5-svc
spec:
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    apps: nginx-app5
[root@master1 yamldir]# kubectl apply -f 05_create_deployment_nginx_app5_service.yaml 
deployment.apps/nginx-app5 created
service/nginx-app5-svc created
[root@master1 yamldir]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        9h
nginx-app2       ClusterIP   10.106.82.170    <none>        80/TCP         50m
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP         33m
nginx-app5-svc   NodePort    10.106.96.13     <none>        80:30844/TCP   22s
[root@master1 yamldir]# ss -anput|grep 30844
tcp    LISTEN     0      128       *:30844                 *:*                   users:(("kube-proxy",pid=18555,fd=11))
[root@worker1 ~]# ss -anput|grep 30844
tcp    LISTEN     0      128       *:30844                 *:*                   users:(("kube-proxy",pid=17785,fd=11))
You have new mail in /var/spool/mail/root
[root@worker1 ~]# 
*nginx首页
http://192.168.216.100:30844/
http://192.168.216.101:30844/
http://192.168.216.102:30844/
指定端口30001
[root@master1 yamldir]# cat 06_create_deployment_nginx_app6_service.yaml 
---
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: nginx-app6
spec: 
  replicas: 2 
  selector: 
    matchLabels:
       apps: nginx-app6 
  template: 
    metadata: 
      labels: 
        apps: nginx-app6 
    spec: 
      containers: 
      - name: nginxapp6
        image: nginx:latest 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app6-svc
spec:
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30001
  selector:
    apps: nginx-app6
[root@master1 yamldir]# kubectl apply -f 06_create_deployment_nginx_app6_service.yaml 
deployment.apps/nginx-app6 created
service/nginx-app6-svc created
[root@master1 yamldir]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        9h
nginx-app2       ClusterIP   10.106.82.170    <none>        80/TCP         58m
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP         41m
nginx-app5-svc   NodePort    10.106.96.13     <none>        80:30844/TCP   7m47s
nginx-app6-svc   NodePort    10.100.30.251    <none>        80:30001/TCP   27s
http://192.168.216.100:30001/
http://192.168.216.102:30001/

删除:命令行和资源清单两种方式

[root@master1 yamldir]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        9h
nginx-app2       ClusterIP   10.106.82.170    <none>        80/TCP         66m
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP         49m
nginx-app5-svc   NodePort    10.106.96.13     <none>        80:30844/TCP   15m
nginx-app6-svc   NodePort    10.100.30.251    <none>        80:30001/TCP   8m28s
[root@master1 yamldir]# kubectl delete service ngix-app2
Error from server (NotFound): services "ngix-app2" not found
[root@master1 yamldir]# kubectl delete service nginx-app2
service "nginx-app2" deleted
[root@master1 yamldir]# kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        9h
nginx-app3-svc   ClusterIP   10.110.142.231   <none>        80/TCP         50m
nginx-app5-svc   NodePort    10.106.96.13     <none>        80:30844/TCP   17m
nginx-app6-svc   NodePort    10.100.30.251    <none>        80:30001/TCP   9m51s
[root@master1 yamldir]# ls
01_create_ns.yaml   03_create_deployment_nginx_app2.yaml          05_create_deployment_nginx_app5_service.yaml
02_create_pod.yaml  04_create_deployment_nginx_app3_service.yaml  06_create_deployment_nginx_app6_service.yaml
[root@master1 yamldir]# kubectl delete -f 03_create_deployment_nginx_app2.yaml 
deployment.apps "nginx-app2" deleted
[root@master1 yamldir]# kubectl delete -f 04_create_deployment_nginx_app3_service.yaml 
deployment.apps "nginx-app3" deleted
service "nginx-app3-svc" deleted
[root@master1 yamldir]# kubectl delete -f 05_create_deployment_nginx_app5_service.yaml 
deployment.apps "nginx-app5" deleted
service "nginx-app5-svc" deleted
[root@master1 yamldir]# kubectl delete -f 06_create_deployment_nginx_app6_service.yaml 
deployment.apps "nginx-app6" deleted
service "nginx-app6-svc" deleted
[root@master1 yamldir]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   9h
[root@master1 yamldir]# kubectl get pods
No resources found in default namespace.
[root@master1 yamldir]# kubectl get deployment.apps
No resources found in default namespace.
[root@master1 yamldir]# 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。