k8s+docker:部署留言板php+redis

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s+docker:部署留言板php+redis相关的知识,希望对你有一定的参考价值。

开始前的准备

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

[[email protected]]# yum -y install docker

[[email protected]]# systemctl enable docker && systemctl start docker

配置阿里代理

vim /etc/docker/daemon.json

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

systemctl daemon-reload && systemctl restart docker

[[email protected] ~]# 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

[[email protected]]# yum -y install --enablerepo=virt7-testing kubernetes

安装kubenetes的数据库包

[[email protected]]# 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

这个在每个节点上都配置

[[email protected]]# 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)主机上配置

[[email protected] 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

[[email protected] yum.repos.d]# ps -efw|grep kube

修改servera(node)上的配置

[[email protected] ~]# 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服务

[[email protected] ymal-pod]# systemctl restart kube-apiserver.service

[[email protected] 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

[[email protected] 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

排错:

[[email protected] ymal-pod]# kubectl describe pod redis-master-jr638

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

[[email protected] 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

[[email protected] ymal-pod]# kubectl create -f redis-service.yaml

[[email protected] ymal-pod]# kubectl get pods -o wide

创建从数据库

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] pod-yaml]# kubectl create -f frontend-service.yaml

[[email protected] 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

[[email protected] ymal-pod]# kubectl get service

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

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

以上是关于k8s+docker:部署留言板php+redis的主要内容,如果未能解决你的问题,请参考以下文章

使用docker部署多个本地redis

部署一个redis exporter监控所有的Redis实例

skywalking—docker镜像构建k8s部署

项目环境搭建Docker+k8s十三 || 项目管理工具"禅道"部署

docker部署并运行Redis

k8s的docker环境下 , redis批量删除key