kubernetes Service 関連

#ServiceYamlファイルの書き方

```ruby:myapp-service.yaml
apiVersion: v1
kind: Service
metadata:
 name: myapp
 namespace: default
spec:
 type: ClusterIP
 selector:
  app: myapp
  release: stabel
 ports:
 - name: http
   port: 8080  ###Service Port
   targetPort: 80　　###Myapp Podのポート。ServiceはportからtargetPortへのマッピングを行う。
```

#Service作成
```ruby:ServiceCreation
kubectl apply -f myapp-service.yaml
```

#Service確認
```ruby:ServiceCheck
kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    224d
myapp        ClusterIP   10.99.167.164    <none>        8080/TCP   8s
=========================================================================
###サービス稼働確認
[root@k8s-master01 ~]# curl 10.99.167.164:8080

###hostname確認（Round Robin）
[root@k8s-master01 ~]# curl 10.99.167.164:8080/hostname.html
myapp-deploy-654b86f4cb-crzrq
[root@k8s-master01 ~]# curl 10.99.167.164:8080/hostname.html
myapp-deploy-654b86f4cb-dd7wf
[root@k8s-master01 ~]# curl 10.99.167.164:8080/hostname.html
myapp-deploy-654b86f4cb-hnk76
[root@k8s-master01 ~]# 

```
#IPVS確認
```ruby:IPVS-Check
ipvsadm -Ln
[root@k8s-master01 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr
  -> 192.168.66.10:6443           Masq    1      3          0         
TCP  10.96.0.10:53 rr
  -> 10.244.0.17:53               Masq    1      0          0         
  -> 10.244.0.18:53               Masq    1      0          0         
TCP  10.96.0.10:9153 rr
  -> 10.244.0.17:9153             Masq    1      0          0         
  -> 10.244.0.18:9153             Masq    1      0          0         
TCP  10.99.167.164:8080 rr
  -> 10.244.1.34:80               Masq    1      0          0         
  -> 10.244.1.35:80               Masq    1      0          0         
  -> 10.244.2.31:80               Masq    1      0          0  
```

#Headless Service
spec.clusterIPの値をNoneに指定

#CoreDNS確認
```ruby:CoreDNS‐Check
kubectl get pod -n kube-system
###digコマンドインストール
yum -y install bind-utils
###CoreDNS IP取得
kubectl get pod -n kube-system -o wide
[root@k8s-master01 ~]# kubectl get pod -n kube-system -o wide
NAME                                   READY   STATUS    RESTARTS   AGE    IP              NODE           NOMINATED NODE   READINESS GATES
coredns-5c98db65d4-mkr7c               1/1     Running   9          224d   10.244.0.18     k8s-master01   <none>           <none>
coredns-5c98db65d4-zxqzg               1/1     Running   9          224d   10.244.0.17     k8s-master01   <none>           <none>
etcd-k8s-master01                      1/1     Running   7          224d   192.168.66.10   k8s-master01   <none>           <none>
kube-apiserver-k8s-master01            1/1     Running   8          224d   192.168.66.10   k8s-master01   <none>           <none>
kube-controller-manager-k8s-master01   1/1     Running   7          224d   192.168.66.10   k8s-master01   <none>           <none>
kube-flannel-ds-85jwm                  1/1     Running   8          224d   192.168.66.21   k8s-node02     <none>           <none>
kube-flannel-ds-p5tw2                  1/1     Running   9          224d   192.168.66.10   k8s-master01   <none>           <none>
kube-flannel-ds-w6jdf                  1/1     Running   6          224d   192.168.66.20   k8s-node01     <none>           <none>
kube-proxy-9vwdd                       1/1     Running   6          224d   192.168.66.20   k8s-node01     <none>           <none>
kube-proxy-f2ptd                       1/1     Running   6          224d   192.168.66.21   k8s-node02     <none>           <none>
kube-proxy-kdkd7                       1/1     Running   7          224d   192.168.66.10   k8s-master01   <none>           <none>
kube-scheduler-k8s-master01            1/1     Running   7          224d   192.168.66.10   k8s-master01   <none>           <none>

###名前解決確認
dig -t A myapp-headless.default.svc.cluster.local. @10.144.0.18
```