春风十里不如你 —— Taozi - httpd https://www.xiongan.host/index.php/tag/httpd/ zh-CN Wed, 17 May 2023 13:35:50 +0800 Wed, 17 May 2023 13:35:50 +0800 【k8s】service服务和job服务 https://www.xiongan.host/index.php/archives/208/ https://www.xiongan.host/index.php/archives/208/ Wed, 17 May 2023 13:35:50 +0800 admin Service服务发现

使用Service

使用Service实验使用的目录病创建后端的httpd-Dy

[root@master servicefile]# vim httpd-dy.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: httpd
spec:
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

部署Deployment并查看信息

68428286502

创建httpd-service.yaml文件

[root@master servicefile]# vim httpd-service.yaml

68428302420

创建service并查看该信息(下kubernetes服务是系统服务)

68428311268

测试服务可用性,通过curl命令查看服务是否正常

[root@master servicefile]# curl 10.102.124.67:8080

68428339308

可以删除刚刚创建的服务

68428345193

创建httpd-expose.yaml,并部署

[root@master servicefile]# vim httpd-expose.yaml
kind: Service
apiVersion: v1
metadata:
  name: httpd-svc
spec:
  type: NodePort
  selector:
    app: httpd
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
    nodePort: 30144

68428366522

使用跳板机浏览器登录,查看node节点ip:端口

68428375684

使用DNS

创建client.yaml,创建一个客户端pod,测试DNS功能

[root@master servicefile]# vim client.yaml
kind: Pod
apiVersion: v1
metadata:
  name: clientpod
spec:
  containers:
    - name: clientpod
      image: busybox:1.28.3
      args:
      - /bin/sh
      - -c
      - sleep 30000

创建并进入Pod命令行

[root@master servicefile]# kubectl apply -f client.yaml

使用nslookup命令查看服务域名,wget命令通过域名访问服务

68428417383

实训任务

创建deployment1

要求: 2 副本,镜像类型 httpd

68428612091

68428608656

创建deployment2

要求: 3副本,镜像类型 httpd

68428614089

68428616050

创建 service1,service1 后端为 deployment1 和 deployment2 中所有 pod。

68428621914

68428623761

创建 service2,service2 后端为 deployment1 中的第一个 pod 和 deployment2 中的第一个pod

为dy的第一个pod和dy2的第一个pod打上标签tz=httpd01

68428650498

68428651581

68428665013

68428668963

查看容器的详细信息

68428682792

查看端口信息,可以看到svc2的pod是要求所说的

68428673602

DeamonSet 与 Job

使用DaemonSet

创建一个Daemonset的yaml文件,并运行

[root@master servicefile]# vim DS-nginx.yaml
kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: nginx-daemonset
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

68429120276

查看daemonset的pod信息位置

68429141307

删除pod,查看daemonset的自动恢复功能

68429156829

查看到已经恢复好了

68429158434

使用Job

创建Job的yaml文件

[root@master servicefile]# vim pi-job.yaml
kind: Job
apiVersion: batch/v1
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4

创建Job并查看运行状态,他运行完毕后自动关闭了

68429246585

68429247721

查看他的运行结果

68429253813

使用CronJob

创建CronJob的yaml文件,设置每一分钟运行一次返回一次hello

[root@master servicefile]# vim CJ-hello.yaml
kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernets cluster-tz123
          restartPolicy: OnFailure

运行cronjob,查看运行情况

68429293265

查看pod的状态已经完成

68429294371

68429299912

可以查看到运行cronjob后,每隔一分钟就会创建新的pod的,并输出信息

实训任务

创建一个 DaemonSet

  1. 包含两个 pod
  2. 镜像为 nginx

创建一个job,用于输出helloworld

创建一个 cronjob,在每日的 xx 小时 xx 点输出 helloworld。

删除本次实验创建 DaemonSet,Job 和 CronJob。

]]>
0 https://www.xiongan.host/index.php/archives/208/#comments https://www.xiongan.host/index.php/feed/tag/httpd/
【centos】环境下keepalived管家的实际应用 https://www.xiongan.host/index.php/archives/37/ https://www.xiongan.host/index.php/archives/37/ Tue, 04 Oct 2022 15:57:00 +0800 admin 案例要求:一台web服务器宕机后,被keepalived检测到,切换到从服务器第二台备用,使服务正常运行,之后进行排错进行整改第一台机器
准备:
主机表
另外在准备好一台可以访问网页的机器便于测试

