环境: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
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 10m default-scheduler Successfully assigned default/my-nginx-546db9c8f7-wxtzl to k8s-slave1
Normal SandboxChanged 8m34s (x3 over 8m41s) kubelet, k8s-slave1 Pod sandbox changed, it will be killed and re-created.
Normal Pulling 6m36s (x3 over 10m) kubelet, k8s-slave1 Pulling image "nginx"
Warning Failed 5m20s (x3 over 8m41s) kubelet, k8s-slave1 Failed to pull image "nginx": rpc error: code = Unknown desc = context canceled
Warning Failed 5m20s (x3 over 8m41s) kubelet, k8s-slave1 Error: ErrImagePull
Warning Failed 4m44s (x7 over 8m38s) kubelet, k8s-slave1 Error: ImagePullBackOff
Normal BackOff 2s (x14 over 8m38s) kubelet, k8s-slave1 Back-off pulling image "nginx"
原因:拉取nginx的镜像失败
解决方法:手动通过docker pull nginx 拉一次镜像下来。若手动拉取也一直卡住,则尝试重启node节点主机(因为我是在虚拟机上搭建的,所以可以随意重启,哈哈)
5.验证nginx是否正常提供服务
5.2 在k8s集群环境,通过clusterIp访问
因为安装了calico,所以可以直接在所有节点机器上执行该操作
5.3 通过podIP(当前其中一个nginx pod ip为192.168.92.52,另一个是192.168.0.100)访问