k8s 编写nginx资源文件
通过explain定义deployment类型的控制器跑一个nginx的pod,要做端口映射,要能够在宿主机外面访问
·
k8s 编写nginx资源文件
文章目录
1. 要求
- 通过explain定义deployment类型的控制器跑一个nginx的pod,要做端口映射,要能够在宿主机外面访问
2. 编写用一个资源文件用来运行nginx pod
[root@k8s-master manifest]# vim nginx-pod.yml
[root@k8s-master manifest]# kubectl apply -f nginx-pod.yml
deployment.apps/nginx-pod created
[root@k8s-master manifest]# cat nginx-pod.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
abb: mushuang
app: test
name: nginx-pod
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginxlab
template:
metadata:
labels:
app: nginxlab
name: containerlab
namespace: dev
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
[root@k8s-master manifest]#
[root@k8s-master manifest]# kubectl get -f nginx-pod.yml
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-pod 3/3 3 3 34s
[root@k8s-master manifest]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-pod-5c7b58d6bb-bltl9 1/1 Running 0 44s
nginx-pod-5c7b58d6bb-p5hwr 1/1 Running 0 44s
nginx-pod-5c7b58d6bb-rlzn4 1/1 Running 0 44s
[root@k8s-master manifest]#
3. 创建service
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d21h
[root@k8s-master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-pod-5c7b58d6bb-bltl9 1/1 Running 0 2m43s
nginx-pod-5c7b58d6bb-p5hwr 1/1 Running 0 2m43s
nginx-pod-5c7b58d6bb-rlzn4 1/1 Running 0 2m43s
[root@k8s-master ~]#
[root@k8s-master manifest]# cat nginx-svc.yml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginxlab
name: containerlab
namespace: dev
spec:
ports:
- name: svcport
port: 80
targetPort: 80
protocol: TCP
selector:
app: nginxlab
type: NodePort
[root@k8s-master manifest]#
[root@k8s-master manifest]# kubectl apply -f nginx-svc.yml
service/containerlab created
3.1 查看并在集群中访问
[root@k8s-master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-pod-5c7b58d6bb-bltl9 1/1 Running 0 13m
nginx-pod-5c7b58d6bb-p5hwr 1/1 Running 0 13m
nginx-pod-5c7b58d6bb-rlzn4 1/1 Running 0 13m
[root@k8s-master ~]# kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-5c7b58d6bb-bltl9 1/1 Running 0 14m 10.244.2.39 k8s-node2 <none> <none>
nginx-pod-5c7b58d6bb-p5hwr 1/1 Running 0 14m 10.244.1.28 k8s-node1 <none> <none>
nginx-pod-5c7b58d6bb-rlzn4 1/1 Running 0 14m 10.244.2.40 k8s-node2 <none> <none>
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
containerlab NodePort 10.99.32.163 <none> 80:30514/TCP 2m47s
[root@k8s-master manifest]# kubectl get -f nginx-svc.yml
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
containerlab NodePort 10.99.32.163 <none> 80:30514/TCP 43s
[root@k8s-master manifest]# curl 10.99.32.163
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@k8s-master manifest]#
3.2 在浏览器中访问
- http://192.168.232.128:30514/
3.3 查看deploy和svc标签
[root@k8s-master manifest]# kubectl get -f nginx-pod.yml
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-pod 3/3 3 3 29m
[root@k8s-master manifest]# kubectl get -f nginx-svc.yml
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
containerlab NodePort 10.99.32.163 <none> 80:30514/TCP 17m
[root@k8s-master manifest]# kubectl get -f nginx-pod.yml --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-pod 3/3 3 3 29m app=test
[root@k8s-master manifest]# kubectl get -f nginx-svc.yml --show-labels
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS
containerlab NodePort 10.99.32.163 <none> 80:30514/TCP 17m app=nginxlab
[root@k8s-master manifest]#
更多推荐
已为社区贡献5条内容
所有评论(0)