如何使用kubectl补丁更改kubernetes容器的端口? [英] how to change port of a kubernetes container using kubectl patch?
问题描述
我想更改在我的kubernetes集群上运行的容器的端口.手动,我知道可以在基础YAML文件本身中进行更改.但是我想使用"kubectl patch"之类的命令来更改端口.
I want to change the port of a container running on my kubernetes clusture. 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.
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
任何人都可以使用我的nginx.yaml作为更改容器属性(例如containerport,targetport,nodeport,port)的示例,向我展示"kubectl patch"命令的示例.我也想知道在什么基础上应用kubectl补丁.我的意思是它如何知道要对哪个容器进行补丁以及以什么标准(例如containerid,name等)知道,因为稍后我将创建一个html按钮来基于以下内容进行kubectl补丁一些条件,例如containerid或名称.请提供帮助.
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. 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.
推荐答案
例如,您要将服务中的目标端口更新为8080.请按照以下步骤
say, you want to update the target port to 8080 in service. 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
使用以下命令修补nginx服务
Patch the nginx service using the below command
# 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 } ] } }'
验证targetPort已更新为8080
verify that the targetPort is updated to 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: {}
遵循类似的部署方式
kubectl patch deploy nginx --patch .....
这篇关于如何使用kubectl补丁更改kubernetes容器的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!