1. Server端、client端都需要安装keepalived httpd服务
yum install -y httpd keepalived

2. Server端:
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node3.tao.com
}
vrrp_instance websrv {
state MASTER <==指定此节点为 Master 节点
interface br0(修改成本机使用的网卡) <==指定监听的网卡
virtual_router_id 51
priority 100 <==指定优先级,数字越高约优先
advert_int 1 <==心跳监测,单位为 s
authentication {
auth_type PASS <==设定验证方式
auth_pass 1111 <==设定密码为 1111
}
virtual_ipaddress {
192.168.123.250 <==指定 VIP
}
}
保存后 重启 keepalived服务
之后在/var/www/html/中写一个测试页
vi index.html
server.tao.com
保存后 重启httpd服务

3. Client端
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node4.tao.com
}
vrrp_instance websrv {
state MASTER <==指定此节点为 Master 节点
interface br0(修改成本机使用的网卡) <==指定监听的网卡
virtual_router_id 51
priority 50 <==指定优先级,数字越高约优先
advert_int 1 <==心跳监测,单位为 s
authentication {
auth_type PASS <==设定验证方式
auth_pass 1111 <==设定密码为 1111
}
virtual_ipaddress {
192.168.123.250 <==指定 VIP
}
}
保存后 重启 keepalived服务
之后在/var/www/html/中写一个测试页
Vi index.html
Server.tao.com
保存后 重启httpd服务

4. 测试结果
在server 和client端 keepalived都正常 显示的是server端页面
Server端关闭keepalived模拟宕机 显示的就是client端
测试

]]>
0 https://www.xiongan.host/index.php/archives/37/#comments https://www.xiongan.host/index.php/feed/tag/httpd/
docker环境下httpd【镜像构建】 https://www.xiongan.host/index.php/archives/25/ https://www.xiongan.host/index.php/archives/25/ Wed, 28 Sep 2022 19:54:00 +0800 admin 案例要求:需要centos镜像下的httpd环境直接打包成新的镜像,直接使用
准备:一台装有docker环境的虚拟机,拉取centos:7的镜像(最新的也可以)

1.使用docker commit构建镜像

  • 拉取镜像
    拉取镜像
  • 创建环境

    [root@xiongan /]# yum clean all
    [root@xiongan /]# yum install -y httpd
    [root@xiongan /]# vi /var/www/html/index.html
    [root@xiongan /]# httpd -k start #使httpd服务立即生效

    【扩展】:
    扩展

    此为httpd的配置文件中servername没有修改 95行 ServerName loaclhost:80

    [root@xiongan /]# curl http://172.17.0.2
    hello,docker-xiongan

    以上环境就搭建完成

    #使用docker commit 构建新的镜像
    [root@docker-tz ~]# docker commit xiongan centos-httpd:v1

    构建

2.使用docker build构建镜像-Dockerfile

  • 创建dockerfile目录
    在本机内创建dockerfile目录→创建Dockerfile文件 run-httpd.sh文件 index.html文件
    创建dockerfile
  • 编辑脚本内容

    Dockerfile:
    FROM centos:7
    MAINTAINER "tz taozi@taozi.com"
    RUN yum clean all
    RUN yum -y install httpd
    ADD run-httpd.sh /run-httpd.sh
    RUN chmod 755 /run-httpd.sh
    ADD index.html /var/www/html
    EXPOSE 80
    WORKDIR /
    CMD ["/bin/bash","/run-httpd.sh"]
    run-httpd.sh:
    #! /bin/bash
    rm -rf /run/httpd*
    exec /sbin/httpd -D FOREGROUND
  • 测试执行
    [root@docker-tz dockerbuild]# docker build -t centos-test:v1 . #末尾有个. 不要丢掉 代表当前运行目录下的脚本文件
    成功
    exec查看
]]>
0 https://www.xiongan.host/index.php/archives/25/#comments https://www.xiongan.host/index.php/feed/tag/httpd/