# homework **Repository Path**: leechao/homework ## Basic Information - **Project Name**: homework - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-11-30 - **Last Updated**: 2023-11-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # homework ## 1、创建nginx的空间 创建名为nginx的命名空间。在Kubernetes集群上执行以下命令: kubectl create namespace nginx ```bash [root@linux-node1 salt-kubeadm]# kubectl create namespace nginx namespace/nginx created [root@linux-node1 salt-kubeadm]# kubectl get ns NAME STATUS AGE default Active 7h1m ingress-nginx Active 5h34m kube-flannel Active 7h1m kube-node-lease Active 7h1m kube-public Active 7h1m kube-system Active 7h1m nginx Active 16s [root@linux-node1 salt-kubeadm]# ``` ## 2 创建Deployment 在/apps/haohua 下新建nginx-deployment.yaml 创建并设置一个副本 创建Nginx Deployment并设置1个副本: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx1.13.12 ports: - containerPort: 80 ``` kubectl apply -f nginx-deployment.yaml ```bash [root@linux-node1 haohua]# [root@linux-node1 haohua]# kubectl get all -n nginx NAME READY STATUS RESTARTS AGE pod/nginx-deployment-7b5975b44b-999q7 1/1 Running 0 14m pod/nginx-deployment-7b5975b44b-ldpmm 1/1 Running 0 14m pod/nginx-deployment-7b5975b44b-q9pwn 1/1 Running 0 19m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx-deployment 3/3 3 3 71m NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-deployment-64686b4fdb 0 0 0 71m replicaset.apps/nginx-deployment-689c8b6988 0 0 0 19m replicaset.apps/nginx-deployment-7b5975b44b 3 3 3 19m ``` ## 创建Service 要通过NodePort访问Nginx,请使用以下YAML文件(nginx-service.yaml)创建一个使用NodePort暴露端口32222的Service: ```ymal apiVersion: v1 kind: Service metadata: name: nginx-service namespace: nginx spec: type: NodePort selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 32222 ``` ## 创建 nginx-ingress.ymal [root@linux-node1 workloads]# cat nginx-ingress2.yaml ```ymal apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nginx-ingress namespace: nginx annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: nginx.k8stest.com http: paths: - path: / pathType: Prefix backend: service: name: nginx-service port: number: 80 ``` ```bash [root@linux-node1 nginx-ingress]# k get ingress -n nginx NAME CLASS HOSTS ADDRESS PORTS AGE nginx-ingress nginx.k8stest.com 80 10m [root@linux-node1 nginx-ingress]# ``` ## host 解析至ingress控制器节点 ```bash [root@linux-node1 nginx-ingress]# cat /etc/hosts ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 172.31.168.46 iZbp183iiuhaznqycgklczZ iZbp183iiuhaznqycgklczZ 172.31.168.46 linux-node1 linux-node1.example.com 172.31.168.45 linux-node2 linux-node2.example.com 172.31.168.44 linux-node3 linux-node3.example.com 172.31.168.43 linux-node4 linux-node4.example.com 121.40.113.189 www.example.com 121.40.113.189 www.k8stest.com 121.40.113.189 nginx.k8stest.com [root@linux-node1 nginx-ingress]# ``` ## 访问 nginx.k8stest.com ```bash [root@linux-node1 nginx-ingress]# curl --head nginx.k8stest.com HTTP/1.1 200 OK Date: Mon, 27 Nov 2023 09:12:47 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT ETag: "5acb8e45-264" Accept-Ranges: bytes`这里输入代码` ```