ช่วยอธิบายแบบนี้หมายความว่างัยครับ varnish

ระบบของผม varnish แบบนี้ผมเอามาจาก top ครับ
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15096 varnish 20 0 43.4g 1.0g 847m S 11.9 6.5 106:38.38 varnishd

แต่ละส่วนหมายถึงอะไรครับ

ขอบคุณครับ

http://www.icez.net/blog/715/top-linux-administrative-tools
By : พี่ไอซ์ @icez

ขอบคุณครับ

ถ้าดูจากshow ระบบของผมมานทำงานปกติมั๊ยครับ
Mem: 16319252k total, 15829624k used, 489628k free, 1601072k buffers
Swap: 8232952k total, 8692k used, 8224260k free, 9112004k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15221 varnish 20 0 38.4g 200m 155m S 5.2 1.3 0:57.83 varnishd

ปัญหาคือถ้า Mem used = 16100000 k โดยประมาณจะทำให้ระบบเว็บใช้งานไม่ได้
คนเข้าใช้งานเยอะ หรือว่า ระบบไม่ยอม คืน ram ถ้าไม่มีการใช้งาน

httpd.conf
Timeout 20
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 3

<IfModule prefork.c>
StartServers 10
MinSpareServers 10
MaxSpareServers 24
ServerLimit 500
MaxClients 500
MaxRequestsPerChild 4000
</IfModule>

Ram 16 gb. ครับ
Intel(R) Xeon(R) CPU E5506 @ 2.13GHz

ขอบคุณครับ

จะก๊อป top ก็ก๊อปมาให้หมดครับ อย่าตัดหัวตัดหางมันทิ้งครับ มันสำคัญ

เรื่องดู mem usage นี่บอกกันหลายทีจนขี้เกียจจะพิมพ์แล้วครับ

ธรรมชาติของ linux จะพยายามใช้แรมให้เต็มที่ เพื่อให้เกิดประโยชน์สูงสุดตลอดเวลา เพราะงั้นถ้าดูที่ mem used / mem free มันจะใช้เต็มเป็นปกติ "และมันไม่ปล่อยให้ว่างเปล่าๆ ปลี้ๆ"
โดยส่วนที่ใช้แล้วไม่เกิดปัญหาคือ cache / nuffer ที่เห็นใน top นั่นแหละครับ ให้นับตรงนี้เป็น ram ว่างเหลือพร้อมใช้ไปด้วย

เพราะงั้น ตัดความคิดที่ว่าใช้เมมเต็มแล้วเครื่องจะค้างทิ้งไปครับ
ตราบใดที่มันยังไม่ไปกินพื้นที่ swap ก็ไม่มีปัญหาอะไรทั้งสิ้น

ที่เห็นใช้ varnish ตอนนี้ตั้งค่าไว้ยังไง ก๊อป config /etc/sysconfig/varnish มาให้หน่อยครับ

file config varnish

Configuration file for varnish

/etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this

shell script fragment.

Maximum number of open files (for ulimit -n)

NFILES=131072

Locked shared memory (for ulimit -l)

Default log size is 82MB + header

MEMLOCK=82000

Maximum number of threads (for ulimit -u)

NPROCS=“unlimited”

Maximum size of corefile (for ulimit -c). Default in Fedora is 0

DAEMON_COREFILE_LIMIT=“unlimited”

Set this to 1 to make init script reload try to switch vcl without restart.

To make this work, you need to set the following variables

explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,

VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,

use Alternative 3, Advanced configuration, below

RELOAD_VCL=1

This file contains 4 alternatives, please use only one.

Alternative 1, Minimal configuration, no VCL

Listen on port 6081, administration on localhost:6082, and forward to

content server on localhost:8080. Use a fixed-size cache file.

#DAEMON_OPTS="-a :6081 \

-T localhost:6082 \

-b localhost:8080 \

-u varnish -g varnish \

-s file,/var/lib/varnish/varnish_storage.bin,1G"

Alternative 2, Configuration with VCL

