k8s1

流行的自动运维平台:

Ansible / Git / Jenkins / Docker

平台化、自动化、分类 (配置类、搭建类)、容器化

传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;而容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。

Docker重要概念:

● 镜像(Image)
● 容器(Container)
● 仓库(Repository)

镜像采用了AUFS (联合文件系统),采用了分层结构,镜像是只读的,基础镜像一旦构建完成就不能再改了,但是可以在它上面封装一个层次,把改动的内容放到高层中。

容器:相当于是镜像的一次执行,容器也相当于是镜像可读写副本。

容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的命名空间。

不要假定容器会永远运行,应该假定容器随时可能出现故障,出现故障后,也不会再去恢复它

数据卷的生存周期独立于容器,容器消亡,数据卷不会消亡。

一个 DockerRegistry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。

我们可以通过<仓库名>:<标签>的格式来指定具体是这个软件哪个版本的镜像。
如果不给出标签,将以latest作为默认标签。

在物理机上安装 docker 并启动

[root@666 k8s_pkgs]# yum -y install docker

[root@666 ~]# vim /etc/docker/daemon.json
{
    "registry-mirrors": ["https://8sj91tbl.mirror.aliyuncs.com"],
    "insecure-registries" : ["192.168.8.254:5000"]
}
[root@666 ~]# systemctl restart docker
[root@666 ~]# mkdir k8s
[root@666 ~]# cd k8s/

[root@666 k8s]# docker run busybox echo Hello World
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
53071b97a884: Pull complete 
Digest: sha256:4b6ad3a68d34da29bf7c8ccb5d355ba8b4babcad1f99798204e7abb43e54ee3d
Status: Downloaded newer image for busybox:latest
Hello World

容器、镜像的使用

[root@666 k8s]# docker run -d busybox echo 'Hello World'

如果本机没有 busybox:latest 将会到仓库中下载,然后启动容器
容器运行echo命令后,退出
因为容器只是宿主机上的一个进程,echo是个临时进程,运行后就退出,所以容器也就结束了。
因为容器只是个进程,不是一个完整的系统,所以如果希望它一直处于运行状态,就必须。
使容器内的进程在前台运行程序,不要让前台程序退出

[root@666 k8s]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              64f5d945efcc        3 weeks ago         1.199 MB
ubuntu              latest              452a96d81c30        13 months ago       79.62 MB
centos              latest              e934aafc2206        14 months ago       198.6 MB
registry            latest              d1fd7d86a825        16 months ago       33.26 MB
nginx               latest              a5311a310510        2 years ago         181.4 MB
redis               latest              1aa84b1b434e        2 years ago         182.8 MB
[root@666 k8s]# docker run -it centos bash
[root@242ccfad7ea7 /]# ps aux 
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  11780  1868 ?        Ss   03:35   0:00 bash
root        15  0.0  0.0  47452  1656 ?        R+   03:36   0:00 ps aux

如果执行exit退出,容器又停止运行了,如果希望退回到宿主机的shell但不要停止容器,可以按Ctrl+p+q

