kubernetes集群系列资料11--service介绍

一、service案例

vim svc-deploy.yaml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apiVersion: apps/v1         #首选extensions/v1beta1
kind: Deployment
metadata:
    name: svc-deployment
    namespace: default
spec:
    replicas: 3
    selector:
        matchLabels:
            app: svc-deployment
            release: stable
    template:                   #以下为嵌套pod的字段;
        metadata:
            labels:
                app: svc-deployment
                release: stable
                env: test
        spec:
            containers:
            - name: svc-deployment
              image: hub.atguigu.com/library/nginx:latest
              ports:
              - name: http
                containerPort: 80
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim svc.yaml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apiVersion: v1         #首选extensions/v1beta1
kind: Service
metadata:
    name: svc-deployment
    namespace: default
spec:
    type: ClusterIP
    selector:
        app: svc-deployment
        release: stable
    ports:
    - name: http
      port: 80
      targetPort: 80
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

kubectl apply -f svc-deploy.yaml
kubectl apply -f svc.yaml
kubectl get svc;kubectl get deployment;kubectl get rs;kubectl get pod   #查看创建资源;
ipvsadm -ln;curl 10.109.120.103 #查看转发路径;测试:能访问服务;

kubectl delete -f svc.yaml #删除svc资源;因此yaml文件尽量保留,以便后期维护。
sed -i "s/app: svc-deployment/app: svc-deployment1/g" svc.yaml #修改svc模板;
kubectl apply -f svc.yaml;kubectl get svc
curl 10.105.124.206 #再测试,不能访问服务;其原因svc的label于pod的label不一致。
ipvsadm -ln #查看svc转发策略:无。

sed -i "s/app: svc-deployment1/app: svc-deployment/g" svc.yaml
kubectl apply -f svc.yaml;kubectl get svc
curl 10.105.124.206     #测试:能访问服务;

vim svc-headless.yaml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apiVersion: v1         #首选extensions/v1beta1
kind: Service
metadata:
    name: svc-deployment-headless
    namespace: default
spec:
    clusterIP: "None"
    selector:
        app: svc-deployment
        release: stable
    ports:
    - name: http
      port: 80
      targetPort: 80
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kubectl apply -f svc-headless.yaml
kubectl get svc;kubectl get pod -n kube-system
yum -y install bind-utils
dig -t A svc-deployment-headless.default.svc.cluster.local. @10.244.0.24  #对域名进行A记录解析。svc创建后,会将“svc的名称.名称空间.集群域名.@coreDNS的IP地址”写入coreDNS中。我们可通过dig命令解析该SVC域名为IP。???如何查询集群域名???


vim svc-nodePort.yaml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apiVersion: v1
kind: Service
metadata:
    name: svc-deployment
    namespace: default
spec:
    type: NodePort
    selector:
        app: svc-deployment
        release: stable
    ports:
    - name: http
      port: 80
      targetPort: 80
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kubectl apply -f svc-nodePort.yaml
kubectl get svc             #查看svc的IP及端口;
iptables -t nat -nvL


vim svc-externalName.yaml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
apiVersion: v1
kind: Service
metadata:
    name: svc-deployment-1
    namespace: default
spec:
    type: ExternalName
    externalName: hub.atguigu.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kubectl apply -f svc-externalName.yaml
kubectl get svc
dig -t A svc-deployment-1.default.svc.cluster.local. @10.244.0.24

  

posted on 2021-06-17 22:43  chalon  阅读(47)  评论(0编辑  收藏  举报