无法在我的 GKE 集群中将服务应用为 "type: ClusterIP"

Can not apply Service as "type: ClusterIP" in my GKE cluster

我想将我的服务部署为 ClusterIP,但无法针对给定的错误消息应用它:

[xetra11@x11-work coopr-infrastructure]$ kubectl apply -f teamcity-deployment.yaml 
deployment.apps/teamcity unchanged
ingress.extensions/teamcity unchanged
The Service "teamcity" is invalid: spec.ports[0].nodePort: Forbidden: may not be used when `type` is 'ClusterIP'

这是我的 .yaml 文件:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: teamcity
  labels:
    app: teamcity
spec:
  replicas: 1
  selector:
    matchLabels:
      app: teamcity
  template:
    metadata:
      labels:
        app: teamcity
    spec:
      containers:
      - name: teamcity-server
        image: jetbrains/teamcity-server:latest
        ports:
        - containerPort: 8111
---
apiVersion: v1
kind: Service
metadata:
  name: teamcity
  labels:
    app: teamcity
spec:
  type: ClusterIP
  ports:
  - port: 8111
    targetPort: 8111
    protocol: TCP
  selector:
    app: teamcity
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: teamcity
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  backend:
    serviceName: teamcity
    servicePort: 8111

在 GKE 上,ingress 只能指向类型为 LoadBalancerNodePort 的服务,您可以通过 运行:

查看 ingress 的错误输出
kubectl describe ingress teamcity

根据您的 yaml,如果您使用的是 nginx 控制器,您可能会看到一个错误,您必须使用 NodePort

类型的服务

Somo 文档:

https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md#gce-gke

按文件名将配置应用于资源:

kubectl apply -f [.yaml file] --force

如果该资源尚不存在,将创建该资源。要使用 'apply',始终先使用 'apply' 或 'create --save-config' 创建资源。

2) 如果第一个失败,你可以强制替换,删除然后重新创建资源:

kubectl replace -f grav-deployment.yml

此命令仅在grace-period=0时使用。如果为真,则立即从 API 中删除资源并绕过正常删除。请注意,立即删除某些资源可能会导致不一致或数据丢失,需要确认。

您最近是否将服务说明从 NodePort 更改为 ClusterIP

那么可能就是这个问题github.com/kubernetes/kubectl/issues/221

您需要使用kubectl replacekubectl apply --force