[root@666 k8s]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bf7570c87a0e        centos              "bash"              6 minutes ago       Up 6 minutes                            tender_swartz
[root@666 docker+kubernetes]# ls
docker                  k8s              keepalived健康检查.docx
Docker从入门到实践.pdf  k8s_pkgs.tar.gz  Kubernetes权威指南.pdf
images.tar.gz           k8s_setup.txt    mykube
[root@666 docker+kubernetes]# tar -xzf k8s_pkgs.tar.gz
[root@666 k8s_pkgs]# ls
atomic-registries-1.22.1-26.gitb507039.el7.centos.x86_64.rpm
conntrack-tools-1.4.4-4.el7.x86_64.rpm
containers-common-0.1.31-8.gitb0b750d.el7.centos.x86_64.rpm
container-selinux-2.74-1.el7.noarch.rpm
container-storage-setup-0.11.0-2.git5eaf76c.el7.noarch.rpm
docker-1.13.1-91.git07f3374.el7.centos.x86_64.rpm
docker-client-1.13.1-91.git07f3374.el7.centos.x86_64.rpm
docker-common-1.13.1-91.git07f3374.el7.centos.x86_64.rpm
etcd-3.3.11-2.el7.centos.x86_64.rpm
kubernetes-1.5.2-0.7.git269f928.el7.x86_64.rpm
kubernetes-client-1.5.2-0.7.git269f928.el7.x86_64.rpm
kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64.rpm
kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64.rpm
libnetfilter_cthelper-1.0.0-9.el7.x86_64.rpm
libnetfilter_cttimeout-1.0.0-6.el7.x86_64.rpm
libnetfilter_queue-1.0.2-2.el7_2.x86_64.rpm
libselinux-2.5-14.1.el7.x86_64.rpm
libselinux-python-2.5-14.1.el7.x86_64.rpm
libselinux-utils-2.5-14.1.el7.x86_64.rpm
libsemanage-2.5-14.el7.x86_64.rpm
libsemanage-python-2.5-14.el7.x86_64.rpm
libsepol-2.5-10.el7.x86_64.rpm
libyaml-0.1.4-11.el7_0.x86_64.rpm
oci-register-machine-0-6.git2b44233.el7.x86_64.rpm
oci-systemd-hook-0.1.18-3.git8787307.el7_6.x86_64.rpm
oci-umount-2.3.4-2.git87f9237.el7.x86_64.rpm
policycoreutils-2.5-29.el7_6.1.x86_64.rpm
policycoreutils-python-2.5-29.el7_6.1.x86_64.rpm
python-pytoml-0.1.14-1.git7dea353.el7.noarch.rpm
PyYAML-3.10-11.el7.x86_64.rpm
selinux-policy-3.13.1-229.el7_6.9.noarch.rpm
selinux-policy-targeted-3.13.1-229.el7_6.9.noarch.rpm
setools-libs-3.3.8-4.el7.x86_64.rpm
socat-1.7.3.2-2.el7.x86_64.rpm
subscription-manager-rhsm-certificates-1.21.10-3.el7.centos.x86_64.rpm

进入容器的方法:

(1)不启动新进程

[root@666 k8s]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bf7570c87a0e        centos              "bash"              19 minutes ago      Up 19 minutes                           tender_swartz
[root@666 k8s]# docker attach bf
[root@bf7570c87a0e /]# ps a
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:00 bash
   14 ?        R+     0:00 ps a

(2)启用新进程

[root@666 k8s]# docker exec -it bf bash
[root@bf7570c87a0e /]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  11780  1872 ?        Ss+  03:39   0:00 bash
root        15  0.1  0.0  11780  1880 ?        Ss   05:16   0:00 bash
root        28  0.0  0.0  47452  1660 ?        R+   05:16   0:00 ps aux

虚悬镜像

上面的镜像列表中,还可以看到一个特殊的镜像,这个镜像既没有仓库名,也没有
标签,均为 。:

			00285df0df87		5	d  	ays		ago		342	MB

[root@666 k8s]# docker images -f dangling=true

比如说我们去运行一个镜像

