最完整的-Kubernetes集群安装步骤-文档

k8s文档

用于自动部署、扩展和管理容器化应用程序的开源系统

中文官网: https://kubernetes.io/zh/

中文社区:https://www.kubernetes.org.cn/

官方文档:https://kubernetes.io/zh/docs/home/

社区文档:http://docs.kubernetes.org.cn/

k8s集群安装

1.kubeadm

kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具

这个工具能通过两条指令完成一个kubernetes集群的部署:

#创建一个Master节点
$ kubeadm init

#将一个Node 节点加入到当前集群中
$ kubeadm join <Master 节点的IP和端口>

2.前置要求

一台或多台机器,操作系统Centos7.x-86_x64

硬件配置:2GB或更多RAM,2个或CPU或更多CPU,硬盘30GB或更多

集群中所有的机器之间网络互通

可以访问外网,需要拉取镜像

禁止swap分区

3.部署步骤

1.在所有的节点上安装Docker和kubeadm

2.部署Kuberbetes Master

3.部署容器网络插件

4.部署Kubernetes Node,将节点加入Kubernetes集群中

5.部署Dashboard Web页面,可视化查看Kubernetes资源
最完整的-Kubernetes集群安装步骤-文档_第1张图片

设置好net网络

3.1设置linux环境(3个节点都执行)

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

#关闭selinux
cat /etc/selinux/config
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

#关闭swap
swapoff -a         #临时关闭
cat /etc/fstab
sed -ri 's/.*swap.*/#&/' /etc/fstab        #永久关闭
cat /etc/fstab

#添加主机名与IP对应关系
vim /etc/hosts
10.0.2.21 k8s-node1
10.0.2.23 k8s-node2
10.0.2.22 k8s-node3
#指定新的hostname
#hostnamectl set-hostname <新的主机名> 

#将桥接的IPv4流量传递到iptables的链:
cat /etc/sysctl.d/k8s.config << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl-system

最完整的-Kubernetes集群安装步骤-文档_第2张图片

3.2所有节点安装Docker、kubeadm、kubelet、kubectl

Kubernetes默认CRI(容器运行时)为docker,因此先安装Docker.

3.2.1 所有节点:卸载系统之前的docker
sudo yum remove docker \
				docker-client \
				docker-client-latest \
				docker-common \
				docker-latest \
				docker-latest-logrotate \
				docker-logrotate \
				docker-engine
3.2.2 所有节点:安装 Docker-CE
#安装必须的依赖
sudo yum install -y yum-utils \
	device-mapper-persistent-data \
	lvm2

#设置docker ce的yum源(位置)
sudo yum-config-manager \
	--add-repo \
	https://download.docker.com/linux/centos/docker-ce.repo