Listen on port 6081, administration on localhost:6082, and forward to

one content server selected by the vcl file, based on the request. Use a

fixed-size cache file.

DAEMON_OPTS="-a :8080
-T localhost:6082
-f /etc/varnish/default.vcl
-u varnish -g varnish
-p purge_dups=on
-p shm_workspace=32768
-p ping_interval=1
-p thread_pool_add_delay=2
-p thread_pools=<Number of CPU cores>
-p thread_pool_min=<800 / Number of CPU cores>
-p thread_pool_max=4000
-p session_linger=50
-p sess_workspace=262144 \

-s malloc,512M \

         -S /etc/varnish/secret \
         -p send_timeout=1500 \
         -s file,/var/lib/varnish/varnish_storage.bin,3G"

Alternative 3, Advanced configuration

See varnishd(1) for more information.

# Main configuration file. You probably want to change it :slight_smile:

VARNISH_VCL_CONF=/etc/varnish/default.vcl

# Default address and port to bind to

# Blank address means all IPv4 and IPv6 interfaces, otherwise specify

# a host name, an IPv4 dotted quad, or an IPv6 address in brackets.

VARNISH_LISTEN_ADDRESS=

VARNISH_LISTEN_PORT=8080

# Telnet admin interface listen address and port

VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082

# Shared secret file for admin interface

VARNISH_SECRET_FILE=/etc/varnish/secret

# The minimum number of worker threads to start

#VARNISH_MIN_THREADS=50
#VARNISH_MIN_THREADS=1
VARNISH_MIN_THREADS=200
#VARNISH_MIN_THREADS=10

# The Maximum number of worker threads to start

#VARNISH_MAX_THREADS=1000
VARNISH_MAX_THREADS=2000

# Idle timeout for worker threads

#VARNISH_THREAD_TIMEOUT=120
#VARNISH_THREAD_TIMEOUT=20
VARNISH_THREAD_TIMEOUT=30

# Cache file location

VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin

# Cache file size: in bytes, optionally using k / M / G / T suffix,

# or in percentage of available disk space using the % suffix.

VARNISH_STORAGE_SIZE=3G

# Backend storage specification

VARNISH_STORAGE=“file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}”
#VARNISH_STORAGE=“malloc,${VARNISH_STORAGE_SIZE}”

# Default TTL used when the backend does not specify one

#VARNISH_TTL=120
VARNISH_TTL=30
#VARNISH_TTL=60

# DAEMON_OPTS is used by the init script. If you add or remove options, make

# sure you update this section, too.

DAEMON_OPTS=“-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT}
-f ${VARNISH_VCL_CONF}
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT}
-t ${VARNISH_TTL}
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT}
-u varnish -g varnish
-S ${VARNISH_SECRET_FILE}
-s ${VARNISH_STORAGE}”

Alternative 4, Do It Yourself. See varnishd(1) for more information.

DAEMON_OPTS=“”

default.conf

set default backend if no server cluster specified

backend default {
.host = “127.0.0.1”;
.port = “80”;
.probe = {
.url = “/”;
.interval = 5s;
.timeout = 1s;
.window = 5;
.threshold = 3;
}
.connect_timeout = 120s;
.first_byte_timeout = 120s;
.between_bytes_timeout = 120s;
.max_connections = 700;
# .port = “80” led to issues with competing for the port with apache.
}

access control list for “purge”: open to only localhost and other local nodes

acl purge {
“127.0.0.1”;
}

vcl_recv is called whenever a request is received