[root@666 k8s]# docker exec -it bf bash
[root@bf7570c87a0e /]# yum -y install net-tools
[root@bf7570c87a0e /]# ifconfig 
eth0: flags=4163  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8687  bytes 12831662 (12.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7084  bytes 580480 (566.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

创建镜像的方法:
1、基于原始镜像启动容器,修改容器,完毕后commit成新镜像。不推荐!!!

[root@666 k8s]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bf7570c87a0e        centos              "bash"              About an hour ago   Up About an hour                        tender_swartz
[root@666 k8s]# docker rm -f bf
指定容器的name和主机名
[root@666 k8s]# docker run -it --name centos1 -h mycent centos bash
[root@mycent /]# Ctrl+p+q
[root@666 k8s]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2daf06956b34        centos              "bash"              2 minutes ago       Up 2 minutes                            centos1

2、使用dockerfile
(1) 每个dockerfile的指令都会增加一层,所以要注意书写,不要出现太多层次

[root@666 k8s]# mkdir k8s1
[root@666 k8s]# cd k8s1/
[root@666 k8s1]# vim Dockerfile
FROM centos
RUN yum -y install net-tools vim
[root@666 ~]# docker ps
[root@666 k8s1]# vim Dockerfile
FROM centos
RUN echo 'hello' > /tmp/hi.txt
RUN echo 'workd' > /tmp/abc.txt
[root@666 k8s1]# docker build -t centos:v1 .
[root@666 k8s1]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos              v1                  3e7f3661ca4d        About a minute ago   199MB
                            1dd9747a60b0        8 minutes ago        347MB
centos                            9f38484d220f        2 months ago         202MB
ubuntu              latest              452a96d81c30        13 months ago        79.6MB
centos              latest              e934aafc2206        14 months ago        199MB
registry            latest              d1fd7d86a825        16 months ago        33.3MB
nginx               latest              a5311a310510        2 years ago          181MB
redis               latest              1aa84b1b434e        2 years ago          183MB
[root@666 k8s1]# docker history centos:v1		#新镜像增加了两层
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
3e7f3661ca4d        2 minutes ago       /bin/sh -c echo 'workd' > /tmp/abc.txt          6B                  
a55528e108f3        2 minutes ago       /bin/sh -c echo 'hello' > /tmp/hi.txt           6B                  
e934aafc2206        14 months ago       /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
           14 months ago       /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
           14 months ago       /bin/sh -c #(nop) ADD file:f755805244a649ecc…   199MB 
[root@666 k8s1]# vim Dockerfile
FROM centos
RUN echo 'hello' > /tmp/hi.txt \
    && echo 'workd' > /tmp/abc.txt
[root@666 k8s1]# docker build -t centos:v2 .
[root@666 k8s1]# docker history centos:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
61328c8b5f9b        29 seconds ago      /bin/sh -c echo 'hello' > /tmp/hi.txt     &&…   12B                 
e934aafc2206        14 months ago       /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
           14 months ago       /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
           14 months ago       /bin/sh -c #(nop) ADD file:f755805244a649ecc…   199MB 

(2)镜像构建上下文
默认情况下,构建镜像时,docker会将当前目录所有内容拷贝到context上下文环境中,在Dockerfile中的当前目录(.)实际上指的是context中的当前目录。

[root@666 k8s1]# du -sh /tmp/
    90M			/tmp/
[root@666 ~]# cd /tmp/
[root@666 tmp]# vim Dockerfile
FROM centos
RUN echo 'hello world' > /root/hi.txt
[root@666 tmp]# docker build -t centos:v5 .
[root@666 tmp]# docker images | grep centos
centos              v5                  3e64d6680906        3 minutes ago       199MB
centos              v2                  61328c8b5f9b        16 hours ago        199MB
centos                            9f38484d220f        2 months ago        202MB
centos              latest              e934aafc2206        14 months ago       199MB

Dockerfile指令

  • FROM:基于哪个镜像进行构建

  • RUN:在构建过程中运行的指令

  • COPY:将从构建上下文目录中 <源路径> 的文件/目录复制到新的一层的镜像内的<目标路径>位置

  • CMD:用于指定默认的容器主进程的启动命令的
    (3)构建时,应该创建一个空目录,将所需文件拷贝到空目录中,然后构建

    [root@666 k8s1]# vim Dockerfile 
    FROM centos
    RUN echo 'hello world' > /root/hi.txt
    CMD echo 'abcd'
    [root@666 k8s1]# docker build -t centos:v6 .
    [root@666 k8s1]# docker run centos:v6
    abcd
    

    创建私有藏库

    [root@666 ~]# vim /etc/docker/daemon.json.rpmsave
    {
      "insecure-registries" : ["176.130.2.254:5000"]
    }
    [root@666 ~]# systemctl restart docker
    [root@666 ~]# tar xvzf images.tar.gz
    [root@666 ~]# docker run -d -p 5000:5000 registry
    a1caa3d241a6748e8c11f3fca8b57b293531694e3a3e592b999775cd955949f8   
    [root@666 ~]# curl http://176.130.2.101:5000/v2/
    {}[root@666 ~]#
    
    [root@666 ~]# du -sh images
    2.1G	images
    [root@666 ~]# docker load < images/mysql.tar
    5dacd731af1b: Loading layer [==================================================>]  58.45MB/58.45MB
    f411d8bde01c: Loading layer [==================================================>]  338.4kB/338.4kB
    0aa7d65147ef: Loading layer [==================================================>]  10.44MB/10.44MB
    3437f67a712b: Loading layer [==================================================>]  4.472MB/4.472MB
    ec41e34b35a0: Loading layer [==================================================>]  1.536kB/1.536kB
    458d25c646d8: Loading layer [==================================================>]  46.15MB/46.15MB
    97874ea0e7f9: Loading layer [==================================================>]  31.74kB/31.74kB
    67f6124a308a: Loading layer [==================================================>]  3.584kB/3.584kB
    1717d824958a: Loading layer [==================================================>]  362.1MB/362.1MB
    8dd5a21494bb: Loading layer [==================================================>]  5.632kB/5.632kB
    b5e500c1e699: Loading layer [==================================================>]  9.728kB/9.728kB
    b3318162e91a: Loading layer [==================================================>]  1.536kB/1.536kB
    Loaded image: mysql:latest
    [root@666 ~]# docker images | grep mysql
    mysql               latest              7bb2586065cd        2 months ago        477MB
    [root@666 ~]# docker tag mysql:latest 192.168.4.254:5000/mysql
    [root@666 ~]# docker push 192.168.4.254:5000/mysql
    查看
    [root@666 ~]# curl http://192.168.4.254:5000/v2/_catalog
    

k8s的组成

master: 一般是物理服务器,用于对容器进行控制、调度
node: 可以是物理服务器,也可以是云主机

[root@k8s k8s_pkgs]# ls /etc/rhsm/ca/
redhat-uep.pem		证书
[root@k8s k8s_pkgs]# cp /etc/rhsm/ca/redhat-uep.pem ~
[root@k8s k8s_pkgs]# yum -y remove python-rhsm
[root@k8s ~]# tar xzf k8s_pkgs.tar.gz
[root@k8s ~]# cd k8s_pkgs/
[root@k8s k8s_pkgs]# yum -y install *.rpm

修改docker配置文件,使之可以使用私有仓库

[root@k8s k8s_pkgs]# vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'			4行
[root@k8s k8s_pkgs]# vim /etc/docker/daemon.json
{
  "insecure-registries":["192.168.4.254:5000"]
}
[root@k8s k8s_pkgs]# systemctl restart docker
[root@k8s k8s_pkgs]# systemctl enable docker

启动相关服务
master:
etcd:是一个数据库,各组件信息将会写到etcd中
kube-apiserver:负责执行指令
kube-controller-manager:控制管理器
kune-scheduler:调度
node:
kubelet:node节点接收指令,k8s客户端
kube-proxy:代理节点
docker:真正的容器服务

[root@k8s k8s_pkgs]# systemctl restart etcd
[root@k8s k8s_pkgs]# systemctl enable etcd
[root@k8s k8s_pkgs]# systemctl restart kube-apiserver.service
[root@k8s k8s_pkgs]# systemctl enable kube-apiserver.service
[root@k8s k8s_pkgs]# systemctl restart kube-controller-manager.service 
[root@k8s k8s_pkgs]# systemctl enable kube-controller-manager.service
[root@k8s k8s_pkgs]# systemctl restart kube-scheduler.service
[root@k8s k8s_pkgs]# systemctl enable kube-scheduler.service
[root@k8s k8s_pkgs]# systemctl restart kubelet.service
[root@k8s k8s_pkgs]# systemctl enable kubelet.service
[root@k8s k8s_pkgs]# systemctl restart kube-proxy.service
[root@k8s k8s_pkgs]# systemctl enable kube-proxy.service
[root@k8s k8s_pkgs]# kubectl get node
NAME			STATUS			AGE
127.0.0.1		Ready				7m

配置文件

[root@k8s ~]# ls /etc/kubernetes/
apiserver  config  controller-manager  kubelet  proxy  scheduler
[root@k8s ~]# netstat -ltnp | grep :8080
[root@k8s ~]# vim /etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"			8行
把ServiceAccount,从KUBE_ADMISSION_CONTROL="............."
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"																	23行
[root@k8s ~]# vim /etc/kubernetes/config
KUBE_MASTER="--master=http://192.168.11:8080"						22行
[root@k8s ~]# vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"									5行
KUBELET_API_SERVER="--api-servers=http://192.168.4.254:8080"
																										14行
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=192.168.4.11:5000/pod-infrastructure"								17行
[root@k8s ~]# systemctl restart kubelet.service
[root@k8s ~]# systemctl status kubelet

应用测试

[root@k8s ~]# cd mykube/tomcat_mysql/
[root@k8s tomcat_mysql]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@k8s tomcat_mysql]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@k8s tomcat_mysql]# vim mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: 192.168.4.254:5000/mysql
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
[root@k8s tomcat_mysql]# kubectl create -f mysql-rc.yaml
												创建
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pods
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pods
[root@k8s tomcat_mysql]# docker ps
[root@k8s tomcat_mysql]# kubectl create -f mysql-svc.yaml
												启服务
