“K8s使用deploy部署nginx”的版本间的差异
跳到导航
跳到搜索
(创建页面,内容为“== <pre> k8smaster 192.168.88.70 k8snode1 192.168.88.71 k8snode2192.168.88.72 </pre>”) |
|||
第1行: | 第1行: | ||
− | == | + | =info= |
<pre> | <pre> | ||
k8smaster 192.168.88.70 | k8smaster 192.168.88.70 | ||
k8snode1 192.168.88.71 | k8snode1 192.168.88.71 | ||
k8snode2192.168.88.72 | k8snode2192.168.88.72 | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | =使用deployment 部署nginx服务= | ||
+ | <pre> | ||
+ | deploy文件如下: | ||
+ | |||
+ | root@k8s-master:~# cat ngdeploy.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 ngdeploy.yaml | ||
+ | |||
+ | |||
+ | 查看pod及svc服务 | ||
+ | root@k8s-master:~# kubectl get pod,svc | ||
+ | NAME READY STATUS RESTARTS AGE | ||
+ | pod/my-nginx-585449566-6g2rh 1/1 Running 0 3m37s | ||
+ | pod/my-nginx-585449566-9nwgt 1/1 Running 0 3m37s | ||
+ | pod/my-nginx-585449566-ww28p 1/1 Running 0 3m37s | ||
+ | pod/nginx-deployment 1/1 Running 0 10m | ||
+ | |||
+ | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE | ||
+ | service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h38m | ||
+ | service/ngx-service NodePort 10.96.36.220 <none> 80:32500/TCP 3m37s | ||
+ | root@k8s-master:~# | ||
+ | |||
+ | |||
+ | |||
+ | masterip | ||
+ | http://192.168.88.70:32500/ | ||
+ | </pre> | ||
+ | |||
+ | = 命令行方式创建nginx服务= | ||
+ | <pre> | ||
+ | 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 | ||
+ | |||
+ | |||
+ | 现在我们查看pod和service | ||
+ | |||
+ | [root@k8s-master ~]# kubectl get pod,svc -o wide | ||
+ | NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES | ||
+ | |||
+ | pod/nginx-6799fc88d8-9wjjt 1/1 Running 0 33s 10.244.2.4 k8s-node2 <none> <none> | ||
+ | |||
+ | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR | ||
+ | service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h40m <none> | ||
+ | service/nginx NodePort 10.96.172.103 <none> 80:30346/TCP 22s app=nginx | ||
+ | service/ngx-service NodePort 10.96.36.220 <none> 80:32500/TCP 6m2s app=nginx | ||
+ | root@k8s-master:~# | ||
+ | |||
+ | 打印的结果中,前半部分是pod相关信息,后半部分是service相关信息。我们看service/nginx这一行可以看出service暴漏给集群的端口是 32500。记住这个端口。 | ||
+ | 然后从pod的详细信息可以看出此时pod在node2节点之上。node2节点的IP地址是192.168.88.72 | ||
</pre> | </pre> | ||
+ | =see also= | ||
+ | |||
+ | [https://www.cnblogs.com/saneri/p/14463509.html K8S 使用deploy部署nginx ] | ||
+ | |||
+ | [[category:k8s]] [[category:devops]] [[category: container]] |
2021年8月6日 (五) 13:26的版本
info
k8smaster 192.168.88.70 k8snode1 192.168.88.71 k8snode2192.168.88.72
使用deployment 部署nginx服务
deploy文件如下: root@k8s-master:~# cat ngdeploy.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 ngdeploy.yaml 查看pod及svc服务 root@k8s-master:~# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/my-nginx-585449566-6g2rh 1/1 Running 0 3m37s pod/my-nginx-585449566-9nwgt 1/1 Running 0 3m37s pod/my-nginx-585449566-ww28p 1/1 Running 0 3m37s pod/nginx-deployment 1/1 Running 0 10m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h38m service/ngx-service NodePort 10.96.36.220 <none> 80:32500/TCP 3m37s root@k8s-master:~# masterip http://192.168.88.70:32500/
命令行方式创建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 现在我们查看pod和service [root@k8s-master ~]# kubectl get pod,svc -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/nginx-6799fc88d8-9wjjt 1/1 Running 0 33s 10.244.2.4 k8s-node2 <none> <none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h40m <none> service/nginx NodePort 10.96.172.103 <none> 80:30346/TCP 22s app=nginx service/ngx-service NodePort 10.96.36.220 <none> 80:32500/TCP 6m2s app=nginx root@k8s-master:~# 打印的结果中,前半部分是pod相关信息,后半部分是service相关信息。我们看service/nginx这一行可以看出service暴漏给集群的端口是 32500。记住这个端口。 然后从pod的详细信息可以看出此时pod在node2节点之上。node2节点的IP地址是192.168.88.72