sub vcl_recv {
# Serve objects up to 2 minutes past their expiry if the backend
# is slow to respond.
set req.grace = 120s;
#set req.grace = 180s;
set req.http.X-Forwarded-For = client.ip;
set req.backend = default;

    # This uses the ACL action called "purge". Basically if a request to
    # PURGE the cache comes from anywhere other than localhost, ignore it.
    if (req.request == "PURGE")
        {if (!client.ip ~ purge)
            {error 405 "Not allowed.";}
        return(lookup);}

Pass any requests that Varnish does not understand straight to the backend.

    if (req.request != "GET" && req.request != "HEAD" &&
        req.request != "PUT" && req.request != "POST" &&
        req.request != "TRACE" && req.request != "OPTIONS" &&
        req.request != "DELETE")
        {return(pipe);}     /* Non-RFC2616 or CONNECT which is weird. */


    # Pass anything other than GET and HEAD directly.
    if (req.request != "GET" && req.request != "HEAD")
       {return(pass);}      /* We only deal with GET and HEAD by default */


    # Pass requests from logged-in users directly.
    if (req.http.Authorization || req.http.Cookie)
       {return(pass);}      /* Not cacheable by default */


    # Pass any requests with the "If-None-Match" header directly.
    if (req.http.If-None-Match)
       {return(pass);}


    # Force lookup if the request is a no-cache request from the client.
    if (req.http.Cache-Control ~ "no-cache")
       {ban_url(req.url);}


    # normalize Accept-Encoding to reduce vary
    if (req.http.Accept-Encoding) {
      if (req.http.User-Agent ~ "MSIE 6") {
        unset req.http.Accept-Encoding;
      } elsif (req.http.Accept-Encoding ~ "gzip") {
        set req.http.Accept-Encoding = "gzip";
      } elsif (req.http.Accept-Encoding ~ "deflate") {
        set req.http.Accept-Encoding = "deflate";
      } else {
        unset req.http.Accept-Encoding;
      }
    }


    return(lookup);

}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set req.http.connection = “close”;

    # This is otherwise not necessary if you do not do any request rewriting.


    set req.http.connection = "close";

}

Called if the cache has a copy of the page.

sub vcl_hit {
if (req.request == “PURGE”)
{ban_url(req.url);
error 200 “Purged”;}

    if (!obj.ttl &gt; 0s)
       {return(pass);}

}

Called if the cache does not have a copy of the page.

sub vcl_miss {
if (req.request == “PURGE”)
{error 200 “Not in cache”;}
}

Called after a document has been successfully retrieved from the backend.

sub vcl_fetch {

    # set minimum timeouts to auto-discard stored objects

set beresp.prefetch = -30s;

    set beresp.grace = 120s;


    #if (beresp.ttl &lt; 48h) {
    if (beresp.ttl &lt; 3h) {
      #set beresp.ttl = 48h;}
      set beresp.ttl = 3h;}

if (!beresp.ttl > 0s)
{return(hit_for_pass);}

    if (beresp.http.Set-Cookie)
        {return(hit_for_pass);}

if (beresp.http.Cache-Control ~ “(private|no-cache|no-store)”)

{return(hit_for_pass);}

    if (req.http.Authorization && !beresp.http.Cache-Control ~ "public")
        {return(hit_for_pass);}

}

ผมใช้ firewall redirect port ไปครับ มานจะทำให้เว็บช้าลงหรือปล่าว ^^ !

อานนี้ผมปรับตามประสาคนไม่รู้ครับ รองผิดรองถูก ไปเรื่อยๆ ครับ
ขอคำแนะนำด้วยครับ ขอบคุณครับ

ขออภัยครับคุณ iLhay ^^

อ่านไปอ่านมา เหมือนกับว่าแค่ถามว่า มันอ่านยังไง

จริงๆแล้ว อ่านใน blog พี่ icez ก็น่าจะเข้าใจแล้วนะครับ

เรื่องแรม ตามพี่ไอซ์บอกครับ ตราบใดที่ไม่เตะลง swap เยอะๆนี่ไม่ต้องกังวลอะไรเลย

เพิ่มอีกนิด วิธีคิด MaxClients http://www.icez.net/blog/75280/myth-apache-config

เพิ่มอีกนิด กะ command : http://www.cyberciti.biz/tips/top-linux-monitoring-tools.html

ขอกลับไปเริ่มต้นใหม่

คำถามแรก และคำถามเดียว ปัญหาของคุณคืออะไรครับ

