# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 25h v1.17.3
k8s-node2 Ready 25h v1.17.3
集群只有两个节点,这里打算将应用部署在k8s-node2节点上,需要先记下这个节点的名称:k8s-node2。
k8s编排文件中指定nodeName,只需这一行配置
nodeName: k8s-node2
完整配置如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: drone-project-go-deployment
namespace: gin-api-project
spec:
selector:
matchLabels:
app: drone-project-go
replicas: 2
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: drone-project-go
spec:
containers:
- name: drone-project-go
image: project-go-api
imagePullPolicy: Always
resources:
requests:
cpu: "50m"
memory: 50Mi
limits:
cpu: "80m"
memory: 80Mi
ports:
- containerPort: 9911
nodeName: k8s-node2 #指定部署节点在k8s-node2
---
apiVersion: v1
kind: Service
metadata:
name: drone-project-go-service
namespace: gin-api-project
labels:
app: drone-project-go
spec:
type: NodePort
selector:
app: drone-project-go
ports:
- port: 9911
targetPort: 9911
nodePort: 30091
以上这段配置部署了一个Go服务,指定节点在k8s-node2,并且限制了资源,同时暴露了一个30091外部访问端口,看下服务是不是部署在指定的节点
服务确实部署在k8s-node2节点上,这样就成功在Kubernetes部署服务到集群中的指定节点。