[root@k8s tomcat_mysql]# kubectl get service
[root@k8s tomcat_mysql]# vim myweb-rc.yaml
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 5
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: 192.168.4.254:5000/tomcat-app
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: 'mysql'
          - name: MYSQL_SERVICE_PORT
            value: '3306'
[root@k8s tomcat_mysql]# kubectl create -f myweb-rc.yaml
[root@k8s tomcat_mysql]# kubectl get pods
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl create -f myweb-rc.yaml
[root@k8s tomcat_mysql]# kubectl get service
访问:http://192.168.4.11:30001
[root@k8s tomcat_mysql]# docker ps
[root@k8s tomcat_mysql]# docker ps | wc -l
15
[root@k8s tomcat_mysql]# docker rm -f c54188
# 删除一个容器后,k8s会发现容器数目已经少于rc声明了,它会自动创建新容器
[root@k8s tomcat_mysql]# kubectl get pods
删除服务-----rc
[root@k8s tomcat_mysql]# kubectl delete service mysql
[root@k8s tomcat_mysql]# kubectl delete service myweb
[root@k8s tomcat_mysql]# kubectl delete rc myweb
[root@k8s tomcat_mysql]# kubectl delete rc mysql

创建mysql的rc声明文件

[root@k8s tomcat_mysql]# vim mysql-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1   # 要求标签是app:mysql的pod数目是1
  selector:     # 选择器,查找标签是app:mysql的pod
    app: mysql
  template:     # 如果pod的数目不达标,创建满足以下条件的pod
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: 192.168.4.254:5000/mysql
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"

