美文网首页docker. k8s
Kubernets(k8s)安装部署

Kubernets(k8s)安装部署

作者: 樱桃小丸子zz | 来源:发表于2018-10-18 15:04 被阅读0次

    一、 部署前准备

    单节点部署,系统为centos

    1. 关闭防火墙, sawp等。。
    systemctl stop firewalld
    systemctl disable firewalld
    
    
    swapoff -a
    
    1. 禁用selinux:
    setenforce 0
    
    vi /etc/selinux/config
    SELINUX=disabled
    
    1. 创建/etc/sysctl.d/k8s.conf文件,添加如下内容:
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    

    ---执行以下命令使修改生效:

    sysctl -p /etc/sysctl.d/k8s.conf
    
    1. 安装kubelet/kubeadm/kubectl

    (1)首先设置安装源:

    cat <<EOF > /etc/yum.repos.d/kubernetes.repo 
    [kubernetes] 
    name=Kubernetes 
    baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=0
    repo_gpgcheck=0
    gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    

    (2)然后进行安装

    yum install -y kubelet kubeadm kubectl
    

    (3)启动kubectl服务, 查看状态

    systemctl enable kubelet && systemctl start kubelet
    systemctl status kubelet
    

    (4) 修改/etc/sysconfig/kubelet,加入:

    KUBELET_EXTRA_ARGS=--fail-swap-on=false
    

    二、 导入镜像

    由于无法从谷歌源下载镜像或速度太慢,可在网上提前找到镜像的tar包并导入-docker load

    必备的镜像如下:

    k8s的主要镜像,版本号和kube要保持大版本一致

    k8s.gcr.io/kube-apiserver
    k8s.gcr.io/kube-controller-manager
    k8s.gcr.io/kube-proxy
    k8s.gcr.io/kube-scheduler
    

    etcd的镜像,版本号在github仓库里找

    `k8s.gcr.io/etcd`
    

    dns镜像,用作服务发现,版本号在github仓库里找

    k8s.gcr.io/k8s-dns-sidecar-amd64
    k8s.gcr.io/k8s-dns-kube-dns-amd64
    k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64
    k8s.gcr.io/coredns
    

    其他镜像

    k8s.gcr.io/kubernetes-dashboard-amd64 webui
    下面四个是用于创建网络的镜像
    quay.io/calico/node:3.1.3                     
    quay.io/calico/kube-controllers:3.1.3 
    quay.io/calico/cni:3.1.3 
    k8s.gcr.io/pause-amd64:3.1
    

    三、创建k8s

    1. 找到自己本机的网卡绑定内网ip
      ifconfig

    2. 创建
      kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version=v1.12.0 --apiserver-advertise-address=*****(内网ip)

    3. 输入k8s init之后默认显示的三行命令

    4. 创建网络
      kubectl apply -f calico.yaml

    # Calico Version v3.1.3
    # https://docs.projectcalico.org/v3.1/releases#v3.1.3
    # This manifest includes the following component versions:
    #   calico/node:v3.1.3
    #   calico/cni:v3.1.3
    #   calico/kube-controllers:v3.1.3
    
    # This ConfigMap is used to configure a self-hosted Calico installation.
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: calico-config
      namespace: kube-system
    data:
      # The location of your etcd cluster.  This uses the Service clusterIP defined below.
      etcd_endpoints: "http://10.96.232.136:6666"
    
      # Configure the Calico backend to use.
      calico_backend: "bird"
    
      # The CNI network configuration to install on each node.
      cni_network_config: |-
        {
          "name": "k8s-pod-network",
          "cniVersion": "0.3.0",
          "plugins": [
            {
              "type": "calico",
              "etcd_endpoints": "__ETCD_ENDPOINTS__",
              "log_level": "info",
              "mtu": 1500,
              "ipam": {
                  "type": "calico-ipam"
              },
              "policy": {
                  "type": "k8s"
              },
              "kubernetes": {
                  "kubeconfig": "__KUBECONFIG_FILEPATH__"
              }
            },
            {
              "type": "portmap",
              "snat": true,
              "capabilities": {"portMappings": true}
            }
          ]
        }
    
    ---
    
    # This manifest installs the Calico etcd on the kubeadm master.  This uses a DaemonSet
    # to force it to run on the master even when the master isn't schedulable, and uses
    # nodeSelector to ensure it only runs on the master.
    apiVersion: extensions/v1beta1
    kind: DaemonSet
    metadata:
      name: calico-etcd
      namespace: kube-system
      labels:
        k8s-app: calico-etcd
    spec:
      template:
        metadata:
          labels:
            k8s-app: calico-etcd
          annotations:
            # Mark this pod as a critical add-on; when enabled, the critical add-on scheduler
            # reserves resources for critical add-on pods so that they can be rescheduled after
            # a failure.  This annotation works in tandem with the toleration below.
            scheduler.alpha.kubernetes.io/critical-pod: ''
        spec:
          tolerations:
            # This taint is set by all kubelets running `--cloud-provider=external`
            # so we should tolerate it to schedule the calico pods
            - key: node.cloudprovider.kubernetes.io/uninitialized
              value: "true"
              effect: NoSchedule
            # Allow this pod to run on the master.
            - key: node-role.kubernetes.io/master
              effect: NoSchedule
            # Allow this pod to be rescheduled while the node is in "critical add-ons only" mode.
            # This, along with the annotation above marks this pod as a critical add-on.
            - key: CriticalAddonsOnly
              operator: Exists
          # Only run this pod on the master.
          nodeSelector:
            node-role.kubernetes.io/master: ""
          hostNetwork: true
          containers:
            - name: calico-etcd
              image: quay.io/coreos/etcd:v3.1.10
              env:
                - name: CALICO_ETCD_IP
                  valueFrom:
                    fieldRef:
                      fieldPath: status.podIP
              command:
              - /usr/local/bin/etcd
              args:
              - --name=calico
              - --data-dir=/var/etcd/calico-data
              - --advertise-client-urls=http://$CALICO_ETCD_IP:6666
              - --listen-client-urls=http://0.0.0.0:6666
              - --listen-peer-urls=http://0.0.0.0:6667
              - --auto-compaction-retention=1
              volumeMounts:
                - name: var-etcd
                  mountPath: /var/etcd
          volumes:
            - name: var-etcd
              hostPath:
                path: /var/etcd
    
    ---
    
    # This manifest installs the Service which gets traffic to the Calico
    # etcd.
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        k8s-app: calico-etcd
      name: calico-etcd
      namespace: kube-system
    spec:
      # Select the calico-etcd pod running on the master.
      selector:
        k8s-app: calico-etcd
      # This ClusterIP needs to be known in advance, since we cannot rely
      # on DNS to get access to etcd.
      clusterIP: 10.96.232.136
      ports:
        - port: 6666
    
    ---
    
    # This manifest installs the calico/node container, as well
    # as the Calico CNI plugins and network config on
    # each master and worker node in a Kubernetes cluster.
    kind: DaemonSet
    apiVersion: extensions/v1beta1
    metadata:
      name: calico-node
      namespace: kube-system
      labels:
        k8s-app: calico-node
    spec:
      selector:
        matchLabels:
          k8s-app: calico-node
      updateStrategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 1
      template:
        metadata:
          labels:
            k8s-app: calico-node
          annotations:
            # Mark this pod as a critical add-on; when enabled, the critical add-on scheduler
            # reserves resources for critical add-on pods so that they can be rescheduled after
            # a failure.  This annotation works in tandem with the toleration below.
            scheduler.alpha.kubernetes.io/critical-pod: ''
        spec:
          hostNetwork: true
          tolerations:
            # Make sure calico/node gets scheduled on all nodes.
            - effect: NoSchedule
              operator: Exists
            # Mark the pod as a critical add-on for rescheduling.
            - key: CriticalAddonsOnly
              operator: Exists
            - effect: NoExecute
              operator: Exists
          serviceAccountName: calico-cni-plugin
          # Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force
          # deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods.
          terminationGracePeriodSeconds: 0
          containers:
            # Runs calico/node container on each Kubernetes node.  This
            # container programs network policy and routes on each
            # host.
            - name: calico-node
              image: quay.io/calico/node:v3.1.3
              env:
                # The location of the Calico etcd cluster.
                - name: ETCD_ENDPOINTS
                  valueFrom:
                    configMapKeyRef:
                      name: calico-config
                      key: etcd_endpoints
                # Enable BGP.  Disable to enforce policy only.
                - name: CALICO_NETWORKING_BACKEND
                  valueFrom:
                    configMapKeyRef:
                      name: calico-config
                      key: calico_backend
                # Cluster type to identify the deployment type
                - name: CLUSTER_TYPE
                  value: "kubeadm,bgp"
                # Disable file logging so `kubectl logs` works.
                - name: CALICO_DISABLE_FILE_LOGGING
                  value: "true"
                # Set noderef for node controller.
                - name: CALICO_K8S_NODE_REF
                  valueFrom:
                    fieldRef:
                      fieldPath: spec.nodeName
                # Set Felix endpoint to host default action to ACCEPT.
                - name: FELIX_DEFAULTENDPOINTTOHOSTACTION
                  value: "ACCEPT"
                # The default IPv4 pool to create on startup if none exists. Pod IPs will be
                # chosen from this range. Changing this value after installation will have
                # no effect. This should fall within `--cluster-cidr`.
                - name: CALICO_IPV4POOL_CIDR
                  value: "192.168.0.0/16"
                - name: CALICO_IPV4POOL_IPIP
                  value: "Always"
                # Disable IPv6 on Kubernetes.
                - name: FELIX_IPV6SUPPORT
                  value: "false"
                # Set MTU for tunnel device used if ipip is enabled
                - name: FELIX_IPINIPMTU
                  value: "1440"
                # Set Felix logging to "info"
                - name: FELIX_LOGSEVERITYSCREEN
                  value: "info"
                # Auto-detect the BGP IP address.
                - name: IP
                  value: "autodetect"
                - name: FELIX_HEALTHENABLED
                  value: "true"
              securityContext:
                privileged: true
              resources:
                requests:
                  cpu: 250m
              livenessProbe:
                httpGet:
                  path: /liveness
                  port: 9099
                periodSeconds: 10
                initialDelaySeconds: 10
                failureThreshold: 6
              readinessProbe:
                httpGet:
                  path: /readiness
                  port: 9099
                periodSeconds: 10
              volumeMounts:
                - mountPath: /lib/modules
                  name: lib-modules
                  readOnly: true
                - mountPath: /var/run/calico
                  name: var-run-calico
                  readOnly: false
                - mountPath: /var/lib/calico
                  name: var-lib-calico
                  readOnly: false
            # This container installs the Calico CNI binaries
            # and CNI network config file on each node.
            - name: install-cni
              image: quay.io/calico/cni:v3.1.3
              command: ["/install-cni.sh"]
              env:
                # Name of the CNI config file to create.
                - name: CNI_CONF_NAME
                  value: "10-calico.conflist"
                # The location of the Calico etcd cluster.
                - name: ETCD_ENDPOINTS
                  valueFrom:
                    configMapKeyRef:
                      name: calico-config
                      key: etcd_endpoints
                # The CNI network config to install on each node.
                - name: CNI_NETWORK_CONFIG
                  valueFrom:
                    configMapKeyRef:
                      name: calico-config
                      key: cni_network_config
              volumeMounts:
                - mountPath: /host/opt/cni/bin
                  name: cni-bin-dir
                - mountPath: /host/etc/cni/net.d
                  name: cni-net-dir
          volumes:
            # Used by calico/node.
            - name: lib-modules
              hostPath:
                path: /lib/modules
            - name: var-run-calico
              hostPath:
                path: /var/run/calico
            - name: var-lib-calico
              hostPath:
                path: /var/lib/calico
            # Used to install CNI.
            - name: cni-bin-dir
              hostPath:
                path: /opt/cni/bin
            - name: cni-net-dir
              hostPath:
                path: /etc/cni/net.d
    
    ---
    
    # This manifest deploys the Calico Kubernetes controllers.
    # See https://github.com/projectcalico/kube-controllers
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: calico-kube-controllers
      namespace: kube-system
      labels:
        k8s-app: calico-kube-controllers
    spec:
      # The controllers can only have a single active instance.
      replicas: 1
      strategy:
        type: Recreate
      template:
        metadata:
          name: calico-kube-controllers
          namespace: kube-system
          labels:
            k8s-app: calico-kube-controllers
          annotations:
            # Mark this pod as a critical add-on; when enabled, the critical add-on scheduler
            # reserves resources for critical add-on pods so that they can be rescheduled after
            # a failure.  This annotation works in tandem with the toleration below.
            scheduler.alpha.kubernetes.io/critical-pod: ''
        spec:
          # The controllers must run in the host network namespace so that
          # it isn't governed by policy that would prevent it from working.
          hostNetwork: true
          tolerations:
            # Allow this pod to be rescheduled while the node is in "critical add-ons only" mode.
            # This, along with the annotation above marks this pod as a critical add-on.
            - key: CriticalAddonsOnly
              operator: Exists
            - key: node-role.kubernetes.io/master
              effect: NoSchedule
          serviceAccountName: calico-kube-controllers
          containers:
            - name: calico-kube-controllers
              image: quay.io/calico/kube-controllers:v3.1.3
              env:
                # The location of the Calico etcd cluster.
                - name: ETCD_ENDPOINTS
                  valueFrom:
                    configMapKeyRef:
                      name: calico-config
                      key: etcd_endpoints
                # Choose which controllers to run.
                - name: ENABLED_CONTROLLERS
                  value: policy,profile,workloadendpoint,node
    
    ---
    
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: calico-cni-plugin
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: calico-cni-plugin
    subjects:
    - kind: ServiceAccount
      name: calico-cni-plugin
      namespace: kube-system
    
    ---
    
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: calico-cni-plugin
    rules:
      - apiGroups: [""]
        resources:
          - pods
          - nodes
        verbs:
          - get
    
    ---
    
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: calico-cni-plugin
      namespace: kube-system
    
    ---
    
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: calico-kube-controllers
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: calico-kube-controllers
    subjects:
    - kind: ServiceAccount
      name: calico-kube-controllers
      namespace: kube-system
    
    ---
    
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1beta1
    metadata:
      name: calico-kube-controllers
    rules:
      - apiGroups:
        - ""
        - extensions
        resources:
          - pods
          - namespaces
          - networkpolicies
          - nodes
        verbs:
          - watch
          - list
      - apiGroups:
        - networking.k8s.io
        resources:
          - networkpolicies
        verbs:
          - watch
          - list
    
    ---
    
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: calico-kube-controllers
      namespace: kube-system
    
    1. 设为工作节点
      kubectl taint nodes --all node-role.kubernetes.io/master-

    2. 创建dashboard
      kubectl apply -f dashboard.yaml

    # Copyright 2017 The Kubernetes Authors.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    # ------------------- Dashboard Secret ------------------- #
    
    apiVersion: v1
    kind: Secret
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard-certs
      namespace: kube-system
    type: Opaque
    
    ---
    # ------------------- Dashboard Service Account ------------------- #
    
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard
      namespace: kube-system
    
    ---
    # ------------------- Dashboard Role & Role Binding ------------------- #
    
    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: kubernetes-dashboard-minimal
      namespace: kube-system
    rules:
      # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret.
    - apiGroups: [""]
      resources: ["secrets"]
      verbs: ["create"]
      # Allow Dashboard to create 'kubernetes-dashboard-settings' config map.
    - apiGroups: [""]
      resources: ["configmaps"]
      verbs: ["create"]
      # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
    - apiGroups: [""]
      resources: ["secrets"]
      resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
      verbs: ["get", "update", "delete"]
      # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
    - apiGroups: [""]
      resources: ["configmaps"]
      resourceNames: ["kubernetes-dashboard-settings"]
      verbs: ["get", "update"]
      # Allow Dashboard to get metrics from heapster.
    - apiGroups: [""]
      resources: ["services"]
      resourceNames: ["heapster"]
      verbs: ["proxy"]
    - apiGroups: [""]
      resources: ["services/proxy"]
      resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
      verbs: ["get"]
    
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: kubernetes-dashboard-minimal
      namespace: kube-system
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
    - kind: ServiceAccount
      name: kubernetes-dashboard
      namespace: kube-system
    
    ---
    # ------------------- Dashboard Deployment ------------------- #
    
    kind: Deployment
    apiVersion: apps/v1beta2
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard
      namespace: kube-system
    spec:
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          k8s-app: kubernetes-dashboard
      template:
        metadata:
          labels:
            k8s-app: kubernetes-dashboard
        spec:
          containers:
          - name: kubernetes-dashboard
            image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.0
            ports:
            - containerPort: 8443
              protocol: TCP
            args:
              - --auto-generate-certificates
              # Uncomment the following line to manually specify Kubernetes API server Host
              # If not specified, Dashboard will attempt to auto discover the API server and connect
              # to it. Uncomment only if the default does not work.
              # - --apiserver-host=http://my-address:port
            volumeMounts:
            - name: kubernetes-dashboard-certs
              mountPath: /certs
              # Create on-disk volume to store exec logs
            - mountPath: /tmp
              name: tmp-volume
            livenessProbe:
              httpGet:
                scheme: HTTPS
                path: /
                port: 8443
              initialDelaySeconds: 30
              timeoutSeconds: 30
          volumes:
          - name: kubernetes-dashboard-certs
            secret:
              secretName: kubernetes-dashboard-certs
          - name: tmp-volume
            emptyDir: {}
          serviceAccountName: kubernetes-dashboard
          # Comment the following tolerations if Dashboard must not be deployed on master
          tolerations:
          - key: node-role.kubernetes.io/master
            effect: NoSchedule
    
    ---
    # ------------------- Dashboard Service ------------------- #
    
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      name: kubernetes-dashboard
      namespace: kube-system
    spec:
      ports:
        - port: 443
          targetPort: 8443
      selector:
        k8s-app: kubernetes-dashboard
    
    
    1. 启动apiserver
      kubectl proxy --address=0.0.0.0 --accept-hosts='^*$'

    2. 外网访问验证
      http://${ip}:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login

    相关文章

      网友评论

        本文标题:Kubernets(k8s)安装部署

        本文链接:https://www.haomeiwen.com/subject/bcxszftx.html