k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere

在准备三个虚拟机的环境下,所有节点安装Docker、kubelet、kubeadm、kubectl。

Xsheel工具:查看 > 撰写 > 撰写栏,选“全部”可多个窗口(多个节点)一起执行命令。

所有节点安装Docker

1.如果在此之前安装过旧版的Docker,需要先卸载旧版本。

yum remove docker \
              docker-client \
              docker-client-latest \
              docker-common \
              docker-latest \
              docker-latest-logrotate \
              docker-logrotate \
              docker-engine

2.安装Docker

# 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 3: 安装docker-ce、docker-ce-cli
# sudo yum makecache fast
yum install docker-ce docker-ce-cli containerd.io

# 4: 配置docker加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://l10nt4hq.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

# 5: 开启Docker并设置docker开机自启
systemctl enable docker

#6: 检查一下是否启动
docker ps

所有节点安装kubelet、kubeadm、kubectl

kubelet:每个node的代理,创建pod等;
kubeadm: 方便设置master和node节点;
kubectl:通过命令行来操作整个k8s,装在master节点也可以;

1.配置国内Kubernetes源

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

2.安装

# 可以先检查yum源
# yum list|grep kube

yum install -y kubelet-1.17.3 kubectl-1.17.3 kubeadm-1.17.3

3.设置开机自启并启动

# 设置开机启动
systemctl enable kubelet

# 启动
systemctl start kubelet

其中一个节点安装master节点

准备:
准备以下文件传到linux待用(文件下载):
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第1张图片

上传此文件(待用文件已包含)到虚拟机目录:/root/k8s/master_images.sh

#!/bin/bash

images=(
	kube-apiserver:v1.17.3
    kube-proxy:v1.17.3
	kube-controller-manager:v1.17.3
	kube-scheduler:v1.17.3
	coredns:1.6.5
	etcd:3.4.3-0
    pause:3.1
)

for imageName in ${images[@]} ; do
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
#   docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName  k8s.gcr.io/$imageName
done

将此文件上传到虚拟机,然后设置权限:

[root@k8s-node1 k8s]# chmod 700 master_images.sh

运行.sh文件进行下载:

[root@k8s-node1 ~]#  k8s/master_images.sh

使用kubeadm 进行安装master:

参照:使用kubeadm部署k8s多master集群(一)集群部署

# 使用kubeadm-config.yaml配置k8s1.14.5集群

# cat init.sh
LOAD_BALANCER_DNS="192.168.56.100"     **\\这个是上一步配置的VIP地址**
LOAD_BALANCER_PORT="6443"
# 生成kubeadm配置文件
cat > kubeadm-master.config <<EOF
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
# kubernetes版本
kubernetesVersion: v1.17.3
# 使用国内阿里镜像
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

apiServer:
  certSANs:
  - "$LOAD_BALANCER_DNS"
controlPlaneEndpoint: "$LOAD_BALANCER_DNS:$LOAD_BALANCER_PORT"

networking:
  podSubnet: 10.244.0.0/16
EOF

#初始化k8s集群
kubeadm init --config=kubeadm-master.config

提示安装成功,需要执行以下几步:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第2张图片
复制需要执行的代码运行之后,需要安装pod网络插件:
应用(apply)和删除(delete)
master节点上增加如下文件:/root/k8s/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": "10.244.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: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.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.11.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.11.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        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
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds-arm64
  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: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.kubernetes.io/arch
                    operator: In
                    values:
                      - arm64
      hostNetwork: true
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.11.0-arm64
        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.11.0-arm64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        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
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds-arm
  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: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.kubernetes.io/arch
                    operator: In
                    values:
                      - arm
      hostNetwork: true
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.11.0-arm
        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.11.0-arm
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        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
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds-ppc64le
  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: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.kubernetes.io/arch
                    operator: In
                    values:
                      - ppc64le
      hostNetwork: true
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.11.0-ppc64le
        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.11.0-ppc64le
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        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
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds-s390x
  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: beta.kubernetes.io/os
                    operator: In
                    values:
                      - linux
                  - key: beta.kubernetes.io/arch
                    operator: In
                    values:
                      - s390x
      hostNetwork: true
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.11.0-s390x
        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.11.0-s390x
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        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
[root@k8s-node1 ~]# chmod 700 k8s/kube-flannel.yml
[root@k8s-node1 ~]# kubectl apply -f k8s/kube-flannel.yml 

获取所有名称pods,kube-flannel…需要是运行状态:

[root@k8s-node1 ~]# kubectl get pods --all-namespaces

k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第3张图片

获取一下所有节点:
master需要时Ready状态,即可开始增加node节点。

[root@k8s-node1 ~]# kubectl get nodes

在这里插入图片描述

每个node加入:
每个node节点执行:

kubeadm join 192.168.56.100:6443 --token w2scc1.p19wjlk0jq6n71kd \
    --discovery-token-ca-cert-hash sha256:f3a6c850b727ba520e1616f287147b2ed181134481478a498187a509d170020d \
    --control-plane 

k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第4张图片
实际自己操作时加入node失败,报如下错误:如何解决?
在这里插入图片描述

如果过期可用此方法再次生成永久的:

[root@k8s-node1 ~]# kubeadm token create --ttl 0 --print-join-command

k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第5张图片

监控一下网络是否可以了:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第6张图片

可见还在初始化中:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第7张图片

试着创建一个tomcat

master执行:镜像来源于镜像仓库dockerhub
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第8张图片
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第9张图片
发现已经有了tomcat的信息:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第10张图片
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第11张图片
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第12张图片

扩容和缩容:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第13张图片

删除部署信息:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第14张图片

删除service信息:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第15张图片

yaml使用

dry-run:试着运行一下,不是真正部署
yaml:打印yaml信息;
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第16张图片
查看创建时的yaml并保存到文件,并通过yaml创建:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第17张图片

通过pod查看yaml的信息:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第18张图片
通过pod把yaml信息保存进文件:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第19张图片

Ingress

负载均衡:Ingressd底层是一个nginx
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第20张图片
部署Ingress:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第21张图片
配置:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第22张图片
使用域名访问:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第23张图片

可视化界面

使用yaml安装默认控制台dashboard:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第24张图片
获取token进行登录:
地址:http://NodeIp:30001
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第25张图片

KubeSphere

k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第26张图片

官网安装文档:https://v2-1.docs.kubesphere.io/docs/zh-CN/installation/prerequisites/

安装helm:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第27张图片
设置权限:
设置权限的yaml:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第28张图片
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第29张图片
初始化:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第30张图片

验证已安装tiller:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第31张图片

安装Open:https://v2-1.docs.kubesphere.io/docs/zh-CN/appendix/install-openebs/

使用官网中git的yaml进行安装:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第32张图片
安装成功:
k8s集群搭建-安装Docker、Kubelet、Kubeadm、Kubectl、KubeSphere_第33张图片

你可能感兴趣的:(k8s,docker,容器,kubernetes,linux,运维)