操做系統:CentOS Linux release 7.4.1708node
在 /etc/sysconfig/network-scripts 路徑下找到 ifcfg- , 表明具體網卡,本文修改的網卡是 ifcfg-enp0s3redis
ONBOOT=yes #開機啓動 BOOTPROTO=static #靜態IP IPADDR=192.168.1.200 #本機地址 NETMASK=255.255.255.0 #子網掩碼 GATEWAY=192.168.1.1 #默認網關
配置文件/etc/sysconfig/networkdocker
# Created by anaconda DNS1=192.168.1.1 DNS2=8.8.8.8
hostnamectl --static set-hostname [主機名]
注:修改爲與/etc/hosts對應的相同名稱! shell
經過ssh-keygen -t rsa和ssh-copy-id命令,再也不贅述json
yum install ansible
編輯/etc/ansible/hosts,在末尾添加服務器信息,添加組:kube、master、nodes:centos
[kube] 192.168.1.[210:213] [master] 192.168.1.210 [nodes] 192.168.1.[211:213]
查詢kube組內全部服務器啓動運行時間:api
ansible kube -a 'uptime'
ansible kube -m shell -a 'yum -y install epel-release'
ansible kube -m shell -a 'echo -e "192.168.1.210 kube-master\n192.168.1.211 kube-minion-1\n192.168.1.212 kube-minion-2\n192.168.1.213 kube-minion-3" >> /etc/hosts'
注:若是不加參數-m shell,會默認使用command模塊致使添加失敗瀏覽器
ansible kube -a 'cat /etc/hosts'
ansible kube -m shell -a 'yum -y install docker' 或 ansible kube -m yum -a 'name=docker state=present'
ansible kube -m yum -a 'name=docker state=present'
ansible kube -m service -a 'name=docker state=restarted enabled=yes'
ansible kube -m shell -a 'systemctl status docker'
ansible kube -m yum -a 'name=ntp state=present'
注:NTP配置文件在/etc/ntp.conf,能夠對NTP服務器進行設置,NTP服務器可訪問http://www.pool.ntp.org/zh/查看,pool.ntp.org是一個高可用時間服務器虛擬集羣項目,網站建議使用下列默認域名,每一個域名會每小時隨機一組NTP服務器,進行時間同步時它會隨機返回離你較近的NTP服務器。bash
server 0.pool.ntp.org server 1.pool.ntp.org server 2.pool.ntp.org server 3.pool.ntp.org
ansible kube -m service -a 'name=ntpd state=restarted enabled=yes'
ansible kube -m shell -a 'systemctl status ntpd'
ansible kube -a 'ntpq -p'
注:NTP服務器列表可能須要等幾分鐘後才能得到並完成對時,每一個服務器得到的NTP服務器不同是正常的。服務器
如下步驟參考Kubernetes官網教程:
https://kubernetes.io/docs/getting-started-guides/centos/centos_manual_config/
ansible kube -m shell -a 'echo "[virt7-docker-common-release] name=virt7-docker-common-release baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/ gpgcheck=0" > /etc/yum.repos.d/virt7-docker-common-release.repo'
ansible kube -m shell -a 'yum -y install --enablerepo=virt7-docker-common-release kubernetes etcd flannel'
默認內容:
### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR="--logtostderr=true" # journal message level, 0 is debug KUBE_LOG_LEVEL="--v=0" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV="--allow-privileged=false" # How the controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER="--master=http://127.0.0.1:8080"
須要把KUBE_MASTER改爲:
KUBE_MASTER="--master=http://kube-master:8080"
操做機執行修改命令:
ansible kube -m shell -a 'echo "### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR=\"--logtostderr=true\" # journal message level, 0 is debug KUBE_LOG_LEVEL=\"--v=0\" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV=\"--allow-privileged=false\" # How the controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER=\"--master=http://kube-master:8080\"" > /etc/kubernetes/config'
ansible kube -m shell -a 'setenforce 0; systemctl disable firewalld; systemctl stop firewalld; reboot'
配置文件在/etc/etcd/etcd.conf,注意確認配置文件中的如下參數與下文一致,主要是兩個localhost改爲0.0.0.0
# [member] ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" #[cluster] ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
打開/etc/kubernetes/apiserver,用如下內容覆蓋:
# The address on the local server to listen to. KUBE_API_ADDRESS="--address=0.0.0.0" # The port on the local server to listen on. KUBE_API_PORT="--port=8080" # Port kubelets listen on KUBELET_PORT="--kubelet-port=10250" # Comma separated list of nodes in the etcd cluster KUBE_ETCD_SERVERS="--etcd-servers=http://kube-master:2379" # Address range to use for services KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # default admission control policies KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota" # Add your own! KUBE_API_ARGS=""
注:KUBE_ADMISSION_CONTROL裏去掉了ServiceAccount
systemctl start etcd etcdctl mkdir /kube-centos/network etcdctl mk /kube-centos/network/config "{ \"Network\": \"172.30.0.0/16\", \"SubnetLen\": 24, \"Backend\": { \"Type\": \"vxlan\" } }"
配置文件/etc/sysconfig/flanneld,修改爲如下內容:
# Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://kube-master:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/kube-centos/network" # Any additional options that you want to pass #FLANNEL_OPTIONS=""
批量修改指令:
ansible kube -m shell -a 'echo "# Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS=\"http://kube-master:2379\" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX=\"/kube-centos/network\" # Any additional options that you want to pass #FLANNEL_OPTIONS=\"\"" > /etc/sysconfig/flanneld'
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler flanneld; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done
配置文件/etc/kubernetes/kubelet,改成如下內容:
# The address for the info server to serve on KUBELET_ADDRESS="--address=0.0.0.0" # The port for the info server to serve on KUBELET_PORT="--port=10250" # You may leave this blank to use the actual hostname # Check the node number! KUBELET_HOSTNAME="--hostname-override=kube-minion-n" # Location of the api-server KUBELET_API_SERVER="--api-servers=http://kube-master:8080" KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" # Add your own! KUBELET_ARGS=""
注:這裏註釋掉了KUBELET_HOSTNAME,是爲了使用服務器主機名當kubelet名
批量操做指令:
ansible nodes -m shell -a 'echo "# The address for the info server to serve on KUBELET_ADDRESS=\"--address=0.0.0.0\" # The port for the info server to serve on KUBELET_PORT=\"--port=10250\" # You may leave this blank to use the actual hostname # Check the node number! KUBELET_HOSTNAME=\"kube-minion-n\" # Location of the api-server KUBELET_API_SERVER=\"--api-servers=http://kube-master:8080\" # Add your own! KUBELET_ARGS=\"\"" >/etc/kubernetes/kubelet'
注:KUBELET_HOSTNAME要改爲和/etc/hosts裏的一致
ansible nodes -m shell -a 'for SERVICES in kube-proxy kubelet flanneld docker; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done'
kubectl config set-cluster default-cluster --server=http://kube-master:8080 kubectl config set-context default-context --cluster=default-cluster --user=default-admin kubectl config use-context default-context kubectl get nodes
目前爲止Kubernetes就搭建完了,拍拍本身肩膀說乾的不錯小夥~ :P
須要用到下列兩個Docker鏡像:
因爲國內被屏蔽沒法直接下載到,因此要用能夠訪問的主機下載後添加到全部主機裏。也能夠經過docker tag命令添加到Docker私有庫後使用。
操做指令:
# 導出鏡像 docker save gcr.io/google_containers/kubernetes-dashboard-amd64:v1.5.1 > dashboard.tar # 導入鏡像 docker load < dashboard.tar
編輯kubernetes-dashboard.yaml,內容以下:
kind: Deployment apiVersion: extensions/v1beta1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 selector: matchLabels: app: kubernetes-dashboard template: metadata: labels: app: kubernetes-dashboard # Comment the following annotation if Dashboard must not be deployed on master annotations: scheduler.alpha.kubernetes.io/tolerations: | [ { "key": "dedicated", "operator": "Equal", "value": "master", "effect": "NoSchedule" } ] spec: containers: - name: kubernetes-dashboard image: gcr.io/google_containers/kubernetes-dashboard-amd64:v1.5.1 imagePullPolicy: IfNotPresent ports: - containerPort: 9090 protocol: TCP args: # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. - --apiserver-host=http://192.168.1.210:8080 #注意這裏是master的api的地址,要寫master的IP,寫域名會報錯提示訪問不到 livenessProbe: httpGet: path: / port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 --- kind: Service apiVersion: v1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 80 targetPort: 9090 selector: app: kubernetes-dashboard
在kube-master上運行:
#開啓Dashboard kubectl create -f kubernetes-dashboard.yaml #查看pod運行狀態 kubectl get pods --all-namespaces #返回結果示例: #NAMESPACE NAME READY STATUS #RESTARTS AGE #kube-system kubernetes-dashboard-3345393181-6vq94 1/1 Running 0 44m #kube-system zl-redis-1545002913-89r4m 1/1 Running 0 38m #kube-system zl-redis-1545002913-cbgv5 1/1 Running 0 38m #查看單個pod的描述 kubectl describe pod/[pod名字] --namespace=[命名空間] #例:kubectl describe pod/zl-redis-1545002913-cbgv5 --namespace=kube-system #查看pod日誌 kubectl logs -f [pod名字] --namespace=[命名空間] #例:kubectl logs -f zl-redis-1545002913-cbgv5 --namespace=kube-system
訪問kube-master網址:http://192.168.1.210:8080/ui