#安装docker,以及docker-cli
sudo yum install -y docker-ce docker-ce-cli containerd.io
3.2.3 所有节点:配置Docker 镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
     
  "registry-mirrors": ["https://75km7aas.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

#docker version
#docker ps
3.2.4 所有节点:启动docker & 设置docker 开机启动
systemctl enable docker
3.2.5 所有节点:添加阿里云 yum源
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

最完整的-Kubernetes集群安装步骤-文档_第3张图片

3.2.6 所有节点:安装kubeadm、kubelet 和 kubectl
#列出kube的安装源
yum list |grep kube

#指定版本安装
yum install -y kubelet-1.17.3 kubeadm-1.17.3 kubectl-1.17.3
3.2.7 所有节点:设置kubelet开机自动 、运行启动kubelet
sudo systemctl enable kubelet
sudo systemctl start kubelet  #默认kubelet是启动不成功的,因为其他配置还未配置,无须担心

最完整的-Kubernetes集群安装步骤-文档_第4张图片

3.3 部署k8s-Master节点

3.3.1 先使用脚本 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

最完整的-Kubernetes集群安装步骤-文档_第5张图片
授权并执行脚本master_images.sh,下载镜像时间较长

#授权
chomd 700 master_images.sh
#执行脚本
./master_images.sh

查看镜像

docker images

最完整的-Kubernetes集群安装步骤-文档_第6张图片

3.3.2 master 节点初始化:
kubeadm init \
--apiserver-advertise-address=10.0.2.15 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version v1.17.3 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 

最完整的-Kubernetes集群安装步骤-文档_第7张图片

注:由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。可以手动安装我们的images.sh 先拉取镜像。地址变为 registry.aliyuncs.com/google_containers 也可以

master 节点初始化成功:
最完整的-Kubernetes集群安装步骤-文档_第8张图片

3.3.3 master 节点初始化成功后,执行上面截图中的命令:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.3.4 配置网络,执行kube-flannel.yml
kubectl apply -f kube-flannel.yml

最完整的-Kubernetes集群安装步骤-文档_第9张图片

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
3.3.5 查询kube的名称空间 namespace
kubectl get ns

kubectl get pods --all-namespaces

最完整的-Kubernetes集群安装步骤-文档_第10张图片
最完整的-Kubernetes集群安装步骤-文档_第11张图片

3.3.6 查看获取 所有节点-主节点操作
kubectl get nodes 

最完整的-Kubernetes集群安装步骤-文档_第12张图片

3.3.7 加入从节点信息,在剩下的从节点执行红色区域的命令:

最完整的-Kubernetes集群安装步骤-文档_第13张图片
在node2 node3节点执行:
最完整的-Kubernetes集群安装步骤-文档_第14张图片

3.3.8 在主节点master 查看所有节点信息
kubectl get nodes 

最完整的-Kubernetes集群安装步骤-文档_第15张图片
执行以下命令,监控pod 进度信息:

等待3-10分钟,状态status 完全都是 running 以后继续

#监控pod 进度
watch kubectl get pod -n kube-system -o wide

最完整的-Kubernetes集群安装步骤-文档_第16张图片
最完整的-Kubernetes集群安装步骤-文档_第17张图片

k8s集群 安装结束

k8s部署测试

1.部署一个tomcat

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8

最完整的-Kubernetes集群安装步骤-文档_第18张图片

# 可以获取到tomcat的信息 完整的信息
kubectl get pods -o wide 

最完整的-Kubernetes集群安装步骤-文档_第19张图片
可以看到tomcat6部署在 node3 节点上,查看node3节点:

docker images
docker ps

最完整的-Kubernetes集群安装步骤-文档_第20张图片
在主节点 Master节点,查看创建的 tomcat6 的pod:

#查看默认的【default】的名称空间下的pod
kubectl get pods
#查看所有的名称空间下的pod
kubectl get pods --all-namespaces

最完整的-Kubernetes集群安装步骤-文档_第21张图片
测试【容灾恢复】:模拟 node3 节点宕机,关机
查看所有节点信息,确认node3 节点 宕机 NotReady

kubectl get ndoes

最完整的-Kubernetes集群安装步骤-文档_第22张图片
查看pod 信息,tomcat6 重新在 node2节点 创建新的pod:

kubectl get pods -o wide 

最完整的-Kubernetes集群安装步骤-文档_第23张图片
再在node2 节点查看:

docker images
docker ps

最完整的-Kubernetes集群安装步骤-文档_第24张图片

2.暴露给 nginx 访问

kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
# pod 的80 映射容器的 8080
# service 会代理pod 的80

# 查看k8s服务信息   svc: services
kubectl get svc 
或
kubectl get svc -o wide 

最完整的-Kubernetes集群安装步骤-文档_第25张图片
可以看到随机分配了一个 31533 的端口,给浏览器访问:
最完整的-Kubernetes集群安装步骤-文档_第26张图片

3.动态扩容/缩容 测试

# 查看部署信息
kubectl get deployment 

# 扩容 3个 tomcat 副本
kubectl scale --replicas=3 delpoyment tomcat6
# 扩容了多份,所有无论如何访问哪个node的指定端口,都可以访问到 tomcat6

最完整的-Kubernetes集群安装步骤-文档_第27张图片
最完整的-Kubernetes集群安装步骤-文档_第28张图片
注意:也可以动态缩容:

#指定副本数为 1
kubectl scale --replicas=1 delpoyment tomcat6

kubectl get pods -o wide 

最完整的-Kubernetes集群安装步骤-文档_第29张图片

你可能感兴趣的:(Linux,Kubernetes,kubernetes,docker,linux)