题目一:暴露服务service 设置配置环境: [candidate@node-1] $ kubectl config use-context k8s Task 请重新配置现有的 deployment front-end 以及添加名为 http 的端口规范来公开现有容器 nginx 的端口 80/tcp。 创建一个名为 front-end-svc 的新 service,以公开容器端口 http。 配置此 service,以通过各个 Pod 所在的节点上的 NodePort 来公开他们。
解答: # 1.直接修改deployment.yaml [root@master service]# kubectl edit deployment myapp-v1 spec: containers: - image: docker.io/janakiramm/myapp:v1 imagePullPolicy: IfNotPresent name: nginx # 容器名称 ports: # 添加这4行 - containerPort: 80 # 端口 name: http # 添加名称 protocol: TCP # 协议 # 2.将现有的 deploy 暴露成 nodeport 的 service [root@master service]# cat service.yaml apiVersion: v1 kind: Service metadata: name: front-end-svc spec: type: NodePort selector: app: myapp version: v1 ports: - port: 80 targetPort: 80 name: http [root@master service]# kubectl apply -f service.yaml service/front-end-svc created [root@master service]# kubectl get svc front-end-svc -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR front-end-svc NodePort 10.100.226.255 <none> 80:32057/TCP 23s app=myapp,version=v1 [root@master service]# kubectl describe svc front-end-svc Name: front-end-svc Namespace: default Labels: <none> Annotations: <none> Selector: app=myapp,version=v1 Type: NodePort IP Family Policy: SingleStack IP Families: IPv4 IP: 10.100.226.255 IPs: 10.100.226.255 Port: http 80/TCP TargetPort: 80/TCP NodePort: http 32057/TCP Endpoints: 10.244.166.140:80,10.244.166.141:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>
# 命令行式写法 [root@master2 service]# kubectl expose deployment front-end --type=NodePort --port=80 --target-port=http --name=front-end-svc service/front-end-svc exposed [root@master2 service]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE front-end-svc NodePort 10.100.120.100 <none> 80:30230/TCP 4s