Linux学习-Kubernetes之Service

Service的三种代理模式
  • userspace:1.1-
  • iptables:1.10-
  • ipvs:1.11+
Service类型
  • ExternalName
  • ClusterIP
  • NodePort
  • LoadBlance
资源记录

SVC_NAME.NS_NAME.DOMAIN.LTD.
默认:svc.cluster.local.

ClusterIP
#查看service定义
[root@master ~]# kubectl explain svc
#创建一个deployment类型的redis的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
        - name: redis
          image: redis:4.0-alpine
          ports:
          - name: redis
            containerPort: 6379
[root@master manifests]# kuberctl apply -f ds-demo.yaml

#为redis创建一个ClusterIP类型的service
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: default
spec:
  selector:
    app: redis
    role: logstor
  clusterIP: 10.97.97.97
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
[root@master manifests]# kubectl apply -f redis-svc.yaml 
service/redis created
[root@master manifests]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d22h
nginx        NodePort    10.105.125.19   <none>        80:31526/TCP   4d23h
redis        ClusterIP   10.97.97.97     <none>        6379/TCP       14s
[root@master manifests]# kubectl describe service redis
Name:              redis
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=redis,role=logstor
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.97.97.97
IPs:               10.97.97.97
Port:              <unset>  6379/TCP
TargetPort:        6379/TCP
Endpoints:         10.244.1.35:6379
Session Affinity:  None
Events:            <none>

NodePort
#myapp的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: tye
  template:
    metadata:
      labels:
        app: myapp
        release: tye
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v4
        ports:
        - name: http
          containerPort: 80
#创建一个NodePort的Service
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: tye
  clusterIP: 10.99.99.99
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
[root@master manifests]# kubectl apply -f myapp-svc.yaml 
service/myapp created
#查看service
[root@master manifests]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d23h
myapp        NodePort    10.99.99.99     <none>        80:30080/TCP   41s
nginx        NodePort    10.105.125.19   <none>        80:31526/TCP   4d23h
redis        ClusterIP   10.97.97.97     <none>        6379/TCP       28m
[root@master manifests]# kubectl describe service myapp
Name:                     myapp
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=myapp,release=tye
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.99.99.99
IPs:                      10.99.99.99
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.244.3.46:80,10.244.3.47:80,10.244.3.49:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
#找一台其它的虚拟机测试访问情况
[root@lotus ~]# while true; do curl http://192.168.88.101:30080/hostname.html;sleep 1; done
myapp-deploy-5cf7978485-jwvtl
myapp-deploy-5cf7978485-gsppv
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-gsppv
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-jwvtl

#修改sessionAffinity属性,添加客户端的session亲密性,同一客户端访问调度至同一个容器
[root@master manifests]# kubectl patch svc myapp  -p '{"spec":{"sessionAffinity":"ClientIP"}}'
service/myapp patched
[root@lotus ~]# while true; do curl http://192.168.88.101:30080/hostname.html; sleep 1;done
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
Headless Service无头服务
apiVersion: v1
kind: Service
metadata:
  name: myapp-headless
  namespace: default
spec:
  selector:
    app: myapp
    release: tye
  clusterIP: None
  ports:
  - port: 80
    targetPort: 80

[root@master manifests]# kubectl apply -f myapp-svc-headless.yaml 
service/myapp-headless created
[root@master manifests]# kubectl get svc
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        5d23h
myapp            NodePort    10.99.99.99     <none>        80:30080/TCP   31m
myapp-headless   ClusterIP   None            <none>        80/TCP         4s
nginx            NodePort    10.105.125.19   <none>        80:31526/TCP   5d
redis            ClusterIP   10.97.97.97     <none>        6379/TCP       59m
#查看service解析情况
[root@master manifests]# dig -t A myapp-headless.default.svc.cluster.local. @10.96.0.10

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.7 <<>> -t A myapp-headless.default.svc.cluster.local. @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1280
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;myapp-headless.default.svc.cluster.local. IN A

;; ANSWER SECTION:
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.47
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.49
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.46

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Fri Nov 12 09:58:24 EST 2021
;; MSG SIZE  rcvd: 237

[root@master manifests]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS      AGE     IP            NODE    NOMINATED NODE   READINESS GATES
myapp-deploy-5cf7978485-b4sdw   1/1     Running   1 (87m ago)   23h     10.244.3.47   node1   <none>           <none>
myapp-deploy-5cf7978485-gsppv   1/1     Running   1 (87m ago)   23h     10.244.3.46   node1   <none>           <none>
myapp-deploy-5cf7978485-jwvtl   1/1     Running   1 (87m ago)   23h     10.244.3.49   node1   <none>           <none>
myapp-ds-9lw8z                  1/1     Running   1 (87m ago)   23h     10.244.1.36   node2   <none>           <none>
myapp-ds-n54vw                  1/1     Running   1 (87m ago)   23h     10.244.3.48   node1   <none>           <none>
nginx                           1/1     Running   6 (87m ago)   5d14h   10.244.1.37   node2   <none>           <none>
redis-56fd57fd76-lcndb          1/1     Running   1 (87m ago)   23h     10.244.1.35   node2   <none>           <none>

通过启动一个Pod来访问service服务
#创建一个nginx-Pod
[root@master manifests]# kubectl create deployment nginx-deploy --image=nginx:1.14-alpine --replicas=1
deployment.apps/nginx-deploy created

#创建一个services通过8080暴露nginx容器的80端口
[root@master manifests]# kubectl expose deployment nginx-deploy --name nginx --port=8080 --target-port=80 
service/nginx exposed
[root@master manifests]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    9h
nginx        ClusterIP   10.101.109.69   <none>        8080/TCP   5s

[root@master manifests]# kubectl run client --image=busybox -it --restart=Never
If you don't see a command prompt, try pressing enter.
/ # 
/ # wget -O - -q nginx:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值