标签:condition 验证 template erp dex resources ati ted 搭建nginx
环境:k8s v1.18.5
网络环境: calico,通过nodePort方式对外提供nginx服务
一、 创建nginx的service
1.定义nginx的service(nginx-service.yml)
apiVersion: v1
kind: Service
metadata:
name: nginx-service #定义service名称为nginx-service
labels:
app: nginx-service #为service打上app标签
spec:
type: NodePort #使用NodePort方式开通,在每个Node上分配一个端口作为外部访问入口
selector:
app: my-nginx
ports:
- port: 8000 #port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service
targetPort: 80 #targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器
nodePort: 32500 #nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service
端口type类型:
? ClusterIP:默认,分配一个集群内部可以访问的虚拟IP(VIP)
? NodePort:在每个Node上分配一个端口作为外部访问入口
? LoadBalancer:工作在特定的Cloud Provider上,例如Google Cloud,AWS,OpenStack
2.创建nginx-service服务
kubectl create -f nginx-service.yml
3.验证服务是否创建成功
kubectl get svc
二、创建ngixn的configmap配置文件(nginx-configmap.yml)
1.定义nginx的configmap配置
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configmap
data:
nginx_conf: |-
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.创建nginx-configmap
kubectl create -f nginx-configmap.yml
3.验证是否创建成功
kubectl get cm
三、创建nginx的deployment
1.定义nginx的deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
app: my-nginx
replicas: 2
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx
subPath: nginx.conf
volumes:
- name: nginx
configMap:
name: nginx-configmap
items:
- key: nginx_conf
path: nginx.conf
#resources:
# requests:
# cpu: 1
# memory: 500Mi
# limits:
# cpu: 2
# memory: 1024Mi
2.创建nginx的deployment
kubectl create -f nginx-deployment.yml
3.验证deployment是否创建成功
kubectl get deploy
my-nginx就是创建的deploy,但是ready状态是0,证明pod为能成功创建。后面排查发现,pod一直卡在了容器创建阶段
4.验证deployment对应的pod是否创建成功
kubectl get pod
容器一直卡在容器创建阶段,需排查问题。
通过查看某个容器的详细信息,发现是nginx的configmap的问题:
kubectl describe pod my-nginx-65b859bc7b-qvh2t
Warning FailedMount 7m56s (x35 over 62m) kubelet, k8s-slave1 MountVolume.SetUp failed for volume "nginx" : configmap "nginx_configmap" not found
Warning FailedMount 2m (x23 over 60m) kubelet, k8s-slave1 Unable to attach or mount volumes: unmounted volumes=[nginx], unattached volumes=[nginx default-token-jd5zv]: timed out waiting for the condition。
原因:拉取nginx的最新镜像失败
解决方法:手动通过docker pull nginx 拉一次镜像下来
k8s 搭建nginx实例(service、configmap和deployment组合)
标签:condition 验证 template erp dex resources ati ted 搭建nginx
原文地址:https://www.cnblogs.com/danny-djy/p/13846241.html