原文网址:K8S--安装Nginx-CSDN博客
本文介绍K8S安装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>
此配置文件用于创建Namespace、Deployment、Service。
到/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 #与Deployment.spec.template.metadata的labels对应
spec:
# 副本的数量
replicas: 1
selector:
# 选择app=nginx的Pod
matchLabels:
app: nginx #与Deployment.spec.template.metadata的labels对应
# 选择或创建的Pod的模板
template:
metadata:
# Pod的标签元数据。与上边Deployment.spec.selector.matchLabels对应
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
# 与Deployment.spec.template.metadata.labels对应
selector:
app: nginx
# NodePort类型可以对外暴露端口
type: NodePort
到/work/devops/k8s/app/nginx/路径下,执行此命令:
kubectl apply -f k8s.yaml
结果:
kubectl get pods -A
结果:?
kubectl get services -A
结果?
访问:ip:nodePort端口号
curl 10.100.95.158:8000
结果:
先进入容器内部,再访问容器端口。
1.找到pod的名字
kubectl get pods -A
结果:
2.进入容器内部?
kubectl exec nginx-deployment-557c7b98b6-8l5r5 -n middle -it -- bash
结果:
?
3.访问Nginx
curl localhost:80
结果
备注:可以通过Ctrl+D退出。?
到/work/devops/k8s/app/nginx/路径下,执行此命令:
kubectl delete -f k8s.yaml
结果:
?