以下实验环境k8s版本号是1.9.1版本,操作系统为centos7

一、环境介绍

IP

角色

备注

192.63.63.1

Master

不启动kubelet,只做master

192.63.63.10

Node1

 

Master上不启动kubelet目的是保证k8s在部署服务的时候能够部署到Node

[root@localhost demo]# kubectl get node
NAME        STATUS     ROLES     AGE       VERSION
127.0.0.1   NotReady   <none>    59d       v1.9.1
node1       Ready      <none>    41d       v1.9.1
[root@localhost demo]#
[root@localhost demo]#

二、创建pod以及service 

[root@localhost demo]# kubectl get pod
NAME          READY     STATUS    RESTARTS   AGE
mysql-sg544   1/1       Running   0          3m
myweb-rfwf6   1/1       Running   0          3m
[root@localhost demo]#
[root@localhost demo]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.252.0.1       <none>        443/TCP          35m
mysql        ClusterIP   10.252.235.138   <none>        3306/TCP         2m
myweb        NodePort    10.252.202.240   <none>        8085:30001/TCP   2m
[root@localhost demo]#
[root@localhost demo]# kubectl describe pod myweb | grep Node
Node:           node1/192.63.63.10
Node-Selectors:  <none>
[root@localhost demo]# kubectl describe pod mysql | grep Node
Node:           node1/192.63.63.10
Node-Selectors:  <none>
[root@localhost demo]#

环境部署成功之后, 访问http://192.63.63.10:30001/demo/页面,但是报了如下错误:

Error:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

 然而访问http://192.63.63.10:30001/ 是可以出现tomcat主页,也就是说服务tomcat服务没有,只是tomcat连接mysql数据库不行。

 解决方法:

进入tomcat容器中,设置hosts文件,指定渔民mysqlip的映射,例如:

root@myweb-q8gm6:/usr/local/tomcat# echo "172.17.0.2      mysql" >> /etc/hosts

然后再次刷新页面http://192.63.63.10:30001/demo/则有数据显示

【特别说明】

1、两个pod都在同一台虚拟机上所以才能保证容器mysql和容器tomcat在同一个网段172.17.0.0/16

2、Ip地址172.17.0.2是容器mysqlip地址

我们不能每次都去手动修改hosts文件,因此我们需要部署dns系统。

三、部署dns系统

通过dns自动发现功能帮助我们完成域名和ip的映射。需要把之前rcservice删除掉

3.1下载镜像文件

名称

当前最新版本号

gcr.io/google_containers/k8s-dns-sidecar-amd64

1.14.5

gcr.io/google_containers/k8s-dns-kube-dns-amd64

1.14.5

gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64

1.14.5

备注:在k8s1.3版本之前,部署dns需要kube2skyskydns等。

3.2 yaml文件配置

3.2.1github中下载yaml文件

克隆kubernetes仓库:git clone https://github.com/kubernetes/kubernetes.git

下载完成进入目录:cluster\addons\dns,将kube-dns.yaml.sed拷贝到另一目录中。

 

3.2.2 分割yaml文件

谷歌把dns相关配置整合到一个文件中kube-dns.yaml.sed,我们要分割出来,如下:

 

分割出来的文件如下:kubedns-cm.yaml  kubedns-controller.yaml  kubedns-sa.yaml  kubedns-svc.yaml

3.2.3 修改文件中变量,替换成实际值

变量名

实际值

备注

$DNS_DOMAIN

cluster.local

替换成实际有意义即可。随意写

$DNS_SERVER_IP

10.252.0.2

这个是cluster-ip,设置本dns系统服务ip。取值范围要符合/etc/kubernetes/apiserver文件配置项【--service-cluster-ip-range】的定义内。

备注1:需要指定kube-master监听地址,格式如kube-master-url=http://IP:Port,其中IP地址不要写127.0.0.1,因为k8s在调度POD是随机调度,不能保证调度在master节点(除非自己设置)。

备注2:增加位置在镜像k8s-dns-kube-dns-amd64args- --kube-master-url=http://192.63.63.1:8080

 

3.2.4 修改kubelet的配置

需要kubelet配置文件中增加dns server以及域名

--cluster-dns=10.252.0.2 --cluster-domain=cluster.local

备注:kubednstomcatmysql部署在同一台node上,具体原因可参考这篇文章

