开始前的准备

dhcpdns.lab.example.com 192.168.0.5  dhcp+dns+ntp
serverc.lab.example.com 192.168.0.30 as a master server
servera.lab.example.com 192.168.0.10 
serverb.lab.example.com 192.168.0.20

禁用防火墙

servera and serverb and serverc disable firewalld

配置NTP

三台服务器都安装docker kubernetes

[root@server]# yum -y install docker

[root@server]# systemctl enable docker && systemctl start docker

配置阿里代理

vim /etc/docker/daemon.json

    {
        "registry-mirrors": ["https://0i5z79j3.mirror.aliyuncs.com"]
    }

systemctl daemon-reload && systemctl restart docker

[root@serverc ~]# vi /etc/yum.repos.d/virt7-testing.repo

[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-docker-el-testing/x86_64/os/
gpgcheck=0

[root@server]# yum -y install --enablerepo=virt7-testing kubernetes

安装kubenetes的数据库包

[root@server]# yum -y install http://cbs.centos.org/kojifiles/packages/etcd/0.4.6/7.el7.centos/x86_64/etcd-0.4.6-7.el7.centos.x86_64.rpm

这个在每个节点上都配置

[root@server]# vi /etc/kubernetes/config

KUBE_MASTER="--master=http://serverc.lab.example.com:8080"
KUBE_ETCD_SERVERS="--etcd_servers=http://serverc.lab.example.com:4001"

serverc(master)主机上配置

[root@serverc yum.repos.d]# vi /etc/kubernetes/apiserver

KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
KUBE_API_PORT="--port=8080"
# KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"

重启服务 etcd kube-apiserver kube-controller-manager kube-scheduler

[root@serverc yum.repos.d]# ps -efw|grep kube

修改servera(node)上的配置

[root@servera ~]# vi /etc/kubernetes/kubelet

KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250"
KUBELET_HOSTNAME="--hostname-override=servera.lab.example.com"
KUBELET_API_SERVER="--api_servers=http://serverc.lab.example.com:8080"

重启服务 docker kubelet kube-proxy

报错解决方案

No API token found for service account "default", retry after the token is automatically created and added to the service account
解决办法是去除认证编辑/etc/kubernetes/apiserver 去除 KUBE_ADMISSION_CONTROL中的SecurityContextDeny,ServiceAccount
并重启kube-apiserver.service服务

[root@serverc ymal-pod]# systemctl restart kube-apiserver.service

[root@serverc pod-yaml]# kubectl get nodes

创建数据库

创建redis的pod

apiVersion: v1
kind: ReplicationController
metadata:
    name: redis-master
    labels:
        name: redis-master
spec:
    replicas: 1
    selector:
        name: redis-master
    template:
        metadata:
            labels:
                name: redis-master
        spec:
            containers:
            - name: master
                image: kubeguide/redis-master
                ports:
                - containerPort: 6379

创建redis的service

[root@serverc pod-yaml]# vi redis-master.yaml

    apiVersion: v1
    kind: Service
    metadata:
        name: redis-master
        labels:
        name: redis-master
    spec:
        ports:
        - port: 6379
        targetPort: 6379
        selector:
        name: redis-master

排错:

[root@serverc ymal-pod]# kubectl describe pod redis-master-jr638

需要在节点(node)上安装
[root@serverc ymal-pod]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
[root@serverc ymal-pod]# yum install *rhsm*

[root@serverc ymal-pod]# vi redis-service.yaml

apiVersion: v1
kind: Service
metadata:
    name: redis-master
    labels:
        name: redis-master
spec:
    ports:
    - port: 6379
        targetPort: 6379
    selector:
        name: redis-master

[root@serverc ymal-pod]# kubectl create -f redis-service.yaml

[root@serverc ymal-pod]# kubectl get pods -o wide

创建从数据库

[root@serverc ymal-pod]# vi redis-slave.yaml

apiVersion: v1
kind: ReplicationController
metadata:
    name: redis-slave
    labels:
        name: redis-slave
spec:
    replicas: 2
    selector:
        name: redis-slave
    template:
        metadata:
            labels:
                name: redis-slave
        spec:
            containers:
            - name: slave
                image: kubeguide/guestbook-redis-slave
                env:
                - name: GET_HOSTS_FROM
                    value: env
                ports:
                - containerPort: 6379

[root@serverc ymal-pod]# vi redis-slave-service.yaml

apiVersion: v1
kind: Service
metadata:
    name: redis-slave
    labels:
        name: redis-slave
spec:
    ports:
    - port: 6379
    selector:
        name: redis-slave

实现redis集群的主从数据同步, redis-slave需要知道redis-master的地址,所以在redis-slave镜像的启动命令/run.sh 中,可以看到如下内容:
redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379

创建前端pod

[root@serverc ymal-pod]# vi frontend-pod.yaml

apiVersion: v1
kind: ReplicationController
metadata:
    name: frontend
    labels:
        name: frontend
spec:
    replicas: 3
    selector:
        name: frontend
    template:
        metadata:
            labels:
                name: frontend
        spec:
            containers:
            - name: frontend
                image: kubeguide/guestbook-php-frontend
                env:
                - name: GET_HOSTS_FROM
                    value: env
                ports:
                - containerPort: 80

创建前端service

[root@serverc ymal-pod]# vi frontend-service.yaml

Version: v1
kind: Service
metadata:
    name: frontend
    labels:
        name: frontend
spec:
    type: NodePort
    ports:
    - port: 80
        nodePort: 30001
    selector:
        name: frontend

[root@serverc pod-yaml]# kubectl create -f frontend-service.yaml

[root@serverc ymal-pod]# kubectl get pod -o wide

NAME                 READY     STATUS    RESTARTS   AGE       IP           NODE
frontend-22b67       1/1       Running   0          4m        172.17.0.3   servera.lab.example.com
frontend-lksvk       1/1       Running   0          4m        172.17.0.4   serverb.lab.example.com
frontend-v81wh       1/1       Running   0          4m        172.17.0.4   servera.lab.example.com
redis-master-xbkwj   1/1       Running   0          51m       172.17.0.2   serverb.lab.example.com
redis-slave-cqj43    1/1       Running   0          32m       172.17.0.2   servera.lab.example.com
redis-slave-q1q4j    1/1       Running   0          32m       172.17.0.3   serverb.lab.example.com

[root@serverc ymal-pod]# kubectl get service

NAME           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
frontend       10.254.127.111          80:30001/TCP   21s
kubernetes     10.254.0.1               443/TCP        1h
redis-master   10.254.20.7              6379/TCP       42m
redis-slave    10.254.156.197           6379/TCP       29m

firefox:http://serverb.lab.example.com:30001/