现在系统中没有rc / pod / 容器 / 镜像

[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pod
[root@k8s tomcat_mysql]# docker ps
[root@k8s tomcat_mysql]# docker images

根据ymal文件创建相关资源

[root@k8s tomcat_mysql]# kubectl create -f mysql-rc.yaml  
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pod
[root@k8s tomcat_mysql]# docker images
[root@k8s tomcat_mysql]# docker ps

查看到pod名称后,获取它的详细信息

[root@k8s tomcat_mysql]# kubectl describe pod mysql-9vj45

创建服务

[root@k8s tomcat_mysql]# vim mysql-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
[root@k8s tomcat_mysql]# kubectl get service
[root@k8s tomcat_mysql]# kubectl create -f mysql-svc.yaml 
[root@k8s tomcat_mysql]# kubectl get service

mysql服务没有EXTERNAL IP,因为用户不直接访问mysql,而是访问web服务

创建web服务

[root@k8s tomcat_mysql]# vim myweb-rc.yaml 
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 5
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: 192.168.4.254:5000/tomcat-app
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: 'mysql'
          - name: MYSQL_SERVICE_PORT
            value: '3306'
[root@k8s tomcat_mysql]# kubectl create -f myweb-rc.yaml
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pod
[root@k8s tomcat_mysql]# docker ps

myweb将起动10个容器,因为每个pod中需要有一个架构容器,还需要有一个工作容器

[root@k8s tomcat_mysql]# vim myweb-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb

访问node的300001,将会映射到pod的8080

[root@k8s tomcat_mysql]# kubectl create -f myweb-svc.yaml 
[root@k8s tomcat_mysql]# kubectl get service

访问 http://node_ip:30001

如果需要动态调整pod数目,只是将rc改个数字即可

[root@k8s tomcat_mysql]# kubectl scale --replicas=3 replicationcontroller myweb 
[root@k8s tomcat_mysql]# kubectl get rc
[root@k8s tomcat_mysql]# kubectl get pod
删除
[root@k8s tomcat_mysql]# kubectl delete service myweb
[root@k8s tomcat_mysql]# kubectl delete service mysql
[root@k8s tomcat_mysql]# kubectl delete rc myweb 
[root@k8s tomcat_mysql]# kubectl delete rc mysql 

php+redis主从
redis-master

[root@k8s php_redis]# vim redis-master-controller.yaml 
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: 192.168.4.254:5000/redis-master
        ports:
        - containerPort: 6379
[root@k8s php_redis]# kubectl create -f redis-master-controller.yaml
[root@k8s php_redis]# vim redis-master-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@k8s php_redis]# kubectl create -f redis-master-service.yaml 

redis-slave

[root@k8s php_redis]# vim redis-slave-controller.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: 192.168.4.254:5000/guestbook-redis-slave
        env:
        - name: GET_HOSTS_FORM
          value: env
        ports:
        - containerPort: 6379
[root@k8s php_redis]# kubectl create -f redis-slave-controller.yaml
[root@k8s php_redis]# vim redis-slave-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  ports:
  - port: 6379
  selector:
    name: redis-slave
[root@k8s php_redis]# kubectl create -f redis-slave-service.yaml

php

[root@k8s php_redis]# vim frontend-controller.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: 192.168.4.254:5000/guestbook-php-frontend
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
          - containerPort: 80
[root@k8s php_redis]# kubectl create -f frontend-controller.yaml
[root@k8s php_redis]# vim frontend-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30001
  selector:
    name: frontend
[root@k8s php_redis]# kubectl create -f frontend-service.yaml

访问宿主机http://宿主机IP:30001

你可能感兴趣的:(k8s1)