然后重启各个服务以及创建podservice,之后基本上问题不大

四、所有用例

【dns相关yaml文件】

kubedns-cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
kubedns-controller.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  # replicas: not specified here:
  # 1. In order to make Addon Manager do not reconcile this replicas parameter.
  # 2. Default is 1.
  # 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
  strategy:
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
  selector:
    matchLabels:
      k8s-app: kube-dns
  template:
    metadata:
      labels:
        k8s-app: kube-dns
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      tolerations:
      - key: "CriticalAddonsOnly"
        operator: "Exists"
      volumes:
      - name: kube-dns-config
        configMap:
          name: kube-dns
          optional: true
      containers:
      - name: kubedns
        image: gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5
        resources:
          # TODO: Set memory limits when we've profiled the container for large
          # clusters, then set request = limit to keep this container in
          # guaranteed class. Currently, this container falls into the
          # "burstable" category so the kubelet doesn't backoff from restarting it.
          limits:
            memory: 170Mi
          requests:
            cpu: 100m
            memory: 70Mi
        livenessProbe:
          httpGet:
            path: /healthcheck/kubedns
            port: 10054
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /readiness
            port: 8081
            scheme: HTTP
          # we poll on pod startup for the Kubernetes master service and
          # only setup the /readiness HTTP server once that's available.
          initialDelaySeconds: 3
          timeoutSeconds: 5
        args:
        - --domain=cluster.local.
        - --dns-port=10053
        - --config-dir=/kube-dns-config
        - --kube-master-url=http://192.63.63.1:8080
        - --v=0
        env:
        - name: PROMETHEUS_PORT
          value: "10055"
        ports:
        - containerPort: 10053
          name: dns-local
          protocol: UDP
        - containerPort: 10053
          name: dns-tcp-local
          protocol: TCP
        - containerPort: 10055
          name: metrics
          protocol: TCP
        volumeMounts:
        - name: kube-dns-config
          mountPath: /kube-dns-config
      - name: dnsmasq
        image: gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
        livenessProbe:
          httpGet:
            path: /healthcheck/dnsmasq
            port: 10054
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        args:
        - -v=2
        - -logtostderr
        - -configDir=/etc/k8s/dns/dnsmasq-nanny
        - -restartDnsmasq=true
        - --
        - -k
        - --cache-size=1000
        - --log-facility=-
        - --server=/cluster.local/127.0.0.1#10053
        - --server=/in-addr.arpa/127.0.0.1#10053
        - --server=/ip6.arpa/127.0.0.1#10053
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
        # see: https://github.com/kubernetes/kubernetes/issues/29055 for details
        resources:
          requests:
            cpu: 150m
            memory: 20Mi
        volumeMounts:
        - name: kube-dns-config
          mountPath: /etc/k8s/dns/dnsmasq-nanny
      - name: sidecar
        image: gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5
        livenessProbe:
          httpGet:
            path: /metrics
            port: 10054
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        args:
        - --v=2
        - --logtostderr
        - --probe=kubedns,127.0.0.1:10053,kubernetes.default.svc.cluster.local,5,A
        - --probe=dnsmasq,127.0.0.1:53,kubernetes.default.svc.cluster.local,5,A
        ports:
        - containerPort: 10054
          name: metrics
          protocol: TCP
        resources:
          requests:
            memory: 20Mi
            cpu: 10m
      dnsPolicy: Default  # Don't use cluster DNS.
      serviceAccountName: kube-dns

kubedns-sa.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
kubedns-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    kubernetes.io/name: "KubeDNS"
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.252.0.2
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP
【app相关yaml】

mysql-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeName: 127.0.0.1
      containers:
      - name: mysql
        image: mysql
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  clusterIP: 10.252.141.93
  ports:
    - port: 3306
  selector:
    app: mysql
myweb-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      nodeName: 127.0.0.1
      containers:
        - name: myweb
          image: kubeguide/tomcat-app:v1
          imagePullPolicy: IfNotPresent
          ports:
               - containerPort: 8080
          env:
               - name: MYSQL_SERVICE_HOST
                 value: 'mysql'
               - name: MYSQL_SERVICE_PORT
                 value: '3306'

myweb-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  clusterIP: 10.252.202.240
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb

下一篇介绍Flannel的安装配置,解决上面提到kubedns、tomcat、mysql不在同一个node的问题。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