大部分借鉴于:https://blog.csdn.net/smokelee/article/details/104453902

 

1. 环境


esxi 7.0 

CentOS 7.9

虚拟机网络设置
3台虚拟机设置
主机名    IP    配置
Master    172.16.2.31    2CPU,4G
node1    172.16.2.32    2CPU,4G
node2    172.16.2.33    2CPU,4G


2. 执行环境(无特殊说明,全部都要这样做)


1 三台主机互通

2 master ssh passwdless 访问其它两台主机

 master上执行ssh-keygen -t rsa

执行 ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.2.32  copy至node01

执行 ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.2.33  copy至node02

3 打开内核转发

  添加net.ipv4.ip_forward=1 到/etc/sysctl.conf,执行sysctl -p 后配置才会生效

  Docker配置

  修改/usr/lib/systemd/system/docker.service

  service段增加 ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT

  修改/etc/sysconfig/docker,selinux由enable修改为false

  OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'

5 禁用防火墙
  systemctl stop firewalld
  systemctl disable firewalld

6 关闭SElinux

  修改/etc/selinux/config
  修改SELINUX=disabled

  reboot后生效

7 修改Docker配置,增加镜像

   {
     "registry-mirrors":["http://hub-mirror.c.163.com"]
   }
8 设置时区与时间同步

   yum -y install ntp
   timedatectl  set-timezone Asia/Shanghai

  添加自动对时任务

  */10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP



3. 节点功能规划

主机名运行的服务
Masterdocker
etcd
kube-apiserver
flanneld
kube-proxy
kube-controller-manager
kube-scheduler
node1docker
flanneld
kubelet
kube-proxy
node2docker
flanneld
kubelet
kube-proxy


4. Master设置

1 安装必要的 软件

yum install docker kubernetes flannel etcd
2 设置etcd参数并启动

vim /etc/etcd/config

ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
# 要注意这个参数的IP,这里因为是连接服务,不是监听,所以必须用IP,不能用0.0.0.0
ETCD_ADVERTISE_CLIENT_URLS="http://172.16.2.31:2379"

systemctl  restart etcd

其实ETCD_LISTEN_CLIENT_URLS怎么设置都无所谓,只要正常监听就可以。放到这里,可以设置为http://192.168.56.4:2379

3 设置flannel

vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://192.168.56.4:2379"
FLANNEL_ETCD_PREFIX="/coreos.com/network"

4 手动向etcd,设置flannel网段信息

执行 etcdctl set /coreos.com/network/config '{ "Network": "10.254.0.0/16" }'
此处coreos.com必须与上面配置文件内的FLANNEL_ETCD_PREFIX="/coreos.com/network",设置一致,否则报错

5 修改kube-apiserver配置文件

vim /etc/kubernetes/apiserver

KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
KUBE_ETCD_SERVERS="--etcd-servers=http://172.16.2.31:2379"
 

6 修改controller-manager

vim /etc/kubernetes/controller-manager
KUBE_CONTROLLER_MANAGER_ARGS="--node-monitor-grace-period=10s --pod-eviction-timeout=10s"

7 编辑kubenetes配置

vim /etc/kubernetes/config
KUBE_MASTER="--master=http://172.16.2.31:8080"
 
7 设置服务并启动

systemctl enable docker flanneld kube-apiserver kube-controller-manager kube-scheduler
systemctl start docker flanneld kube-apiserver kube-controller-manager kube-scheduler


5. Node节点设置

两个节点都按此配置,注意修改node节点的ip
1 修改kubenetes配置

vim /etc/kubernetes/config
KUBE_MASTER="--master=http://172.16.2.31:8080"

2 修改kubelets配置

vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_HOSTNAME="--hostname-override=172.16.2.32"
KUBELET_API_SERVER="--api-servers=http://172.16.2.31:8080"


3 修改flanneld

vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://172.16.2.31:2379"
FLANNEL_ETCD_PREFIX="/coreos.com/network"

此处 FLANNEL_ETCD_PREFIX="/coreos.com/network",与master一致


4 设置服务并启动

systemctl enable docker flanneld kubelet kube-proxy
systemctl start docker flanneld kubelet kube-proxy


6. 验证


1 验证节点
$kubectl get nodes -o wide
NAME           STATUS    AGE       EXTERNAL-IP
192.168.56.5   Ready     1h        <none>
192.168.56.6   Ready     1h        <none>

2 创建RC文件mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-rc
spec:
  replicas: 1
  selector:
    app: mysql-pod
  template:
    metadata:
      labels:
        app: mysql-pod
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123"
3 创建Service文件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: 30006
  selector:
    app: mysql-pod
4 运行
kubectl create -f mysql-rc.yaml
kubectl create -f mysql-svc.yaml

5 结果检查
$kubectl get pods -o wide
NAME             READY     STATUS    RESTARTS   AGE       IP           NODE
mysql-rc-82g2m   1/1       Running   0          1h        172.17.0.2   192.168.56.5

$kubectl get svc -o wide
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
kubernetes   10.254.0.1       <none>        443/TCP          2d        <none>
mysql-svc    10.254.255.244   <nodes>       3306:30006/TCP   1h        app=mysql-pod

$kubectl get endpoints
NAME         ENDPOINTS            AGE
kubernetes   192.168.1.114:6443   2d
mysql-svc    172.17.0.2:3306      1h

6 全部关联上,创建没问题

$ mysql -uroot -p123 -P30006 -h 192.168.56.5 
mysql>
mysql连接成功

如果容器一直处于创建中ContainerCreating,请参考 https://blog.csdn.net/u014447716/article/details/114590967

重启,再尝试

 

 

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