ปัญหาที่ถามคือ varnish ครับ
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15096 varnish 20 0 43.4g 1.0g 847m S 11.9 6.5 106:38.38 varnishd

ตัวสีแดงครับ
ปัญหาคือถ้า Mem used = 16100000 k โดยประมาณจะทำให้ระบบเว็บใช้งานไม่ได้ หรือว่า และทำให้ ตัวเลขสีแดงเพิ่มขึ้นเรื่อยๆ
ปัญหาคนเข้าใช้งานเยอะเลยทำให้ varnish ทำงานหนัก หรือว่า อะไรที่ทำให้ varnish ใช้งานระบบหนัก ครับ

mysql กระจาย server ไปอีกตัวแล้วครับ

ระบบเว็บใช้งานไม่ได้ที่ว่า อาการเป็นแบบไหนครับ?

แล้วก็ ตัดความฝังใจเรื่อง mem used ทิ้งไปไกลๆ เลยครับ (ตามเหตุผลด้านบน) ถ้ามันไม่พุ่งไปกิน swap ก็ไม่เกี่ยวอะไรทั้งสิ้น

เว็บล่ม ครับ ^^‘’ ถ้า ram หมด ยังไม่ได้ไปกิน swap เว็บก็ล่มแล้วครับ ^^

ล่มยังไงครับ connect time out? หรืออะไรยังไง

varnish error
Error 503 Service Unavailable
Service Unavailable
Guru Meditation:
[FONT=Lucida Grande]XID: 1525473736

ปัญหานี้น่าจะเกี่ยวกับอะไรเหรอ ครับ k. icez[/FONT]

backend ตายครับ ไม่ใช่ varnish

k .icez ครับ
จาก
# set default backend if no server cluster specified
backend default {
.host = “127.0.0.1”;
.port = “80”;
.probe = {
.url = “/”;
.interval = 5s;
.timeout = 1s;
.window = 5;
.threshold = 3;
}
.connect_timeout = 120s;
.first_byte_timeout = 120s;
.between_bytes_timeout = 120s;

.max_connections = 700;
# .port = “80” led to issues with competing for the port with apache.
}
ของผมถ้าผมเพิ่ม ตัวแดง เป็น 600s จะช่วยแก้ปัญหา
backend ได้บ้างมั๊ยครับ

ถ้า 120s apache ยังตอบไม่ทัน เพิ่มเป็น 600s ก็ไม่ได้ช่วยอะไร
วิธีแก้คือดู apache status ว่า request ใหนที่ทำงานไม่ทัน แล้วไป optimize ให้มันทำงานเร็วขึ้น ทุก request ไม่ควรจะตอบกลับช้ากว่า 15s ด้วยซ้ำ
เมื่อไหร่ที่ตอบช้ากว่าปริมาณการเรียกใหม่ มันก็จะสะสมเพิ่มพูนขึ้นเรื่อยๆจนเต็มในที่สุด การเพิ่ม max client ก็ไม่ช่วยอะไร เพราะเพิ่มไปเท่าไหร่มันก็ไปเต็มเท่านั้น และถ้ามากเกินก็ทำให้ ram หมด พาระบบล่มไปด้วย

ขอบคุณครับ

ผมเคยเจอแบบนี้ แหละครับ หาจนท้อเลย ลูกค้าบ่นมาบ่อยมาก จนหนีไปหลายคน

สุดท้าย มาลองปรับค่าของ APC Cache (ผมใช้ APC Cache แทน XCache)

/usr/local/lib/php.ini

extension=apc.so
apc.enabled=1
apc.shm_size=2048M <<= ของเดิม 128MB แต่แทบทุกเว็บเป็น CMS เลยโดนสูบจนหมด
apc.ttl=7200
apc.user_ttl=7200
apc.enable_cli=1

เครื่องผมแรมเยอะ (24GB) เลยตั้ง buffer ไว้ที่ 2048MB

อาการนี้ก็หายขาดเลยครับ ยังไงลองเอาไปดูเป็นแนวทางนะครับ