配置kubernetes 集群 1.14

环境:

192.168.122.171    master
192.168.122.146    node1
192.168.122.2    node2
 

1. 禁用firewalld, selinux。

2. 创建/etc/sysctl.d/k8s.conf文件,添加如下内容:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

3. 执行命令使修改生效

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

4. 开启ipvs:

kube-proxy开启ipvs的前置条件需要加入以下内核模块:

cat > /etc/sysconfig/modules/ipvs.modules <

5. 关闭swap:

swapoff -a

修改 /etc/fstab 文件,注释掉 SWAP 的自动挂载,使用free -m确认swap已经关闭。 swappiness参数调整,修改/etc/sysctl.d/k8s.conf添加下面一行:

vm.swappiness=0

生效:

sysctl -p /etc/sysctl.d/k8s.conf

6. 安装 Docker(所有节点都要执行)

6.1:添加源

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

6.2安装Docker:

yum makecache fast

yum install -y --setopt=obsoletes=0 \
  docker-ce-18.06.1.ce-3.el7

systemctl start docker
systemctl enable docker

7. 使用kubeadm部署Kubernetes(1、2两步所有节点都要执行)

7.1 添加阿里的源

echo '[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'>/etc/yum.repos.d/kubernetes.repo

7.2 安装必要组件:

yum makecache fast
yum install -y kubelet kubeadm kubectl

7.3 使用kubeadm init初始化集群(master上执行)

[root@master ~]# cd k8s-1.14/
[root@master k8s-1.14]# ls
coredns.tar  kube-apiserver.tar           kube-proxy.tar      pause.tar
etcd.tar     kube-controller-manager.tar  kube-scheduler.tar

[root@master k8s-1.14]# for i in `ls`
> do
> docker load < ${i}
> done
fb61a074724d: Loading layer  479.7kB/479.7kB
c6a5fc8a3f01: Loading layer  40.05MB/40.05MB
Loaded image: k8s.gcr.io/coredns:1.3.1
8a788232037e: Loading layer   1.37MB/1.37MB
30796113fb51: Loading layer    232MB/232MB
6fbfb277289f: Loading layer  24.98MB/24.98MB
Loaded image: k8s.gcr.io/etcd:3.3.10
5ba3be777c2d: Loading layer  43.88MB/43.88MB
97f70f3a7a0c: Loading layer  167.6MB/167.6MB
Loaded image: k8s.gcr.io/kube-apiserver:v1.14.1
d8ca6e1aa16e: Loading layer  115.6MB/115.6MB
Loaded image: k8s.gcr.io/kube-controller-manager:v1.14.1
0b8d2e946c93: Loading layer  3.403MB/3.403MB
8b9a8fc88f0d: Loading layer  36.69MB/36.69MB
Loaded image: k8s.gcr.io/kube-proxy:v1.14.1
e04ef32df86e: Loading layer  39.26MB/39.26MB
Loaded image: k8s.gcr.io/kube-scheduler:v1.14.1
e17133b79956: Loading layer  744.4kB/744.4kB
Loaded image: k8s.gcr.io/pause:3.1

*以上步骤是因为k8s集群所需部分镜像无法下载,若您下载镜像有问题,可在评论区留下邮箱,可免费发送一份。

systemctl enable kubelet.service
kubeadm init \
  --kubernetes-version=v1.14.1 \
  --pod-network-cidr=10.244.0.0/16 \
  --apiserver-advertise-address=192.168.122.23

 

8. 将kubenetest配置写入环境变量

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile

9. node节点注册到master(node节点执行)

node节点使用master 节点kubeadmin init最后结果中的kubeadm join XXX这串命令,形如:

kubeadm join 192.168.122.171:6443 --token wkuu0r.n7hi3uj6y2qv471b \
    --discovery-token-ca-cert-hash sha256:46d8463f92a68de637f9698c8ce69dffac9b0ff33a04921843a6172e5d390f20 

10. 验证:

[root@master ~]# kubectl get node
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   150m   v1.14.1
node1    Ready       149m   v1.14.1
node2    Ready       148m   v1.14.1
 

11. 编辑RC创建pod,svc:

[root@master ~]# cat mysql-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 3
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
[root@master ~]# cat mysql-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql

[root@master ~]# docker pull mysql:5.6

[root@master ~]# kubectl create -f mysql-rc.yaml

[root@master ~]# kubectl create -f mysql-svc.yaml 

[root@master ~]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
mysql-j22d4   1/1     Running   1          116m
mysql-m9gf6   1/1     Running   3          116m
mysql-nnz4g   1/1     Running   3          116m
 

(副本数为3,所以生成了3个pod)

[root@master ~]# kubectl scale --current-replicas=3 --replicas=5 -f mysql-rc.yaml 
replicationcontroller/mysql scaled
[root@master ~]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
mysql-875lr   1/1     Running   0          3s
mysql-j22d4   1/1     Running   1          117m
mysql-m9gf6   1/1     Running   3          117m
mysql-nnz4g   1/1     Running   3          117m
mysql-z5thb   1/1     Running   0          3s
(将副本数调整为5个)

 

你可能感兴趣的:(CentOS)