apiVersion: v1
kind: Service
#apiVersion: v1
#kind: Servive
metadata:
name: nginx-svc
namespace: default
spec:
type: NodePort
#定义类型为NodePort
selector:
#标签选择,把需要转发的后端的标签写在下面
app: nginx
ports:
- port: 80
#service端口
targetPort: 80
#容器端口
nodePort: 30080
#node端口,如果是ClusterIP的这段不需要,也可以不填会随机
---
apiVersion: v1
kind: Pod
metadata:
name: ng1
labels:
app: web
spec:
containers:
- name: ng1
image: hub.c.163.com/library/nginx
imagePullPolicy: IfNotPresent
nodeport 和ClusterIP 用法差不多就类型填的不一样,还有ClusterIP不用填nodePort
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: default
spec:
type: NodePort
#定义类型为NodePort
selector:
#标签选择,把需要转发的后端的标签写在下面
app: web
ports:
- port: 90
#service端口
targetPort: 80
#容器端口
nodePort: 30080
#node端口,如果是ClusterIP的这段不需要,也可以不填会随机
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
#表示启动两个容器
selector:
matchLabels:
app: nginx
#匹配标签
template:
#这下面的和pod写法一样
metadata:
labels:
app: nginx
spec:
containers:
- name: ng1
image: hub.c.163.com/library/nginx
imagePullPolicy: IfNotPresent
apiVersion: apps/v1
kind: DaemonSet
#控制器类型dae...,这种控制器会把每一个node的节点都启动一个下面标签匹配的容器,有污点的除外
metadata:
name: nginx-daemonset
namespace: default
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: "ikubernetes/myapp:v1"
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
selector:
app: nginx
clusterIP: None
ports:
- port: 80
targetPort: 80
service的无头服务适用于有些服务直接要和后端的容器建立连接场景使用的
[root@cs25 ~]# kubectl get pod -l app=myapp -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-6c7db749dd-4kjdh 1/1 Running 0 31m 192.168.94.170 cs27 <none> <none>
myapp-deploy-6c7db749dd-x95d6 1/1 Running 0 31m 192.168.100.2 cs25 <none> <none>
#查看两个后端容器的ip
[root@cs25 ~]# dig nginx-service.default.svc.cluster.local
; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> nginx-service.default.svc.cluster.local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16814
;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;nginx-service.default.svc.cluster.local. IN A
;; ANSWER SECTION:
nginx-service.default.svc.cluster.local. 30 IN A 192.168.100.47
nginx-service.default.svc.cluster.local. 30 IN A 192.168.94.168
;; Query time: 4 msec
;; SERVER: 192.168.100.37#53(192.168.100.37)
;; WHEN: 一 12月 23 14:03:19 CST 2019
;; MSG SIZE rcvd: 178
#解析域名 nginx-service.default.svc.cluster.local 看是不是解析的容器ip,上面可以看到两个容器的ip都被解析到了,
#如果这边是service带ip就会解析成service ip由service代理至后端容器,无头服务就是直接解析后端容器ip直接和容器建立连接。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。