k8s之service

使用deploy-nginx.yaml创建控制器

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx
  namespace: dev
spec: 
  replicas: 3
  selector: 
    matchLabels: 
      run: nginx
  template:
    metadata:
      labels: 
        run: nginx
    spec:
      containers:
      - image: nginx:1.17.1
        name: nginx
        ports: 
        - containerPort: 80
          protocol: TCP
[root@master ~]# kubectl create -f deploy-nginx.yaml 
deployment.apps/nginx created

 

查看pod列表

[root@master ~]# kubectl get pod -n dev -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
nginx-64777cd554-4m2nj   1/1     Running   0          3m30s   10.244.1.15   node2   <none>           <none>
nginx-64777cd554-lp8v2   1/1     Running   0          3m30s   10.244.1.16   node2   <none>           <none>
nginx-64777cd554-w2m2t   1/1     Running   0          3m30s   10.244.2.9    node1   <none>           <none>

删除其中一个pod

[root@master ~]# kubectl delete pod nginx-64777cd554-4m2nj -n dev
pod "nginx-64777cd554-4m2nj" deleted

查看新的pod

[root@master ~]# kubectl get pod -n dev -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
nginx-64777cd554-lp8v2   1/1     Running   0          7m32s   10.244.1.16   node2   <none>           <none>
nginx-64777cd554-rlbm8   1/1     Running   0          47s     10.244.1.17   node2   <none>           <none>
nginx-64777cd554-w2m2t   1/1     Running   0          7m32s   10.244.2.9    node1   <none>           <none>

 发现ip地址和以前不一样了

Service介绍 

虽然每个pod都会分配一个单独的pod ip,然而却存在如下问题:

  • pod ip 会随着pod的重建产生变化
  • pod ip仅仅是集群内可见的虚拟ip,外部无法访问

这样对于访问这个服务带来了难度,因此k8s设计了service来解决这个问题

service可以看作是一组同类pod对外的访问接口,借助service,应用可以方便地实现服务发现和负载均衡

 

创建集群内部可访问的service

暴露service

[root@master ~]# kubectl expose deployment nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
service/svc-nginx1 exposed
[root@master ~]# kubectl get service -n dev
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
svc-nginx1   ClusterIP   10.100.210.11   <none>        80/TCP    44s
#这里产生了一个ClusterIP,这就是service的ip,在service的生命周期中,这个地址是不会变的,可以通过这个Ip访问当前service对应的pod

#访问service [root@master
~]# curl 10.100.210.11:80 <!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>

创建集群外部也可以访问的service

上面搭建的service的type类型为ClusterIP,这个ip地址只能集群内部可访问

如果需要创建外部也可以访问的Service,需要修改type为NodePort

[root@master ~]# kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
service/svc-nginx2 exposed
[root@master ~]# kubectl get svc -n dev
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc-nginx1   ClusterIP   10.100.210.11   <none>        80/TCP         20m
svc-nginx2   NodePort    10.104.222.53   <none>        80:32714/TCP   67s

在浏览器中访问

 

 删除service

[root@master ~]# kubectl delete svc svc-nginx2 -n dev
service "svc-nginx2" deleted
[root@master ~]# kubectl delete svc svc-nginx1 -n dev
service "svc-nginx1" deleted

使用配置方式

apiVersion: v1
kind: Service
metadata: 
  name: svc-nginx
  namespace: dev
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx
  type: ClusterIP

使用配置文件

[root@master ~]# vim svc-nginx.yaml
[root@master ~]# kubectl create -f svc-nginx.yaml 
service/svc-nginx created
[root@master ~]# kubectl get svc -n dev
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
svc-nginx   ClusterIP   10.102.204.135   <none>        80/TCP    14s

删除

[root@master ~]# kubectl delete -f svc-nginx.yaml 
service "svc-nginx" deleted
[root@master ~]# kubectl get svc -n dev
No resources found in dev namespace.

 

posted @ 2021-07-02 21:55  路过的柚子厨  阅读(157)  评论(0编辑  收藏  举报