K8s阿里源Kubeadm快速部署Kubernetes v1.18.0集群实战

概述

Kubernetes (通常称为 K8s) 是来自 Google 云平台的开源容器集群管理系统,用于自动部署、扩展和管理容器化(containerized)应用程序。该系统基于 Docker 构建一个,Gitee指数为58,超过73%的开源项,本文将从K8s国内阿里源基于Kubeadm快速部署Kubernetes v1.18.0集群演练。

一、Kubernetes部署环境准备

1、关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2、关闭selinux

setenforce 0  

3、关闭swap

##  临时
swapoff -a
##  永久
sed -i.bak '/swap/s/^/#/' /etc/fstab

4、IPv4流量传递到iptables的链

cat > /etc/sysctl.d/k8s.conf << EOF

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF
sysctl --system

主机名称

操作系统

IP

系统配置

节点应用规划

master

CentOS-7-x86_64

172.19.19.111

4核16G

Docker-ce、Kubelet、Kubeadm、Kubectl

node1

CentOS-7-x86_64

172.19.19.112

4核8G

docker、kubelet、kubeadm

node2

CentOS-7-x86_64

172.19.19.113

4核8G

docker、kubelet、kubeadm

K8S集群环境节点规划

二、安装部署Docker/Kubeadm/Kubelet与kubectl

1、安装Docker

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum list docker-ce --showduplicates | sort -r

yum -y install docker-ce-18.06.1.ce-3.el7

systemctl enable docker && systemctl start docker

##  保证各节点mac和uuid唯一
cat /sys/class/net/ens33/address
cat /sys/class/dmi/id/product_uuid

2、安装kubeadm,kubelet和kubectl

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
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0

3、初始化master节点

kubeadm init --apiserver-advertise-address=172.19.19.111 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.18.0  --service-cidr=10.96.0.0/12  --pod-network-cidr=10.244.0.0/16  --ignore-preflight-errors=Swap
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

三、加入Node节点

## noed节点均操作
kubeadm join 172.19.19.111:6443 --token gv7fq3.s9s91gkacplk5h7d \
>     --discovery-token-ca-cert-hash sha256:8a093c7ce10339365c06c6d73fe444cc27b6a3870abef4f6aed7ba99d8004738

四、部署CNI网络插件

## 如网络环境无法执行,可自行下载kube-flannel.ymlm,再执行kubectl apply -f kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

## 查看PODS状态是否全为Running,表示K8s集群环境搭建成功
kubectl get pods -n kube-system


五、创建一个pod,验证是否正常运行

1、命令验证

Kukubectl get nodes

k8s集群nodes节点状态k8s集群nodes节点状态全为running

2、创建POD验证

kubectl create deployment nginx --image=nginx

kubectl expose deployment nginx --port=80 --type=NodePort

六、访问前端(http://172.19.19.111:31101)

kubectl get service/nginx

Kubeadm快速部署Kubernetes

七、Kubeadm快速部署Kubernetes集群之软件部署体验

1、K8s集群内部部署MySQL-8.0.25

首先,新建mysql-rc.yaml和mysql-svc.yaml配置文件

vim mysql-rc.yaml
## 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-rc
  labels:
    name: mysql-rc
spec:
  replicas: 1
  selector:
    name: mysql-pod
  template:
    metadata:
      labels: 
        name: mysql-pod
    spec:
      containers:
      - name: mysql
        image: mysql
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "mysql"
vim mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  labels: 
    name: mysql-svc
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 33306
  selector:
    name: mysql-pod

然后,启动ReplicationController和Service

kubectl create -f mysql-rc.yaml
kubectl create -f mysql-svc.yaml

启动后查看mysql部署在了哪个node上,执行以下命令可以查看

kubectl get pod -o wide

最后,验证登录MySQL

执行以下命令进入容器

## docker ps查看mysql容器CONTAINER ID
docker ps
CONTAINER ID        IMAGE      COMMAND                  CREATED             STATUS
1167c7b6b365        mysql      "docker-entrypoint.s…"   29 minutes ago      Up 29 minutes
docker exec -it <CONTAINER ID> /bin/bash

mysql -h127.0.0.1 -uroot -pmysql

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user 'ley'@'10.244.2.1' identified WITH mysql_native_password by 'qwqw1212';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* to 'ley'@'10.244.2.1' ;
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on *.* to 'ley'@'10.244.2.1' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>

八、Kubeadm快速部署Kubernetes集群之常见BUG错误解决方案

Kubeadm快速部署Kubernetes集群之常见BUG

九、 Kubernetes常用命令汇总

1、


K8s阿里源Kubeadm快速部署Kubernetes v1.18.0集群实战
https://cn.10691.cn//archives/10028
作者
Ley
发布于
2021年07月15日
许可协议