简体   繁体   English

如何使用kubectl补丁更改kubernetes容器的端口?

[英]how to change port of a kubernetes container using kubectl patch?

I want to change the port of a container running on my kubernetes clusture. 我想更改在Kubernetes集群上运行的容器的端口。 Manually I know this can be changed in the underlying YAML file itself.But I want to do this using a command like "kubectl patch" to change the port. 手动,我知道可以在基础YAML文件本身中进行更改。但是我想使用“ kubectl patch”之类的命令来更改端口。

Nginx.yaml Nginx.yaml


apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels: 
    app: nginx 
spec:
  type: NodePort     
  ports:
  - name: nginxport
    port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    app: nginx
    tier: frontend    

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nginx
        tier: frontend
    spec:

      containers:
      - image: suji165475/devops-sample:#{Build.BuildId}#
        name: nginx
        ports:
        - containerPort: 80
          name: nginxport

Can anybody show me an example of the "kubectl patch" command using my nginx.yaml as an example for changing the container properties like the containerport,targetport,nodeport,port. 有人可以使用我的nginx.yaml作为更改容器属性(例如containerport,targetport,nodeport,port)的示例,向我展示“ kubectl patch”命令的示例。 And i also want to know on what basis the kubectl patch is applied.I mean how does it know what container to patch and on what criteria like containerid,name etc because later I will be creating a html button to do a kubectl patch based on some criteria like the containerid or name.So kindly help. 我也想知道在什么基础上应用kubectl补丁。我的意思是它如何知道要对哪个容器进行补丁以及以什么标准(例如containerid,name等)知道,因为稍后我将创建一个html按钮来基于以下内容进行kubectl补丁一些条件,例如containerid或名称。请提供帮助。

say, you want to update the target port to 8080 in service. 例如,您要在使用中将目标端口更新为8080。 follow the below steps 遵循以下步骤

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels: 
    app: nginx 
spec:
  type: NodePort     
  ports:
  - name: nginxport
    port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    app: nginx
    tier: frontend 

Patch the nginx service using the below command 使用以下命令修补Nginx服务

# kubectl patch svc nginx --patch \
>   '{"spec": { "type": "NodePort", "ports": [ { "nodePort": 30000, "port": 80, "protocol": "TCP", "targetPort": 8080 } ] } }'
service/nginx patched

to update nodeport and targetport use the below command

kubectl patch svc nginx --patch \
  '{"spec": { "type": "NodePort", "ports": [ { "nodePort": 32000, "port": 80, "protocol": "TCP", "targetPort": 8080 } ] } }'

verify that the targetPort is updated to 8080 验证targetPort已更新为8080

master $ kubectl get svc nginx -oyamlapiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2019-08-29T11:08:45Z"
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "5837"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: 5e7f6165-ca4d-11e9-be03-0242ac110042
spec:
  clusterIP: 10.105.220.186
  externalTrafficPolicy: Cluster
  ports:
  - name: nginxport
    nodePort: 30000
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: nginx
    tier: frontend
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

follow similar approach for deployment using 遵循类似的方法进行部署

kubectl patch deploy nginx --patch .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM