cloudinyachao 2020-05-05
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl stop NetworkManager systemctl enable NetworkManager service network restart #重启网络 ping www.baidu.com #要实现网络通信
systemctl start firewalld #开启防火墙 iptables -F #清空防火墙规则 setenforce 0 #关闭核心防护
[ ~]# mkdir k8s [ ~]# cd k8s/ //编写cfssl.sh脚本,从官网下载制作证书的工具cfssl,直接放在/usr/local/bin目录下,方便系统识别,最后给工具加执行权限 [ k8s]# vi cfssl.sh curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo chmod +x /usr/local/bin/cfssl /usr/local/bin/cfssljson /usr/local/bin/cfssl-certinfo #执行脚本等待安装下载软件 [ k8s]# bash cfssl.sh [ k8s]# ls /usr/local/bin/ #可以看到三个制作证书的工具 cfssl cfssl-certinfo cfssljson #cfssl:生成证书工具 #cfssl-certinfo:查看证书信息 #cfssljson:通过传入json文件生成证书
[ k8s]# mkdir etcd-cert // 所有证书存放的位置 [ k8s]# mv etcd-cert.sh etcd-cert //生成证书的素材 [ k8s]# cd etcd-cert/
cat > ca-config.json <<EOF { "signing": { "default": { "expiry": "87600h" }, "profiles": { "www": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } } EOF
cat > ca-csr.json <<EOF { "CN": "etcd CA", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing" } ] } EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca –
// IP地址修改成自己的节点 cat > server-csr.json <<EOF { "CN": "etcd", "hosts": [ "192.168.66.130", "192.168.66.132", "192.168.66.133" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "BeiJing", "ST": "BeiJing" } ] } EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
[ k8s]# vim etcd.sh #!/bin/bash # example: ./etcd.sh etcd01 192.168.1.10 etcd02=https://192.168.1.11:2380,etcd03=https://192.168.1.12:2380 ETCD_NAME=$1 ETCD_IP=$2 ETCD_CLUSTER=$3 WORK_DIR=/opt/etcd # 创建节点的配置文件模板 cat <<EOF >$WORK_DIR/cfg/etcd #[Member] ETCD_NAME="${ETCD_NAME}" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://${ETCD_IP}:2380" ETCD_LISTEN_CLIENT_URLS="https://${ETCD_IP}:2379" #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${ETCD_IP}:2380" ETCD_ADVERTISE_CLIENT_URLS="https://${ETCD_IP}:2379" ETCD_INITIAL_CLUSTER="etcd01=https://${ETCD_IP}:2380,${ETCD_CLUSTER}" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_INITIAL_CLUSTER_STATE="new" EOF # 创建节点的启动脚本模板 cat <<EOF >/usr/lib/systemd/system/etcd.service [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target [Service] Type=notify EnvironmentFile=${WORK_DIR}/cfg/etcd ExecStart=${WORK_DIR}/bin/etcd --name=\${ETCD_NAME} --data-dir=\${ETCD_DATA_DIR} --listen-peer-urls=\${ETCD_LISTEN_PEER_URLS} --listen-client-urls=\${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 --advertise-client-urls=\${ETCD_ADVERTISE_CLIENT_URLS} --initial-advertise-peer-urls=\${ETCD_INITIAL_ADVERTISE_PEER_URLS} --initial-cluster=\${ETCD_INITIAL_CLUSTER} --initial-cluster-token=\${ETCD_INITIAL_CLUSTER_TOKEN} --initial-cluster-state=new --cert-file=${WORK_DIR}/ssl/server.pem --key-file=${WORK_DIR}/ssl/server-key.pem --peer-cert-file=${WORK_DIR}/ssl/server.pem --peer-key-file=${WORK_DIR}/ssl/server-key.pem --trusted-ca-file=${WORK_DIR}/ssl/ca.pem --peer-trusted-ca-file=${WORK_DIR}/ssl/ca.pem Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF # 重启服务,并设置开机自启 systemctl daemon-reload systemctl enable etcd systemctl restart etcd
[ k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz // 解压 [ k8s]# ls etcd-v3.3.10-linux-amd64 Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md #稍后使用源码包中的etcd、etcdctl 应用程序命令 [ k8s]# mkdir /opt/etcd/{cfg,bin,ssl} -p //配置文件,命令文件,证书 [ k8s]# ls /opt/etcd/ bin cfg ssl
[ k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin
[ k8s]# cp etcd-cert/*.pem /opt/etcd/ssl/
// 注意修改IP地址 [ k8s]# bash etcd.sh etcd01 192.168.66.130 etcd02=https://192.168.66.132:2380,etcd03=https://192.168.66.133:2380 //使用另外一个会话窗口,会发现etcd进程已经开启 [ ~]# ps aux | grep etcd
[ k8s]# scp -r /opt/etcd/ :/opt [ k8s]# scp -r /opt/etcd/ :/opt
[ k8s]# scp /usr/lib/systemd/system/etcd.service :/usr/lib/systemd/system [ k8s]# scp /usr/lib/systemd/system/etcd.service :/usr/lib/systemd/system
[ system]# cd /opt/etcd/cfg/ [ cfg]# ls etcd [ cfg]# vim etcd
[ system]# cd /opt/etcd/cfg/ [ cfg]# ls etcd [ cfg]# vim etcd
[ k8s]# bash etcd.sh etcd01 192.168.66.130 etcd02=https://192.168.66.132:2380,etcd03=https://192.168.66.133:2380
[ ~]# systemctl start etcd [ ~]# systemctl status etcd
[ k8s]# cd etcd-cert/ [ etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.66.130:2379,https://192.168.66.132:2379,https://192.168.66.133:2379" cluster-health
[ etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.66.130:2379,https://192.168.66.132:2379,https://192.168.66.133:2379" set /coreos.com/network/config ‘{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}‘
[ etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.66.130:2379,https://192.168.66.132:2379,https://192.168.66.133:2379" get /coreos.com/network/config
//拷贝到所有node节点(只需要部署在node节点即可) [ k8s]# scp flannel-v0.10.0-linux-amd64.tar.gz :/root [ k8s]# scp flannel-v0.10.0-linux-amd64.tar.gz :/root //所有node节点操作解压 [ ~]# tar zxvf flannel-v0.10.0-linux-amd64.tar.gz flanneld mk-docker-opts.sh README.md
[ ~]# mkdir /opt/kubernetes/{cfg,bin,ssl} -p [ ~]# mv mk-docker-opts.sh flanneld /opt/kubernetes/bin/
[ ~]# vim flannel.sh #!/bin/bash ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"} cat <<EOF >/opt/kubernetes/cfg/flanneld FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem" EOF cat <<EOF >/usr/lib/systemd/system/flanneld.service [Unit] Description=Flanneld overlay address etcd agent After=network-online.target network.target Before=docker.service [Service] Type=notify EnvironmentFile=/opt/kubernetes/cfg/flanneld ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable flanneld systemctl restart flanneld
[ ~]# bash flannel.sh https://192.168.66.130:2379,https://192.168.66.132:2379,https://192.168.66.133:2379
[ ~]# systemctl status flanneld
[ ~]# vim /usr/lib/systemd/system/docker.service //修改添加两处: EnvironmentFile=/run/flannel/subnet.env $DOCKER_NETWORK_OPTIONS
[ ~]# cat /run/flannel/subnet.env
[ ~]# systemctl daemon-reload [ ~]# systemctl restart docker
[ ~]# docker run -it centos:7 /bin/bash [ /]# yum install net-tools -y #安装后可以使用ifconfig命令
###host字段指定授权使用该证书的etcd节点IP或子网列表,需要将etcd集群的3个节点都添加其中。cp etcd-v3.3.13-linux-amd64/etcd* /opt/k8s/bin/