K8S 使用deploy部署nginx
1. K8S 使用deployment 部署nginx服务
deploy文件如下:
[root@k8s-master ~]# cat deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: ngx-service labels: app: nginx spec: type: NodePort selector: app: nginx ports: - port: 80 targetPort: 80 nodePort: 32500
创建deploy
# kubectl apply -f deploy.yaml deployment.apps/nginx-deploy created service/ngx-service created
查看pod及svc服务
# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/nginx-deploy-cc7df4f8f-5ntrk 1/1 Running 0 34s pod/nginx-deploy-cc7df4f8f-q44p5 1/1 Running 0 34s pod/nginx-deploy-cc7df4f8f-stgv6 1/1 Running 0 34s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/ngx-service NodePort 10.1.171.28 <none> 80:32500/TCP 34s
浏览器访问地址:ip:32500,正常即可.
apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: selector: matchLabels: app: my-nginx template: metadata: labels: app: my-nginx spec: containers: - name: my-nginx image: nginx ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: ngx-service labels: app: my-nginx spec: type: NodePort ports: - port: 80 protocol: TCP name: http selector: app: my-nginx
2. 命令行方式创建nginx服务
(1)、创建一个nginx的pod 现在我们在kubernetes集群中创建一个nginx的pod,验证是否能正常运行。 在master节点执行一下步骤: [root@k8s-master ~]# kubectl create deployment nginx --image=nginx deployment.apps/nginx created [root@k8s-master ~]# kubectl expose deployment nginx --port=80 --type=NodePort service/nginx exposed [root@k8s-master ~]# 现在我们查看pod和service [root@k8s-master ~]# kubectl get pod,svc -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/nginx-f89759699-rrg5g 1/1 Running 0 3m33s 10.244.2.5 k8s-node02 <none> <none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 71m <none> service/nginx NodePort 10.1.12.175 <none> 80:30990/TCP 3m22s app=nginx [root@k8s-master ~]# 打印的结果中,前半部分是pod相关信息,后半部分是service相关信息。我们看service/nginx这一行可以看出service暴漏给集群的端口是30990。记住这个端口。 然后从pod的详细信息可以看出此时pod在node2节点之上。node2节点的IP地址是192.168.10.40 (2)、访问nginx验证集群 那现在我们访问一下。打开浏览器(建议火狐浏览器),访问地址就是: http://192.168.10.40:30990
deploy部署tomcat脚本.
部署tomcat脚本:
apiVersion: apps/v1 kind: Deployment metadata: labels: app: tomcat name: tomcat spec: replicas: 3 selector: matchLabels: app: tomcat template: metadata: labels: app: tomcat spec: containers: - image: tomcat:7.0 imagePullPolicy: IfNotPresent name: tomcat8 ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: tomcat spec: ports: - port: 8099 targetPort: 8080 nodePort: 30899 #浏览器访问的端口 selector: app: tomcat type: NodePort