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: 
    app: 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-76d6c9b8c-hus7d   1/1     Running   0          44s
nginx-pod-76d6c9b8c-3dhjd   1/1     Running   0          44s
nginx-pod-76d6c9b8c-dhasi   1/1     Running   0          44s
[root@k8s-master manifest]# 

3. 创建service

[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-76d6c9b8c-hus7d  1/1     Running   0          13m
nginx-pod-76d6c9b8c-3dhjd   1/1     Running   0          13m
nginx-pod-76d6c9b8c-dhasi   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-76d6c9b8c-hus7d   1/1     Running   0          14m   10.232.2.44   k8s-node2   <none>           <none>
nginx-pod-76d6c9b8c-3dhjd   1/1     Running   0          14m   10.232.1.27   k8s-node1   <none>           <none>
nginx-pod-76d6c9b8c-dhasi   1/1     Running   0          14m   10.232.2.45   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.72.15.134   <none>        80:30274/TCP   2m47s

[root@k8s-master manifest]# kubectl get -f nginx-svc.yml 
NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
containerlab   NodePort   10.72.15.134   <none>        80:30274/TCP   43s
[root@k8s-master manifest]# curl 10.72.15.134
<!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]#

效果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值