原文网址:K8S--安装Nginx-CSDN博客
简介
本文介绍K8S安装Nginx的方法。
1.创建Nginx目录及配置文件
mkdir -p /work/devops/k8s/app/nginx/{config,html}
在config目录下创建nginx.conf配置文件,内容如下:
# events必须要有
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#日志的路径
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server{
# 监听的端口
listen 80;
server_name _;
root /usr/share/nginx/html/;
index index.html;
}
}
在html目录下创建index.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>K8S搭建nginx</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
2.编写K8S配置文件
此配置文件用于创建Namespace、Deployment、Service。
- Namespace:命名空间,用于区分应用。
- Deployment:用于生成pod。
- Service:用于将pod暴露到集群中,供其他pod或者外部访问。
到/work/devops/k8s/app/nginx/路径下,创建名为k8s.yaml的配置文件
# 创建命名空间,Deployment和Service中要引用。
apiVersion: v1
kind: Namespace
metadata:
name: middle
labels:
name: middle
---
# 创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: middle
labels:
app: nginx
spec:
# 副本的数量
replicas: 1
selector:
# 选择app=nginx的Pod
matchLabels:
app: nginx
# 选择或创建的Pod的模板
template:
metadata:
labels:
app: nginx
spec:
# 容器的路径
containers:
- image: nginx:latest
name: nginx-latest
ports:
- containerPort: 80
name: nginx-80
# 容器内的路径
volumeMounts:
- name: config
mountPath: /etc/nginx/nginx.conf
- name: config-include
mountPath: /etc/nginx/conf.d/
- name: log
mountPath: /var/log/nginx/
- name: html
mountPath: /usr/share/nginx/html/
- name: cert
mountPath: /etc/nginx/cert/
# 主机的路径
volumes:
- name: config #和volumeMounts中的内容要对应
hostPath:
path: /work/devops/k8s/app/nginx/config/nginx.conf
type: File
- name: config-include #和volumeMounts中的内容要对应
hostPath:
path: /work/devops/k8s/app/nginx/config/conf.d/
type: DirectoryOrCreate
- name: log #和volumeMounts中的内容要对应
hostPath:
path: /work/devops/k8s/app/nginx/logs/
type: DirectoryOrCreate
- name: html #和volumeMounts中的内容要对应
hostPath:
path: /work/devops/k8s/app/nginx/html/
type: DirectoryOrCreate
- name: cert #和volumeMounts中的内容要对应
hostPath:
path: /work/devops/k8s/app/nginx/cert/
type: DirectoryOrCreate
---
# 创建Service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: middle
labels:
app: nginx
spec:
ports:
- name: nginx-port
port: 8000 # Service监听的端口
targetPort: 80 # nginx自身暴露的端口。对应Deployment的containerPort
# 对外的端口号
nodePort: 30003
selector:
app: nginx
# NodePort类型可以对外暴露端口
type: NodePort
3.启动Nginx
到/work/devops/k8s/app/nginx/路径下,执行此命令:
kubectl apply -f k8s.yaml
结果:
4.查看启动结果
法1:通过dashboard查看
法2:通过命令查看
kubectl get pods -A
结果:
kubectl get services -A
结果
4.访问Nginx
测试1:外部访问NodePort
访问:ip:nodePort端口号
测试2:访问集群
curl 10.100.95.158:8000
结果:
测试3:访问容器内部
先进入容器内部,再访问容器端口。
1.找到pod的名字
kubectl get pods -A
结果:
2.进入容器内部
kubectl exec nginx-deployment-557c7b98b6-8l5r5 -n middle -it -- bash
结果:
3.访问Nginx
curl localhost:80
结果
备注:可以通过Ctrl+D退出。
5.删除Nginx
到/work/devops/k8s/app/nginx/路径下,执行此命令:
kubectl delete -f k8s.yaml
结果:文章来源:https://www.toymoban.com/news/detail-831512.html
文章来源地址https://www.toymoban.com/news/detail-831512.html
到了这里,关于K8S--安装Nginx的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!