K8S/Kubernetes k8s创建服务与公开接口

k8s创建服务与公开接口

8.通过服务访问pod1.创建服务服务创建会用应用的标签确定哪个pod部署服务cat nginx.yml apiVersion: apps/v1beta1kind: Deploymentmetadata: name: nginxspec:replicas: 4template:metadata:labels:app: nginxspec:...

8. 通过服务访问pod

1.创建服务

服务创建会用应用的标签确定哪个pod

部署服务

cat nginx.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: nginx

spec:

 replicas: 4

 template:

  metadata:

   labels:

     app: nginx

  spec:

   containers:

   - name: nginx

     image: nginx

     ports:

     - containerPort: 80 #此处暂不起作用

创建服务

cat servi.yml

apiVersion: v1

kind: Service

metadata:

 name: nginx

spec:

# type: NodePort

 selector:

  app: nginx

 ports:

 - protocol: TCP

#   nodePort: 30000

   port: 8080

   targetPort: 80

 

kubectl apply -f servi.yml

kubectl  get service

image.png

kubectl  describe service nginx #查看服务详细信息

所有实现均通过防火墙实现

2.访问服务

查看命名空间

kubectl get namespace

image.png


kubectl  get deployment --namespace=kube-system #查看kube-dns (服务器)

访问service

可以通过服务名.空间名:端口形式访问

查看服务空间名


image.png


3.创建服务分配不同命名空间

多个服务写入一个文件---分隔

cat ng.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: nginx

 namespace: kube-public

spec:

 replicas: 3

 template:

  metadata:

   labels:

     app: nginx

  spec:

   containers:

   - name: nginx

     image: nginx

     ports:

     - containerPort: 80

 

---

 

apiVersion: v1

kind: Service

metadata:

 name: nginx

 namespace: kube-public

spec:

  selector:

    app: nginx

  ports:

      - protocol: TCP

        port: 8080

        targetPort: 80

 

 

跨空间访问

kubectl  run busybox --rm -ti --image=busybox /bin/sh

image.png

8. 公网访问pod

cat ng.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: nginx

 namespace: kube-public

spec:

 replicas: 3

 template:

  metadata:

   labels:

     app: nginx

  spec:

   containers:

   - name: nginx

     image: nginx

     ports:

     - containerPort: 80

 

---

 

apiVersion: v1

kind: Service

metadata:

 name: nginx

 namespace: kube-public

spec:

  selector:

    app: nginx

  ports:

      - protocol: TCP

        port: 8080

        targetPort: 80

 

---

apiVersion: v1

kind: Service

metadata:

 name: nginx

 namespace: kube-public

spec:

  type: NodePort

  selector:

    app: nginx

  ports:

      - protocol: TCP

        nodePort: 30000  #固定端口30000-32767,不写会自动分配

        port: 8080

        targetPort: 80

 

kubectl get service --namespace=kube-public 查看非默认空间

kubectl get pod --namespace=kube-public

 

 






 




转载于:https://blog.51cto.com/13272050/2156043

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐

  • 浏览量 671
  • 收藏 0
  • 0

所有评论(0)

查看更多评论 
已为社区贡献4条内容