K8S安装1.17.9+DOCKER 19.08

DOCKER+KUBEADM源安装–以下操作为每台服务器

cat <<EOF > /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/\$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y --nogpgcheck docker-ce-19.03.9 kubelet-1.17.9 kubeadm-1.17.9 kubectl-1.17.9 tc kubernetes-cni-0.8.7

调整DOCKER参数

mkdir /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
  "bip": "192.44.1.1/24",
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "1G",
    "max-file": "3",
    "labels": "production_status",
    "env": "os,customer"
  }
}
EOF
加上面的或者使用下面的替换
sed -i 's#containerd.sock#containerd.sock --exec-opt native.cgroupdriver=systemd#' /usr/lib/systemd/system/docker.service

systemctl enable kubelet && systemctl enable docker && systemctl restart kubelet && systemctl restart docker

调整内核参数

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
sysctl --system
swapoff -a

获取IMAGES方法

kubeadm config images list

docker pull k8s.gcr.io/kube-apiserver:v1.17.9
docker pull k8s.gcr.io/kube-controller-manager:v1.17.9
docker pull k8s.gcr.io/kube-scheduler:v1.17.9
docker pull k8s.gcr.io/kube-proxy:v1.17.9
docker pull k8s.gcr.io/pause:3.1
docker pull k8s.gcr.io/etcd:3.4.3-0
docker pull k8s.gcr.io/coredns:1.6.5
docker pull quay.io/coreos/flannel:v0.12.0-amd64

docker tag k8s.gcr.io/kube-apiserver:v1.17.9 registry.cn-hangzhou.aliyuncs.com/yayaw/kube-apiserver:v1.17.9
docker tag k8s.gcr.io/kube-controller-manager:v1.17.9 registry.cn-hangzhou.aliyuncs.com/yayaw/kube-controller-manager:v1.17.9
docker tag k8s.gcr.io/kube-scheduler:v1.17.9 registry.cn-hangzhou.aliyuncs.com/yayaw/kube-scheduler:v1.17.9
docker tag k8s.gcr.io/kube-proxy:v1.17.9 registry.cn-hangzhou.aliyuncs.com/yayaw/kube-proxy:v1.17.9
docker tag k8s.gcr.io/pause:3.1 registry.cn-hangzhou.aliyuncs.com/yayaw/pause:3.1
docker tag k8s.gcr.io/etcd:3.4.3-0 registry.cn-hangzhou.aliyuncs.com/yayaw/etcd:3.4.3-0
docker tag k8s.gcr.io/coredns:1.6.5 registry.cn-hangzhou.aliyuncs.com/yayaw/coredns:1.6.5
docker tag quay.io/coreos/flannel:v0.12.0-amd64 registry.cn-hangzhou.aliyuncs.com/yayaw/flannel:v0.12.0-amd64

docker push registry.cn-hangzhou.aliyuncs.com/yayaw/kube-apiserver:v1.17.9
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/kube-controller-manager:v1.17.9
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/kube-scheduler:v1.17.9
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/kube-proxy:v1.17.9
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/pause:3.1
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/etcd:3.4.3-0
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/coredns:1.6.5
docker push registry.cn-hangzhou.aliyuncs.com/yayaw/flannel:v0.12.0-amd64
----------------------------------------------------------------------------
如果主机能上网,只用PULL,就可以了
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/kube-apiserver:v1.17.9
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/kube-controller-manager:v1.17.9
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/kube-scheduler:v1.17.9
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/kube-proxy:v1.17.9
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/etcd:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/coredns:1.6.5
docker pull registry.cn-hangzhou.aliyuncs.com/yayaw/flannel:v0.12.0-amd64
----------------------------------------------------------------------------
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/kube-apiserver:v1.17.9 k8s.gcr.io/kube-apiserver:v1.17.9
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/kube-controller-manager:v1.17.9 k8s.gcr.io/kube-controller-manager:v1.17.9
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/kube-scheduler:v1.17.9 k8s.gcr.io/kube-scheduler:v1.17.9
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/kube-proxy:v1.17.9 k8s.gcr.io/kube-proxy:v1.17.9
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/coredns:1.6.5 k8s.gcr.io/coredns:1.6.5
docker tag registry.cn-hangzhou.aliyuncs.com/yayaw/flannel:v0.12.0-amd64 quay.io/coreos/flannel:v0.12.0-amd64

docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/kube-apiserver:v1.17.9
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/kube-controller-manager:v1.17.9
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/kube-scheduler:v1.17.9
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/kube-proxy:v1.17.9
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/pause:3.1
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/etcd:3.4.3-0
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/coredns:1.6.5
docker rmi registry.cn-hangzhou.aliyuncs.com/yayaw/flannel:v0.12.0-amd64
直接使用命令,不用YAML文件
kubeadm init --control-plane-endpoint=172.16.110.110:6443 --pod-network-cidr=192.3.0.0/16 --service-cidr=10.2.0.0/16 --image-repository=registry.cn-hangzhou.aliyuncs.com/yayaw --upload-certs --kubernetes-version=1.17.9

也可以使用公共镜像:registry.cn-hangzhou.aliyuncs.com/google_containers

初始化文件,并初始化–主节点

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of the control-plane node running the following command on each as root:

  kubeadm join 172.16.110.110:6443 --token 0p6y3o.lr2spyvouvfcgidq \
    --discovery-token-ca-cert-hash sha256:73b95f5fc0fcc0d3e818c367997bcce2caf2c51397f4fde47cb847f2e907aef4 \
    --control-plane --certificate-key f2fb79cdb2e926056044c76b8b2305b2d4d6200ec4c1ff4ef43e6372ed680c6e

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.16.110.110:6443 --token 0p6y3o.lr2spyvouvfcgidq \
    --discovery-token-ca-cert-hash sha256:73b95f5fc0fcc0d3e818c367997bcce2caf2c51397f4fde47cb847f2e907aef4 
    

开启本地端口后面用RANCHER没有警告

sed -i 's/    - --port=0//' /etc/kubernetes/manifests/kube-scheduler.yaml 
sed -i 's/    - --port=0//' /etc/kubernetes/manifests/kube-controller-manager.yaml 
systemctl restart kubelet

安装网络插件

参数地址:https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
---
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
    - configMap
    - secret
    - emptyDir
    - hostPath
  allowedHostPaths:
    - pathPrefix: "/etc/cni/net.d"
    - pathPrefix: "/etc/kube-flannel"
    - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: ['NET_ADMIN']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: flannel
rules:
  - apiGroups: ['extensions']
    resources: ['podsecuritypolicies']
    verbs: ['use']
    resourceNames: ['psp.flannel.unprivileged']
  - apiGroups:
      - ""
    resources:
      - pods
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - nodes/status
    verbs:
      - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "192.3.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds-amd64
  namespace: kube-system
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: kubernetes.io/arch
                    operator: In
                    values:
                      - amd64
      hostNetwork: true
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.12.0-amd64
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: quay.io/coreos/flannel:v0.12.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "1000m"
            memory: "500Mi"
          limits:
            cpu: "1000m"
            memory: "500Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
        - name: run
          hostPath:
            path: /run/flannel
        - name: cni
          hostPath:
            path: /etc/cni/net.d
        - name: flannel-cfg
          configMap:
            name: kube-flannel-cfg

增加高可用–从节点(如果不会自动复制证书,可以用这个)

复制主节点上的/etc/kubernetes/pki/ 到从节点
scp -r /etc/kubernetes/pki/ 192.9.202.76:/etc/kubernetes/pki/
从节点执行
cd /etc/kubernetes/pki/
rm -rf apiserver*
rm -rf etcd/server.*
rm -rf etcd/peer.*
要删除不要证书,除了CA证书
之后执行加入节点并成为主节点