Create deployment
kubectl create deployment (deployment name) --image=nginx
ClusterIP
kubectl expose deployment -n (namespace name) (deployments name) --type=ClusterIP --port=80 --target-port=80 --name=(service name)
NodePort
kubectl expose deployment -n (namespace name) (deployments name) --type=NodePort --port80 --target-port=80 --name=(service name) --dry-run=client -o yaml > testNodePort.yaml
(修正前) testNodePort.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: front-end
name: front-end-nodesvc
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: front-end
type: NodePort
status:
loadBalancer: {}
(修正後) testNodePort.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: front-end
name: front-end-nodesvc
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 32000
selector:
app: front-end
type: NodePort
(修正後 複数のポートを指定する場合) testNodePort.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: front-end
name: front-end-nodesvc
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 32000
- port: 443
protocol: TCP
targetPort: 443
nodePort: 30300
selector:
app: front-end
type: NodePort
kubectl apply -f ./testNodePort.yaml
kubectl get svc
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# front-end-nodesvc NodePort 10.104.26.176 <none> 80:30200/TCP 5s